示例#1
0
def generateWeightsUsingShapefile(shapeFilePath,
                                  idVariable=None,
                                  weights=None,
                                  kind="queen",
                                  k=None,
                                  binary=False):
    # use weights from shapefile for purely geographic
    w = None
    if weights == None:
        if kind == "queen":
            w = pysal.queen_from_shapefile(shapeFilePath,
                                           idVariable=idVariable)
        if kind == "rook":
            w = pysal.rook_from_shapefile(shapeFilePath, idVariable=idVariable)
        if kind == "knn" and type(k) == int:
            w = pysal.knnW_from_shapefile(shapefile=shapeFilePath,
                                          k=k,
                                          idVariable=idVariable)
        if kind == "band":
            threshold = pysal.min_threshold_dist_from_shapefile(
                shapeFilePath, idVariable=idVariable)
            if binary == True:
                w = pysal.weights.DistanceBand.from_shapefile(
                    shapeFilePath,
                    threshold=threshold,
                    binary=True,
                    idVariable=idVariable)
            else:
                w = pysal.threshold_continuousW_from_shapefile(
                    shapefile=shapeFilePath,
                    threshold=threshold,
                    idVariable=idVariable)
        if kind == "kernel":
            w = pysal.adaptive_kernelW_from_shapefile(shapeFilePath,
                                                      diagonal=True,
                                                      k=5,
                                                      idVariable=idVariable)

    # else use user defined weights to create "space" instead of "place"
    else:
        if kind == "rook":
            w = pysal.rook_from_shapefile(shapeFilePath, idVariable=idVariable)
        if kind == "knn":
            w = pysal.knnW_from_shapefile(shapeFilePath,
                                          k=k,
                                          idVariable=idVariable)
        else:
            w = pysal.queen_from_shapefile(shapeFilePath,
                                           idVariable=idVariable)
        neighbors = w.neighbor_offsets
        w = pysal.W(neighbors, weights=weights)

    # row standardize the matrix. better to do it here and use it somewhere else.
    w.transform = 'r'
    if binary == True:
        w.transform = 'b'
    return w
示例#2
0
 def test_threshold(self):
     md = pysal.min_threshold_dist_from_shapefile(self.polyShp)
     self.assertEqual(md, 0.61886415807685413)
     wid = pysal.threshold_continuousW_from_array(self.points, 11.2)
     self.assertEqual(wid.weights[0], [0.10000000000000001,
                                       0.089442719099991588])
     wid2 = pysal.threshold_continuousW_from_array(
         self.points, 11.2, alpha=-2.0)
     self.assertEqual(wid2.weights[0], [0.01, 0.0079999999999999984])
     w = pysal.threshold_continuousW_from_shapefile(
         self.polyShp, 0.62, idVariable="POLYID")
     self.assertEqual(w.weights[1], [1.6702346893743334,
                                     1.7250729841938093])
示例#3
0
 def test_threshold(self):
     md = pysal.min_threshold_dist_from_shapefile(self.polyShp)
     self.assertEqual(md, 0.61886415807685413)
     wid = pysal.threshold_continuousW_from_array(self.points, 11.2)
     self.assertEqual(wid.weights[0], [0.10000000000000001,
                                       0.089442719099991588])
     wid2 = pysal.threshold_continuousW_from_array(
         self.points, 11.2, alpha=-2.0)
     self.assertEqual(wid2.weights[0], [0.01, 0.0079999999999999984])
     w = pysal.threshold_continuousW_from_shapefile(
         self.polyShp, 0.62, idVariable="POLYID")
     self.assertEqual(w.weights[1], [1.6702346893743334,
                                     1.7250729841938093])
示例#4
0
 def test_threshold(self):
     md = pysal.min_threshold_dist_from_shapefile(self.polyShp)
     self.assertEqual(md, 0.61886415807685413)
     wid = pysal.threshold_continuousW_from_array(self.points, 11.2)
     wds = {wid.neighbors[0][i]: v for i, v in enumerate(wid.weights[0])}
     self.assertEqual(wds, {1: 0.10000000000000001,
                            3: 0.089442719099991588})
     wid2 = pysal.threshold_continuousW_from_array(
         self.points, 11.2, alpha=-2.0)
     wds = {wid2.neighbors[0][i]: v for i, v in enumerate(wid2.weights[0])}
     self.assertEqual(wds, {1: 0.01, 3: 0.0079999999999999984})
     w = pysal.threshold_continuousW_from_shapefile(
         self.polyShp, 0.62, idVariable="POLYID")
     wds = {w.neighbors[1][i]: v for i, v in enumerate(w.weights[1])}
     self.assertEqual(wds, {2: 1.6702346893743334,
                            3: 1.7250729841938093})
