2016-03-05 02:08:16 +03:00
|
|
|
package geojson
|
|
|
|
|
2018-04-19 18:43:32 +03:00
|
|
|
import "github.com/tidwall/tile38/pkg/geojson/geohash"
|
2016-03-05 02:08:16 +03:00
|
|
|
|
|
|
|
// MultiPolygon is a geojson object with the type "MultiPolygon"
|
|
|
|
type MultiPolygon struct {
|
|
|
|
Coordinates [][][]Position
|
|
|
|
BBox *BBox
|
2018-01-11 23:57:18 +03:00
|
|
|
bboxDefined bool
|
2018-03-07 23:18:25 +03:00
|
|
|
polygons []Polygon
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
2016-12-17 00:02:58 +03:00
|
|
|
func fillMultiPolygon(coordinates [][][]Position, bbox *BBox, err error) (MultiPolygon, error) {
|
2018-03-07 23:18:25 +03:00
|
|
|
polygons := make([]Polygon, len(coordinates))
|
2016-03-05 02:08:16 +03:00
|
|
|
if err == nil {
|
2018-03-07 23:18:25 +03:00
|
|
|
for i, ps := range coordinates {
|
|
|
|
polygons[i], err = fillPolygon(ps, nil, nil)
|
|
|
|
if err != nil {
|
2016-03-05 02:08:16 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-11 23:57:18 +03:00
|
|
|
bboxDefined := bbox != nil
|
|
|
|
if !bboxDefined {
|
2018-03-10 05:05:31 +03:00
|
|
|
cbbox := calculatedBBox(polygons, nil)
|
2018-01-11 23:57:18 +03:00
|
|
|
bbox = &cbbox
|
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
return MultiPolygon{
|
|
|
|
Coordinates: coordinates,
|
|
|
|
BBox: bbox,
|
2018-01-11 23:57:18 +03:00
|
|
|
bboxDefined: bboxDefined,
|
2018-03-08 22:16:12 +03:00
|
|
|
polygons: polygons,
|
2016-12-17 00:02:58 +03:00
|
|
|
}, err
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
2018-03-10 05:05:31 +03:00
|
|
|
func calculatedBBox(polygons []Polygon, bbox *BBox) BBox {
|
2018-03-07 23:18:25 +03:00
|
|
|
if bbox != nil {
|
|
|
|
return *bbox
|
|
|
|
}
|
|
|
|
var cbbox BBox
|
2018-03-10 05:05:31 +03:00
|
|
|
for i, p := range polygons {
|
2018-03-07 23:18:25 +03:00
|
|
|
if i == 0 {
|
2018-03-10 05:05:31 +03:00
|
|
|
cbbox = p.CalculatedBBox()
|
2018-03-07 23:18:25 +03:00
|
|
|
} else {
|
2018-03-10 05:05:31 +03:00
|
|
|
cbbox = cbbox.union(p.CalculatedBBox())
|
2018-03-07 23:18:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return cbbox
|
|
|
|
}
|
|
|
|
|
2016-03-05 02:08:16 +03:00
|
|
|
// CalculatedBBox is exterior bbox containing the object.
|
|
|
|
func (g MultiPolygon) CalculatedBBox() BBox {
|
2018-03-10 05:05:31 +03:00
|
|
|
return calculatedBBox(g.polygons, g.BBox)
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// CalculatedPoint is a point representation of the object.
|
|
|
|
func (g MultiPolygon) CalculatedPoint() Position {
|
|
|
|
return g.CalculatedBBox().center()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Geohash converts the object to a geohash value.
|
|
|
|
func (g MultiPolygon) Geohash(precision int) (string, error) {
|
|
|
|
p := g.CalculatedPoint()
|
|
|
|
return geohash.Encode(p.Y, p.X, precision)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PositionCount return the number of coordinates.
|
|
|
|
func (g MultiPolygon) PositionCount() int {
|
|
|
|
return level4PositionCount(g.Coordinates, g.BBox)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Weight returns the in-memory size of the object.
|
|
|
|
func (g MultiPolygon) Weight() int {
|
|
|
|
return level4Weight(g.Coordinates, g.BBox)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON allows the object to be encoded in json.Marshal calls.
|
|
|
|
func (g MultiPolygon) MarshalJSON() ([]byte, error) {
|
2018-01-11 23:57:18 +03:00
|
|
|
return g.appendJSON(nil), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g MultiPolygon) appendJSON(json []byte) []byte {
|
|
|
|
return appendLevel4JSON(json, "MultiPolygon", g.Coordinates, g.BBox, g.bboxDefined)
|
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 MultiPolygon) JSON() string {
|
2018-01-11 23:57:18 +03:00
|
|
|
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 MultiPolygon) String() string {
|
|
|
|
return g.JSON()
|
|
|
|
}
|
|
|
|
|
2016-03-05 02:08:16 +03:00
|
|
|
func (g MultiPolygon) bboxPtr() *BBox {
|
|
|
|
return g.BBox
|
|
|
|
}
|
|
|
|
func (g MultiPolygon) hasPositions() bool {
|
2018-01-11 23:57:18 +03:00
|
|
|
if g.bboxDefined {
|
2016-03-05 02:08:16 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
for _, c := range g.Coordinates {
|
|
|
|
for _, c := range c {
|
|
|
|
if len(c) > 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-03-07 23:18:25 +03:00
|
|
|
func (g MultiPolygon) getPolygon(index int) Polygon {
|
|
|
|
if index < len(g.polygons) {
|
|
|
|
return g.polygons[index]
|
|
|
|
}
|
|
|
|
return Polygon{Coordinates: g.Coordinates[index]}
|
|
|
|
}
|
|
|
|
|
2016-03-05 02:08:16 +03:00
|
|
|
// WithinBBox detects if the object is fully contained inside a bbox.
|
|
|
|
func (g MultiPolygon) 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.Coordinates) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
2018-03-07 23:18:25 +03:00
|
|
|
for i := range g.Coordinates {
|
|
|
|
if !g.getPolygon(i).WithinBBox(bbox) {
|
2016-03-05 02:08:16 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// IntersectsBBox detects if the object intersects a bbox.
|
|
|
|
func (g MultiPolygon) IntersectsBBox(bbox BBox) bool {
|
2018-03-05 19:00:14 +03:00
|
|
|
if g.bboxDefined {
|
2016-03-05 02:08:16 +03:00
|
|
|
return rectBBox(g.CalculatedBBox()).IntersectsRect(rectBBox(bbox))
|
|
|
|
}
|
2018-03-07 23:18:25 +03:00
|
|
|
for i := range g.Coordinates {
|
|
|
|
if g.getPolygon(i).IntersectsBBox(bbox) {
|
2016-03-05 02:08:16 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Within detects if the object is fully contained inside another object.
|
|
|
|
func (g MultiPolygon) Within(o Object) bool {
|
2018-03-10 05:05:31 +03:00
|
|
|
return withinObjectShared(g, o,
|
|
|
|
func(v Polygon) bool {
|
|
|
|
if len(g.Coordinates) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !v.Within(o) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
)
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
2018-05-04 20:21:40 +03:00
|
|
|
// WithinCircle detects if the object is fully contained inside a circle.
|
|
|
|
func (g MultiPolygon) WithinCircle(center Position, meters float64) bool {
|
|
|
|
if len(g.polygons) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, polygon := range g.polygons {
|
|
|
|
if !polygon.WithinCircle(center, meters) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-03-05 02:08:16 +03:00
|
|
|
// Intersects detects if the object intersects another object.
|
|
|
|
func (g MultiPolygon) Intersects(o Object) bool {
|
2018-03-10 05:05:31 +03:00
|
|
|
return intersectsObjectShared(g, o,
|
|
|
|
func(v Polygon) bool {
|
|
|
|
if len(g.Coordinates) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if v.Intersects(o) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
)
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
2018-05-04 20:21:40 +03:00
|
|
|
// IntersectsCircle detects if the object intersects a circle.
|
|
|
|
func (g MultiPolygon) IntersectsCircle(center Position, meters float64) bool {
|
|
|
|
for _, polygon := range g.polygons {
|
|
|
|
if polygon.IntersectsCircle(center, meters) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-03-05 02:08:16 +03:00
|
|
|
// Nearby detects if the object is nearby a position.
|
|
|
|
func (g MultiPolygon) 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 MultiPolygon) 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 MultiPolygon) IsGeometry() bool {
|
|
|
|
return true
|
|
|
|
}
|