2

I have two line layers assume A and B and each layer contains multiple features. I have selected one feature in layer A, by using that I need to find nearest nodes of a features in layer B .

I tried this

for rfeat in LayerA.selectedFeatures():
    rgeom = rfeat.geometry()
    geom_buffer = rgeom.buffer(50, -1)
    for feats in LayerB.getFeatures():
        sgeom = feats.geometry()
        if sgeom.intersects(geom_buffer ) :
            print(feats["id"])

but I am getting all features in layerB.

  • Yes, based on one selected feature in layerA i need to find nearest nodes of features exists in layerB. – Virat ABD Sep 11 '18 at 9:34
1

Here is one workflow:

  1. Get selected layerA feature xy centroid coordinate (Finding middle point (midpoint) of line in QGIS?)
  2. Use xy coordinate from step 1 with nearestNeighbor() to get nearest featureB to featureA (Finding nearest line to point in QGIS?)
  3. Create an in-memory point layer and add nearest featureB vertex points to the in-memory point layer (not exactly but something like @Joseph answer here Can vector layer get start point and end point of line using PyQGIS?, you'll have to iterate over the geom object)
  4. Use nearestNeighbor() method again and featureA xy centroid coordinate to find nearest in-memory point feature

This should return the nearest feature and point vertex of featureB to featureA.

| improve this answer | |
  • Thank you for the information , i will do step by step as you mentioned .Hope it works – Virat ABD Sep 11 '18 at 13:36
  • Added another helpful link to step 3 – artwork21 Sep 11 '18 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.