2016-03-05 02:08:16 +03:00
|
|
|
package geojson
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
2016-11-07 23:04:21 +03:00
|
|
|
"encoding/json"
|
2016-03-05 02:08:16 +03:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
|
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/poly"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
point = 0
|
|
|
|
multiPoint = 1
|
|
|
|
lineString = 2
|
|
|
|
multiLineString = 3
|
|
|
|
polygon = 4
|
|
|
|
multiPolygon = 5
|
|
|
|
geometryCollection = 6
|
|
|
|
feature = 7
|
|
|
|
featureCollection = 8
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
errNotEnoughData = errors.New("not enough data")
|
|
|
|
errTooMuchData = errors.New("too much data")
|
|
|
|
errInvalidData = errors.New("invalid data")
|
|
|
|
)
|
|
|
|
|
|
|
|
var ( // json errors
|
|
|
|
fmtErrTypeIsUnknown = "The type '%s' is unknown"
|
|
|
|
errInvalidTypeMember = errors.New("Type member is invalid. Expecting a string")
|
|
|
|
errInvalidCoordinates = errors.New("Coordinates member is invalid. Expecting an array")
|
|
|
|
errCoordinatesRequired = errors.New("Coordinates member is required.")
|
|
|
|
errInvalidGeometries = errors.New("Geometries member is invalid. Expecting an array")
|
|
|
|
errGeometriesRequired = errors.New("Geometries member is required.")
|
|
|
|
errInvalidGeometryMember = errors.New("Geometry member is invalid. Expecting an object")
|
|
|
|
errGeometryMemberRequired = errors.New("Geometry member is required.")
|
|
|
|
errInvalidFeaturesMember = errors.New("Features member is invalid. Expecting an array")
|
|
|
|
errFeaturesMemberRequired = errors.New("Features member is required")
|
|
|
|
errInvalidFeature = errors.New("Invalid feature in collection")
|
|
|
|
errInvalidPropertiesMember = errors.New("Properties member in invalid. Expecting an array")
|
|
|
|
errInvalidCoordinatesValue = errors.New("Coordinates member has an invalid value")
|
|
|
|
errLineStringInvalidCoordinates = errors.New("Coordinates must be an array of two or more positions")
|
|
|
|
errInvalidNumberOfPositionValues = errors.New("Position must have two or more numbers")
|
|
|
|
errInvalidPositionValue = errors.New("Position has an invalid value")
|
|
|
|
errCoordinatesMustBeArray = errors.New("Coordinates member must be an array of positions")
|
|
|
|
errMustBeALinearRing = errors.New("Polygon must have at least 4 positions and the first and last position must be the same")
|
|
|
|
errBBoxInvalidType = errors.New("BBox member is an invalid. Expecting an array")
|
|
|
|
errBBoxInvalidNumberOfValues = errors.New("BBox member requires exactly 4 or 6 values")
|
|
|
|
errBBoxInvalidValue = errors.New("BBox has an invalid value")
|
|
|
|
errInvalidGeometry = errors.New("Invalid geometry in collection")
|
|
|
|
)
|
|
|
|
|
|
|
|
const nilz = 0
|
|
|
|
|
|
|
|
// Object is a geojson object
|
|
|
|
type Object interface {
|
|
|
|
bboxPtr() *BBox
|
|
|
|
hasPositions() bool
|
|
|
|
// WithinBBox detects if the object is fully contained inside a bbox.
|
|
|
|
WithinBBox(bbox BBox) bool
|
|
|
|
// IntersectsBBox detects if the object intersects a bbox.
|
|
|
|
IntersectsBBox(bbox BBox) bool
|
|
|
|
// Within detects if the object is fully contained inside another object.
|
|
|
|
Within(o Object) bool
|
|
|
|
// Intersects detects if the object intersects another object.
|
|
|
|
Intersects(o Object) bool
|
|
|
|
// Nearby detects if the object is nearby a position.
|
|
|
|
Nearby(center Position, meters float64) bool
|
|
|
|
// CalculatedBBox is exterior bbox containing the object.
|
|
|
|
CalculatedBBox() BBox
|
|
|
|
// CalculatedPoint is a point representation of the object.
|
|
|
|
CalculatedPoint() Position
|
|
|
|
// JSON is the json representation of the object. This might not be exactly the same as the original.
|
|
|
|
JSON() string
|
2016-11-16 23:01:19 +03:00
|
|
|
// String returns a string representation of the object. This may be JSON or something else.
|
2016-07-10 05:44:28 +03:00
|
|
|
String() string
|
2016-03-05 02:08:16 +03:00
|
|
|
// PositionCount return the number of coordinates.
|
|
|
|
PositionCount() int
|
|
|
|
// Weight returns the in-memory size of the object.
|
|
|
|
Weight() int
|
|
|
|
// MarshalJSON allows the object to be encoded in json.Marshal calls.
|
|
|
|
MarshalJSON() ([]byte, error)
|
|
|
|
// Geohash converts the object to a geohash value.
|
|
|
|
Geohash(precision int) (string, error)
|
|
|
|
// IsBBoxDefined returns true if the object has a defined bbox.
|
|
|
|
IsBBoxDefined() bool
|
2016-07-10 05:44:28 +03:00
|
|
|
// IsGeometry return true if the object is a geojson geometry object. false if it something else.
|
|
|
|
IsGeometry() bool
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func writeHeader(buf *bytes.Buffer, objType byte, bbox *BBox, isCordZ bool) {
|
|
|
|
header := objType
|
|
|
|
if bbox != nil {
|
|
|
|
header |= 1 << 4
|
|
|
|
if bbox.Min.Z != nilz || bbox.Max.Z != nilz {
|
|
|
|
header |= 1 << 5
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if isCordZ {
|
|
|
|
header |= 1 << 6
|
|
|
|
}
|
|
|
|
buf.WriteByte(header)
|
|
|
|
if bbox != nil {
|
|
|
|
b := make([]byte, 8)
|
|
|
|
binary.LittleEndian.PutUint64(b, math.Float64bits(bbox.Min.X))
|
|
|
|
buf.Write(b)
|
|
|
|
binary.LittleEndian.PutUint64(b, math.Float64bits(bbox.Min.Y))
|
|
|
|
buf.Write(b)
|
|
|
|
if bbox.Min.Z != nilz || bbox.Max.Z != nilz {
|
|
|
|
binary.LittleEndian.PutUint64(b, math.Float64bits(bbox.Min.Z))
|
|
|
|
buf.Write(b)
|
|
|
|
}
|
|
|
|
binary.LittleEndian.PutUint64(b, math.Float64bits(bbox.Max.X))
|
|
|
|
buf.Write(b)
|
|
|
|
binary.LittleEndian.PutUint64(b, math.Float64bits(bbox.Max.Y))
|
|
|
|
buf.Write(b)
|
|
|
|
if bbox.Min.Z != nilz || bbox.Max.Z != nilz {
|
|
|
|
binary.LittleEndian.PutUint64(b, math.Float64bits(bbox.Max.Z))
|
|
|
|
buf.Write(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func positionBBox(i int, bbox BBox, ps []Position) (int, BBox) {
|
|
|
|
for _, p := range ps {
|
|
|
|
if i == 0 {
|
|
|
|
bbox.Min = p
|
|
|
|
bbox.Max = p
|
|
|
|
} else {
|
|
|
|
if p.X < bbox.Min.X {
|
|
|
|
bbox.Min.X = p.X
|
|
|
|
}
|
|
|
|
if p.Y < bbox.Min.Y {
|
|
|
|
bbox.Min.Y = p.Y
|
|
|
|
}
|
|
|
|
if p.X > bbox.Max.X {
|
|
|
|
bbox.Max.X = p.X
|
|
|
|
}
|
|
|
|
if p.Y > bbox.Max.Y {
|
|
|
|
bbox.Max.Y = p.Y
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return i, bbox
|
|
|
|
}
|
|
|
|
|
|
|
|
func isLinearRing(ps []Position) bool {
|
|
|
|
return len(ps) >= 4 && ps[0] == ps[len(ps)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
// ObjectJSON parses geojson and returns an Object
|
2016-11-02 15:51:48 +03:00
|
|
|
func ObjectJSON(json string) (Object, error) {
|
|
|
|
return objectMap(json, root)
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
root = 0 // accept all types
|
|
|
|
gcoll = 1 // accept only geometries
|
|
|
|
feat = 2 // accept only geometries
|
|
|
|
fcoll = 3 // accept only features
|
|
|
|
)
|
|
|
|
|
2016-11-02 15:51:48 +03:00
|
|
|
func objectMap(json string, from int) (Object, error) {
|
2016-03-05 02:08:16 +03:00
|
|
|
var err error
|
2016-11-02 15:51:48 +03:00
|
|
|
res := gjson.Get(json, "type")
|
|
|
|
if res.Type != gjson.String {
|
2016-03-05 02:08:16 +03:00
|
|
|
return nil, errInvalidTypeMember
|
|
|
|
}
|
2016-11-02 15:51:48 +03:00
|
|
|
typ := res.String()
|
2016-03-05 02:08:16 +03:00
|
|
|
if from != root {
|
|
|
|
switch from {
|
|
|
|
case gcoll, feat:
|
|
|
|
switch typ {
|
2016-11-02 15:51:48 +03:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf(fmtErrTypeIsUnknown, typ)
|
2016-03-05 02:08:16 +03:00
|
|
|
case "Point", "MultiPoint", "LineString", "MultiLineString", "Polygon", "MultiPolygon", "GeometryCollection":
|
|
|
|
}
|
|
|
|
case fcoll:
|
|
|
|
switch typ {
|
2016-11-02 15:51:48 +03:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf(fmtErrTypeIsUnknown, typ)
|
2016-03-05 02:08:16 +03:00
|
|
|
case "Feature":
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var o Object
|
|
|
|
switch typ {
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf(fmtErrTypeIsUnknown, typ)
|
|
|
|
case "Point":
|
2016-12-17 00:02:58 +03:00
|
|
|
o, err = fillSimplePointOrPoint(fillLevel1Map(json))
|
2016-03-05 02:08:16 +03:00
|
|
|
case "MultiPoint":
|
2016-12-17 00:02:58 +03:00
|
|
|
o, err = fillMultiPoint(fillLevel2Map(json))
|
2016-03-05 02:08:16 +03:00
|
|
|
case "LineString":
|
2016-12-17 00:02:58 +03:00
|
|
|
o, err = fillLineString(fillLevel2Map(json))
|
2016-03-05 02:08:16 +03:00
|
|
|
case "MultiLineString":
|
2016-12-17 00:02:58 +03:00
|
|
|
o, err = fillMultiLineString(fillLevel3Map(json))
|
2016-03-05 02:08:16 +03:00
|
|
|
case "Polygon":
|
2016-12-17 00:02:58 +03:00
|
|
|
o, err = fillPolygon(fillLevel3Map(json))
|
2016-03-05 02:08:16 +03:00
|
|
|
case "MultiPolygon":
|
2016-12-17 00:02:58 +03:00
|
|
|
o, err = fillMultiPolygon(fillLevel4Map(json))
|
2016-03-05 02:08:16 +03:00
|
|
|
case "GeometryCollection":
|
2016-12-17 00:02:58 +03:00
|
|
|
o, err = fillGeometryCollectionMap(json)
|
2016-03-05 02:08:16 +03:00
|
|
|
case "Feature":
|
2016-12-17 00:02:58 +03:00
|
|
|
o, err = fillFeatureMap(json)
|
2016-03-05 02:08:16 +03:00
|
|
|
case "FeatureCollection":
|
2016-12-17 00:02:58 +03:00
|
|
|
o, err = fillFeatureCollectionMap(json)
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
return o, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func withinObjectShared(g Object, o Object, pin func(v Polygon) bool, mpin func(v MultiPolygon) bool) bool {
|
|
|
|
bbp := o.bboxPtr()
|
|
|
|
if bbp != nil {
|
|
|
|
return g.WithinBBox(*bbp)
|
|
|
|
}
|
|
|
|
switch v := o.(type) {
|
|
|
|
default:
|
|
|
|
return false
|
2016-12-06 16:20:47 +03:00
|
|
|
case Point:
|
|
|
|
return g.WithinBBox(v.CalculatedBBox())
|
|
|
|
case SimplePoint:
|
|
|
|
return g.WithinBBox(v.CalculatedBBox())
|
2016-03-05 02:08:16 +03:00
|
|
|
case Polygon:
|
|
|
|
if len(v.Coordinates) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return pin(v)
|
|
|
|
case MultiPolygon:
|
|
|
|
if len(v.Coordinates) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return mpin(v)
|
|
|
|
case Feature:
|
|
|
|
return g.Within(v.Geometry)
|
|
|
|
case FeatureCollection:
|
|
|
|
if len(v.Features) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, f := range v.Features {
|
|
|
|
if !g.Within(f) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
case GeometryCollection:
|
|
|
|
if len(v.Geometries) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, f := range v.Geometries {
|
|
|
|
if !g.Within(f) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func intersectsObjectShared(g Object, o Object, pin func(v Polygon) bool, mpin func(v MultiPolygon) bool) bool {
|
|
|
|
bbp := o.bboxPtr()
|
|
|
|
if bbp != nil {
|
|
|
|
return g.IntersectsBBox(*bbp)
|
|
|
|
}
|
|
|
|
switch v := o.(type) {
|
|
|
|
default:
|
|
|
|
return false
|
2016-12-06 16:20:47 +03:00
|
|
|
case Point:
|
|
|
|
return g.IntersectsBBox(v.CalculatedBBox())
|
|
|
|
case SimplePoint:
|
|
|
|
return g.IntersectsBBox(v.CalculatedBBox())
|
2016-03-05 02:08:16 +03:00
|
|
|
case Polygon:
|
|
|
|
if len(v.Coordinates) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return pin(v)
|
|
|
|
case MultiPolygon:
|
|
|
|
if len(v.Coordinates) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return mpin(v)
|
|
|
|
case Feature:
|
|
|
|
return g.Intersects(v.Geometry)
|
|
|
|
case FeatureCollection:
|
|
|
|
if len(v.Features) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, f := range v.Features {
|
|
|
|
if g.Intersects(f) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
case GeometryCollection:
|
|
|
|
if len(v.Geometries) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, f := range v.Geometries {
|
|
|
|
if g.Intersects(f) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-03 05:16:36 +03:00
|
|
|
// CirclePolygon returns a Polygon around the radius.
|
2016-03-05 02:08:16 +03:00
|
|
|
func CirclePolygon(x, y, meters float64, steps int) Polygon {
|
|
|
|
if steps < 3 {
|
|
|
|
steps = 3
|
|
|
|
}
|
|
|
|
p := Polygon{
|
|
|
|
Coordinates: [][]Position{make([]Position, steps+1)},
|
|
|
|
}
|
|
|
|
center := Position{X: x, Y: y, Z: 0}
|
|
|
|
step := 360.0 / float64(steps)
|
|
|
|
i := 0
|
2016-12-05 18:06:48 +03:00
|
|
|
for deg := 360.0; deg > 0; deg -= step {
|
2016-03-05 02:08:16 +03:00
|
|
|
c := Position(poly.Point(center.Destination(meters, deg)))
|
|
|
|
p.Coordinates[0][i] = c
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
p.Coordinates[0][i] = p.Coordinates[0][0]
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// The object's calculated bounding box must intersect the radius of the circle to pass.
|
|
|
|
func nearbyObjectShared(g Object, x, y float64, meters float64) bool {
|
|
|
|
if !g.hasPositions() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
center := Position{X: x, Y: y, Z: 0}
|
|
|
|
bbox := g.CalculatedBBox()
|
|
|
|
if bbox.Min.X == bbox.Max.X && bbox.Min.Y == bbox.Max.Y {
|
|
|
|
// just a point, return is point is inside of the circle
|
|
|
|
return center.DistanceTo(bbox.Min) <= meters
|
|
|
|
}
|
|
|
|
circlePoly := CirclePolygon(x, y, meters, 12)
|
|
|
|
return g.Intersects(circlePoly)
|
|
|
|
}
|
2016-11-02 15:51:48 +03:00
|
|
|
|
2016-11-07 23:04:21 +03:00
|
|
|
func jsonMarshalString(s string) []byte {
|
2016-11-02 15:51:48 +03:00
|
|
|
for i := 0; i < len(s); i++ {
|
2016-11-07 23:04:21 +03:00
|
|
|
if s[i] < ' ' || s[i] == '\\' || s[i] == '"' || s[i] > 126 {
|
|
|
|
b, _ := json.Marshal(s)
|
|
|
|
return b
|
2016-11-02 15:51:48 +03:00
|
|
|
}
|
|
|
|
}
|
2016-11-07 23:04:21 +03:00
|
|
|
b := make([]byte, len(s)+2)
|
|
|
|
b[0] = '"'
|
|
|
|
copy(b[1:], s)
|
|
|
|
b[len(b)-1] = '"'
|
|
|
|
return b
|
2016-11-02 15:51:48 +03:00
|
|
|
}
|
2016-11-02 16:35:05 +03:00
|
|
|
|
|
|
|
func stripWhitespace(s string) string {
|
|
|
|
var p []byte
|
|
|
|
var str bool
|
|
|
|
var escs int
|
|
|
|
for i := 0; i < len(s); i++ {
|
|
|
|
c := s[i]
|
|
|
|
if str {
|
|
|
|
// We're inside of a string. Look out for '"' and '\' characters.
|
|
|
|
if c == '\\' {
|
|
|
|
// Increment the escape character counter.
|
|
|
|
escs++
|
|
|
|
} else {
|
|
|
|
if c == '"' && escs%2 == 0 {
|
|
|
|
// We reached the end of string
|
|
|
|
str = false
|
|
|
|
}
|
|
|
|
// Reset the escape counter
|
|
|
|
escs = 0
|
|
|
|
}
|
|
|
|
} else if c == '"' {
|
|
|
|
// We encountared a double quote character.
|
|
|
|
str = true
|
|
|
|
} else if c <= ' ' {
|
|
|
|
// Ignore the whitespace
|
|
|
|
if p == nil {
|
|
|
|
p = []byte(s[:i])
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Append the character
|
|
|
|
if p != nil {
|
|
|
|
p = append(p, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if p == nil {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
return string(p)
|
|
|
|
}
|