Search code examples
c#gisnettopologysuite

how to calculate two geometry distance via meters in NetTopologySuite?


I use PGSQL to get geometry data.

 var hca = new WKTReader().Read(
        $"SRID=4326;{hcaShape}");
 var point = new GeometryFactory().CreatePoint(new Coordinate(x, y));
 var res = hca.Distance(point)

The result Shows like:'0.00009xxx' But I want meters.

Also important thing is this case is not point to point. How to convert result to meters?(I'm new with NetTopologySuite)


Solution

  • I completed this task by using ProjNet to project the geometries to a metric CoordinateSystem

    public static NetTopologySuite.Geometries.Geometry ProjectGeometry(NetTopologySuite.Geometries.Geometry geom,
        string fromWkt, string toWkt)
    {
        var sourceCoordSystem = new CoordinateSystemFactory().CreateFromWkt(fromWkt);
        var targetCoordSystem = new CoordinateSystemFactory().CreateFromWkt(toWkt);
    
        var trans = new CoordinateTransformationFactory().CreateFromCoordinateSystems(sourceCoordSystem,
            targetCoordSystem);
    
        var projGeom = Transform(geom, transform: trans.MathTransform);
    
        return projGeom;
    }