Skip to content

Instantly share code, notes, and snippets.

@vasrap
Last active March 15, 2021 15:54
Show Gist options
  • Save vasrap/5409402 to your computer and use it in GitHub Desktop.
Save vasrap/5409402 to your computer and use it in GitHub Desktop.
Lat/Lon to Meters using Mercator projection
puts "Enter latitude in decimal degrees:"
lat_deg = gets.to_f
puts "Enter longitude in decimal degrees:"
lon_deg = gets.to_f
lon_rad = (lon_deg / 180.0 * Math::PI)
lat_rad = (lat_deg / 180.0 * Math::PI)
sm_a = 6378137.0
x = sm_a * lon_rad
y = sm_a * Math.log((Math.sin(lat_rad) + 1) / Math.cos(lat_rad))
puts "x: " + x.to_s + ", y: " + y.to_s
# More information: http://gis.stackexchange.com/questions/15269/how-to-convert-lat-long-to-meters-using-mercator-projection-in-c
@arashmad
Copy link

arashmad commented Jan 9, 2021

import proj4 from 'proj4';

const webMercatorCoordinate = proj4('EPSG:3857', [long, lat]);
const x = webMercatorCoordinate[0]
const y = webMercatorCoordinate[1]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment