Jon P Smith
Jon P Smith

Reputation: 2439

Error when using NetTopologySuite TransformGeometry

I am trying to read in a shapefile that has UK Northing/Eastings coordinate system (BritishNationalGridOSGB36) into latitude/longitude (WGS1984). I found an excellent post here on how to use NetTopologySuite to do this. The reading of the shapefile works fine but I get problems when transforming the geography during the load.

I have pinned the problem down to the NetTopologySuite TransformGeometry method. If I transform a point using the DotSpatial ReprojectPoints it works correctly (once I has the right definition for BritishNationalGridOSGB36, see here for useful post on this). However NetTopologySuite's TransformGeometry gives me a wrong answer. The answer is wrong in two respects

  1. it has the Latitude value in the Longitude (should be -0.095399303)
  2. the Latitude is 181560 (should be 51.517489)

I want to use NetTopologySuite's TransformGeometry as it handles geometries much better than unpicking each coordinate and changing it. I also think I must have done something dum so I want to fix it and learn.

Here is my NUNIT test code.

class TestConvert
{
    //The standard DotSpatial definition, KnownCoordinateSystems.Projected.NationalGrids.BritishNationalGridOSGB36, is incorrect so needed defined string below
    const string BritishNationalGridOsgb36String = "+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +towgs84=446.448,-125.157,542.060,0.1502,0.2470,0.8421,-20.4894 +units=m +no_defs";

    readonly ProjectionInfo _britishNationalGridOsgb36 = ProjectionInfo.FromProj4String(BritishNationalGridOsgb36String);
    readonly ProjectionInfo _wgs84 = KnownCoordinateSystems.Geographic.World.WGS1984;

    [Test]
    public void ConvertAPointUsingDotSpatialReproject()
    {
        //SETUP
        var xy = new double[] { 532248.29992272425, 181560.30052819476 };
        var z = new double[] { 0 };

        //ATTEMPT
        Reproject.ReprojectPoints(xy, z, 
            _britishNationalGridOsgb36, _wgs84, 0, z.Length);

        //VERIFY            
        xy[0].ShouldEqualWithTolerance(-0.095399303, 0.001);
        xy[1].ShouldEqualWithTolerance(51.517489, 0.001);
    }


    [Test]
    public void ConvertAPointUsingNetTopologySuiteTransformGeometry()
    {
        //SETUP
        var factory = NetTopologySuite.Geometries.GeometryFactory.Default;
        var pointNatGrid = new NetTopologySuite.Geometries.Point(532248.29992272425, 181560.30052819476);    

        //ATTEMPT
        var transform = new DotSpatialMathTransform(
            _britishNationalGridOsgb36, _wgs84);
        var result = GeometryTransform.TransformGeometry(
            factory, pointNatGrid, transform);

        //VERIFY            
        result.GeometryType.ShouldEqual("Point");
        result.Coordinates.Count().ShouldEqual(1);
        result.Coordinates[0].X.ShouldEqualWithTolerance(-0.095399303, 0.001);
        result.Coordinates[0].Y.ShouldEqualWithTolerance(51.517489, 0.001);
    }

}

The first test passes, the second test fails on both of the ShouldEqualWithTolerance tests.

Your help would be appreciated.

Upvotes: 3

Views: 3457

Answers (1)

Jon P Smith
Jon P Smith

Reputation: 2439

On further research I found a bug in the NetTopologySuite.CoordinateSystems.Transformation.DotSpatial.Projections code. I reported it and it is now fixed. See here for details of the problem I found and confirmation that it has been fixed. http://code.google.com/p/nettopologysuite/issues/detail?id=152

Upvotes: 2

Related Questions