2016-03-05 02:08:16 +03:00
|
|
|
package geojson
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-12-17 22:20:55 +03:00
|
|
|
"compress/gzip"
|
2016-11-07 23:04:21 +03:00
|
|
|
"encoding/binary"
|
2016-12-17 22:20:55 +03:00
|
|
|
"io/ioutil"
|
2016-03-05 02:08:16 +03:00
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Feature is a geojson object with the type "Feature"
|
|
|
|
type Feature struct {
|
2016-11-02 15:51:48 +03:00
|
|
|
Geometry Object
|
|
|
|
BBox *BBox
|
2016-12-17 22:20:55 +03:00
|
|
|
idprops []byte // raw id and properties combined
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
2016-12-17 00:02:58 +03:00
|
|
|
func fillFeatureMap(json string) (Feature, error) {
|
2016-03-05 02:08:16 +03:00
|
|
|
var g Feature
|
2016-11-02 15:51:48 +03:00
|
|
|
v := gjson.Get(json, "geometry")
|
|
|
|
switch v.Type {
|
2016-03-05 02:08:16 +03:00
|
|
|
default:
|
2016-12-17 00:02:58 +03:00
|
|
|
return g, errInvalidGeometryMember
|
2016-11-02 15:51:48 +03:00
|
|
|
case gjson.Null:
|
2016-12-17 00:02:58 +03:00
|
|
|
return g, errGeometryMemberRequired
|
2016-11-02 15:51:48 +03:00
|
|
|
case gjson.JSON:
|
2016-03-05 02:08:16 +03:00
|
|
|
var err error
|
2016-11-02 15:51:48 +03:00
|
|
|
g.Geometry, err = objectMap(v.Raw, feat)
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
var err error
|
2016-11-02 15:51:48 +03:00
|
|
|
g.BBox, err = fillBBox(json)
|
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
|
|
|
}
|
2016-11-02 15:51:48 +03:00
|
|
|
|
|
|
|
var propsExists bool
|
|
|
|
props := gjson.Get(json, "properties")
|
|
|
|
switch props.Type {
|
2016-03-05 02:08:16 +03:00
|
|
|
default:
|
2016-12-17 00:02:58 +03:00
|
|
|
return g, errInvalidPropertiesMember
|
2016-11-02 15:51:48 +03:00
|
|
|
case gjson.Null:
|
|
|
|
case gjson.JSON:
|
|
|
|
propsExists = true
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2016-11-02 15:51:48 +03:00
|
|
|
id := gjson.Get(json, "id")
|
|
|
|
if id.Exists() || propsExists {
|
2016-11-07 23:04:21 +03:00
|
|
|
g.idprops = makeCompositeRaw(id.Raw, props.Raw)
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
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 Feature) 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 Feature) CalculatedPoint() Position {
|
|
|
|
return g.CalculatedBBox().center()
|
|
|
|
}
|
|
|
|
|
|
|
|
// CalculatedBBox is exterior bbox containing the object.
|
|
|
|
func (g Feature) CalculatedBBox() BBox {
|
|
|
|
if g.BBox != nil {
|
|
|
|
return *g.BBox
|
|
|
|
}
|
|
|
|
return g.Geometry.CalculatedBBox()
|
|
|
|
}
|
|
|
|
|
|
|
|
// PositionCount return the number of coordinates.
|
|
|
|
func (g Feature) PositionCount() int {
|
|
|
|
res := g.Geometry.PositionCount()
|
|
|
|
if g.BBox != nil {
|
|
|
|
return 2 + res
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// Weight returns the in-memory size of the object.
|
|
|
|
func (g Feature) Weight() int {
|
|
|
|
res := g.PositionCount() * sizeofPosition
|
2016-11-02 15:51:48 +03:00
|
|
|
res += len(g.idprops)
|
2016-03-05 02:08:16 +03:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON allows the object to be encoded in json.Marshal calls.
|
|
|
|
func (g Feature) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(g.JSON()), nil
|
|
|
|
}
|
|
|
|
|
2016-12-17 22:20:55 +03:00
|
|
|
func (g Feature) getRaw() (id, props []byte) {
|
2016-11-07 23:04:21 +03:00
|
|
|
if len(g.idprops) == 0 {
|
2016-12-17 22:20:55 +03:00
|
|
|
return nil, nil
|
2016-11-07 23:04:21 +03:00
|
|
|
}
|
2016-12-17 22:20:55 +03:00
|
|
|
buf := g.idprops
|
|
|
|
rd, err := gzip.NewReader(bytes.NewReader(buf))
|
|
|
|
if err == nil {
|
|
|
|
buf, _ = ioutil.ReadAll(rd)
|
|
|
|
}
|
|
|
|
switch buf[0] {
|
2016-11-07 23:04:21 +03:00
|
|
|
default:
|
2016-12-17 22:20:55 +03:00
|
|
|
lnp := int(buf[0]) + 1
|
|
|
|
return buf[1:lnp], buf[lnp:]
|
2016-11-07 23:04:21 +03:00
|
|
|
case 255:
|
2016-12-17 22:20:55 +03:00
|
|
|
lnp := int(binary.LittleEndian.Uint64([]byte(buf[1:9]))) + 9
|
|
|
|
return buf[9:lnp], buf[lnp:]
|
2016-11-07 23:04:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-17 22:20:55 +03:00
|
|
|
func makeCompositeRaw(idRaw, propsRaw string) []byte {
|
2016-11-07 23:04:21 +03:00
|
|
|
idRaw = stripWhitespace(idRaw)
|
|
|
|
propsRaw = stripWhitespace(propsRaw)
|
|
|
|
if len(idRaw) == 0 && len(propsRaw) == 0 {
|
2016-12-17 22:20:55 +03:00
|
|
|
return nil
|
2016-11-07 23:04:21 +03:00
|
|
|
}
|
|
|
|
var raw []byte
|
|
|
|
if len(idRaw) > 0xFF-1 {
|
|
|
|
raw = make([]byte, len(idRaw)+len(propsRaw)+9)
|
|
|
|
raw[0] = 0xFF
|
|
|
|
binary.LittleEndian.PutUint64(raw[1:9], uint64(len(idRaw)))
|
|
|
|
copy(raw[9:], idRaw)
|
|
|
|
copy(raw[len(idRaw)+9:], propsRaw)
|
|
|
|
} else {
|
|
|
|
raw = make([]byte, len(idRaw)+len(propsRaw)+1)
|
|
|
|
raw[0] = byte(len(idRaw))
|
|
|
|
copy(raw[1:], idRaw)
|
|
|
|
copy(raw[len(idRaw)+1:], propsRaw)
|
2016-11-02 15:51:48 +03:00
|
|
|
}
|
2016-12-17 22:20:55 +03:00
|
|
|
if len(raw) < 256 {
|
|
|
|
return raw
|
|
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
|
|
gbuf := gzip.NewWriter(&buf)
|
|
|
|
gbuf.Write(raw)
|
|
|
|
gbuf.Close()
|
|
|
|
tight := make([]byte, len(buf.Bytes()))
|
|
|
|
copy(tight, buf.Bytes())
|
|
|
|
return tight
|
2016-11-02 15:51:48 +03:00
|
|
|
}
|
|
|
|
|
2016-03-05 02:08:16 +03:00
|
|
|
// JSON is the json representation of the object. This might not be exactly the same as the original.
|
|
|
|
func (g Feature) JSON() string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
buf.WriteString(`{"type":"Feature","geometry":`)
|
|
|
|
buf.WriteString(g.Geometry.JSON())
|
|
|
|
g.BBox.write(&buf)
|
2016-11-02 15:51:48 +03:00
|
|
|
idRaw, propsRaw := g.getRaw()
|
2016-12-17 22:20:55 +03:00
|
|
|
if len(propsRaw) != 0 {
|
2016-03-05 02:08:16 +03:00
|
|
|
buf.WriteString(`,"properties":`)
|
2016-12-17 22:20:55 +03:00
|
|
|
buf.Write(propsRaw)
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2016-12-17 22:20:55 +03:00
|
|
|
if len(idRaw) != 0 {
|
2016-03-05 02:08:16 +03:00
|
|
|
buf.WriteString(`,"id":`)
|
2016-12-17 22:20:55 +03:00
|
|
|
buf.Write(idRaw)
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
buf.WriteByte('}')
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
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 Feature) String() string {
|
|
|
|
return g.JSON()
|
|
|
|
}
|
|
|
|
|
2016-03-05 02:08:16 +03:00
|
|
|
// Bytes is the bytes representation of the object.
|
|
|
|
func (g Feature) Bytes() []byte {
|
2016-11-02 15:51:48 +03:00
|
|
|
return []byte(g.JSON())
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
func (g Feature) bboxPtr() *BBox {
|
|
|
|
return g.BBox
|
|
|
|
}
|
|
|
|
func (g Feature) hasPositions() bool {
|
|
|
|
if g.BBox != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return g.Geometry.hasPositions()
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithinBBox detects if the object is fully contained inside a bbox.
|
|
|
|
func (g Feature) WithinBBox(bbox BBox) bool {
|
|
|
|
if g.BBox != nil {
|
|
|
|
return rectBBox(g.CalculatedBBox()).InsideRect(rectBBox(bbox))
|
|
|
|
}
|
|
|
|
return g.Geometry.WithinBBox(bbox)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IntersectsBBox detects if the object intersects a bbox.
|
|
|
|
func (g Feature) IntersectsBBox(bbox BBox) bool {
|
|
|
|
if g.BBox != nil {
|
|
|
|
return rectBBox(g.CalculatedBBox()).IntersectsRect(rectBBox(bbox))
|
|
|
|
}
|
|
|
|
return g.Geometry.IntersectsBBox(bbox)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Within detects if the object is fully contained inside another object.
|
|
|
|
func (g Feature) Within(o Object) bool {
|
|
|
|
return withinObjectShared(g, o,
|
|
|
|
func(v Polygon) bool {
|
|
|
|
return g.Geometry.Within(o)
|
|
|
|
},
|
|
|
|
func(v MultiPolygon) bool {
|
|
|
|
return g.Geometry.Within(o)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Intersects detects if the object intersects another object.
|
|
|
|
func (g Feature) Intersects(o Object) bool {
|
|
|
|
return intersectsObjectShared(g, o,
|
|
|
|
func(v Polygon) bool {
|
|
|
|
return g.Geometry.Intersects(o)
|
|
|
|
},
|
|
|
|
func(v MultiPolygon) bool {
|
|
|
|
return g.Geometry.Intersects(o)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nearby detects if the object is nearby a position.
|
|
|
|
func (g Feature) 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 Feature) IsBBoxDefined() bool {
|
|
|
|
return g.BBox != nil
|
|
|
|
}
|
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 Feature) IsGeometry() bool {
|
|
|
|
return true
|
|
|
|
}
|