Does anyone know how to calculate the point in polygon from the given points and the buffer output using python qgis.
I have every thing loading the data and retrieving the output. But now I want to count the points in polygon. I was checking this link but that didn't work.
Code:
from PyQt4.QtCore import * from PyQt4.QtGui import * QgsApplication.initQgis() uri = QgsDataSourceURI()#4326 #loading fisrt layer with multistring uri.setConnection("localhost","5432","db_name","postgres","password") uri.setDataSource("schema_name","table_name","column","") vlayer = QgsVectorLayer(uri.uri(),"LayerName1","postgres") QgsMapLayerRegistry.instance().addMapLayer(vlayer) #loading second layer with points around it uri.setConnection("localhost","5432","db_name","postgres","password") uri.setDataSource("schema_name","table_name","column","") vlayer = QgsVectorLayer(uri.uri(),"LayerName","postgres") QgsMapLayerRegistry.instance().addMapLayer(vlayer) #creating buffer mc = iface.mapCanvas() layer = mc.currentLayer() print layer.name() QgsGeometryAnalyzer().buffer(layer, "C:/Users/***/aa.shp", 0.0004, False, False, -1) #loading back buffer layer1 = iface.addVectorLayer("C:/Users/***/aa.shp", "layer_name", "ogr") layer1 = qgis.utils.iface.activeLayer()#point in polygon provider = layer.dataProvider() provider.select() feat = QgsFeature() vertex_count = 0 while provider.nextFeature(feat): geom = feat.geometry() if geom.type() == QGis.Polygon: layer_vertices = 0 if geom.isMultipart(): polygons = geom.asMultiPolygon() else: polygons = [ geom.asPolygon() ] for polygon in polygons: for ring in polygon: layer_vertices += len(ring) print "Feature %d: Vertices %d" % ( feat.id(), layer_vertices) vertex_count += layer_vertices print "Total vertices: %d" % (vertex_count)
ERROR:
provider.select()AttributeError: 'QgsVectorDataProvider' object has no attribute 'select'
Also please can you guys share the link or the code for counting points in polygon. Few of them posted the same error can anyone share the code with explanation for point in polygon.
أكثر...
I have every thing loading the data and retrieving the output. But now I want to count the points in polygon. I was checking this link but that didn't work.
Code:
from PyQt4.QtCore import * from PyQt4.QtGui import * QgsApplication.initQgis() uri = QgsDataSourceURI()#4326 #loading fisrt layer with multistring uri.setConnection("localhost","5432","db_name","postgres","password") uri.setDataSource("schema_name","table_name","column","") vlayer = QgsVectorLayer(uri.uri(),"LayerName1","postgres") QgsMapLayerRegistry.instance().addMapLayer(vlayer) #loading second layer with points around it uri.setConnection("localhost","5432","db_name","postgres","password") uri.setDataSource("schema_name","table_name","column","") vlayer = QgsVectorLayer(uri.uri(),"LayerName","postgres") QgsMapLayerRegistry.instance().addMapLayer(vlayer) #creating buffer mc = iface.mapCanvas() layer = mc.currentLayer() print layer.name() QgsGeometryAnalyzer().buffer(layer, "C:/Users/***/aa.shp", 0.0004, False, False, -1) #loading back buffer layer1 = iface.addVectorLayer("C:/Users/***/aa.shp", "layer_name", "ogr") layer1 = qgis.utils.iface.activeLayer()#point in polygon provider = layer.dataProvider() provider.select() feat = QgsFeature() vertex_count = 0 while provider.nextFeature(feat): geom = feat.geometry() if geom.type() == QGis.Polygon: layer_vertices = 0 if geom.isMultipart(): polygons = geom.asMultiPolygon() else: polygons = [ geom.asPolygon() ] for polygon in polygons: for ring in polygon: layer_vertices += len(ring) print "Feature %d: Vertices %d" % ( feat.id(), layer_vertices) vertex_count += layer_vertices print "Total vertices: %d" % (vertex_count)
ERROR:
provider.select()AttributeError: 'QgsVectorDataProvider' object has no attribute 'select'
Also please can you guys share the link or the code for counting points in polygon. Few of them posted the same error can anyone share the code with explanation for point in polygon.
أكثر...