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?
}