tile38/vendor/github.com/tidwall/geojson/multilinestring.go

107 lines
2.2 KiB
Go
Raw Permalink Normal View History

package geojson
import (
"github.com/tidwall/geojson/geometry"
"github.com/tidwall/gjson"
)
// MultiLineString ...
type MultiLineString struct{ collection }
// NewMultiLineString ...
func NewMultiLineString(lines []*geometry.Line) *MultiLineString {
g := new(MultiLineString)
for _, line := range lines {
g.children = append(g.children, NewLineString(line))
}
g.parseInitRectIndex(DefaultParseOptions)
return g
}
// AppendJSON ...
func (g *MultiLineString) AppendJSON(dst []byte) []byte {
dst = append(dst, `{"type":"MultiLineString","coordinates":[`...)
for i, g := range g.children {
if i > 0 {
dst = append(dst, ',')
}
dst = append(dst,
gjson.GetBytes(g.AppendJSON(nil), "coordinates").String()...)
}
dst = append(dst, ']')
if g.extra != nil {
dst = g.extra.appendJSONExtra(dst, false)
}
dst = append(dst, '}')
return dst
}
// String ...
func (g *MultiLineString) String() string {
return string(g.AppendJSON(nil))
}
// Valid ...
func (g *MultiLineString) Valid() bool {
valid := true
for _, p := range g.children {
if !p.Valid() {
valid = false
}
}
return valid
}
// JSON ...
func (g *MultiLineString) JSON() string {
return string(g.AppendJSON(nil))
}
// MarshalJSON ...
func (g *MultiLineString) MarshalJSON() ([]byte, error) {
return g.AppendJSON(nil), nil
}
func parseJSONMultiLineString(
keys *parseKeys, opts *ParseOptions,
) (Object, error) {
var g MultiLineString
var err error
if !keys.rCoordinates.Exists() {
return nil, errCoordinatesMissing
}
if !keys.rCoordinates.IsArray() {
return nil, errCoordinatesInvalid
}
var coords []geometry.Point
var ex *extra
keys.rCoordinates.ForEach(func(_, value gjson.Result) bool {
coords, ex, err = parseJSONLineStringCoords(keys, value, opts)
if err != nil {
return false
}
if len(coords) < 2 {
err = errCoordinatesInvalid
return false
}
gopts := toGeometryOpts(opts)
line := geometry.NewLine(coords, &gopts)
g.children = append(g.children, &LineString{base: *line, extra: ex})
return true
})
if err != nil {
return nil, err
}
if err := parseBBoxAndExtras(&g.extra, keys, opts); err != nil {
return nil, err
}
if opts.RequireValid {
if !g.Valid() {
return nil, errCoordinatesInvalid
}
}
g.parseInitRectIndex(opts)
return &g, nil
}