Skip to content

Vertices found by findNearestNode() not correct #146

@Lokno

Description

@Lokno

Problem

I've added OpenStreetMap data to a PostgreGIS database using osm2pgrouting. The generated 'ways' table is EPSG: 4326 - WGS 84. The OpenStreetMap layer provided by QGIS EPSG:3857 - WGS 84 / Pseudo-Mercator. When I use buttonSelectTargetIds or buttonSelectSourceIds to add target or source nodes, clicking the map does not behave as expected and the selected node is no where near where I clicked.

To Reproduce

Using the pgRouting Layer plugin within QGIS with data in the above projections and clicking to select vertices on the map using the buttons next to "From Vids" or "To Vids" in the Arguments tab of the plugin GUI.

Importantly, I've discovered that I can fix this issue, at least in my specific case, by commenting out line 1106:

1106: #pt = trans.transform(pt)

Code in context of the function findNearestNode:

# emulate "matching.sql" - "find_nearest_node_within_distance"
def findNearestNode(self, args, pt):
    ''' finds the nearest node to selected point '''
    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())
    layerCrs = QgsCoordinateReferenceSystem()
    Utils.createFromSrid(layerCrs, args['srid'])
    trans = QgsCoordinateTransform(canvasCrs, layerCrs, QgsProject.instance())
    #pt = trans.transform(pt)
    rect = trans.transform(rect)

    args['canvas_srid'] = Utils.getCanvasSrid(canvasCrs)
    args['dbcanvas_srid'] = sql.Literal(args['canvas_srid'])
    args['x'] = sql.Literal(pt.x())
    args['y'] = sql.Literal(pt.y())
    args['SBBOX'] = self.getBBOX(args['srid'])[0]
    args['geom_t'] = Utils.getTransformedGeom(args['srid'], args['dbcanvas_srid'], args['geometry'])

    db, cur = self._exec_sql(PgrQ.get_closestVertexInfo(args))
    if cur:
        row = cur.fetchone()
        db.con.close()
        return True, row[0], row[2]
    else:
        return False, None, None

Also notice that the variable rect is not used after declaration. Seems like a project mismatch that is resolved in this case by simply not transforming the selected user selected point. I don't currently know enough about the plugin to implement a complete fix.

Sample Data

OSM files exported from https://www.openstreetmap.org/

Platform/versions

OS: Win11
QGIS: 3.28.0-Firenze
pgRoutingLayer: 3.0.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions