2

I have a C# project that may load several vector data files and then needs to transform the spatial reference system (whichever used) to WGS84. I tried mostly the way described here, and also cannot find anything I did severely wrong after studying this. However, any attempt fails.

My code starts with a pre-loaded layer, everything fine here. I obtain the SRS used there (for debug purposes, I also export to WKT for checking). I then setup a new SRS which gets imported from EPSG4326 (also checked, everything fine here). After that I setup the CoordinateTransformation to use. Finally, I acquire the features one by one (I use polygon or multipolygon data), iterate over them, get the references to each geometry, and perform "Transform" on it (I also tried "TransformTo", but to no avail). However, the code fails here every time and the reprojection is not performed.

I tried several source files using several SRSes, but the result is always the same - even if the source file already uses WGS84.

What am I doing wrong? Assuming installed NuGet packages "MaxRev.Gdal.Core" and "MaxRev.Gdal.WindowsRuntime.Minimal", try

MaxRev.Gdal.Core.GdalBase.ConfigureAll();
OSGeo.OGR.Ogr.RegisterAll();

OSGeo.GDAL.Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
OSGeo.GDAL.Gdal.SetConfigOption("SHAPE_ENCODING", "");
OSGeo.GDAL.Gdal.SetConfigOption("PROJ_DEBUG", "5");

OSGeo.OGR.Driver drv = OSGeo.OGR.Ogr.GetDriverByName("ESRI Shapefile");
OSGeo.OGR.DataSource ds = drv.Open(@"C:\[EnterFolderHere]\Test.shp", 0);
OSGeo.OGR.Layer MyLayer = ds.GetLayerByIndex(0);

OSGeo.OSR.SpatialReference FromSRS = MyLayer.GetSpatialRef();
string CheckSRS;
FromSRS.ExportToWkt(out CheckSRS, null); // In my example, this is ETRS89 / UTM zone 33N (zE-N).

OSGeo.OSR.SpatialReference ToSRS = new OSGeo.OSR.SpatialReference(null);
ToSRS.ImportFromEPSG(4326);
ToSRS.SetAxisMappingStrategy(OSGeo.OSR.AxisMappingStrategy.OAMS_TRADITIONAL_GIS_ORDER);
ToSRS.ExportToWkt(out CheckSRS, null);

OSGeo.OSR.CoordinateTransformation CT = new OSGeo.OSR.CoordinateTransformation(FromSRS, ToSRS);

OSGeo.OGR.Geometry OGRGeom;
for (long i = 0; i < MyLayer.GetFeatureCount(0); i++)
{
    OGRGeom = MyLayer.GetFeature(i).GetGeometryRef();

    if (OGRGeom.Transform(CT) == 0) System.Diagnostics.Debug.WriteLine("Error during projection.");

    OGRGeom.AssignSpatialReference(ToSRS); //Even necessary after transformation?
}

1 Answer 1

2

This turned out to be a confusion caused by contradictory GDAL documentation: Here it says "The Transform() function will return TRUE on success, or FALSE if any of the points fail to transform." suggesting that any (in C#, actually integer) return value other than 1 suggests that the method failed, while here it states that

if (geom.Transform(ct) != 0)
    throw new NotSupportedException("projection failed");

suggesting that the success return code is in fact zero. I therefore (and suggest any follow-up reader of this post) started to use

if (OGRGeom.Transform(CT) == OSGeo.OGR.Ogr.OGRERR_NONE)
{
    // Congrats, your transformation succeeded.
    // ...
}

for clarity and to avoid confusion. Anyway, thanks to all readers.

1
  • Do you mean that C# bindings work in a different way than the native C++ code? Have you considered to edit the documentation gdal.org/api/csharp/csharp_vector.html? There is a button Edit on GitHub in the upper right corner of the page.
    – user30184
    Commented Aug 1, 2022 at 14:21

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.