Converting coordinates from EPSG 3857 to 4326 DotSpatial
In the end I find a math formula to convert the coordinates.
I implemented it in a stored procedure because I have a list of point and this stored procedure calculates the distance.
DECLARE @e FLOAT=2.7182818284
DECLARE @X DECIMAL(18,2) =20037508.34
SET @StartLat3857 =(SELECT TOP 1 Latitude FROM Coordinates WHERE IdCoord=@IdCoord ORDER By IdTDFPath ASC)
SET @StartLng3857=(SELECT TOP 1 Longitude FROM Coordinates WHERE IdCoord=@IdCoord ORDER By IdTDFPath ASC)
--converting the logitute from epsg 3857 to 4326
SET @StartLng=(@StartLng3857*180)/@X
--converting the latitude from epsg 3857 to 4326
SET @StartLat = @StartLat3857/(@X/180)
SET @StartLat = ((ATAN(POWER(@e,((PI()/180)*@StartLat))))/(PI()/360))-90
It is a bit tricky to project EPSG 3857
coordinates to EPSG 4326
coordinate system. Microsoft recommends using ProjNet4GeoAPI
so I decided to use that.
https://docs.microsoft.com/en-us/ef/core/modeling/spatial#srid-ignored-during-client-operations
I have verified that it works here:
http://epsg.io/transform#s_srs=3857&t_srs=4326&x=1530088.9600000&y=5085240.8300000
Example conversion:
var x = 1530088.96d;
var y = 5085240.83d;
var epsg3857ProjectedCoordinateSystem = ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WebMercator;
var epsg4326GeographicCoordinateSystem = ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84;
var coordinateTransformationFactory = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory();
var coordinateTransformation = coordinateTransformationFactory.CreateFromCoordinateSystems(epsg3857ProjectedCoordinateSystem, epsg4326GeographicCoordinateSystem);
var epsg3857Coordinate = new GeoAPI.Geometries.Coordinate(x, y);
var epsg4326Coordinate = coordinateTransformation.MathTransform.Transform(epsg3857Coordinate);