예제 #1
0
    def canvasPressEvent(self, event):
        ''' Get point and transform to map coordinates '''
        # Set the active vector layer. Note that this method cannot be called unless the
        # currently active layer is the correct editing layer for the scenario edit type.
        self.activeVLayer = self.mainwindow.activeVLayer
        if self.activeVLayer == None: return
       
        qPoint = event.pos()
        transform = self.canvas.getCoordinateTransform()
        # returns a QgsPoint object in map coordinates
        self.qgsPoint = transform.toMapCoordinates(qPoint.x(), qPoint.y())
        
        #debugging
        print "Tools.addpoints.AddPoints().canvasPressEvent(): The original clicked point in map coordinates is " + str(self.qgsPoint)
        
        ''' 
            Check constraints on the added point for the scenario edit type, 
            and prompt the user if constraints are not met.
        '''
 
        # If the point is a culvert/bridge, check if it can be snapped to a new road in the scenario.
        if self.mainwindow.scenarioEditType == config.scenarioEditTypesList[0]:
            # returns the snapped qgs point if the point can be snapped to a new road, or returns False
            retval = shared.snapToNewRoad(self.mainwindow, qPoint) 
            if retval:
                self.qgsPoint = retval # if the point is returned, set it to be the new feature's geometry
        
        # If not editing a new road check constraints.
        # This method returns False if the constraints are not met.
        if not shared.checkConstraints(self.mainwindow, self.qgsPoint, qPoint):
            self.qgsPoint = None
            return
        # correct editing layer selected and constraints check OK so get the new attributes
        else: self.getNewAttributes()
예제 #2
0
 def canvasPressEvent(self, event):
     ''' Get point and transform to map coordinates '''
     # get the current activeVLayer from mainwindow
     self.activeVLayer = self.mainwindow.activeVLayer
     if self.activeVLayer == None:
         return
     point = event.pos()
     transform = self.canvas.getCoordinateTransform()
     # returns a QgsPoint object in map coordinates
     self.qgsPoint = transform.toMapCoordinates(point.x(), point.y())
     print "The point is " + str(self.qgsPoint)
     
     # check if the proper editing layer is selected
     currentLayerName = self.mainwindow.legend.currentItem().canvasLayer.layer().name()
     if shared.checkSelectedLayer(self.mainwindow, self.mainwindow.scenarioType, 
                                                      currentLayerName) == "Cancel":
         self.qgsPoint = None
         return # if wrong edit layer cancel drawing
     # Check constraints on the added point for the scenario edit type
     # and prompt the user if constraints are not met. Method returns False 
     # if the constraints are not met.
     if not shared.checkConstraints(self.mainwindow, self.qgsPoint):
         self.qgsPoint = None
         return
     else: pass
     
     # correct editing layer selected and constraints check OK so get the new attributes
     self.getNewAttributes()
    def canvasReleaseEvent(self, event):
        ''' Handle the mouse release event '''
        # debugging
        print"Tools.addlinespolygons.AddLinesPolygons.canvasReleaseEvent()"

        if self.down == True:
            if (self.started == False) and (event.button()== QtCore.Qt.RightButton):
                # We have a right button but have not started a poly... just return
                return
            # mouse is released so initialize rubber band
            if self.started==False:
                # Initialize Rubber Band
                self.rubberBand = QgsRubberBand(self.canvas, self.geom)
                self.rubberBand.setWidth(5)
                self.rubberBand.show()
                self.numberOfPoints = 0
                            
            # getCoordinateTransform returns a QgsMapToPixel object
            # which transforms between device coordinates and map coordinates 
            transform = self.canvas.getCoordinateTransform()
            # toMapCoordinates is a QgsMapToPixel method that 
            # takes a QPoint (from device) as a parameter and returns a 
            # QgsPoint object transformed to map coordinates
            self.qgsPoint = transform.toMapCoordinates(event.pos().x(),
                                            event.pos().y())
            
            # We have the first point of either a line or polygon. If it is a line then it is a new road
            # so check constraints for new roads. This method will also check if the point is near a 
            # previously saved new road and snap to it if it is.
            if self.started == False: # only checks the first point
                qPoint = event.pos()
                if self.geom == False:
                    # prompts user and returns false if constraints not met
                    if not shared.checkConstraints(self.mainwindow, self.qgsPoint, qPoint): 
                        return
            
            self.rubberBand.addPoint(self.qgsPoint)
            self.started=True
            self.numberOfPoints = self.numberOfPoints + 1
            if self.geom: # polygon
                if (event.button() == QtCore.Qt.RightButton) and (self.numberOfPoints > 2):
                    #self.down=False
                    #self.started=False
                    self.getNewAttributes()
            else: # line
                if (event.button() == QtCore.Qt.RightButton) and (self.numberOfPoints > 1):
                    #self.down=False
                    #self.started=False
                    self.getNewAttributes()