示例#1
0
    def findNearestLink(self, args, pt):
        distance = self.iface.mapCanvas().getCoordinateTransform().mapUnitsPerPixel() * self.FIND_RADIUS
        rect = QgsRectangle(pt.x() - distance, pt.y() - distance, pt.x() + distance, pt.y() + distance)
        canvasCrs = Utils.getDestinationCrs(self.iface.mapCanvas().mapRenderer())
        try:
            dados = str(self.dock.comboConnections.currentText())
            db = self.actionsDb[dados].connect()

            con = db.con
            cur = con.cursor()
            srid, geomType = self.getSridAndGeomType(con, args)
            if self.iface.mapCanvas().hasCrsTransformEnabled():
                layerCrs = QgsCoordinateReferenceSystem()
                Utils.createFromSrid(layerCrs, srid)
                trans = QgsCoordinateTransform(canvasCrs, layerCrs)
                pt = trans.transform(pt)
                rect = trans.transform(rect)

            args["canvas_srid"] = Utils.getCanvasSrid(canvasCrs)
            args["srid"] = srid
            args["x"] = pt.x()
            args["y"] = pt.y()
            args["minx"] = rect.xMinimum()
            args["miny"] = rect.yMinimum()
            args["maxx"] = rect.xMaximum()
            args["maxy"] = rect.yMaximum()

            Utils.setTransformQuotes(args)

            # Searching for a link within the distance
            query = (
                """
            SELECT %(id)s,
                ST_Distance(
                    %(geometry)s,
                    ST_GeomFromText('POINT(%(x)f %(y)f)', %(srid)d)
                ) AS dist,
                ST_AsText(%(transform_s)s%(geometry)s%(transform_e)s)
                FROM %(edge_table)s
                WHERE ST_SetSRID('BOX3D(%(minx)f %(miny)f, %(maxx)f %(maxy)f)'::BOX3D, %(srid)d)
                    && %(geometry)s ORDER BY dist ASC LIMIT 1"""
                % args
            )

            ##QMessageBox.information(self.dock, self.dock.windowTitle(), query)
            cur = con.cursor()
            cur.execute(query)
            row = cur.fetchone()
            if not row:
                return False, None, None
            link = row[0]
            wkt = row[2]

            return True, link, wkt

        except psycopg2.DatabaseError, e:
            QApplication.restoreOverrideCursor()
            QMessageBox.critical(self.dock, self.dock.windowTitle(), "%s" % e)
            return False, None, None
示例#2
0
 def test_getDestinationCrs(self):
     QApplication(sys.argv)
     # create a map canvas widget
     canvas = QgsMapCanvas()
     canvas.setCanvasColor(QColor('white'))
     canvas.enableAntiAliasing(True)
     canvas.setMinimumSize(800, 600)
     # load a shapefile
     layer = QgsVectorLayer('test_data', 'poly', 'ogr')
     # add the layer to the canvas and zoom to it
     QgsMapLayerRegistry.instance().addMapLayer(layer)
     canvas.setLayerSet([QgsMapCanvasLayer(layer)])
     canvas.setExtent(layer.extent())
     self.assertIsNotNone(utils.getDestinationCrs(canvas))
示例#3
0
 def test_getDestinationCrs(self):
     QApplication(sys.argv)
     # create a map canvas widget
     canvas = QgsMapCanvas()
     canvas.setCanvasColor(QColor('white'))
     canvas.enableAntiAliasing(True)
     canvas.setMinimumSize(800, 600)
     # load a shapefile
     layer = QgsVectorLayer('test_data' ,'poly', 'ogr')
     # add the layer to the canvas and zoom to it
     QgsMapLayerRegistry.instance().addMapLayer(layer)
     canvas.setLayerSet([QgsMapCanvasLayer(layer)])
     canvas.setExtent(layer.extent())
     self.assertIsNotNone(utils.getDestinationCrs(canvas))
