Calculate distance between two GeoJSON features

982 views Asked by At

i'm using GeoJSON.Net library (https://github.com/GeoJSON-Net/GeoJSON.Net) to create some GeoJSON features in my code. Points, multilines etc. All that works fine until i need to calculate distance between some pair of features. This library does not have any ability to do that.

I've tried using GeoJSON.Net.Contrib.MsSqlSpatial (https://www.nuget.org/packages/GeoJSON.Net.Contrib.MsSqlSpatial) to convert features to SqlGeographies and then use the method on that type to calculate the distance. That works perfectly with one caveat: it's .NET Framework-only. The library is not available for .NET Core.

Please note, that I need an ability to calculate distance between two arbitrary features - points, multilines, polygons and not just between two points. This has to be done for SRID 4326 (WGS84).

Any idea?

Best regards, Dmytro.

1

There are 1 answers

2
siskat On

GeoJSON.Net is just a library to de-serialize GeoJSON according to RFC 7946.

For calculating distances you could either write your own implementation, or use one of the existing libraries like Geolocation.

For convenience you can write some extension methods.

public static class PositionExtension
{
    public static double GetDistanceTo(this Position origin, Position destination,
        DistanceUnit unit = DistanceUnit.Kilometers)
    {
        return GeoCalculator.GetDistance(
            origin.Latitude, origin.Longitude,
            destination.Latitude, destination.Longitude, distanceUnit: unit);
    }
}