Search code examples
javascriptgeojsonmapbox-gl-js

Translate a geojson polygon to different coordinates


I'm trying to move a geoJSON polygon (the outline of a city's administrative borders) from it's original location to another arbitrary one, maintaining its shape while respecting the Mercator projection. Much like how this site does it: https://thetruesize.com/

I've experimented with moving images around the map and those do respect the mercator projection when moved north, but this isn't ideal as the geoJSON would have to be converted to an image on the fly...

Is there an easy way to do this in mapbox without modifying the entire geoJSON to new coordinates?

Thanks!


Solution

  • transformTranslate from Turf.js can help you to translate GeoJSON features or geometries.

    I sometimes had some problems when moving polygons across the globe with transformTranslate, so I wrote some function (repl.it) that behaves a bit differently.