示例#5
0
 def test_threshold(self):
     md = pysal.min_threshold_dist_from_shapefile(self.polyShp)
     self.assertEqual(md, 0.61886415807685413)
     wid = pysal.threshold_continuousW_from_array(self.points, 11.2)
     wds = {wid.neighbors[0][i]: v for i, v in enumerate(wid.weights[0])}
     self.assertEqual(wds, {
         1: 0.10000000000000001,
         3: 0.089442719099991588
     })
     wid2 = pysal.threshold_continuousW_from_array(self.points,
                                                   11.2,
                                                   alpha=-2.0)
     wds = {wid2.neighbors[0][i]: v for i, v in enumerate(wid2.weights[0])}
     self.assertEqual(wds, {1: 0.01, 3: 0.0079999999999999984})
     w = pysal.threshold_continuousW_from_shapefile(self.polyShp,
                                                    0.62,
                                                    idVariable="POLYID")
     wds = {w.neighbors[1][i]: v for i, v in enumerate(w.weights[1])}
     self.assertEqual(wds, {2: 1.6702346893743334, 3: 1.7250729841938093})
示例#6
0
文件: test_user.py 项目: xrzhou/pysal
 def test_threshold_continuousW_from_shapefile(self):
     w = pysal.threshold_continuousW_from_shapefile(pysal.examples.get_path(
         "columbus.shp"), 0.62, idVariable="POLYID")
     self.assertEquals(
         w.weights[1], [1.6702346893743334, 1.7250729841938093])
示例#7
0
    def run_distance(self, sfile, var):
        """ Invoked by main run method. """
        if self.model.shapes.type == pysal.cg.Polygon:
            self.warn("The selected shapefile contains polygons and distance \
                      weights can only be computed on points. " +
                      "The centroids of the specified polygons will be used \
                      instead.")
        elif self.model.shapes.type != pysal.cg.Point:
            return self.warn("The selected shapefile does not contain points \
                             and contiguity weights can only be computed \
                             on points.")
        if self.model.distMethod == 0:
            radius = None
        elif self.model.distMethod == 1:  # 'Arc Distance (miles)'
            radius = pysal.cg.RADIUS_EARTH_MILES
        elif self.model.distMethod == 2:  # 'Arc Distance (kilometers)'
            radius = pysal.cg.RADIUS_EARTH_KM

        if self.ThresholdRadio.GetValue():
            try:
                cutoff = float(self.CutoffText.GetValue())
            except:
                return self.warn("The cut-off point is not valid.")
            print "Threshold on %s, ids=%r, cutoff=%f" % (sfile, var, cutoff)
            W = pysal.threshold_binaryW_from_shapefile(sfile,
                                                       cutoff,
                                                       idVariable=var,
                                                       radius=radius)
            W.meta = {
                'shape file': sfile,
                'id variable': var,
                'method': 'distance',
                'method options': ['Threshold', cutoff]
            }
            if radius:
                W.meta['Sphere Radius'] = radius
            self.SetW(W)
        elif self.KnnRadio.GetValue():
            k = int(self.NumNeighSpin.GetValue())
            print "Knn on %s, ids=%r, k=%d" % (sfile, var, k)
            W = pysal.knnW_from_shapefile(sfile,
                                          k=k,
                                          idVariable=var,
                                          radius=radius)
            W.meta = {
                'shape file': sfile,
                'id variable': var,
                'method': 'distance',
                'method options': ['KNN', k]
            }
            if radius:
                W.meta['Sphere Radius'] = radius
            self.SetW(W)
        elif self.InverseRadio.GetValue():
            try:
                cutoff = float(self.CutoffText2.GetValue())
            except:
                return self.warn("The cut-off point is not valid.")
            power = int(self.PowerSpin.GetValue())
            print "Inverse on %s, ids=%r, cutoff=%f, power=%d" % \
                  (sfile, var, cutoff, power)
            try:
                W = pysal.threshold_continuousW_from_shapefile(sfile,
                                                               cutoff,
                                                               alpha=-1 *
                                                               power,
                                                               idVariable=var,
                                                               radius=radius)
                W.meta = {
                    'shape file': sfile,
                    'id variable': var,
                    'method': 'distance',
                    'method options': ['Inverse', cutoff, power]
                }
                if radius:
                    W.meta['Sphere Radius'] = radius
                self.SetW(W)
            except Exception:
                et, e, tb = sys.exc_info()
                d = wx.MessageDialog(self, "\"%s\"\n" % str(e), "Error",
                                     wx.OK | wx.ICON_ERROR)
                d.ShowModal()
