QGIS – Measure Distance Using Field Calculator

field-calculatorqgisvector

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 ) )

Best Answer

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

Related Question