示例#4
0
    def run(self):
        QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))

        function = self.functions[str(
            self.dock.comboBoxFunction.currentText())]
        args = self.getArguments(function.getControlNames())

        empties = []
        for key in args.keys():
            if not args[key]:
                empties.append(key)

        if len(empties) > 0:
            QApplication.restoreOverrideCursor()
            QMessageBox.warning(
                self.dock, self.dock.windowTitle(),
                'Following argument is not specified.\n' + ','.join(empties))
            return

        try:
            dados = str(self.dock.comboConnections.currentText())
            db = self.actionsDb[dados].connect()

            con = db.con

            srid, geomType = self.getSridAndGeomType(con, args)
            function.prepare(con, args, geomType, self.canvasItemList)

            query = function.getQuery(args)
            ##QMessageBox.information(self.dock, self.dock.windowTitle(), query)

            cur = con.cursor()
            cur.execute(query)
            rows = cur.fetchall()

            args['srid'] = srid
            args['canvas_srid'] = Utils.getCanvasSrid(
                Utils.getDestinationCrs(self.iface.mapCanvas().mapRenderer()))
            Utils.setTransformQuotes(args)
            function.draw(rows, con, args, geomType, self.canvasItemList,
                          self.iface.mapCanvas())

        except psycopg2.DatabaseError, e:
            QApplication.restoreOverrideCursor()
            QMessageBox.critical(self.dock, self.dock.windowTitle(), '%s' % e)
示例#5
0
    def run(self):
        QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))

        function = self.functions[str(self.dock.comboBoxFunction.currentText())]
        args = self.getArguments(function.getControlNames())

        empties = []
        for key in args.keys():
            if not args[key]:
                empties.append(key)

        if len(empties) > 0:
            QApplication.restoreOverrideCursor()
            QMessageBox.warning(
                self.dock, self.dock.windowTitle(), "Following argument is not specified.\n" + ",".join(empties)
            )
            return

        try:
            dados = str(self.dock.comboConnections.currentText())
            db = self.actionsDb[dados].connect()

            con = db.con

            srid, geomType = self.getSridAndGeomType(con, args)
            function.prepare(con, args, geomType, self.canvasItemList)

            query = function.getQuery(args)
            ##QMessageBox.information(self.dock, self.dock.windowTitle(), query)

            cur = con.cursor()
            cur.execute(query)
            rows = cur.fetchall()

            args["srid"] = srid
            args["canvas_srid"] = Utils.getCanvasSrid(Utils.getDestinationCrs(self.iface.mapCanvas().mapRenderer()))
            Utils.setTransformQuotes(args)
            function.draw(rows, con, args, geomType, self.canvasItemList, self.iface.mapCanvas())

        except psycopg2.DatabaseError, e:
            QApplication.restoreOverrideCursor()
            QMessageBox.critical(self.dock, self.dock.windowTitle(), "%s" % e)
示例#6
0
    def findNearestLink(self, args, pt):
        distance = self.iface.mapCanvas().getCoordinateTransform(
        ).mapUnitsPerPixel() * self.FIND_RADIUS
        rect = QgsRectangle(pt.x() - distance,
                            pt.y() - distance,
                            pt.x() + distance,
                            pt.y() + distance)
        canvasCrs = Utils.getDestinationCrs(
            self.iface.mapCanvas().mapRenderer())
        try:
            dados = str(self.dock.comboConnections.currentText())
            db = self.actionsDb[dados].connect()

            con = db.con
            cur = con.cursor()
            srid, geomType = self.getSridAndGeomType(con, args)
            if self.iface.mapCanvas().hasCrsTransformEnabled():
                layerCrs = QgsCoordinateReferenceSystem()
                Utils.createFromSrid(layerCrs, srid)
                trans = QgsCoordinateTransform(canvasCrs, layerCrs)
                pt = trans.transform(pt)
                rect = trans.transform(rect)

            args['canvas_srid'] = Utils.getCanvasSrid(canvasCrs)
            args['srid'] = srid
            args['x'] = pt.x()
            args['y'] = pt.y()
            args['minx'] = rect.xMinimum()
            args['miny'] = rect.yMinimum()
            args['maxx'] = rect.xMaximum()
            args['maxy'] = rect.yMaximum()

            Utils.setTransformQuotes(args)

            # Searching for a link within the distance
            query = """
            SELECT %(id)s,
                ST_Distance(
                    %(geometry)s,
                    ST_GeomFromText('POINT(%(x)f %(y)f)', %(srid)d)
                ) AS dist,
                ST_AsText(%(transform_s)s%(geometry)s%(transform_e)s)
                FROM %(edge_table)s
                WHERE ST_SetSRID('BOX3D(%(minx)f %(miny)f, %(maxx)f %(maxy)f)'::BOX3D, %(srid)d)
                    && %(geometry)s ORDER BY dist ASC LIMIT 1""" % args

            ##QMessageBox.information(self.dock, self.dock.windowTitle(), query)
            cur = con.cursor()
            cur.execute(query)
            row = cur.fetchone()
            if not row:
                return False, None, None
            link = row[0]
            wkt = row[2]

            return True, link, wkt

        except psycopg2.DatabaseError, e:
            QApplication.restoreOverrideCursor()
            QMessageBox.critical(self.dock, self.dock.windowTitle(), '%s' % e)
            return False, None, None
