I have a large postcode dataset of ~1.6 million observations in Layer A.

I have a smaller dataset of ~5000 observations in Layer B.

Layer A contains an ID that matches postcodes with observations in Layer B.

I want to find the distance between every observation in Layer A, and the associated observation in Layer B, using the common ID field.

Note, I do not want to do Nearest Neighbor Analysis, there is a specific network structure.

  • So does each point in Layer A have a unique ID? And points in Layer B have a matching ID? Do multiple points in the same layer have the same ID? – Joseph Mar 15 '17 at 12:54
  • Each point in Layer A has a unique ID (a postcode), and then an ID that matches the observations in Layer B. There shouldn't be multiple points in the same layer with the same ID. – Thirst for Knowledge Mar 16 '17 at 7:25
  • Although the solution I posted deals with a fraction of points you have, perhaps you could consider using PostGIS for this? In which case, I'll happily delete my post as I don't think it will work well for that huge a dataset :) – Joseph Mar 17 '17 at 10:34

I would probably do the following:

  1. Copy/paste your 5000 points from Layer B into Layer A (or run a merge tool).
  2. Install the Points2One plugin from:

    Plugins > Manage and Install Plugins...
  3. Run this plugin on your merged layer.


  1. A sample point layer created from two separate layers (both with a common ID field) where I merged them:

    Sample point layer

  2. Then run the Points2One plugin selected the relevant options:

    Points2One plugin interface

  3. Output line shapefile should contain lines for each unique postcode:

    Output line shapefile

  4. Use the Field Calculator on the output to create a new field containing the distance values using the expression $length:

    Final result


Is Virtual Layer an option?

From menu Layer | Add Layer | Add/Edit Virtual layer.

enter image description here


SELECT LayerA.postcodes AS Postcodes, 
       st_distance(LayerA.geometry, LayerB.geometry) AS Distance
ON     LayerA.postcodes = LayerB.postcodes

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.