0

I am attempting to project a geometry using the GDAL c# bindings and I'm getting a ArgumentOutOfRangeException ('Received NULL pointer') exception on the Transform methods of Geometry. I'm also getting the same exception when attempting to project a point using a OSGeo.OSR.CoordinateTransformation (e.g. https://gis.stackexchange.com/a/61574/31).

I am using the DLL in FWTools 2.4.7\csharp.

Has anyone seen this before and know what the fix is?

Here's a test that creates the exception for me:

        [TestMethod()]
        public void TransformPolygonReturnGeoJSON()
        {            
            string jsonLambert = string.Empty;
            string projectedJson = string.Empty;

            string wkt = "POLYGON ((1831321.945291 412165.262452,1831324.478486 412152.596478,1831312.445811 412108.265569,1831298.513239 412108.265569,1831287.747162 412108.265569,1831275.714486 412102.565881,1831271.914694 412100.032686,1831266.215006 412098.766089,1831253.549032 412188.694503,1831321.945291 412165.262452))";
            int srid = 102009;

            int outputSRID = 26918;

            string sridWKT  = string.Format("EPSG:{0}", srid);

            string outsridWKT = string.Format("EPSG:{0}", outputSRID);

            var g = Geometry.CreateFromWkt(wkt);

            var sr = new OSGeo.OSR.SpatialReference(sridWKT);

            g.AssignSpatialReference(sr);

            jsonLambert = g.ExportToJson();

            Assert.IsFalse(string.IsNullOrEmpty(jsonLambert));

            var outSR = new OSGeo.OSR.SpatialReference(outsridWKT);

            try
            {
                var result = g.TransformTo(outSR); // <--- Exception occurs here

                Trace.WriteLine(string.Format("result={0}", result));

                projectedJson = g.ExportToJson();

                Assert.IsFalse(string.IsNullOrEmpty(projectedJson));
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
                Trace.WriteLine(ex.StackTrace);

                throw;
            }
        }

1 Answer 1

1

It looks like the "EPSG: {0]" format doesn't work with the Transform methods.

However, I (actually, credit goes to a co-worker of mine) was able to get the Transforms working with a PROJCS spatial reference definition. And I was also able to get it working with the SpatialReference.ImportFromEPSG.

[TestMethod()]
public void TransformPolygonPROJCSReturnGeoJSON()
{
    string jsonLambert = string.Empty;
    string projectedJson = string.Empty;

    string wkt = "POLYGON ((1831321.945291 412165.262452,1831324.478486 412152.596478,1831312.445811 412108.265569,1831298.513239 412108.265569,1831287.747162 412108.265569,1831275.714486 412102.565881,1831271.914694 412100.032686,1831266.215006 412098.766089,1831253.549032 412188.694503,1831321.945291 412165.262452))";

    int srid = 102009;

    int outputSRID = 26918;

    // string sridWKT  = string.Format("EPSG:{0}", srid);
    string sridWKT = "PROJCS[\"North_America_Lambert_Conformal_Conic\",GEOGCS[\"GCS_North_American_1983\",DATUM[\"North_American_Datum_1983\",SPHEROID[\"GRS_1980\",6378137,298.257222101]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.017453292519943295]],PROJECTION[\"Lambert_Conformal_Conic_2SP\"],PARAMETER[\"False_Easting\",0],PARAMETER[\"False_Northing\",0],PARAMETER[\"Central_Meridian\",-96],PARAMETER[\"Standard_Parallel_1\",20],PARAMETER[\"Standard_Parallel_2\",60],PARAMETER[\"Latitude_Of_Origin\",40],UNIT[\"Meter\",1],AUTHORITY[\"EPSG\",\"102009\"]]";


    //string outsridWKT = string.Format("EPSG:{0}", outputSRID);

    string outsridWKT = "PROJCS[\"NAD83/UTMzone18N\",GEOGCS[\"NAD83\",DATUM[\"North_American_Datum_1983\",SPHEROID[\"GRS1980\",6378137,298.257222101,AUTHORITY[\"EPSG\",\"7019\"]],AUTHORITY[\"EPSG\",\"6269\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4269\"]],UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"latitude_of_origin\",0],PARAMETER[\"central_meridian\",-75],PARAMETER[\"scale_factor\",0.9996],PARAMETER[\"false_easting\",500000],PARAMETER[\"false_northing\",0],AUTHORITY[\"EPSG\",\"26918\"],AXIS[\"Easting\",EAST],AXIS[\"Northing\",NORTH]]";


    var g = Geometry.CreateFromWkt(wkt);

    var sr = new OSGeo.OSR.SpatialReference(sridWKT);

    g.AssignSpatialReference(sr);

    jsonLambert = g.ExportToJson();

    Assert.IsFalse(string.IsNullOrEmpty(jsonLambert));

    var outSR = new OSGeo.OSR.SpatialReference(outsridWKT);

    try
    {
        var result = g.TransformTo(outSR); 

        Trace.WriteLine(string.Format("result={0}", result));

        projectedJson = g.ExportToJson();

        Assert.IsFalse(string.IsNullOrEmpty(projectedJson));
    }
    catch (Exception ex)
    {
        Trace.WriteLine(ex.Message);
        Trace.WriteLine(ex.StackTrace);

        throw;
    }
}

[TestMethod()]
public void TransformPolygonImportEPSGReturnGeoJSON()
{
    string jsonLambert = string.Empty;
    string projectedJson = string.Empty;

    string wkt = "POLYGON ((1831321.945291 412165.262452,1831324.478486 412152.596478,1831312.445811 412108.265569,1831298.513239 412108.265569,1831287.747162 412108.265569,1831275.714486 412102.565881,1831271.914694 412100.032686,1831266.215006 412098.766089,1831253.549032 412188.694503,1831321.945291 412165.262452))";

    int srid = 102009;

    int outputSRID = 26918;

    var g = Geometry.CreateFromWkt(wkt);

    var sr = new OSGeo.OSR.SpatialReference(string.Empty);
    sr.ImportFromEPSG(srid);           

    g.AssignSpatialReference(sr);

    jsonLambert = g.ExportToJson();

    Assert.IsFalse(string.IsNullOrEmpty(jsonLambert));

    var outSR = new OSGeo.OSR.SpatialReference(string.Empty);
    outSR.ImportFromEPSG(outputSRID);

    try
    {
        var result = g.TransformTo(outSR); 

        Trace.WriteLine(string.Format("result={0}", result));

        projectedJson = g.ExportToJson();

        Assert.IsFalse(string.IsNullOrEmpty(projectedJson));
    }
    catch (Exception ex)
    {
        Trace.WriteLine(ex.Message);
        Trace.WriteLine(ex.StackTrace);

        throw;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.