示例#7
0
    def findNearestNode(self, args, pt):
        distance = self.iface.mapCanvas().getCoordinateTransform(
        ).mapUnitsPerPixel() * self.FIND_RADIUS
        rect = QgsRectangle(pt.x() - distance,
                            pt.y() - distance,
                            pt.x() + distance,
                            pt.y() + distance)
        canvasCrs = Utils.getDestinationCrs(
            self.iface.mapCanvas().mapRenderer())
        db = None
        try:
            dados = str(self.dock.comboConnections.currentText())
            db = self.actionsDb[dados].connect()

            con = db.con
            srid, geomType = self.getSridAndGeomType(con, args)
            if self.iface.mapCanvas().hasCrsTransformEnabled():
                layerCrs = QgsCoordinateReferenceSystem()
                Utils.createFromSrid(layerCrs, srid)
                trans = QgsCoordinateTransform(canvasCrs, layerCrs)
                pt = trans.transform(pt)
                rect = trans.transform(rect)

            args['canvas_srid'] = Utils.getCanvasSrid(canvasCrs)
            args['srid'] = srid
            args['x'] = pt.x()
            args['y'] = pt.y()
            args['minx'] = rect.xMinimum()
            args['miny'] = rect.yMinimum()
            args['maxx'] = rect.xMaximum()
            args['maxy'] = rect.yMaximum()

            Utils.setStartPoint(geomType, args)
            Utils.setEndPoint(geomType, args)
            Utils.setTransformQuotes(args)

            # Getting nearest source
            query1 = """
            SELECT %(source)s,
                ST_Distance(
                    %(startpoint)s,
                    ST_GeomFromText('POINT(%(x)f %(y)f)', %(srid)d)
                ) AS dist,
                ST_AsText(%(transform_s)s%(startpoint)s%(transform_e)s)
                FROM %(edge_table)s
                WHERE ST_SetSRID('BOX3D(%(minx)f %(miny)f, %(maxx)f %(maxy)f)'::BOX3D, %(srid)d)
                    && %(geometry)s ORDER BY dist ASC LIMIT 1""" % args

            ##QMessageBox.information(self.dock, self.dock.windowTitle(), query1)
            cur1 = con.cursor()
            cur1.execute(query1)
            row1 = cur1.fetchone()
            d1 = None
            source = None
            wkt1 = None
            if row1:
                d1 = row1[1]
                source = row1[0]
                wkt1 = row1[2]

            # Getting nearest target
            query2 = """
            SELECT %(target)s,
                ST_Distance(
                    %(endpoint)s,
                    ST_GeomFromText('POINT(%(x)f %(y)f)', %(srid)d)
                ) AS dist,
                ST_AsText(%(transform_s)s%(endpoint)s%(transform_e)s)
                FROM %(edge_table)s
                WHERE ST_SetSRID('BOX3D(%(minx)f %(miny)f, %(maxx)f %(maxy)f)'::BOX3D, %(srid)d)
                    && %(geometry)s ORDER BY dist ASC LIMIT 1""" % args

            ##QMessageBox.information(self.dock, self.dock.windowTitle(), query2)
            cur2 = con.cursor()
            cur2.execute(query2)
            row2 = cur2.fetchone()
            d2 = None
            target = None
            wkt2 = None
            if row2:
                d2 = row2[1]
                target = row2[0]
                wkt2 = row2[2]

            # Checking what is nearer - source or target
            d = None
            node = None
            wkt = None
            if d1 and (not d2):
                node = source
                d = d1
                wkt = wkt1
            elif (not d1) and d2:
                node = target
                d = d2
                wkt = wkt2
            elif d1 and d2:
                if d1 < d2:
                    node = source
                    d = d1
                    wkt = wkt1
                else:
                    node = target
                    d = d2
                    wkt = wkt2

            ##QMessageBox.information(self.dock, self.dock.windowTitle(), str(d))
            if (d == None) or (d > distance):
                node = None
                wkt = None
                return False, None, None

            return True, node, wkt

        except psycopg2.DatabaseError, e:
            QApplication.restoreOverrideCursor()
            QMessageBox.critical(self.dock, self.dock.windowTitle(), '%s' % e)
            return False, None, None
