Coordinate System C# – How to Convert Local Coordinates to WGS84 in C#

ccoordinate systemproj

Some people working on a the construction site use a local custom coordinate system. I have been given a proj4 string like this:

+proj=omerc +lat_0=46.325454996 +lonc=7.99050231 +alpha=60.4026421358 +gamma=90 +k=1 +x_0=350 +y_0=200 +datum=WGS84 +units=m +no_defs +type=crs

And x,y coordinates like this 429.079425606807, 261.126436507318

And I need to convert their coordinates into WGS84 and back

I tried to use the DotSpatial to make the transformation. Any other libraries in C# can be accepted.

 string proj4_custom = "+proj=omerc +lat_0=46.325454996 +lonc=7.99050231 +alpha=60.4026421358 +gamma=90 +k=1 +x_0=350 +y_0=200 +datum=WGS84 +units=m +no_defs +type=crs";

 DotSpatial.Projections.ProjectionInfo src = DotSpatial.Projections.ProjectionInfo.FromProj4String(proj4_custom);
 int googleEPSGCode = 4326;
 DotSpatial.Projections.ProjectionInfo trg = DotSpatial.Projections.ProjectionInfo.FromEpsgCode(googleEPSGCode);

 double[] xy = new[] { 429.079425606807, 261.126436507318 };
 double[] z = new[] { 0d };
 DotSpatial.Projections.Reproject.ReprojectPoints(xy, z, src, trg, 0, 1);

But the result in xy is not correct, on the map it's about 50 to 100 meters away. I should have a result really close to this correct result

Local coordinates: 429.07942560 261.1264365
WGS84: 7.99100318 46.326284519

What am I doing wrong?? If the problem is related to a bug in the libraries, what other libraries can I use that do it correctly?

Best Answer

I finally used the nuget package MaxRev.Gdal.WindowsRuntime.Minimal.

string proj = "+proj=omerc +lat_0=46.325454996 +lonc=7.99050231+alpha=60.4026421358 +gamma=90 +k=1 +x_0=350 +y_0=200 +datum=WGS84 +units=m +no_defs +type=crs";  SpatialReference local = new SpatialReference("");
local.ImportFromProj4(proj);
    
SpatialReference wgs84Reference = new SpatialReference("");
wgs84Reference.ImportFromProj4("+proj=latlong +datum=WGS84 +no_defs");
    
CoordinateTransformation coordinateTransform = new CoordinateTransformation(wgs84Reference, local);
double[] xy = new[] { lon, lat };
double[] z = new[] { alt };
coordinateTransform.TransformPoint(xy);
lon = xy[0];
lat = xy[1];