Пример #1
0
    def canvasReleaseEvent(self,event):
        #Get the click
        x = event.pos().x()
        y = event.pos().y()

        layer = self.canvas.currentLayer()
        visibleLayers = []

        if self.allLayers:
            legendIface = self.iface.legendInterface()

            for aLayer in legendIface.layers():
                if 0 == aLayer.type():
                    if legendIface.isLayerVisible(aLayer) and \
                            self.isPolygonLayer(aLayer):
                        visibleLayers.append(aLayer)
        else:
            if layer <> None:
                visibleLayers.append(layer)

        if len(visibleLayers) > 0:
            #the clicked point is our starting point
            startingPoint = QtCore.QPoint(x,y)
            mapToPixel = self.canvas.getCoordinateTransform()
            thisQgsPoint = mapToPixel.toMapCoordinates(startingPoint)
            multiGeom = None

            for aLayer in visibleLayers:
                if not self.allLayers and aLayer.selectedFeatureCount() > 0:
                    #we assume, that the gap is between the selected polyons
                    hadSelection = True
                else:
                    hadSelection = False
                    spatialIndex = dtutils.dtSpatialindex(aLayer)
                    # get the 100 closest Features
                    featureIds = spatialIndex.nearestNeighbor(thisQgsPoint, 100)
                    aLayer.setSelectedFeatures(featureIds)

                multiGeom = dtutils.dtCombineSelectedPolygons(aLayer, self.iface, multiGeom)

                if self.allLayers or not hadSelection:
                    aLayer.removeSelection()

                if multiGeom == None:
                    return None

            if multiGeom != None:
                rings = dtutils.dtExtractRings(multiGeom)

                if len(rings) > 0:
                    for aRing in rings:
                        if aRing.contains(thisQgsPoint):
                            self.gapSelected.emit([aRing])
                            break
Пример #2
0
    def getFeatureForPoint(self, layer, startingPoint, inRing = False):
        '''
        return the feature this QPoint is in (polygon layer)
        or this QPoint snaps to (point or line layer)
        '''
        result = []

        if self.isPolygonLayer(layer):
            mapToPixel = self.canvas.getCoordinateTransform()
            #thisQgsPoint = mapToPixel.toMapCoordinates(startingPoint)
            thisQgsPoint = self.transformed(layer, mapToPixel.toMapCoordinates(startingPoint))
            spatialIndex = dtutils.dtSpatialindex(layer)
            featureIds = spatialIndex.nearestNeighbor(thisQgsPoint, 0)
            # if we use 0 as neighborCount then only features that contain the point
            # are included

            for fid in featureIds:
                feat = dtutils.dtGetFeatureForId(layer, fid)

                if feat != None:
                    geom = QgsGeometry(feat.geometry())

                    if geom.contains(thisQgsPoint):
                        result.append(feat)
                        result.append([])
                        return result
                        break
                    else:
                        if inRing:
                            rings = dtutils.dtExtractRings(geom)

                            if len(rings) > 0:
                                for aRing in rings:
                                    if aRing.contains(thisQgsPoint):
                                        result.append(feat)
                                        result.append([])
                                        result.append(aRing)
                                        return result
                                        break
        else:
            #we need a snapper, so we use the MapCanvas snapper
            snapper = self.canvas.snappingUtils()
            snapper.setCurrentLayer(layer)
            # snapType = 0: no snap, 1 = vertex, 2 vertex & segment, 3 = segment
            snapMatch = snapper.snapToCurrentLayer(startingPoint, QgsPointLocator.All)

            if not snapMatch.isValid():
                dtutils.showSnapSettingsWarning(self.iface)
            else:
                feat = dtutils.dtGetFeatureForId(layer, snapMatch.featureId())

                if feat != None:
                    result.append(feat)

                    if snapMatch.hasVertex():
                        result.append([snapMatch.point(), None])

                    if snapMatch.hasEdge():
                        result.append(snapMatch.edgePoints())

                    return result

        return result