paulmach / orb

Types and utilities for working with 2d geometry in Golang
MIT License
886 stars 103 forks source link

When i use a large polygon and clip it,it looks bad #143

Closed liujiusheng closed 6 months ago

liujiusheng commented 7 months ago

problem image

When i use a large polygon to generate Mapbox Vector Tile,it looks bad. But it has no problem in mapbox studio,with the same data. My data is a China province boundary.

data in mapbox studio

I have try to remove the Clip function,and then it looks right,but the result mvt binary is too big.

tile := maptile.New(x, y, maptile.Zoom(z))
layer := mvt.Layer{
Name:    path,
Version: 2,
Extent:  4096,
//Features: geoFeatures,
}
layer.Features = Tile(layer, geoFeatures, tile, fields)
layer.Clip(mvt.MapboxGLDefaultExtentBound)

layer.Simplify(simplify.DouglasPeucker(0.0))
layer.RemoveEmpty(1.0, 1.0)
layers := mvt.Layers{&layer}
return mvt.Marshal(layers)
paulmach commented 7 months ago

Can you provide the data and tile number for the province, so I can debug? Possible issues are polygon not closed (endpoints not the same), or the wrong winding order. But hard to know without the actual data.

liujiusheng commented 7 months ago

thanks.

shpdata.zip

it may happen when zoom>9

z: 10,x: 817,y: 422

z: 10,x: 816,y: 421

z: 10,x: 816,y: 420

the ring function in orb lead to the problem when the clip bound is in the polygon and whitout intersect with the polygon outline.

paulmach commented 7 months ago
Screenshot 2024-01-27 at 8 40 35 PM

I am not able to duplicate.

One thing to consider is that Clip modifies the input data. If you need to reuse it then you need to Clone() it before passing it in.

liujiusheng commented 6 months ago

thanks. Maybe it recurrence only when we render the whole map. mapbox mapbox/tippecanoe project use Vatti algorithm called "wagyu",your clip algorithm is Sutherland-Hodgeman clip algorithm. Sutherland-Hodgeman clip algorithm can not deal with polyon with hole and Concave polygon.

paulmach commented 6 months ago

Sutherland-Hodgeman clip algorithm can not deal with polyon with hole and Concave polygon.

Is that true? the wikipedia page has an example of a concave polygon. From the Vatti clipping algorithm wikipedia page it says it support "Even complex (self-intersecting) polygons" which I bet Sutherland-Hodgeman doesn't support.

You could try https://github.com/paulmach/orb/tree/master/clip/smartclip I don't remember how it handles all the edge cases, but it is smarter.