How to use System.Spatial.GeographyPoint

I need a function to calculate the distance between point A and point B.

The System.Spatial seems to be exactly what I need, but I cannot figure out how to use it.

IPoint is the interface that Latitude and Longitude provide, both of double types.

First try:

  public static double CalculateDistance(IPoint pointA, IPoint pointB) { var gp1 = GeographyPoint.Create(pointA.Latitude, pointA.Longitude); var gp2 = GeographyPoint.Create(pointB.Latitude, pointB.Longitude); return gp1.Distance(gp2) ?? 0; } 

This ends with a NotImplementedException at point1.Distance(point2) . The message is in French, but basically it says: "No operation is registered. Provide the operation using the SpatialImplementation.CurrentImplementation.Operations property."

Ok, I will try this:

 public static double CalculateDistance(IPoint pointA, IPoint pointB) { var gp1 = GeographyPoint.Create(pointA.Latitude, pointA.Longitude); var gp2 = GeographyPoint.Create(pointB.Latitude, pointB.Longitude); return SpatialImplementation.CurrentImplementation.Operations.Distance(gp1, gp2); } 

Now I got a NullReferenceException on SpatialImplementation.CurrentImplementation.Operations .

Msdn not many details.

Can anyone explain how to get these implementations?

+6
source share
1 answer

IMHO, you should not use System.Spatial . This is a semi-whitened library designed for OData to interact with Spatial, specifically for WCF data services.

You can check here for more information. In addition, most of the classes in this library are abstract, so you can implement most of them.

In addition, and even worse, they are not compatible with spatial support for the Entity Framework.

I am not sure which project you are using, but I would offer the following recommendations:

  • if you just need to calculate the distances between the points, just copy the implementation of the Haversin Formula or Vincenty Formula in C #.

  • if you need a spatial database just go to Entity Framework 5/6.

  • if you need further spatial operations in C #, use something like NetTopologySuite. There NuGet is for him, and it is very easy to use.

+5
source

Source: https://habr.com/ru/post/954948/


All Articles