1

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}
}
2
  • It seems like you're trying to reinvent the wheel here. Isn't there a javascript package with geospatial functions you could use?
    – csk
    Commented Apr 29, 2019 at 17:13
  • This answer on SO might be helpful: stackoverflow.com/a/13323592/2258 Commented Apr 29, 2019 at 18:05

1 Answer 1

1

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

2
  • How would I use proj4() to translate x and y from a mercator projection image to longitude and latitude? Commented May 1, 2019 at 12:32
  • 1
    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. Commented May 2, 2019 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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