Search code examples
xamarinnettopologysuite

NetTopologySuite convert coordinates to another coordinate system


I am trying to convert my coordinates to a different coordinate system using the NetTopologySuite. But I cannot get it to work. My intial convertion is to go from WGS84 to Lambert72.

I found this sample somewhere on stackoverflow but I cannot get it to work:

public static double CalculateDistance(this MvxCoordinates baseLocation, double targetLat, double targetLon)
    {
        CoordinateSystemFactory csFact = new CoordinateSystemFactory();
        CoordinateTransformationFactory ctFact = new CoordinateTransformationFactory();

        ICoordinateSystem utm35ETRS = Coo.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])));
    }

The big problem with this code is that it uses ProjNET4GeoAPI, and this is not compatible with Xamarin core projects!


Solution

  • Latest prereleases of ProjNet4GeoAPI now support .NET Standard 1.0 and .NET Standard 2.0 targets.