I'm using the following JS to transform Latitude and Longitude to X and Y coordinates but now I need the contrary, transforming Latitude and Longitude to X and Y.

export const latLon2xy = (lat, lon, width, height) => {
    const x = Math.round((width * (180 + lon) / 360) % (1.5 * width))
    const rad = lat * Math.PI / 180
    const merc = 0.5 * Math.log((1 + Math.sin(rad)) / (1 - Math.sin(rad)))
    const y = Math.round((width * merc / (2 * Math.PI)))
    return {x: x, y: y}
  • It seems like you're trying to reinvent the wheel here. Isn't there a javascript package with geospatial functions you could use? – csk Apr 29 at 17:13
  • This answer on SO might be helpful: stackoverflow.com/a/13323592/2258 – Richard Morgan Apr 29 at 18:05

I think you will find that the proj4js library at http://proj4js.org/ does everything you need.

  • How would I use proj4() to translate x and y from a mercator projection image to longitude and latitude? – riccardolardi May 1 at 12:32
  • If you still need help after reading the fine manual and web searching I suggest you post a new question with more specifics about what you don't understand. – Jersey Andy May 2 at 14:05

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.