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

73 lines
1.7 KiB
Go

package geojson
import (
"strconv"
"github.com/tidwall/geojson/geo"
"github.com/tidwall/geojson/geometry"
)
// Circle ...
type Circle struct {
Object
center geometry.Point
meters float64
steps int
km bool
extra *extra
}
// NewCircle returns an circle object
func NewCircle(center geometry.Point, meters float64, steps int) *Circle {
if steps < 3 {
steps = 3
}
g := new(Circle)
g.center = center
g.meters = meters
g.steps = steps
if meters <= 0 {
g.Object = NewPoint(center)
} else {
var points []geometry.Point
step := 360.0 / float64(steps)
i := 0
for deg := 360.0; deg > 0; deg -= step {
lat, lon := geo.DestinationPoint(center.Y, center.X, meters, deg)
points = append(points, geometry.Point{X: lon, Y: lat})
i++
}
// TODO: account for the pole and antimerdian. In most cases only a polygon
// is needed, but when the circle bounds passes the 90/180 lines, we need
// to create a multipolygon
points = append(points, points[0])
g.Object = NewPolygon(
geometry.NewPoly(points, nil, geometry.DefaultIndex),
)
}
return g
}
// AppendJSON ...
func (g *Circle) AppendJSON(dst []byte) []byte {
dst = append(dst, `{"type":"Feature","geometry":`...)
dst = append(dst, `{"type":"Point","coordinates":[`...)
dst = strconv.AppendFloat(dst, g.center.X, 'f', -1, 64)
dst = append(dst, ',')
dst = strconv.AppendFloat(dst, g.center.Y, 'f', -1, 64)
dst = append(dst, `]},"properties":{"type":"Circle","radius":`...)
dst = strconv.AppendFloat(dst, g.meters, 'f', -1, 64)
dst = append(dst, `",radius_units":"m"}}`...)
return dst
}
// JSON ...
func (g *Circle) JSON() string {
return string(g.AppendJSON(nil))
}
// String ...
func (g *Circle) String() string {
return string(g.AppendJSON(nil))
}