Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vertices found by findNearestNode() not correct #146

Open
Lokno opened this issue Dec 9, 2022 · 0 comments
Open

Vertices found by findNearestNode() not correct #146

Lokno opened this issue Dec 9, 2022 · 0 comments
Labels

Comments

@Lokno
Copy link

Lokno commented Dec 9, 2022

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

@Lokno Lokno added the bug label Dec 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant