0

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.

Dmytro Gokun
  • 405
  • 3
  • 24

1 Answers1

0

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);
    }
}
siskat
  • 55
  • 1
  • 5
  • Thanks a lot for the suggestion, but you missed this part in my question: "I need an ability to calculate distance between two arbitrary features - points, multilines, polygons and not just between two points" – Dmytro Gokun Nov 16 '20 at 08:18
  • @DmytroGokun You can break it down to `IPosition` which holds lat/lon and is inherited through all classes dealing with coordinates. Points, multilines, polygons either inherit `IPosition`, or holding a collection of it. How you define the point of measure from a polygon for example, depends on your needs. You need to calculate this or maybe just iterate and find the smallest `IPosition` inside the collection. – siskat Nov 16 '20 at 12:19