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
def test_knnW_from_shapefile(self): wc = pysal.knnW_from_shapefile(pysal.examples.get_path("columbus.shp")) self.assertAlmostEquals(wc.pct_nonzero, 0.040816326530612242) wc3 = pysal.knnW_from_shapefile(pysal.examples.get_path( "columbus.shp"), k=3) self.assertEquals(wc3.weights[1], [1, 1, 1]) self.assertEquals(set(wc3.neighbors[1]), set([3, 0, 7])) self.assertEquals(set(wc.neighbors[0]), set([2, 1])) w = pysal.knnW_from_shapefile(pysal.examples.get_path('juvenile.shp')) self.assertAlmostEquals(w.pct_nonzero, 0.011904761904761904) w1 = pysal.knnW_from_shapefile( pysal.examples.get_path('juvenile.shp'), k=1) self.assertAlmostEquals(w1.pct_nonzero, 0.0059523809523809521)
def get(self,shpName='',width=0,height=0): print shpName,width,height if not shpName in self.SHPS: return self.index() shp = pysal.open(self.SHPS[shpName]) W = None if 'w' in self.request.GET: wtype = self.request.GET['w'] if wtype.lower() == 'rook': W = pysal.rook_from_shapefile(self.SHPS[shpName]) elif wtype.lower() == 'queen': W = pysal.queen_from_shapefile(self.SHPS[shpName]) else: try: k = int(wtype) W = pysal.knnW_from_shapefile(self.SHPS[shpName],k) except: print "No valid W" print shp if width and height: width=int(width) height=int(height) if W: return self.write({'len':len(shp), 'polygons':shift_scale_shp(shp,width,height),'width':width,'height':height,'W':W.neighbors}) else: return self.write({'len':len(shp), 'polygons':shift_scale_shp(shp,width,height),'width':width,'height':height,'W':'null'}) return self.write({'len':len(shp)})
def __init__(self, filepath, outname, namelist, idlist, nb="queen", factor=2): """ Initiation of modules """ f=Dbf(filepath+".dbf") #Create mapping of locations to row id self.locations = dict() i=0 for row in f: uid=unicode("".join([row[k] for k in idlist])) locnames = unicode(", ".join([row[k] for k in namelist]),"ascii","ignore") self.locations[i] = {outname:locnames,"id":uid} i+=1 self.__dict__[outname]= self.locations self.outname = outname #Get Neightbor weights by queen, rook, knn, distance if nb=="queen": self.wt = pysal.queen_from_shapefile(filepath+".shp") elif nb=="rook": self.wt = pysal.rook_from_shapefile(filepath+".shp") elif nb=="knn": self.wt = pysal.knnW_from_shapefile(filepath+".shp", k=factor) elif nb=="distance": self.wt = pysal.threshold_binaryW_from_shapefile(filepath+".shp",k) #Create dictionary of neighbors for each region self.neighbors ={} for i,j in enumerate(self.wt): self.neighbors[self.locations[i]["id"]] = {self.outname:self.locations[i][self.outname] ,"neighbors":dict([[self.locations[k]["id"],self.locations[k][self.outname]] for k in j.keys()])}
def create_weights_from_shp(self, config): os.chdir(config['data_dir']) shapefile_name = config['output_shp']+".shp" w = ps.knnW_from_shapefile(shapefile_name, k=config['number_of_neighbors'], idVariable=config['shapefile_id_name']) w.transform = 'r' fo = ps.open(config['output_shp']+".gwt", "w") fo.write(w) fo.close() return config['output_shp']+".gwt"
def test_knnW(self): x = np.indices((5, 5)) x, y = np.indices((5, 5)) x.shape = (25, 1) y.shape = (25, 1) data = np.hstack([x, y]) wnn2 = pysal.knnW(data, k=2) wnn4 = pysal.knnW(data, k=4) wnn4.neighbors[0] self.assertEqual(set(wnn4.neighbors[0]), set([1, 5, 6, 2])) self.assertEqual(set(wnn2.neighbors[5]), set([0, 6])) self.assertEqual(wnn2.pct_nonzero, 8.0) wnn3e = pysal.knnW(data, p=2, k=3) self.assertEqual(set(wnn3e.neighbors[0]), set([1, 5, 6])) wc = pysal.knnW_from_shapefile(self.polyShp) self.assertEqual(wc.pct_nonzero, 4.081632653061225) self.assertEqual(set(wc.neighbors[0]), set([2, 1])) wc3 = pysal.knnW_from_shapefile(self.polyShp, k=3) self.assertEqual(wc3.weights[1], [1, 1, 1]) self.assertEqual(set(wc3.neighbors[1]), set([0, 3, 7]))
def test_knnW(self): x = np.indices((5, 5)) x, y = np.indices((5, 5)) x.shape = (25, 1) y.shape = (25, 1) data = np.hstack([x, y]) wnn2 = pysal.knnW(data, k=2) wnn4 = pysal.knnW(data, k=4) wnn4.neighbors[0] self.assertEqual(wnn4.neighbors[0], [1, 5, 6, 2]) self.assertEqual(wnn2.neighbors[5], [0, 6]) self.assertEqual(wnn2.pct_nonzero, 0.080000000000000002) wnn3e = pysal.knnW(data, p=2, k=3) self.assertEqual(wnn3e.neighbors[0], [1, 5, 6]) wc = pysal.knnW_from_shapefile(self.polyShp) self.assertEqual(wc.pct_nonzero, 0.040816326530612242) self.assertEqual(wc.neighbors[0], [2, 1]) wc3 = pysal.knnW_from_shapefile(self.polyShp, k=3, idVariable="POLYID") self.assertEqual(wc3.weights[1], [1, 1, 1]) self.assertEqual(wc3.neighbors[1], [3, 2, 4])
def _distance(arg_dict): """ General handler for distance based weights obtained from shapefiles """ input1 = arg_dict['input1'] uri = input1['uri'] weight_type = arg_dict['weight_type'] weight_type = weight_type.lower() k = 2 id_variable = None p = 2 radius = None if 'parameters' in arg_dict: k = arg_dict['parameters'].get('k', k) # set default to 2 id_variable = arg_dict['parameters'].get('id_variable', id_variable) p = arg_dict['parameters'].get('p', p) radius = arg_dict['parameters'].get('radius', radius) else: parameters = {} parameters['k'] = 2 parameters['id_variable'] = None parameters['radius'] = None parameters['p'] = 2 arg_dict['parameters'] = parameters if weight_type == 'knn': w = ps.knnW_from_shapefile(uri, k=k, p=p, idVariable=id_variable, radius=radius) w = WMD(w.neighbors, w.weights) w.meta_data = {} w.meta_data["input1"] = {"type": 'shp', 'uri': uri} w.meta_data["weight_type"] = 'knn' w.meta_data["transform"] = w.transform w.meta_data['parameters'] = arg_dict['parameters'] return w
def replace_empty_neighbours_with_KNN(data_countries, w): shapefile = SHAPEFILE no_neighbors_idx = w.islands knn = 10 wknn = pysal.knnW_from_shapefile(shapefile, knn) knn_countries = get_countries_from_shapefile(shapefile) neighbors = w.neighbors for nn_idx in no_neighbors_idx: country = data_countries[nn_idx] print country if country not in knn_countries: continue knn_country_idx = knn_countries.index(country) knn_country_neighbors = [ knn_countries[nn] for nn in wknn.neighbors[knn_country_idx] ] for knn_nn in knn_country_neighbors: if len(neighbors[nn_idx]) > 2: continue data_country_idx = np.where(data_countries == knn_nn)[0] if len(data_country_idx) > 0: neighbors[nn_idx][data_country_idx[0]] = 1.0 w = pysal.weights.W(neighbors, id_order=range(len(data_countries))) return w
def _distance(arg_dict): """ General handler for distance based weights obtained from shapefiles """ input1 = arg_dict['input1'] uri = input1['uri'] weight_type = arg_dict['weight_type'] weight_type = weight_type.lower() k = 2 id_variable = None p = 2 radius = None if 'parameters' in arg_dict: k = arg_dict['parameters'].get('k',k) # set default to 2 id_variable = arg_dict['parameters'].get('id_variable', id_variable) p = arg_dict['parameters'].get('p',p) radius = arg_dict['parameters'].get('radius', radius) else: parameters = {} parameters['k'] = 2 parameters['id_variable'] = None parameters['radius'] = None parameters['p'] = 2 arg_dict['parameters'] = parameters if weight_type == 'knn': w = ps.knnW_from_shapefile(uri,k=k,p=p,idVariable=id_variable, radius=radius) w = WMD(w.neighbors, w.weights) w.meta_data = {} w.meta_data["input1"] = {"type": 'shp', 'uri':uri} w.meta_data["weight_type"] = 'knn' w.meta_data["transform"] = w.transform w.meta_data['parameters'] = arg_dict['parameters'] return w
print("time elapsed for rook... using bins: " + str(t1 - t0)) from pysal.weights._contW_rtree import ContiguityWeights_rtree t0 = time.time() rt = ContiguityWeights_rtree(pysal.open(fname), ROOK) t1 = time.time() print("time elapsed for rook... using rtree: " + str(t1 - t0)) print(rt.w == rb.w) print('QUEEN') t0 = time.time() qt = ContiguityWeights_rtree(pysal.open(fname), QUEEN) t1 = time.time() print("using " + str(fname)) print("time elapsed for queen... using rtree: " + str(t1 - t0)) print(qb.w == qt.w) print('knn4') t0 = time.time() knn = pysal.knnW_from_shapefile(fname, k=4) t1 = time.time() print(t1 - t0) print('rook from shapefile') t0 = time.time() knn = pysal.rook_from_shapefile(fname) t1 = time.time() print(t1 - t0)
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()
ds = ps.open(name_ds, 'r') print(list(enumerate(ds.header))) logger.info('Dataset = ' + name_ds) logger.info('Shapefile = ' + shapefile) x = np.zeros((len(ds), 1)) for i, c in enumerate(x_columns): data = ds.by_col(ds.header[c]) data = list(map(float, data)) data = np.array(data).reshape(len(ds), 1) logger.info('Variable #{} :'.format(i + 1) + ds.header[c]) x = np.hstack((x, data)) x = x[:, 1:] # Weights, row-standarsized w = ps.knnW_from_shapefile(shapefile, k=4, idVariable=None) w.transform = 'r' name_w = shapefile.split('/')[-1] # Kernel-weights kw = ps.adaptive_kernelW_from_shapefile(shapefile, k=12, idVariable=None) for name_y in names_y: y = ds.by_col(name_y) y = list(map(float, y)) y = np.array(y).reshape(len(ds), 1) logger.info('y = ' + name_y) ## For Spatial Lag Model -- Maximum Likelihood logger.info('Method = Spatial Lag Model -- Maximum Likelihood') ml_lag = ps.spreg.ML_Lag(y, x,
def generate_matrix(self): if self.dlg.txtOutput.text()=='': QtGui.QMessageBox.warning(None,"Error","Oops! Generate weight matrix. Try again...") else: filenamein=self.myfile filename = self.dlg.txtOutput.text() self.filename=filename ext=filename[-3:] """matrix""" if self.dlg.rdButtonContW.isChecked()==True: if self.dlg.rdButtonContWQ.isChecked()==True: w = pysal.queen_from_shapefile(filenamein,idVariable=None) if ext=='gal': output_file = pysal.open(filename,'w') output_file.write(w) else: output_file = pysal.open(filename,'w','arcgis_dbf') output_file.write(w, useIdIndex=True) output_file.close() if self.dlg.rdButtonContWR.isChecked()==True: w = pysal.rook_from_shapefile(filenamein,idVariable=None) if ext=='gal': output_file = pysal.open(filename,'w') output_file.write(w) else: output_file = pysal.open(filename,'w','arcgis_dbf') output_file.write(w, useIdIndex=True) output_file.close() if self.dlg.rdButtonContWB.isChecked()==True: wr = pysal.rook_from_shapefile(filenamein,idVariable=None) wq = pysal.queen_from_shapefile(filenamein,idVariable=None) wb = pysal.w_difference(wq, wr,constrained = False) if ext=='gal': output_file = pysal.open(filename,'w') output_file.write(wb) else: output_file = pysal.open(filename,'w','arcgis_dbf') output_file.write(wb, useIdIndex=True) output_file.close() if self.dlg.rdButtonDisW.isChecked()==True: w = pysal.threshold_binaryW_from_shapefile(filenamein, float(self.dlg.txtThresh.text())) if ext=='gal': output_file = pysal.open(filename,'w') output_file.write(w) else: output_file = pysal.open(filename,'w','arcgis_dbf') output_file.write(w, useIdIndex=True) output_file.close() if self.dlg.rdButtonKNW.isChecked()==True: w = pysal.knnW_from_shapefile(filenamein, k=self.dlg.spinBox.value()) if ext=='gal': output_file = pysal.open(filename,'w') output_file.write(w) else: output_file = pysal.open(filename,'w','arcgis_dbf') output_file.write(w, useIdIndex=True) output_file.close() """end matrix""" QtGui.QMessageBox.warning(None,"Complete","Weight matrix is generated")
data = list(map(float, data)) data = np.array(data).reshape(len(db), 1) if log_variables[c]: data = np.log(data) print('Variable #{} :'.format(i+1), 'log({})'.format(db.header[c])) else: print('Variable #{} :'.format(i+1), db.header[c]) x = np.hstack((x, data)) x = x[:, 1:] logfile = open('Results.log', 'a') # Weights, row-standarsized w = ps.knnW_from_shapefile('Listings shp/Tract_With_Airbnb.shp', k=4, idVariable=None) w.transform = 'r' # Kernel-weights kw = ps.adaptive_kernelW_from_shapefile('Listings shp/Tract_With_Airbnb.shp', k=12, idVariable=None) ## For OLS + White-test + Spatial Diagnostics ols_spat = ps.spreg.OLS(y, x, w=w, robust=None, gwk=kw, sig2n_k=True, nonspat_diag=False, spat_diag=True, moran=True, white_test=True, vm=False, name_y=None, name_x=None, name_w=None, name_gwk=None, name_ds=None) #print(ols_spat.summary) #logfile.write('\n'*2 + ols_spat.summary + '\n'*2) logfile.close() ## Langrange Multiplier error and lag values. vw = ps.spreg.diagnostics_sp.LMtests(ols_spat, w) print(vw)
def run(self): """Run method that performs all the real work""" # show the dialog self.clear_ui() layers, layers_shp = self.loadLayerList() if len(layers) == 0: return self.dlg.show() self.load_comboBox() # Run the dialog event loop result = self.dlg.exec_() # See if OK was pressed and fields are not empty if result and (self.validator() == 1): selectedLayerIndex = self.dlg.comboBox.currentIndex() if selectedLayerIndex < 0 or selectedLayerIndex > len(layers): return selectedLayer = layers_shp[selectedLayerIndex] layerName = selectedLayer.dataProvider().dataSourceUri() C = selectedLayer.fieldNameIndex(self.dlg.comboBox_C.currentText()) C2 = selectedLayer.fieldNameIndex( self.dlg.comboBox_C_2.currentText()) filename = self.dlg.lineEdit.text() (path, layer_id) = layerName.split('|') inDriver = ogr.GetDriverByName("ESRI Shapefile") inDataSource = inDriver.Open(path, 0) inLayer = inDataSource.GetLayer() type = inLayer.GetLayerDefn().GetGeomType() u = [] for i in range(0, inLayer.GetFeatureCount()): geometry = inLayer.GetFeature(i) u.append(geometry.GetField(C)) y = numpy.array(u) # attributes vector if self.dlg.checkBox_moranBi.isChecked() == 1: v = [] for i in range(0, inLayer.GetFeatureCount()): geometry = inLayer.GetFeature(i) v.append(geometry.GetField(C2)) x = numpy.array(v) if type == 1: # point t = () for feature in inLayer: geometry = feature.GetGeometryRef() xy = (geometry.GetX(), geometry.GetY()) t = t + (xy, ) # t = get_points_array_from_shapefile(layerName.split("|")[0]) if self.dlg.lineEditThreshold.text( ) and self.dlg.lineEditThreshold.text( ) != "": # if threshold is given threshold1 = int(self.dlg.lineEditThreshold.text()) elif self.dlg.checkBox_knn.isChecked( ) == 0: # if user needs to optimize threshold (no knn) mx_moran = -1000.0 mx_i = -1000.0 minT = int(self.dlg.lineEdit_minT.text()) maxT = int(self.dlg.lineEdit_maxT.text()) dist = int(self.dlg.lineEdit_dist.text()) for i in range(minT, maxT + dist, dist): w = DistanceBand(t, threshold=i, p=2, binary=False) moran = pysal.Moran(y, w) # print moran.z_norm if moran.z_norm > mx_moran: mx_i = i mx_moran = moran.z_norm threshold1 = int(mx_i) if self.dlg.checkBox_knn.isChecked() == 1: weightValue = int(self.dlg.knn_number.text()) w = pysal.knnW_from_shapefile(layerName.split("|")[0], k=weightValue, p=1) threshold1 = "None/KNN used " + self.dlg.knn_number.text() else: w = DistanceBand(t, threshold1, p=2, binary=False) else: # polygon w = pysal.queen_from_shapefile(layerName.split("|")[0]) threshold1 = "None/Queen's Case used" if self.dlg.checkBox_rowStandard.isChecked() == 1: type_w = "R" else: type_w = "B" if self.dlg.checkBox_randomPerm.isChecked() == 1: permutationsValue = int(self.dlg.lineEdit_random.text()) else: permutationsValue = 999 numpy.random.seed(12345) if self.dlg.checkBox_gi.isChecked() == 1: statistics = G_Local(y, w, star=True, transform=type_w, permutations=permutationsValue) elif self.dlg.checkBox_moran.isChecked() == 1: statistics = Moran_Local(y, w, transformation=type_w, permutations=permutationsValue) else: statistics = Moran_Local_BV(y, x, w, transformation=type_w, permutations=permutationsValue) self.write_file(filename, statistics, layerName, inLayer, inDataSource, y, threshold1) # assign the style to the output layer on QGIS if self.dlg.checkBox_gi.isChecked() == 1: if type == 1: # point stylePath = "/layer_style/hotspots_class.qml" else: stylePath = "/layer_style/hotspots_class_poly.qml" self.iface.activeLayer().loadNamedStyle( os.path.dirname(__file__) + stylePath) else: if type == 1: # point stylePath = "/layer_style/moran_class.qml" else: stylePath = "/layer_style/moran_class_poly.qml" self.iface.activeLayer().loadNamedStyle( os.path.dirname(__file__) + stylePath) elif result and (self.validator() == 0): self.error_msg() else: self.clear_ui() pass
rcParams['figure.figsize'] = 8, 6 pd.set_option('display.max_rows', 500) get_ipython().magic(u'matplotlib inline') # In[ ]: data = pd.read_csv('food_geo3.csv') data.head() # In[ ]: #setting the weight matrix for spatial autocorrelation #using k-nearest neighbors-3,5,and9 w_knn3 = ps.knnW_from_shapefile(('newer_polys.shp'), k=3, idVariable='CAMIS') w_knn5 = ps.knnW_from_shapefile(('newer_polys.shp'), k=5, idVariable='CAMIS') w_knn9 = ps.knnW_from_shapefile(('newer_polys.shp'), k=9, idVariable='CAMIS') # In[ ]: #normalize the dependent variable Y = data.SCORE.values Y = (Y-Y.mean())/Y.std() print Y.shape # In[ ]: # Now we would like to standardize all the weights. This can be
def run(self): """Run method that performs all the real work""" # show the dialog self.clear_ui() layers, layers_shp = self.loadLayerList() if len(layers) == 0: return self.dlg.show() self.load_comboBox() # Run the dialog event loop result = self.dlg.exec_() # See if OK was pressed and fields are not empty if result and (self.validator() == 1): selectedLayerIndex = self.dlg.comboBox.currentIndex() if selectedLayerIndex < 0 or selectedLayerIndex > len(layers): return selectedLayer = layers_shp[selectedLayerIndex] layerName = selectedLayer.dataProvider().dataSourceUri() C = selectedLayer.fieldNameIndex(self.dlg.comboBox_C.currentText()) C2 = selectedLayer.fieldNameIndex(self.dlg.comboBox_C_2.currentText()) filename = self.dlg.lineEdit.text() (path, layer_id) = layerName.split('|') inDriver = ogr.GetDriverByName("ESRI Shapefile") inDataSource = inDriver.Open(path, 0) inLayer = inDataSource.GetLayer() type = inLayer.GetLayerDefn().GetGeomType() u = [] for i in range(0, inLayer.GetFeatureCount()): geometry = inLayer.GetFeature(i) u.append(geometry.GetField(C)) y = numpy.array(u) # attributes vector if self.dlg.checkBox_moranBi.isChecked() == 1: v = [] for i in range(0, inLayer.GetFeatureCount()): geometry = inLayer.GetFeature(i) v.append(geometry.GetField(C2)) x = numpy.array(v) if type == 1: # point t = () for feature in inLayer: geometry = feature.GetGeometryRef() xy = (geometry.GetX(), geometry.GetY()) t = t + (xy,) # t = get_points_array_from_shapefile(layerName.split("|")[0]) if self.dlg.lineEditThreshold.text() and self.dlg.lineEditThreshold.text() != "": # if threshold is given threshold1 = int(self.dlg.lineEditThreshold.text()) elif self.dlg.checkBox_knn.isChecked() == 0: # if user needs to optimize threshold (no knn) mx_moran = -1000.0 mx_i = -1000.0 minT = int(self.dlg.lineEdit_minT.text()) maxT = int(self.dlg.lineEdit_maxT.text()) dist = int(self.dlg.lineEdit_dist.text()) for i in range(minT, maxT + dist, dist): w = DistanceBand(t, threshold=i, p=2, binary=False) moran = pysal.Moran(y, w) # print moran.z_norm if moran.z_norm > mx_moran: mx_i = i mx_moran = moran.z_norm threshold1 = int(mx_i) if self.dlg.checkBox_knn.isChecked() == 1: weightValue = int(self.dlg.knn_number.text()) w = pysal.knnW_from_shapefile(layerName.split("|")[0], k=weightValue, p=1) threshold1 = "None/KNN used " + self.dlg.knn_number.text() else: w = DistanceBand(t, threshold1, p=2, binary=False) else: # polygon w = pysal.queen_from_shapefile(layerName.split("|")[0]) threshold1 = "None/Queen's Case used" if self.dlg.checkBox_rowStandard.isChecked() == 1: type_w = "R" else: type_w = "B" if self.dlg.checkBox_randomPerm.isChecked() == 1: permutationsValue = int(self.dlg.lineEdit_random.text()) else: permutationsValue = 999 numpy.random.seed(12345) if self.dlg.checkBox_gi.isChecked() == 1: statistics = G_Local(y, w, star=True, transform=type_w, permutations=permutationsValue) elif self.dlg.checkBox_moran.isChecked() == 1: statistics = Moran_Local(y, w, transformation=type_w, permutations=permutationsValue) else: statistics = Moran_Local_BV(y, x, w, transformation=type_w, permutations=permutationsValue) self.write_file(filename, statistics, layerName, inLayer, inDataSource, y, threshold1) # assign the style to the output layer on QGIS if self.dlg.checkBox_gi.isChecked() == 1: if type == 1: # point stylePath = "/layer_style/hotspots_class.qml" else: stylePath = "/layer_style/hotspots_class_poly.qml" self.iface.activeLayer().loadNamedStyle(os.path.dirname(__file__) + stylePath) else: if type == 1: # point stylePath = "/layer_style/moran_class.qml" else: stylePath = "/layer_style/moran_class_poly.qml" self.iface.activeLayer().loadNamedStyle(os.path.dirname(__file__) + stylePath) elif result and (self.validator() == 0): self.error_msg() else: self.clear_ui() pass
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()