Last active
August 22, 2024 14:22
-
-
Save springmeyer/871897 to your computer and use it in GitHub Desktop.
convert from long/lat to google mercator (or EPSG:4326 to EPSG:900913)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
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)
Thank you so much! It was really helpful.
console.log(degrees2meters(-180,-90)) // [-20037508.34, -Infinity]
Can you please help with converting meters to lat long?
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
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(){
}
is cool for java
but would like to know ways to do it in R