2

I have a point shapefile with 80K+ records. Shapefile is projected in wgs84. The shapefile contains fields with the latitude and longitude of the point. I need a new field that contains a distance from a known point (Point B) in each record. Point B is the same for every record in the shapefile. There is a distance function in the field calculator (field calculator -> geometry -> distance). But I can't make sense of the syntax.

Example:

distance( geom_from_wkt( 'POINT(4 4)' ), geom_from_wkt( 'POINT(4 8)' ) ) → 4

What I want to do is measure the distance from the geometry of the current point to the geometry of a point I supply (for example point at "25.906543, -80.546800"). How do I refer to the current record using that tool and how to a create the new geometry? Is is something like:

distance( geom_from_wkt( 'POINT(25.906543, -80.546800)' ), 
($geometry ) )
3
  • 1
    Have you had a look at the tool distance matrix? If so, how does it not suit your needs? Also, distance calculations using the field calculator in QGIS are done in the CRS of the layer, so based on a WGS84 layer you'd get distance in degree.
    – Erik
    Commented Sep 11, 2019 at 13:29
  • Disclaimer: I have experience with ESRI software, so I my comments might use ESRI-specific terminology. Distance matrix might work. If I understand it correctly, it will create a new table with fields for the unique id of the "from" table and one with the calculated distance. I could then join those tables on the unique ID and then calculate the distance into a new field in the "from" table.
    – bdwyer
    Commented Sep 11, 2019 at 17:21
  • Thanks for the response. I went with babel's solution because it added a field with the distance. I didn't have to create another table.
    – bdwyer
    Commented Sep 11, 2019 at 19:15

1 Answer 1

1

This should work:

length(make_line(make_point(25.906543, -80.546800),$geometry))

you create a line (make_line), starting at the point with your fixed coordinates (created with the command make_point(25.906543, -80.546800)) and ending at the second point, defined with $geometry as the current point of your layer. You then measure the length with the expression length() If you create a new field with the field-calculator, you get a new field containing the distances for each point of your layer to the point at 25.906543, -80.546800

3
  • Nitpicky question: what dictates what units the length is in?
    – bdwyer
    Commented Sep 11, 2019 at 17:24
  • The CRS of your layer: check the CRS settings for the units used
    – Babel
    Commented Sep 11, 2019 at 18:30
  • That worked. Thanks for the response.
    – bdwyer
    Commented Sep 11, 2019 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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