2018-10-11 00:25:40 +03:00
|
|
|
package clip
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/tidwall/geojson"
|
|
|
|
"github.com/tidwall/geojson/geometry"
|
|
|
|
)
|
|
|
|
|
|
|
|
func clipPolygon(
|
|
|
|
polygon *geojson.Polygon, clipper geojson.Object,
|
|
|
|
) geojson.Object {
|
|
|
|
rect := clipper.Rect()
|
|
|
|
var newPoints [][]geometry.Point
|
|
|
|
base := polygon.Base()
|
|
|
|
rings := []geometry.Ring{base.Exterior}
|
|
|
|
rings = append(rings, base.Holes...)
|
|
|
|
for _, ring := range rings {
|
|
|
|
ringPoints := make([]geometry.Point, ring.NumPoints())
|
|
|
|
for i := 0; i < len(ringPoints); i++ {
|
|
|
|
ringPoints[i] = ring.PointAt(i)
|
|
|
|
}
|
2020-03-24 00:07:52 +03:00
|
|
|
if clippedRing := clipRing(ringPoints, rect); len(clippedRing) > 0 {
|
|
|
|
newPoints = append(newPoints, clippedRing)
|
|
|
|
}
|
2018-10-11 00:25:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var exterior []geometry.Point
|
|
|
|
var holes [][]geometry.Point
|
|
|
|
if len(newPoints) > 0 {
|
|
|
|
exterior = newPoints[0]
|
|
|
|
}
|
|
|
|
if len(newPoints) > 1 {
|
|
|
|
holes = newPoints[1:]
|
|
|
|
}
|
|
|
|
newPoly := geojson.NewPolygon(
|
2018-10-22 05:08:56 +03:00
|
|
|
geometry.NewPoly(exterior, holes, nil),
|
2018-10-11 00:25:40 +03:00
|
|
|
)
|
|
|
|
if newPoly.Empty() {
|
|
|
|
return geojson.NewMultiPolygon(nil)
|
|
|
|
}
|
2019-02-09 00:56:43 +03:00
|
|
|
return newPoly
|
2018-10-11 00:25:40 +03:00
|
|
|
}
|