I have a GeoJson file whose geometries are in 3857.
I want to read it using C# and insert its rows in a postgres table and I want the geometries there to be in 4326.
Also, I have some other files in other SRIDs like 7991.
How can I convert all geometries to 4326?
Geometry ConverteTo4326(Geometry geometry, string sourceSrid)
{
// What to do here?
}
2
First off you need to get the well-known-text-representation for your epsg-codes. This is
PROJCS["WGS 84 / Pseudo-Mercator",
GEOGCS["WGS 84",
DATUM["WGS_1984",
SPHEROID["WGS 84",6378137,298.257223563,
AUTHORITY["EPSG","7030"]],
AUTHORITY["EPSG","6326"]],
PRIMEM["Greenwich",0,
AUTHORITY["EPSG","8901"]],
UNIT["degree",0.0174532925199433,
AUTHORITY["EPSG","9122"]],
AUTHORITY["EPSG","4326"]],
PROJECTION["Mercator_1SP"],
PARAMETER["central_meridian",0],
PARAMETER["scale_factor",1],
PARAMETER["false_easting",0],
PARAMETER["false_northing",0],
UNIT["metre",1,
AUTHORITY["EPSG","9001"]],
AXIS["Easting",EAST],
AXIS["Northing",NORTH],
EXTENSION["PROJ4","+proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs"],
AUTHORITY["EPSG","3857"]]
for EPSG:3857
and
GEOGCS["WGS 84",
DATUM["WGS_1984",
SPHEROID["WGS 84",6378137,298.257223563,
AUTHORITY["EPSG","7030"]],
AUTHORITY["EPSG","6326"]],
PRIMEM["Greenwich",0,
AUTHORITY["EPSG","8901"]],
UNIT["degree",0.0174532925199433,
AUTHORITY["EPSG","9122"]],
AUTHORITY["EPSG","4326"]]
for EPSG:4326
(just look at https://epsg.io).
Now you can easily do the transformation:
CoordinateSystemFactory csFact = new CoordinateSystemFactory();
CoordinateTransformationFactory ctFact = new CoordinateTransformationFactory();
ICoordinateSystem epsg3857 = csFact.CreateFromWkt(epsg3857String);
IProjectedCoordinateSystem epsg4326 = csFact.CreateFromWkt(epsg4326String);
ICoordinateTransformation trans = ctFact.CreateFromCoordinateSystems(epsg3857, epsg4326);
var result = trans.MathTransform.TransformList(myCoordinates);
As web-mercator and 4326 are pretty common spatial references anyway, they are typically implemeneted in most GIS-related libs, so in NTS as well. So instead of using the projections-strings you can just use this:
var epsg3857 = ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WebMercator;
var epsg4326 = ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84;
1