Search code examples
c#spatialnettopologysuite

How to store/query co-ordiantes for calculating the distance between in meters?


I am storing co-ordinates in my database like this

var geometryFactory = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326);

var point = geometryFactory.CreatePoint(new NetTopologySuite.Geometries.Coordinate(
    command.GeoCoordinate!.Longitude,
    command.GeoCoordinate!.Latitude));

I then need to query the db for any points within a certain distance of some co-ordinate, so I pass in an expression like this

x => x.Location.Distance(inputPoint) < distance

However, the distance returned just uses a normal pythagarus calculation, giving me a result in degrees rather than meters or miles.

I can manually calculate the distance in meters using haversine formula, but passing that in to my expression reults in a linq error:

The LINQ expression could not be translated. Either rewrite the query in a form that can be translated, or switch to ..

How can I store/query this data such that I can pass a filter to ef core and get back a list of results within the given distance?


Solution

  • Do you really need to work with EPSG:4326 (Lat/lon coordinate pairs, rather than projected coordinates like UTM)?

    If you could switch to UTM coordinates you would have meters directly in the database and wouldn't need to worry about conversion.