示例#8
0
 def test_threshold_continuousW_from_shapefile(self):
     w = pysal.threshold_continuousW_from_shapefile(
         pysal.examples.get_path("columbus.shp"), 0.62, idVariable="POLYID")
     wds = {w.neighbors[1][i]: v for i, v in enumerate(w.weights[1])}
     self.assertEquals(wds, {2: 1.6702346893743334, 3: 1.7250729841938093})
示例#9
0
 def test_threshold_continuousW_from_shapefile(self):
     w = pysal.threshold_continuousW_from_shapefile(pysal.examples.get_path(
         "columbus.shp"), 0.62, idVariable="POLYID")
     wds = {w.neighbors[1][i]:v for i,v in enumerate(w.weights[1])}
     self.assertEquals(wds, {2:1.6702346893743334, 3:1.7250729841938093})
示例#10
0
    def run_distance(self, sfile, var):
        """ Invoked by main run method. """
        if self.model.shapes.type == pysal.cg.Polygon:
            self.warn("The selected shapefile contains polygons and distance \
                      weights can only be computed on points. " +
                      "The centroids of the specified polygons will be used \
                      instead.")
        elif self.model.shapes.type != pysal.cg.Point:
            return self.warn("The selected shapefile does not contain points \
                             and contiguity weights can only be computed \
                             on points.")
        if self.model.distMethod == 0:
            radius = None
        elif self.model.distMethod == 1:  # 'Arc Distance (miles)'
            radius = pysal.cg.RADIUS_EARTH_MILES
        elif self.model.distMethod == 2:  # 'Arc Distance (kilometers)'
            radius = pysal.cg.RADIUS_EARTH_KM

        if self.ThresholdRadio.GetValue():
            try:
                cutoff = float(self.CutoffText.GetValue())
            except:
                return self.warn("The cut-off point is not valid.")
            print "Threshold on %s, ids=%r, cutoff=%f" % (sfile, var, cutoff)
            W = pysal.threshold_binaryW_from_shapefile(
                sfile, cutoff, idVariable=var, radius=radius)
            W.meta = {'shape file': sfile,
                      'id variable': var,
                      'method': 'distance',
                      'method options': ['Threshold', cutoff]}
            if radius:
                W.meta['Sphere Radius'] = radius
            self.SetW(W)
        elif self.KnnRadio.GetValue():
            k = int(self.NumNeighSpin.GetValue())
            print "Knn on %s, ids=%r, k=%d" % (sfile, var, k)
            W = pysal.knnW_from_shapefile(
                sfile, k=k, idVariable=var, radius=radius)
            W.meta = {'shape file': sfile,
                      'id variable': var,
                      'method': 'distance',
                      'method options': ['KNN', k]}
            if radius:
                W.meta['Sphere Radius'] = radius
            self.SetW(W)
        elif self.InverseRadio.GetValue():
            try:
                cutoff = float(self.CutoffText2.GetValue())
            except:
                return self.warn("The cut-off point is not valid.")
            power = int(self.PowerSpin.GetValue())
            print "Inverse on %s, ids=%r, cutoff=%f, power=%d" % \
                  (sfile, var, cutoff, power)
            try:
                W = pysal.threshold_continuousW_from_shapefile(
                    sfile, cutoff, alpha=- 1 * power,
                    idVariable=var, radius=radius)
                W.meta = {'shape file': sfile,
                          'id variable': var,
                          'method': 'distance',
                          'method options': ['Inverse', cutoff, power]}
                if radius:
                    W.meta['Sphere Radius'] = radius
                self.SetW(W)
            except Exception:
                et, e, tb = sys.exc_info()
                d = wx.MessageDialog(self, "\"%s\"\n"
                                     % str(e), "Error", wx.OK | wx.ICON_ERROR)
                d.ShowModal()