Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
convert from long/lat to google mercator (or EPSG:4326 to EPSG:900913)
// See https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames for more details.
var degrees2meters = function(lon,lat) {
var x = lon * 20037508.34 / 180;
var y = Math.log(Math.tan((90 + lat) * Math.PI / 360)) / (Math.PI / 180);
y = y * 20037508.34 / 180;
return [x, y]
}
x= -77.035974
y = 38.898717
console.log(degrees2meters(x,y))
// should result in: -8575605.398444, 4707174.018280
@dikerac

This comment has been minimized.

Copy link

@dikerac dikerac commented Mar 22, 2013

I m new in GIS. Result contain metric result like meters, but in google maps, latitude and longitude needs from me.

How does result convert to latitude longitude? Please, can you help this stituation?

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Feb 15, 2014

Hi,

As would be the inverse function?
Convert EPSG:900913 to EPSG:4326
Anyone know the solution?

Thank you very much for your help.

@jiangfeng79

This comment has been minimized.

Copy link

@jiangfeng79 jiangfeng79 commented Mar 6, 2014

for c/c++ lovers:

#define DEGREE_TO_METER(Y) (log(tan((90.0 + (Y)) * M_PI / 360.0)) / (M_PI / 180.0))*111319.490778
#define DEGREE_TO_METER_REVERSE(Y) (atan(pow(M_E, ((Y)/111319.490778)*M_PI/180.0))*360.0/M_PI-90.0)
@clarkewd

This comment has been minimized.

Copy link

@clarkewd clarkewd commented Aug 2, 2014

@mhanoglu

This comment has been minimized.

Copy link

@mhanoglu mhanoglu commented Nov 7, 2016

Hello how i can convert EPSG:3857 coordinate to EPSG:4326 ?

@royjkim

This comment has been minimized.

Copy link

@royjkim royjkim commented Jan 20, 2017

@marzochi

You may still find the way to convert EPSG:3857 to EPSG:4326, this gonna help you.
https://epsg.io/transform#s_srs=3857&t_srs=4326

@omarshiha

This comment has been minimized.

Copy link

@omarshiha omarshiha commented Aug 15, 2017

how to convert from projection to lon/lat ?

@MightyTedKim

This comment has been minimized.

Copy link

@MightyTedKim MightyTedKim commented Aug 1, 2018

are there ways to convert a geojson 3857 coords to 4326?
doing it with r and java but cant find a way that would solve the propblem
epsg.io is perfect for single data,
function trans2(){

   var test_x = document.getElementById("test_x").value;
   var test_y = document.getElementById("test_y").value;

   var ll2=new OpenLayers.LonLat(test_x, test_y).transform(
			new OpenLayers.Projection("EPSG:900913"), 
			new OpenLayers.Projection("EPSG:4326")
			);
   	      
   $("#test_ll_x").val(ll2.lon);
   $("#test_ll_y").val(ll2.lat);

}

is cool for java

but would like to know ways to do it in R

@tyluRp

This comment has been minimized.

Copy link

@tyluRp tyluRp commented Aug 16, 2018

@MightyTedKim

Here's my general workflow for transforming a CRS in R.

With some GIS file (shp, gpkg, etc):

library(sf)
library(dplyr)
filepath <- system.file("shape/nc.shp", package="sf")
st_read(filepath) %>%
  st_set_crs(4326) %>% 
  st_transform(3857)

With a CSV:

df <- read.csv("some_csv.csv")
df %>% 
  st_as_sf(coords = c("lat_col", "lng_col")) %>% 
  st_set_crs(4326) %>% 
  st_transform(3857)
@subhasreem

This comment has been minimized.

Copy link

@subhasreem subhasreem commented Aug 21, 2019

Thank you so much! It was really helpful.

@rendrom

This comment has been minimized.

Copy link

@rendrom rendrom commented Mar 7, 2020

console.log(degrees2meters(-180,-90)) //  [-20037508.34, -Infinity]

@Us77

This comment has been minimized.

Copy link

@Us77 Us77 commented Mar 24, 2020

Can you please help with converting meters to lat long?

@kb173

This comment has been minimized.

Copy link

@kb173 kb173 commented Apr 14, 2020

@Us77:

The C++ macro posted above works well for converting meters to lat/long degrees: #define DEGREE_TO_METER_REVERSE(Y) (atan(pow(M_E, ((Y)/111319.490778)*M_PI/180.0))*360.0/M_PI-90.0) (Thanks @jiangfeng79!)

Should be trivial to adapt to Python.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.