2018-10-11 00:25:40 +03:00
|
|
|
package clip
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/tidwall/geojson"
|
|
|
|
"github.com/tidwall/geojson/geometry"
|
|
|
|
)
|
|
|
|
|
|
|
|
func clipLineString(
|
|
|
|
lineString *geojson.LineString, clipper geojson.Object,
|
2020-03-26 01:35:31 +03:00
|
|
|
opts *geometry.IndexOptions,
|
2018-10-11 00:25:40 +03:00
|
|
|
) geojson.Object {
|
|
|
|
bbox := clipper.Rect()
|
|
|
|
var newPoints [][]geometry.Point
|
|
|
|
var clipped geometry.Segment
|
|
|
|
var rejected bool
|
|
|
|
var line []geometry.Point
|
|
|
|
base := lineString.Base()
|
|
|
|
nSegments := base.NumSegments()
|
|
|
|
for i := 0; i < nSegments; i++ {
|
|
|
|
clipped, rejected = clipSegment(base.SegmentAt(i), bbox)
|
|
|
|
if rejected {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(line) > 0 && line[len(line)-1] != clipped.A {
|
|
|
|
newPoints = append(newPoints, line)
|
|
|
|
line = []geometry.Point{clipped.A}
|
|
|
|
} else if len(line) == 0 {
|
|
|
|
line = append(line, clipped.A)
|
|
|
|
}
|
|
|
|
line = append(line, clipped.B)
|
|
|
|
}
|
|
|
|
if len(line) > 0 {
|
|
|
|
newPoints = append(newPoints, line)
|
|
|
|
}
|
|
|
|
var children []*geometry.Line
|
|
|
|
for _, points := range newPoints {
|
|
|
|
children = append(children,
|
2020-03-26 01:35:31 +03:00
|
|
|
geometry.NewLine(points, opts))
|
2018-10-11 00:25:40 +03:00
|
|
|
}
|
|
|
|
if len(children) == 1 {
|
|
|
|
return geojson.NewLineString(children[0])
|
|
|
|
}
|
|
|
|
return geojson.NewMultiLineString(children)
|
|
|
|
}
|