turf icon indicating copy to clipboard operation
turf copied to clipboard

@turf/union problem

Open AllKai opened this issue 5 months ago • 8 comments

Before the merger

Image

After the merger

Image

Why are the gaps in the middle filled up after the merger?

AllKai avatar Jul 09 '25 10:07 AllKai

Hi @AllKai. Can you please attach the geojson and function calls you used to generate the result? Thank you!

smallsaucepan avatar Jul 11 '25 14:07 smallsaucepan

This is our merge code:

const mergePolygons = (polygons) => { if (!polygons || polygons.length === 1) return polygons; // 将所有多边形的路径转换为 GeoJSON 格式 let geoPolygons = []; polygons.forEach((item) => { if (item.length && item.length >= 3) { geoPolygons.push(turf.polygon([item])); } }); // 合并至少需要两个多边形 if (geoPolygons.length < 2) return []; let featureCollection = turf.featureCollection(geoPolygons) const unionPolygon = turf.union(featureCollection); return unionPolygon?.geometry?.coordinates || [] };

and the 'polygons' parameter format is: [[x,y],[x,y]...]

out data: polygons.json

AllKai avatar Jul 12 '25 02:07 AllKai

Hi @AllKai. Can you please attach the geojson and function calls you used to generate the result? Thank you!

https://github.com/Turfjs/turf/issues/2917#issuecomment-3064528299

AllKai avatar Jul 14 '25 02:07 AllKai

Hi @AllKai. Have reproduced this locally using your code and the result looks ok.

https://geojson.io

Image

https://geojsonlint.com

Image

I'm not sure which mapping library you are using. Could it be a rendering or shading issue? Perhaps post your actual output geojson and what mapping library you're using if you'd like further help.

smallsaucepan avatar Nov 09 '25 01:11 smallsaucepan

Hi @AllKai. Have reproduced this locally using your code and the result looks ok.

https://geojson.io

Image https://geojsonlint.com Image I'm not sure which mapping library you are using. Could it be a rendering or shading issue? Perhaps post your actual output geojson and what mapping library you're using if you'd like further help.

It looks like you don't have this problem there, we are using the Amap map component, could it be a rendering problem with the map component?

AllKai avatar Nov 09 '25 03:11 AllKai

Maybe. Does it normally display ok for polygons with holes in them?

smallsaucepan avatar Nov 09 '25 03:11 smallsaucepan

Maybe. Does it normally display ok for polygons with holes in them?

I don't know, this problem is also a problem that our testers inadvertently draw, and this kind of aspect almost never appears in actual business, but we have to deal with what to do once this kind of business problem occurs. I have raised this issue with the Amap team and await their response

AllKai avatar Nov 09 '25 03:11 AllKai

Are you passing the entire geojson text straight to amap? Or just the geometry coordinates?

return unionPolygon?.geometry?.coordinates || []

If the latter, perhaps check which direction amap expects the rings to be wound. If it's different from what geojson uses it could be turning the rings "inside out" leading to weird rendering issues.

smallsaucepan avatar Nov 09 '25 09:11 smallsaucepan

Hi @AllKai. Will close this for now as it seems Turf is working properly. If you hear back from Amap or new info comes to light that suggests Turf has a fault please feel free to reopen.

smallsaucepan avatar Dec 16 '25 09:12 smallsaucepan