5

How to get the distance of two point geometry in meters using NetTopologySuite.

I have used the Distance() function, but I'm getting some values and I couldn't identify the unit of those values. Its for a c# application to neglect the tolerance value of 20 meters to be ignored.

using NetTopologySuite.Geometries;
using GeoAPI.Geometries;

private static double findistance()
{
   var geomFactory = new GeometryFactory(new PrecisionModel(), 4326);
   IGeometry geometry1 = geomFactory.CreatePoint(new Coordinate(12.977299, 77.571075));
   IGeometry geometry2 = geomFactory.CreatePoint(new Coordinate(12.977277, 77.571258));
   var distance = geometry1.Distance(geometry2);
   return distance;
}

I need to get the distance calculated in meters.

Anzeem S N
  • 141
  • 1
  • 8

2 Answers2

1

Per docs, SRID 4326 stands for definition below

GEOGCS["WGS 84",
  DATUM["WGS_1984",
    SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],
    AUTHORITY["EPSG","6326"]],
  PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],
  UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],
  AUTHORITY["EPSG","4326"]]

UNIT["degree"... tells us that the result you are getting has values in degrees. We can calculate distance in meters, if we transform these points into Projected Coordinated system. ProjNET4GeoAPI package could do that for us.

private static double findDistance()
{
    CoordinateTransformationFactory ctfac = new CoordinateTransformationFactory();

    var from = GeographicCoordinateSystem.WGS84;
    var to = ProjectedCoordinateSystem.WebMercator;

    var trans = ctfac.CreateFromCoordinateSystems(from, to);
    var mathTransform = trans.MathTransform;

    var p1Coordinate = new GeoAPI.Geometries.Coordinate(12.977299, 77.571075);
    var p2Coordinate = new GeoAPI.Geometries.Coordinate(12.977277, 77.571258);

    p1Coordinate = mathTransform.Transform(p1Coordinate);
    p2Coordinate = mathTransform.Transform(p2Coordinate);

    return p1Coordinate.Distance(p2Coordinate);
}
Yehor Androsov
  • 4,885
  • 2
  • 23
  • 40
  • This will result in huge errors, especially if geometries are far away from each other. – Dmytro Gokun Dec 16 '20 at 13:59
  • @DmytroGokun thanks for the feedback. I am new to this topic myself, and when I had similar question, this is the best thing I have found. for my project I used `ProjectedCoordinateSystem.WGS84_UTM(30, true)` since it covered required country, but this will be inaccurate if point is outside the zone? I thought `WebMercator` is something like "average" on accuracy when we don't know exact zone? – Yehor Androsov Dec 16 '20 at 15:24
  • @DmytroGokun will be glad if you post any suggestions to this answer – Yehor Androsov Dec 16 '20 at 15:25
  • No, I do not know a solution. Previously, i used Microsoft.SqlServer.Types to calculate the distance properly, but it is not available for .NET Core, only for .NET Framework. If that is fine for you, it still works. – Dmytro Gokun Dec 16 '20 at 15:49
  • @DmytroGokun: _"I used Microsoft.SqlServer.Types to calculate the distance"_ Do you know of any sample code illustrating how to do this? – InteXX Jul 08 '23 at 04:02
0

you can use the Pythagoras theorem to calculate the distance.

Here are the steps:

  1. Get Deltas
    • double int deltaX = Math.Abs(point1.X - point2.X);
    • double int deltaY = Math.Abs(point1.Y - point2.Y);
  2. Pythagoras formula
    • double distance = Math.Sqrt((deltaX * deltaX) + (deltaY * deltaY));

If you want to calculate a 3D point it is the same steps except calculate the DeltaZ and then the formula is distance = X^2 + Y^2 + Z^2

mtol
  • 9
  • 1