2016-03-05 02:08:16 +03:00
|
|
|
package geojson
|
|
|
|
|
|
|
|
import (
|
2016-11-02 15:51:48 +03:00
|
|
|
"github.com/tidwall/gjson"
|
2016-03-05 02:08:16 +03:00
|
|
|
"github.com/tidwall/tile38/geojson/geohash"
|
|
|
|
)
|
|
|
|
|
|
|
|
// FeatureCollection is a geojson object with the type "FeatureCollection"
|
|
|
|
type FeatureCollection struct {
|
2018-01-11 23:57:18 +03:00
|
|
|
Features []Object
|
|
|
|
BBox *BBox
|
|
|
|
bboxDefined bool
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
2016-12-17 00:02:58 +03:00
|
|
|
func fillFeatureCollectionMap(json string) (FeatureCollection, error) {
|
2016-03-05 02:08:16 +03:00
|
|
|
var g FeatureCollection
|
2016-11-02 15:51:48 +03:00
|
|
|
res := gjson.Get(json, "features")
|
|
|
|
switch res.Type {
|
2016-03-05 02:08:16 +03:00
|
|
|
default:
|
2016-12-17 00:02:58 +03:00
|
|
|
return g, errInvalidFeaturesMember
|
2016-11-02 15:51:48 +03:00
|
|
|
case gjson.Null:
|
2016-12-17 00:02:58 +03:00
|
|
|
return g, errFeaturesMemberRequired
|
2016-11-02 15:51:48 +03:00
|
|
|
case gjson.JSON:
|
|
|
|
if !resIsArray(res) {
|
2016-12-17 00:02:58 +03:00
|
|
|
return g, errInvalidFeaturesMember
|
2016-11-02 15:51:48 +03:00
|
|
|
}
|
|
|
|
v := res.Array()
|
2016-03-05 02:08:16 +03:00
|
|
|
g.Features = make([]Object, len(v))
|
2016-11-02 15:51:48 +03:00
|
|
|
for i, res := range v {
|
|
|
|
if res.Type != gjson.JSON {
|
2016-12-17 00:02:58 +03:00
|
|
|
return g, errInvalidFeature
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2016-11-02 15:51:48 +03:00
|
|
|
o, err := objectMap(res.Raw, fcoll)
|
2016-03-05 02:08:16 +03:00
|
|
|
if err != nil {
|
2016-12-17 00:02:58 +03:00
|
|
|
return g, err
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
g.Features[i] = o
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var err error
|
2016-11-02 15:51:48 +03:00
|
|
|
g.BBox, err = fillBBox(json)
|
2018-01-11 23:57:18 +03:00
|
|
|
if err != nil {
|
|
|
|
return g, err
|
|
|
|
}
|
|
|
|
g.bboxDefined = g.BBox != nil
|
|
|
|
if !g.bboxDefined {
|
|
|
|
cbbox := g.CalculatedBBox()
|
|
|
|
g.BBox = &cbbox
|
|
|
|
}
|
2016-12-17 00:02:58 +03:00
|
|
|
return g, err
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Geohash converts the object to a geohash value.
|
|
|
|
func (g FeatureCollection) Geohash(precision int) (string, error) {
|
|
|
|
p := g.CalculatedPoint()
|
|
|
|
return geohash.Encode(p.Y, p.X, precision)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CalculatedPoint is a point representation of the object.
|
|
|
|
func (g FeatureCollection) CalculatedPoint() Position {
|
|
|
|
return g.CalculatedBBox().center()
|
|
|
|
}
|
|
|
|
|
|
|
|
// CalculatedBBox is exterior bbox containing the object.
|
|
|
|
func (g FeatureCollection) CalculatedBBox() BBox {
|
|
|
|
if g.BBox != nil {
|
|
|
|
return *g.BBox
|
|
|
|
}
|
|
|
|
var bbox BBox
|
|
|
|
for i, g := range g.Features {
|
|
|
|
if i == 0 {
|
|
|
|
bbox = g.CalculatedBBox()
|
|
|
|
} else {
|
|
|
|
bbox = bbox.union(g.CalculatedBBox())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bbox
|
|
|
|
}
|
|
|
|
|
|
|
|
// PositionCount return the number of coordinates.
|
|
|
|
func (g FeatureCollection) PositionCount() int {
|
|
|
|
var res int
|
|
|
|
for _, g := range g.Features {
|
|
|
|
res += g.PositionCount()
|
|
|
|
}
|
|
|
|
if g.BBox != nil {
|
|
|
|
return 2 + res
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// Weight returns the in-memory size of the object.
|
|
|
|
func (g FeatureCollection) Weight() int {
|
|
|
|
var res int
|
|
|
|
for _, g := range g.Features {
|
|
|
|
res += g.Weight()
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON allows the object to be encoded in json.Marshal calls.
|
|
|
|
func (g FeatureCollection) MarshalJSON() ([]byte, error) {
|
2018-01-11 23:57:18 +03:00
|
|
|
return g.appendJSON(nil), nil
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
2018-01-11 23:57:18 +03:00
|
|
|
func (g FeatureCollection) appendJSON(json []byte) []byte {
|
|
|
|
json = append(json, `{"type":"FeatureCollection","features":[`...)
|
2016-03-05 02:08:16 +03:00
|
|
|
for i, g := range g.Features {
|
|
|
|
if i != 0 {
|
2018-01-11 23:57:18 +03:00
|
|
|
json = append(json, ',')
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2018-01-11 23:57:18 +03:00
|
|
|
json = append(json, g.JSON()...)
|
|
|
|
}
|
|
|
|
json = append(json, ']')
|
|
|
|
if g.bboxDefined {
|
|
|
|
json = appendBBoxJSON(json, g.BBox)
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2018-01-11 23:57:18 +03:00
|
|
|
return append(json, '}')
|
|
|
|
}
|
|
|
|
|
|
|
|
// JSON is the json representation of the object. This might not be exactly the same as the original.
|
|
|
|
func (g FeatureCollection) JSON() string {
|
|
|
|
return string(g.appendJSON(nil))
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
2016-07-10 05:44:28 +03:00
|
|
|
// String returns a string representation of the object. This might be JSON or something else.
|
|
|
|
func (g FeatureCollection) String() string {
|
|
|
|
return g.JSON()
|
|
|
|
}
|
|
|
|
|
2016-03-05 02:08:16 +03:00
|
|
|
// Bytes is the bytes representation of the object.
|
|
|
|
func (g FeatureCollection) Bytes() []byte {
|
2016-11-02 15:51:48 +03:00
|
|
|
return []byte(g.JSON())
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
func (g FeatureCollection) bboxPtr() *BBox {
|
|
|
|
return g.BBox
|
|
|
|
}
|
|
|
|
func (g FeatureCollection) hasPositions() bool {
|
|
|
|
if g.BBox != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for _, g := range g.Features {
|
|
|
|
if g.hasPositions() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithinBBox detects if the object is fully contained inside a bbox.
|
|
|
|
func (g FeatureCollection) WithinBBox(bbox BBox) bool {
|
2018-01-11 23:57:18 +03:00
|
|
|
if g.bboxDefined {
|
2016-03-05 02:08:16 +03:00
|
|
|
return rectBBox(g.CalculatedBBox()).InsideRect(rectBBox(bbox))
|
|
|
|
}
|
|
|
|
if len(g.Features) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, g := range g.Features {
|
|
|
|
if !g.WithinBBox(bbox) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// IntersectsBBox detects if the object intersects a bbox.
|
|
|
|
func (g FeatureCollection) IntersectsBBox(bbox BBox) bool {
|
2018-01-11 23:57:18 +03:00
|
|
|
if g.bboxDefined {
|
2016-03-05 02:08:16 +03:00
|
|
|
return rectBBox(g.CalculatedBBox()).IntersectsRect(rectBBox(bbox))
|
|
|
|
}
|
|
|
|
for _, g := range g.Features {
|
|
|
|
if g.IntersectsBBox(bbox) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Within detects if the object is fully contained inside another object.
|
|
|
|
func (g FeatureCollection) Within(o Object) bool {
|
|
|
|
return withinObjectShared(g, o,
|
|
|
|
func(v Polygon) bool {
|
|
|
|
if len(g.Features) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, f := range g.Features {
|
|
|
|
if !f.Within(o) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
func(v MultiPolygon) bool {
|
|
|
|
if len(g.Features) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, f := range g.Features {
|
|
|
|
if !f.Within(o) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Intersects detects if the object intersects another object.
|
|
|
|
func (g FeatureCollection) Intersects(o Object) bool {
|
|
|
|
return intersectsObjectShared(g, o,
|
|
|
|
func(v Polygon) bool {
|
|
|
|
if len(g.Features) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, f := range g.Features {
|
|
|
|
if f.Intersects(o) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
func(v MultiPolygon) bool {
|
|
|
|
if len(g.Features) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, f := range g.Features {
|
|
|
|
if f.Intersects(o) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nearby detects if the object is nearby a position.
|
|
|
|
func (g FeatureCollection) Nearby(center Position, meters float64) bool {
|
|
|
|
return nearbyObjectShared(g, center.X, center.Y, meters)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsBBoxDefined returns true if the object has a defined bbox.
|
|
|
|
func (g FeatureCollection) IsBBoxDefined() bool {
|
2018-01-11 23:57:18 +03:00
|
|
|
return g.bboxDefined
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2016-07-10 05:44:28 +03:00
|
|
|
|
|
|
|
// IsGeometry return true if the object is a geojson geometry object. false if it something else.
|
|
|
|
func (g FeatureCollection) IsGeometry() bool {
|
|
|
|
return true
|
|
|
|
}
|