9

I have in my DB a list of coordinates in EPSG 3857 format. I need to convert them in EPSG 4326 I'm trying to use DotSpatial but my code always retun a double array of Infinity.

public double[] ConvertCoodinates()
    {
        double[] xy = new double[2];
        xy[0] = 5085240.8300000000;
        xy[1] = 1530088.9600000000;
    //An array for the z coordinate
        double[] z = new double[1];
        z[0] = 0;
        ProjectionInfo pStart = KnownCoordinateSystems.Geographic.World.WGS1984;
        pStart.AuthorityCode = 3857;
        ProjectionInfo pEnd = KnownCoordinateSystems.Geographic.World.WGS1984;
        pEnd.AuthorityCode = 4326;
        Reproject.ReprojectPoints(xy, z, pStart, pEnd, 0, 1);
        return xy;
    }

The xy array always cointain infinity; Can someone help me?

3 Answers 3

11

In the end I found 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

In the end is just a math formula that can be used in every language. for example is Javascript it will be

const e = 2.7182818284
const X = 20037508.34

const lat3857 = 1743704.947843 
const long3857 = 16978473.105100

//converting the logitute from epsg 3857 to 4326
const long4326 = (lat3857*180)/X

//converting the latitude from epsg 3857 to 4326 split in multiple lines for readability

let lat4326 = lat3857/(X / 180)
const exponent = (Math.PI / 180) * lat4326

lat4326 = Math.atan(e ** exponent)
lat4326 = lat4326 / (Math.PI / 360)
lat4326 = lat4326 - 90
8
  • 2
    Can you help me with converting back from 4326 to 3857?
    – ABH
    Nov 21, 2017 at 13:22
  • 2
    @ABH You need to use the inverted formula to calculate the coordinates. In c# it will be private double[] ConvertCoordinate(double lat, double lng) { double x = lng * 20037508.34 / 180; double y = Math.Log(Math.Tan((90 + lat) * Math.PI / 360)) / (Math.PI / 180); y = y * 20037508.34 / 180; return new double[] { x, y }; }
    – Sethlans
    Nov 22, 2017 at 15:36
  • @sethlans, could you perhaps update your post. not everyone understands stored procs
    – chitgoks
    Sep 25, 2021 at 14:48
  • 1
    @chitgoks Hope the javascript version is more readable
    – Sethlans
    Sep 27, 2021 at 6:26
  • I'll share my java version. I just made one.
    – chitgoks
    Sep 28, 2021 at 4:38
5

It seems there is a mistake in the Javascript version of the selected answer... It always gives me a 2 times bigger answer than it really should, so I ended up diving by 360 instead of 180 (but I admit the Math here are beyond my field of expertise).

Here is the working code :

  coord3857To4326(coord) {
    
    const e_value = 2.7182818284;
    const X = 20037508.34;
    
    const lat3857 = coord.lat
    const long3857 = coord.lng;
    
    //converting the longitute from epsg 3857 to 4326
    const long4326 = (long3857*180)/X;
    
    //converting the latitude from epsg 3857 to 4326 split in multiple lines for readability        
    let lat4326 = lat3857/(X / 180);
    const exponent = (Math.PI / 180) * lat4326;
    
    lat4326 = Math.atan(Math.pow(e_value, exponent));
    lat4326 = lat4326 / (Math.PI / 360); // Here is the fixed line
    lat4326 = lat4326 - 90;

    return {lat:lat4326, lng:long4326};
    
}

I also successfully reversed it :

  coord4326To3857(coord) {

    const X = 20037508.34;

    let long3857 = (coord.lng * X) / 180;

    let lat3857 = parseFloat(coord.lat) + 90;
    lat3857 = lat3857 * (Math.PI/360);
    lat3857 = Math.tan(lat3857);
    lat3857 = Math.log(lat3857);
    lat3857 = lat3857 / (Math.PI / 180);
    
    lat3857 = (lat3857 * X) / 180;

    return {lat:lat3857, lng:long3857};
}

Hope this will help others !

1
  • 2
    Thanks, there was a mistake, I have corrected it in the main answer
    – Sethlans
    Jan 30, 2022 at 10:54
4

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://learn.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);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.