tidwall 6257ddba78 Faster point in polygon / GeoJSON updates
The big change is that the GeoJSON package has been completely
rewritten to fix a few of geometry calculation bugs, increase
performance, and to better follow the GeoJSON spec RFC 7946.

GeoJSON updates

- A LineString now requires at least two points.
- All json members, even foreign, now persist with the object.
- The bbox member persists too but is no longer used for geometry
  calculations. This is change in behavior. Previously Tile38 would
  treat the bbox as the object's physical rectangle.
- Corrections to geometry intersects and within calculations.

Faster spatial queries

- The performance of Point-in-polygon and object intersect operations
  are greatly improved for complex polygons and line strings. It went
  from O(n) to roughly O(log n).
- The same for all collection types with many children, including
  FeatureCollection, GeometryCollection, MultiPoint, MultiLineString,
  and MultiPolygon.

Codebase changes

- The pkg directory has been renamed to internal
- The GeoJSON internal package has been moved to a seperate repo at
  https://github.com/tidwall/geojson. It's now vendored.

Please look out for higher memory usage for datasets using complex
shapes. A complex shape is one that has 64 or more points. For these
shapes it's expected that there will be increase of least 54 bytes per
point.
2018-10-13 04:30:48 -07:00

146 lines
2.8 KiB
Go

package geojson
import "github.com/tidwall/geojson/geometry"
// Rect ...
type Rect struct {
base geometry.Rect
}
// NewRect ...
func NewRect(rect geometry.Rect) *Rect {
return &Rect{base: rect}
}
// ForEach ...
func (g *Rect) ForEach(iter func(geom Object) bool) bool {
return iter(g)
}
// Empty ...
func (g *Rect) Empty() bool {
return g.base.Empty()
}
// Rect ...
func (g *Rect) Rect() geometry.Rect {
return g.base
}
// Base ...
func (g *Rect) Base() geometry.Rect {
return g.base
}
// Center ...
func (g *Rect) Center() geometry.Point {
return g.base.Center()
}
// AppendJSON ...
func (g *Rect) AppendJSON(dst []byte) []byte {
var gPoly Polygon
gPoly.base.Exterior = g.base
return gPoly.AppendJSON(dst)
}
// JSON ...
func (g *Rect) JSON() string {
return string(g.AppendJSON(nil))
}
// String ...
func (g *Rect) String() string {
return string(g.AppendJSON(nil))
}
// Contains ...
func (g *Rect) Contains(obj Object) bool {
return obj.Spatial().WithinRect(g.base)
}
// Within ...
func (g *Rect) Within(obj Object) bool {
return obj.Contains(g)
}
// WithinRect ...
func (g *Rect) WithinRect(rect geometry.Rect) bool {
return rect.ContainsRect(g.base)
}
// WithinPoint ...
func (g *Rect) WithinPoint(point geometry.Point) bool {
return point.ContainsRect(g.base)
}
// WithinLine ...
func (g *Rect) WithinLine(line *geometry.Line) bool {
return line.ContainsRect(g.base)
}
// WithinPoly ...
func (g *Rect) WithinPoly(poly *geometry.Poly) bool {
return poly.ContainsRect(g.base)
}
// Intersects ...
func (g *Rect) Intersects(obj Object) bool {
return obj.Spatial().IntersectsRect(g.base)
}
// IntersectsPoint ...
func (g *Rect) IntersectsPoint(point geometry.Point) bool {
return g.base.IntersectsPoint(point)
}
// IntersectsRect ...
func (g *Rect) IntersectsRect(rect geometry.Rect) bool {
return g.base.IntersectsRect(rect)
}
// IntersectsLine ...
func (g *Rect) IntersectsLine(line *geometry.Line) bool {
return g.base.IntersectsLine(line)
}
// IntersectsPoly ...
func (g *Rect) IntersectsPoly(poly *geometry.Poly) bool {
return g.base.IntersectsPoly(poly)
}
// NumPoints ...
func (g *Rect) NumPoints() int {
return 2
}
// Spatial ...
func (g *Rect) Spatial() Spatial {
return g
}
// Distance ...
func (g *Rect) Distance(obj Object) float64 {
return obj.Spatial().DistanceRect(g.base)
}
// DistancePoint ...
func (g *Rect) DistancePoint(point geometry.Point) float64 {
return geoDistancePoints(g.Center(), point)
}
// DistanceRect ...
func (g *Rect) DistanceRect(rect geometry.Rect) float64 {
return geoDistancePoints(g.Center(), rect.Center())
}
// DistanceLine ...
func (g *Rect) DistanceLine(line *geometry.Line) float64 {
return geoDistancePoints(g.Center(), line.Rect().Center())
}
// DistancePoly ...
func (g *Rect) DistancePoly(poly *geometry.Poly) float64 {
return geoDistancePoints(g.Center(), poly.Rect().Center())
}