示例#8
0
    def findNearestNode(self, args, pt):
        distance = self.iface.mapCanvas().getCoordinateTransform().mapUnitsPerPixel() * self.FIND_RADIUS
        rect = QgsRectangle(pt.x() - distance, pt.y() - distance, pt.x() + distance, pt.y() + distance)
        canvasCrs = Utils.getDestinationCrs(self.iface.mapCanvas().mapRenderer())
        db = None
        try:
            dados = str(self.dock.comboConnections.currentText())
            db = self.actionsDb[dados].connect()

            con = db.con
            srid, geomType = self.getSridAndGeomType(con, args)
            if self.iface.mapCanvas().hasCrsTransformEnabled():
                layerCrs = QgsCoordinateReferenceSystem()
                Utils.createFromSrid(layerCrs, srid)
                trans = QgsCoordinateTransform(canvasCrs, layerCrs)
                pt = trans.transform(pt)
                rect = trans.transform(rect)

            args["canvas_srid"] = Utils.getCanvasSrid(canvasCrs)
            args["srid"] = srid
            args["x"] = pt.x()
            args["y"] = pt.y()
            args["minx"] = rect.xMinimum()
            args["miny"] = rect.yMinimum()
            args["maxx"] = rect.xMaximum()
            args["maxy"] = rect.yMaximum()

            Utils.setStartPoint(geomType, args)
            Utils.setEndPoint(geomType, args)
            Utils.setTransformQuotes(args)

            # Getting nearest source
            query1 = (
                """
            SELECT %(source)s,
                ST_Distance(
                    %(startpoint)s,
                    ST_GeomFromText('POINT(%(x)f %(y)f)', %(srid)d)
                ) AS dist,
                ST_AsText(%(transform_s)s%(startpoint)s%(transform_e)s)
                FROM %(edge_table)s
                WHERE ST_SetSRID('BOX3D(%(minx)f %(miny)f, %(maxx)f %(maxy)f)'::BOX3D, %(srid)d)
                    && %(geometry)s ORDER BY dist ASC LIMIT 1"""
                % args
            )

            ##QMessageBox.information(self.dock, self.dock.windowTitle(), query1)
            cur1 = con.cursor()
            cur1.execute(query1)
            row1 = cur1.fetchone()
            d1 = None
            source = None
            wkt1 = None
            if row1:
                d1 = row1[1]
                source = row1[0]
                wkt1 = row1[2]

            # Getting nearest target
            query2 = (
                """
            SELECT %(target)s,
                ST_Distance(
                    %(endpoint)s,
                    ST_GeomFromText('POINT(%(x)f %(y)f)', %(srid)d)
                ) AS dist,
                ST_AsText(%(transform_s)s%(endpoint)s%(transform_e)s)
                FROM %(edge_table)s
                WHERE ST_SetSRID('BOX3D(%(minx)f %(miny)f, %(maxx)f %(maxy)f)'::BOX3D, %(srid)d)
                    && %(geometry)s ORDER BY dist ASC LIMIT 1"""
                % args
            )

            ##QMessageBox.information(self.dock, self.dock.windowTitle(), query2)
            cur2 = con.cursor()
            cur2.execute(query2)
            row2 = cur2.fetchone()
            d2 = None
            target = None
            wkt2 = None
            if row2:
                d2 = row2[1]
                target = row2[0]
                wkt2 = row2[2]

            # Checking what is nearer - source or target
            d = None
            node = None
            wkt = None
            if d1 and (not d2):
                node = source
                d = d1
                wkt = wkt1
            elif (not d1) and d2:
                node = target
                d = d2
                wkt = wkt2
            elif d1 and d2:
                if d1 < d2:
                    node = source
                    d = d1
                    wkt = wkt1
                else:
                    node = target
                    d = d2
                    wkt = wkt2

            ##QMessageBox.information(self.dock, self.dock.windowTitle(), str(d))
            if (d == None) or (d > distance):
                node = None
                wkt = None
                return False, None, None

            return True, node, wkt

        except psycopg2.DatabaseError, e:
            QApplication.restoreOverrideCursor()
            QMessageBox.critical(self.dock, self.dock.windowTitle(), "%s" % e)
            return False, None, None