I have the problem, that i only get polygon features inside my defined rectangle. If i switch off my polygon layer(s) i get my lines.
- 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.
In the tutorials, they say, that i will get all features which intersects my defined rectangle. If I use the index.nearestNeighbor function I will get poylgones and lines together. What did I wrong?
from qgis.gui import QgsMapTool
from qgis.core import QgsMapLayer,QgsPoint,QgsMapToPixel,QgsRectangle,QgsFeature, QgsFeatureRequest, QgsGeometry, QgsSpatialIndex
from PyQt4.QtGui import QCursor, QPixmap
from PyQt4.QtCore import Qt, QVariant
from qgis.utils import iface
import itertools
class 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 rectangles
displayrectangle = 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 = ownrectangle
else:
userectangle = displayrectangle
# Auswahl der Features innerhalb des Rechteckes
for i in index.intersects(userectangle):
print feature_dict[i].attributes()
# -*- coding: utf-8 -*-
from qgis.gui import QgsMapTool
from qgis.core import QgsMapLayer,QgsPoint,QgsMapToPixel,QgsRectangle,QgsFeature, QgsFeatureRequest, QgsGeometry, QgsSpatialIndex
from PyQt4.QtGui import QCursor, QPixmap
from PyQt4.QtCore import Qt, QVariant
from qgis.utils import iface
import itertools
class 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 = []
feat=QgsFeature()
index = QgsSpatialIndex()
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()
if displayrectangle.contains(ownrectangle):
userectangle = ownrectangle
else:
userectangle = displayrectangle
# Print features inside my rectangle
for i in index.intersects(userectangle):
print feature_dict[i].attributes()
No comments:
Post a Comment