NetTopologySuite – Transforming Point Using NetTopologySuite

coordinate systemnettopologysuite

I'm exploring how we can use NetTopologySuite to address a geoprocessing task. based on some code sample I found, I was able to piece together a sample application that reads a shapefile and checks if a point falls within a polygon. The issue is, my point is in WGS84 but the shapefile is in EPSG:2810. I need to transform the input point to the CRS of the shapefile.

How do I convert a point from WGS to EPSG:2810 using NetTopologySuite 1.13.3?

Here's some of the code I have so far:

string shpFilename = @"C:\...\polygons.shp";
GeometryFactory factory = new GeometryFactory();
var coord = new Coordinate(-90.00, 45.00);
var point = factory.CreatePoint(coord);

I should also note that when I manually convert from WGS84 to the projected system and use those converted values, my code works as designed. This confirms this is a matter of transforming the coordinates before attempting to determine if they fall within a polygon.

Best Answer

The standard way of making point-to-point coordinate conversions in NTS is by using the ProjNet4GeoAPI library.

Here is a coordinate transformation example taken from the unit tests:

public void TestTransformListOfCoordinates()
{
    CoordinateSystemFactory csFact = new CoordinateSystemFactory();
    CoordinateTransformationFactory ctFact = new CoordinateTransformationFactory();

    ICoordinateSystem utm35ETRS = csFact.CreateFromWkt(
            "PROJCS[\"ETRS89 / ETRS-TM35\",GEOGCS[\"ETRS89\",DATUM[\"D_ETRS_1989\",SPHEROID[\"GRS_1980\",6378137,298.257222101]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.017453292519943295]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"latitude_of_origin\",0],PARAMETER[\"central_meridian\",27],PARAMETER[\"scale_factor\",0.9996],PARAMETER[\"false_easting\",500000],PARAMETER[\"false_northing\",0],UNIT[\"Meter\",1]]");

    IProjectedCoordinateSystem utm33 = ProjectedCoordinateSystem.WGS84_UTM(33, true);

    ICoordinateTransformation trans = ctFact.CreateFromCoordinateSystems(utm35ETRS, utm33);

    Coordinate[] points = new Coordinate[]
    {
        new Coordinate(290586.087, 6714000), new Coordinate(290586.392, 6713996.224),
        new Coordinate(290590.133, 6713973.772), new Coordinate(290594.111, 6713957.416),
        new Coordinate(290596.615, 6713943.567), new Coordinate(290596.701, 6713939.485)
    };

    Coordinate[] tpoints = trans.MathTransform.TransformList(points).ToArray();
    for (int i = 0; i < points.Length; i++)
        Assert.That(tpoints[i].Equals(trans.MathTransform.Transform(points[i])));
}
Related Question