turf icon indicating copy to clipboard operation
turf copied to clipboard

@turf/buffer stretches polygons infinitely when buffering close to 180 degrees longitude

Open hally9k opened this issue 4 years ago • 6 comments

When buffering a polygon that is close to 180 degrees longitude, if the buffered polygon exceeds the 180 degree threshold it gets stretched around the whole world.

Example here: http://jsfiddle.net/60L7vxwq/4/

hally9k avatar Jan 16 '21 11:01 hally9k

This is a complex problem, see this blog post for a fuller discussion. TurfJS is producing valid coordinates, however leaflet is not sure how to render features that cross the anti-meridian. Not sure if I have a simple answer for this sorry

rowanwins avatar Jan 18 '21 04:01 rowanwins

Oh wow I’ll do some reading then.

FYI this behaves the same on Mapbox.

On Mon, 18 Jan 2021 at 5:00 PM, Rowan Winsemius [email protected] wrote:

This is a complex problem, see this blog post https://tmcw.github.io/2016/09/26/the-180th-meridian.html for a fuller discussion. TurfJS is producing valid coordinates, however leaflet is not sure how to render features that cross the anti-meridian. Not sure if I have a simple answer for this sorry

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Turfjs/turf/issues/2003#issuecomment-761964599, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA5ATECRPLGKN32NKF6ZQ6TS2OW6VANCNFSM4WFFXV5Q .

hally9k avatar Jan 18 '21 19:01 hally9k

@hally9k maybe you can find something inspirational here and here. If you came up with a nice function to safely display any GeoJSON feature on the map we could maybe add to Turf 😃

stebogit avatar Jan 18 '21 19:01 stebogit

@stebogit Thanks for the resources, they were a good read. I'll see if I have time to try my hand at a fix next week.

hally9k avatar Jan 19 '21 20:01 hally9k

Is there any update on this issue? As I have recently encountered the same issue when playing about with buffers in mapbox

MacaScull avatar May 06 '21 11:05 MacaScull

@MacaScull According to https://github.com/mapbox/mapbox-gl-js/issues/3250#issuecomment-294887678, we can map longitude ourselves.

const bufferedFeature = buffer(featureCollection, 500, { units: 'kilometers' })

const coordinates = bufferedFeature.geometry.coordinates[0]

for (let i = 1; i < coordinates.length - 1; ++i) {
  const coords = coordinates[i]
  const prevCoords = coordinates[i - 1]

  coordinates[i][0] =
    coords[0] + (coords[0] - prevCoords[0] > 180 ? -360 : prevCoords[0] - coords[0] > 180 ? 360 : 0)
}

LiuuY avatar Nov 23 '21 02:11 LiuuY