2018-10-11 00:25:40 +03:00
|
|
|
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 {
|
2019-02-12 16:49:13 +03:00
|
|
|
dst = g.extra.appendJSONExtra(dst, false)
|
2018-10-11 00:25:40 +03:00
|
|
|
}
|
|
|
|
dst = append(dst, '}')
|
|
|
|
return dst
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// String ...
|
|
|
|
func (g *MultiLineString) String() string {
|
|
|
|
return string(g.AppendJSON(nil))
|
|
|
|
}
|
|
|
|
|
2019-01-10 19:37:20 +03:00
|
|
|
// Valid ...
|
|
|
|
func (g *MultiLineString) Valid() bool {
|
|
|
|
valid := true
|
|
|
|
for _, p := range g.children {
|
|
|
|
if !p.Valid() {
|
|
|
|
valid = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return valid
|
|
|
|
}
|
|
|
|
|
2018-10-11 00:25:40 +03:00
|
|
|
// JSON ...
|
|
|
|
func (g *MultiLineString) JSON() string {
|
|
|
|
return string(g.AppendJSON(nil))
|
|
|
|
}
|
|
|
|
|
2019-02-12 16:49:13 +03:00
|
|
|
// MarshalJSON ...
|
|
|
|
func (g *MultiLineString) MarshalJSON() ([]byte, error) {
|
|
|
|
return g.AppendJSON(nil), nil
|
|
|
|
}
|
|
|
|
|
2018-10-11 00:25:40 +03:00
|
|
|
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
|
|
|
|
}
|
2018-10-22 05:08:56 +03:00
|
|
|
gopts := toGeometryOpts(opts)
|
|
|
|
line := geometry.NewLine(coords, &gopts)
|
2018-10-11 00:25:40 +03:00
|
|
|
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
|
|
|
|
}
|
2019-01-10 19:37:20 +03:00
|
|
|
if opts.RequireValid {
|
|
|
|
if !g.Valid() {
|
|
|
|
return nil, errCoordinatesInvalid
|
|
|
|
}
|
|
|
|
}
|
2018-10-11 00:25:40 +03:00
|
|
|
g.parseInitRectIndex(opts)
|
|
|
|
return &g, nil
|
|
|
|
}
|