developmentseed / dirty-reprojectors

Make quick and dirty projections to use in your web maps instead of Web Mercator
https://www.developmentseed.org/dirty-reprojectors-app/
MIT License
120 stars 10 forks source link

Can you reverse a dirty projection? #18

Closed alexmuro closed 4 years ago

alexmuro commented 4 years ago

Having successfully created a lovely AlbersUsa map that I can use in a web map, I now want to take coordinates from that map, and transform them back into the standard mercator projection so I can use map clicks to fetch data based on location.

I was hoping the following code would give me the initial coordinates, however it does now.

let start = {
    "type": "Point",
    "coordinates": [-73.7562317,42.6525793]
}

let end = reproject({
    forward: d3.geoAlbersUsa().translate([0, 0]).scale(R), 
    reverse: d3.geoMercator().translate([0, 0]).scale(R) 
}, start)
// end.coordinates = [ 16.575216007966475, 5.8680326060285966 ]

let startAgain = reproject({
    forward: d3.geoMercator().translate([0, 0]).scale(R), 
    reverse: d3.geoAlbersUsa().translate([0, 0]).scale(R) 
}, end)
// startAgain.coordinates = [-76.95772759798182, -31.26254099390082]

Is there a correct way to get your initial coordinates by transformation?

alexmuro commented 4 years ago

If anyone is curious you do it like this

let start =  [-73.7562317,42.6525793]
let end = projections['mercator'].invert(projections['albersUsa'](start))
console.log('end', end) 
// end [ 16.575216007966475, 5.868032606028572 ]

let startAgain = projections['albersUsa'].invert(projections['mercator'](end))
console.log('start again', startAgain)
// start again [ -73.7562317, 42.6525793 ]