2016-03-05 02:08:16 +03:00
|
|
|
package geojson
|
|
|
|
|
|
|
|
import "github.com/tidwall/tile38/geojson/geohash"
|
|
|
|
|
|
|
|
// LineString is a geojson object with the type "LineString"
|
|
|
|
type LineString struct {
|
|
|
|
Coordinates []Position
|
|
|
|
BBox *BBox
|
2018-01-11 23:57:18 +03:00
|
|
|
bboxDefined bool
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
2016-12-17 00:02:58 +03:00
|
|
|
func fillLineString(coordinates []Position, bbox *BBox, err error) (LineString, error) {
|
2016-03-05 02:08:16 +03:00
|
|
|
if err == nil {
|
|
|
|
if len(coordinates) < 2 {
|
|
|
|
err = errLineStringInvalidCoordinates
|
|
|
|
}
|
|
|
|
}
|
2018-01-11 23:57:18 +03:00
|
|
|
bboxDefined := bbox != nil
|
|
|
|
if !bboxDefined {
|
|
|
|
cbbox := level2CalculatedBBox(coordinates, nil)
|
|
|
|
bbox = &cbbox
|
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
return LineString{
|
|
|
|
Coordinates: coordinates,
|
|
|
|
BBox: bbox,
|
2018-01-11 23:57:18 +03:00
|
|
|
bboxDefined: bboxDefined,
|
2016-12-17 00:02:58 +03:00
|
|
|
}, err
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// CalculatedBBox is exterior bbox containing the object.
|
|
|
|
func (g LineString) CalculatedBBox() BBox {
|
|
|
|
return level2CalculatedBBox(g.Coordinates, g.BBox)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CalculatedPoint is a point representation of the object.
|
|
|
|
func (g LineString) CalculatedPoint() Position {
|
|
|
|
return g.CalculatedBBox().center()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Geohash converts the object to a geohash value.
|
|
|
|
func (g LineString) Geohash(precision int) (string, error) {
|
|
|
|
p := g.CalculatedPoint()
|
|
|
|
return geohash.Encode(p.Y, p.X, precision)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PositionCount return the number of coordinates.
|
|
|
|
func (g LineString) PositionCount() int {
|
|
|
|
return level2PositionCount(g.Coordinates, g.BBox)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Weight returns the in-memory size of the object.
|
|
|
|
func (g LineString) Weight() int {
|
|
|
|
return level2Weight(g.Coordinates, g.BBox)
|
|
|
|
}
|
|
|
|
|
2018-01-11 23:57:18 +03:00
|
|
|
func (g LineString) appendJSON(json []byte) []byte {
|
|
|
|
return appendLevel2JSON(json, "LineString", g.Coordinates, g.BBox, g.bboxDefined)
|
|
|
|
}
|
|
|
|
|
2016-03-05 02:08:16 +03:00
|
|
|
// MarshalJSON allows the object to be encoded in json.Marshal calls.
|
|
|
|
func (g LineString) MarshalJSON() ([]byte, error) {
|
2018-01-11 23:57:18 +03:00
|
|
|
return g.appendJSON(nil), nil
|
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 LineString) 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 LineString) String() string {
|
|
|
|
return g.JSON()
|
|
|
|
}
|
|
|
|
|
2016-03-05 02:08:16 +03:00
|
|
|
func (g LineString) bboxPtr() *BBox {
|
|
|
|
return g.BBox
|
|
|
|
}
|
2018-01-11 23:57:18 +03:00
|
|
|
|
2016-03-05 02:08:16 +03:00
|
|
|
func (g LineString) hasPositions() bool {
|
2018-01-11 23:57:18 +03:00
|
|
|
return g.bboxDefined || len(g.Coordinates) > 0
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithinBBox detects if the object is fully contained inside a bbox.
|
|
|
|
func (g LineString) 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))
|
|
|
|
}
|
|
|
|
return polyPositions(g.Coordinates).InsideRect(rectBBox(bbox))
|
|
|
|
}
|
|
|
|
|
|
|
|
// IntersectsBBox detects if the object intersects a bbox.
|
|
|
|
func (g LineString) 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))
|
|
|
|
}
|
|
|
|
return polyPositions(g.Coordinates).IntersectsRect(rectBBox(bbox))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Within detects if the object is fully contained inside another object.
|
|
|
|
func (g LineString) Within(o Object) bool {
|
2018-03-10 05:05:31 +03:00
|
|
|
return withinObjectShared(g, o,
|
|
|
|
func(v Polygon) bool {
|
|
|
|
return polyPositions(g.Coordinates).Inside(polyExteriorHoles(v.Coordinates))
|
|
|
|
},
|
|
|
|
)
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Intersects detects if the object intersects another object.
|
|
|
|
func (g LineString) Intersects(o Object) bool {
|
2018-03-10 05:05:31 +03:00
|
|
|
return intersectsObjectShared(g, o,
|
|
|
|
func(v Polygon) bool {
|
|
|
|
return polyPositions(g.Coordinates).LineStringIntersects(polyExteriorHoles(v.Coordinates))
|
|
|
|
},
|
|
|
|
)
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Nearby detects if the object is nearby a position.
|
|
|
|
func (g LineString) 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 LineString) 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 LineString) IsGeometry() bool {
|
|
|
|
return true
|
|
|
|
}
|