I have the problem, that i only get polygon features inside my definedrectangle. If i switch off my polygon layer(s) i get my lines.
from qgis.gui import QgsMapToolfrom qgis.core import QgsMapLayer,QgsPoint,QgsMapToPixel,QgsRectangle,QgsFeature, QgsFeatureRequest, QgsGeometry, QgsSpatialIndexfrom PyQt4.QtGui import QCursor, QPixmapfrom PyQt4.QtCore import Qt, QVariantfrom qgis.utils import ifaceimport itertoolsclass 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): feat=QgsFeature() 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()}index = QgsSpatialIndex()map(index.insertFeature,feature_dict.values())#define rectanglesdisplayrectangle = self.canvas.extent()#print displayrectangle.toString()ownrectangle = QgsRectangle(layerPoint.x(),layerPoint.y(),layerPoint.x()+200,layerPoint.y()+200)#print ownrectangle.toString()if displayrectangle.contains(ownrectangle): userectangle = ownrectangleelse: userectangle = displayrectangle# Auswahl der Features innerhalb des Rechteckesfor i in index.intersects(userectangle): print feature_dict.attributes()
أكثر...
- TOC: Line-Layer on, Polygon-Layer on: i only get Polygons which are inside
- TOC: Line-Layer on, Polygon-Layer off: i get my lines which are inside.
from qgis.gui import QgsMapToolfrom qgis.core import QgsMapLayer,QgsPoint,QgsMapToPixel,QgsRectangle,QgsFeature, QgsFeatureRequest, QgsGeometry, QgsSpatialIndexfrom PyQt4.QtGui import QCursor, QPixmapfrom PyQt4.QtCore import Qt, QVariantfrom qgis.utils import ifaceimport itertoolsclass 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): feat=QgsFeature() 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()}index = QgsSpatialIndex()map(index.insertFeature,feature_dict.values())#define rectanglesdisplayrectangle = self.canvas.extent()#print displayrectangle.toString()ownrectangle = QgsRectangle(layerPoint.x(),layerPoint.y(),layerPoint.x()+200,layerPoint.y()+200)#print ownrectangle.toString()if displayrectangle.contains(ownrectangle): userectangle = ownrectangleelse: userectangle = displayrectangle# Auswahl der Features innerhalb des Rechteckesfor i in index.intersects(userectangle): print feature_dict.attributes()
أكثر...