In my Plugin I take the polygon where my mouseclick is inside.According the mouseclick i define a +200 Meter rectangle and take allfeatures inside.How can i combine a list of lines and split my polygon of interest?
from qgis.gui import QgsMapToolfrom qgis.core import QgsMapLayer,QgsPoint,QgsMapToPixel,QgsRectangle, QgsFeature, QgsFeatureRequest, QgsGeometry, QgsSpatialIndex,QgsMapLayerRegistry,QgsVectorLayer from PyQt4.QtGui import QCursor, QPixmapfrom PyQt4.QtCore import Qt, QVariantfrom qgis.utils import ifaceimport itertoolsimport shapely.geometryimport shapely.opsclass NearestFeatureMapTool(QgsMapTool): def __init__(self, canvas): super(QgsMapTool, self).__init__(canvas) self.canvas = canvas self.cursor = QCursor(Qt.CrossCursor) def activate(self): self.canvas.setCursor(self.cursor) def canvasReleaseEvent(self, mouseEvent): layerData = [] listmixt = [] listline =[] polywithpoint=QgsFeature() index = QgsSpatialIndex() polyline=QgsFeature() #vl = QgsVectorLayer("polygon", "temporary_points", "memory") #pr = vl.dataProvider() for layer in self.canvas.layers(): if layer.type() != QgsMapLayer.VectorLayer: # Ignore this layer as it's not a vector continue if layer.featureCount() == 0: # There are no features - skip continue layer.removeSelection() # Determine the location of the click in real-world coords layerPoint = self.toLayerCoordinates( layer, mouseEvent.pos() ) feature_dict = {feature.id(): feature for (feature) in layer.getFeatures()} map(index.insertFeature,feature_dict.values()) #define rectangles displayrectangle = self.canvas.extent() #print displayrectangle.toString() ownrectangle = QgsRectangle(layerPoint.x(),layerPoint.y(),layerPoint.x()+200,layerPoint.y()+200) print ownrectangle.toString() #print ownrectangle.toString() if displayrectangle.contains(ownrectangle): userectangle = ownrectangle else: userectangle = displayrectangle # Auswahl der Features innerhalb des Rechteckes for i in index.intersects(userectangle): print feature_dict.attributes() #listmixt.append(feature_dict) if feature_dict.geometry().contains(QgsGeometry.fromPoint(layerPoint)): polywithpoint = feature_dict else: listmixt.append(feature_dict) for i in listmixt: if i.geometry().type()==1: listline.append(i)
أكثر...
from qgis.gui import QgsMapToolfrom qgis.core import QgsMapLayer,QgsPoint,QgsMapToPixel,QgsRectangle, QgsFeature, QgsFeatureRequest, QgsGeometry, QgsSpatialIndex,QgsMapLayerRegistry,QgsVectorLayer from PyQt4.QtGui import QCursor, QPixmapfrom PyQt4.QtCore import Qt, QVariantfrom qgis.utils import ifaceimport itertoolsimport shapely.geometryimport shapely.opsclass NearestFeatureMapTool(QgsMapTool): def __init__(self, canvas): super(QgsMapTool, self).__init__(canvas) self.canvas = canvas self.cursor = QCursor(Qt.CrossCursor) def activate(self): self.canvas.setCursor(self.cursor) def canvasReleaseEvent(self, mouseEvent): layerData = [] listmixt = [] listline =[] polywithpoint=QgsFeature() index = QgsSpatialIndex() polyline=QgsFeature() #vl = QgsVectorLayer("polygon", "temporary_points", "memory") #pr = vl.dataProvider() for layer in self.canvas.layers(): if layer.type() != QgsMapLayer.VectorLayer: # Ignore this layer as it's not a vector continue if layer.featureCount() == 0: # There are no features - skip continue layer.removeSelection() # Determine the location of the click in real-world coords layerPoint = self.toLayerCoordinates( layer, mouseEvent.pos() ) feature_dict = {feature.id(): feature for (feature) in layer.getFeatures()} map(index.insertFeature,feature_dict.values()) #define rectangles displayrectangle = self.canvas.extent() #print displayrectangle.toString() ownrectangle = QgsRectangle(layerPoint.x(),layerPoint.y(),layerPoint.x()+200,layerPoint.y()+200) print ownrectangle.toString() #print ownrectangle.toString() if displayrectangle.contains(ownrectangle): userectangle = ownrectangle else: userectangle = displayrectangle # Auswahl der Features innerhalb des Rechteckes for i in index.intersects(userectangle): print feature_dict.attributes() #listmixt.append(feature_dict) if feature_dict.geometry().contains(QgsGeometry.fromPoint(layerPoint)): polywithpoint = feature_dict else: listmixt.append(feature_dict) for i in listmixt: if i.geometry().type()==1: listline.append(i)
أكثر...