mirror of https://github.com/tidwall/tile38.git
Merge pull request #433 from tidwall/nearby-431
Fixed nearby inaccuracy with geofence
This commit is contained in:
commit
6b937934e3
|
@ -242,7 +242,7 @@
|
|||
version = "v1.0.2"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:fc81262a6ad5aeec27e1bd15356f790e6b2d8fd14acb6bd5ff3f0f51bf67417f"
|
||||
digest = "1:cdab3bce90a53a124ac3982719abde77d779e961d9c180e55c23fb74fc62563a"
|
||||
name = "github.com/tidwall/geojson"
|
||||
packages = [
|
||||
".",
|
||||
|
@ -250,8 +250,8 @@
|
|||
"geometry",
|
||||
]
|
||||
pruneopts = ""
|
||||
revision = "6baab67ab6a9bac4abf153ab779c736254a37fd1"
|
||||
version = "v1.1.0"
|
||||
revision = "eaf6e0a55a79c1e879bbbcc879a3176c720d99cd"
|
||||
version = "v1.1.3"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:3ddca2bd5496c6922a2a9e636530e178a43c2a534ea6634211acdc7d10222794"
|
||||
|
|
|
@ -36,7 +36,7 @@ required = [
|
|||
|
||||
[[constraint]]
|
||||
name = "github.com/tidwall/geojson"
|
||||
version = "1.1.0"
|
||||
version = "1.1.3"
|
||||
|
||||
[[constraint]]
|
||||
name = "github.com/Shopify/sarama"
|
||||
|
|
|
@ -99,6 +99,8 @@ func (g *Circle) Contains(obj Object) bool {
|
|||
switch other := obj.(type) {
|
||||
case *Point:
|
||||
return g.containsPoint(other.Center())
|
||||
case *SimplePoint:
|
||||
return g.containsPoint(other.Center())
|
||||
case *Circle:
|
||||
return other.Distance(g) < (other.meters + g.meters)
|
||||
case *LineString:
|
||||
|
@ -244,8 +246,8 @@ func makeCircleObject(center geometry.Point, meters float64, steps int) Object {
|
|||
// generate the
|
||||
for th := 0.0; th <= 360.0; th += 360.0 / float64(steps) {
|
||||
radians := (math.Pi / 180) * th
|
||||
x := center.X + lats*math.Cos(radians)
|
||||
y := center.Y + lons*math.Sin(radians)
|
||||
x := center.X + lons*math.Cos(radians)
|
||||
y := center.Y + lats*math.Sin(radians)
|
||||
points = append(points, geometry.Point{X: x, Y: y})
|
||||
}
|
||||
// add last connecting point, make a total of steps+1
|
||||
|
|
|
@ -171,3 +171,11 @@ func TestCircleIntersects(t *testing.T) {
|
|||
// }
|
||||
// expect(t, true)
|
||||
//}
|
||||
|
||||
func TestPointCircle(t *testing.T) {
|
||||
p := NewPoint(geometry.Point{X: -0.8856761, Y: 52.7563759})
|
||||
c := NewCircle(geometry.Point{X: -0.8838196, Y: 52.7563395}, 200, 20)
|
||||
if !p.Within(c) {
|
||||
t.Fatal("expected true")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -50,7 +50,7 @@ var _ = []Object{
|
|||
&Point{}, &LineString{}, &Polygon{}, &Feature{},
|
||||
&MultiPoint{}, &MultiLineString{}, &MultiPolygon{},
|
||||
&GeometryCollection{}, &FeatureCollection{},
|
||||
&Rect{}, &Circle{},
|
||||
&Rect{}, &Circle{}, &SimplePoint{},
|
||||
}
|
||||
|
||||
// Collection is a searchable collection type.
|
||||
|
@ -89,7 +89,12 @@ type ParseOptions struct {
|
|||
// IndexGeometryKind is the kind of index implementation.
|
||||
// Default is QuadTreeCompressed
|
||||
IndexGeometryKind geometry.IndexKind
|
||||
RequireValid bool
|
||||
// RequireValid option cause parse to fail when a geojson object is invalid.
|
||||
RequireValid bool
|
||||
// AllowSimplePoints options will force to parse to return the SimplePoint
|
||||
// type when a geojson point only consists of an 2D x/y coord and no extra
|
||||
// json members.
|
||||
AllowSimplePoints bool
|
||||
}
|
||||
|
||||
// DefaultParseOptions ...
|
||||
|
@ -98,6 +103,7 @@ var DefaultParseOptions = &ParseOptions{
|
|||
IndexGeometry: 64,
|
||||
IndexGeometryKind: geometry.QuadTree,
|
||||
RequireValid: false,
|
||||
AllowSimplePoints: false,
|
||||
}
|
||||
|
||||
// Parse a GeoJSON object
|
||||
|
@ -296,10 +302,3 @@ func unionRects(a, b geometry.Rect) geometry.Rect {
|
|||
func geoDistancePoints(a, b geometry.Point) float64 {
|
||||
return geo.DistanceTo(a.Y, a.X, b.Y, b.X)
|
||||
}
|
||||
|
||||
// func geoDistanceCenterToPoint(obj Object, point geometry.Point) float64 {
|
||||
// if obj.Empty() {
|
||||
// return 0
|
||||
// }
|
||||
// return geoDistancePointsA(obj.Center(), point)
|
||||
// }
|
||||
|
|
|
@ -95,6 +95,9 @@ func (g *Point) Contains(obj Object) bool {
|
|||
|
||||
// Intersects ...
|
||||
func (g *Point) Intersects(obj Object) bool {
|
||||
if obj, ok := obj.(*Circle); ok {
|
||||
return obj.Contains(g)
|
||||
}
|
||||
return obj.Spatial().IntersectsPoint(g.base)
|
||||
}
|
||||
|
||||
|
@ -152,21 +155,30 @@ func (g *Point) Z() float64 {
|
|||
}
|
||||
|
||||
func parseJSONPoint(keys *parseKeys, opts *ParseOptions) (Object, error) {
|
||||
var g Point
|
||||
var err error
|
||||
g.base, g.extra, err = parseJSONPointCoords(keys, gjson.Result{}, opts)
|
||||
var o Object
|
||||
base, extra, err := parseJSONPointCoords(keys, gjson.Result{}, opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := parseBBoxAndExtras(&g.extra, keys, opts); err != nil {
|
||||
if err := parseBBoxAndExtras(&extra, keys, opts); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if extra == nil && opts.AllowSimplePoints {
|
||||
var g SimplePoint
|
||||
g.base = base
|
||||
o = &g
|
||||
} else {
|
||||
var g Point
|
||||
g.base = base
|
||||
g.extra = extra
|
||||
o = &g
|
||||
}
|
||||
if opts.RequireValid {
|
||||
if !g.Valid() {
|
||||
if !o.Valid() {
|
||||
return nil, errCoordinatesInvalid
|
||||
}
|
||||
}
|
||||
return &g, nil
|
||||
return o, nil
|
||||
}
|
||||
|
||||
func parseJSONPointCoords(
|
||||
|
@ -244,3 +256,14 @@ func (g *Point) DistanceLine(line *geometry.Line) float64 {
|
|||
func (g *Point) DistancePoly(poly *geometry.Poly) float64 {
|
||||
return geoDistancePoints(g.Center(), poly.Rect().Center())
|
||||
}
|
||||
|
||||
// IsPoint returns true if the object is a {"type":"Point"}
|
||||
func IsPoint(obj Object) (z float64, ok bool) {
|
||||
switch pt := obj.(type) {
|
||||
case *SimplePoint:
|
||||
return 0, true
|
||||
case *Point:
|
||||
return pt.Z(), true
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
|
|
@ -0,0 +1,159 @@
|
|||
package geojson
|
||||
|
||||
import "github.com/tidwall/geojson/geometry"
|
||||
|
||||
// SimplePoint ...
|
||||
type SimplePoint struct {
|
||||
base geometry.Point
|
||||
}
|
||||
|
||||
// NewSimplePoint ...
|
||||
func NewSimplePoint(point geometry.Point) *SimplePoint {
|
||||
return &SimplePoint{base: point}
|
||||
}
|
||||
|
||||
// ForEach ...
|
||||
func (g *SimplePoint) ForEach(iter func(geom Object) bool) bool {
|
||||
return iter(g)
|
||||
}
|
||||
|
||||
// Empty ...
|
||||
func (g *SimplePoint) Empty() bool {
|
||||
return g.base.Empty()
|
||||
}
|
||||
|
||||
// Valid ...
|
||||
func (g *SimplePoint) Valid() bool {
|
||||
return g.base.Valid()
|
||||
}
|
||||
|
||||
// Rect ...
|
||||
func (g *SimplePoint) Rect() geometry.Rect {
|
||||
return g.base.Rect()
|
||||
}
|
||||
|
||||
// Spatial ...
|
||||
func (g *SimplePoint) Spatial() Spatial {
|
||||
return g
|
||||
}
|
||||
|
||||
// Center ...
|
||||
func (g *SimplePoint) Center() geometry.Point {
|
||||
return g.base
|
||||
}
|
||||
|
||||
// Base ...
|
||||
func (g *SimplePoint) Base() geometry.Point {
|
||||
return g.base
|
||||
}
|
||||
|
||||
// AppendJSON ...
|
||||
func (g *SimplePoint) AppendJSON(dst []byte) []byte {
|
||||
dst = append(dst, `{"type":"Point","coordinates":`...)
|
||||
dst = appendJSONPoint(dst, g.base, nil, 0)
|
||||
dst = append(dst, '}')
|
||||
return dst
|
||||
}
|
||||
|
||||
// JSON ...
|
||||
func (g *SimplePoint) JSON() string {
|
||||
return string(g.AppendJSON(nil))
|
||||
}
|
||||
|
||||
// MarshalJSON ...
|
||||
func (g *SimplePoint) MarshalJSON() ([]byte, error) {
|
||||
return g.AppendJSON(nil), nil
|
||||
}
|
||||
|
||||
// String ...
|
||||
func (g *SimplePoint) String() string {
|
||||
return string(g.AppendJSON(nil))
|
||||
}
|
||||
|
||||
// Within ...
|
||||
func (g *SimplePoint) Within(obj Object) bool {
|
||||
return obj.Contains(g)
|
||||
}
|
||||
|
||||
// Contains ...
|
||||
func (g *SimplePoint) Contains(obj Object) bool {
|
||||
return obj.Spatial().WithinPoint(g.base)
|
||||
}
|
||||
|
||||
// Intersects ...
|
||||
func (g *SimplePoint) Intersects(obj Object) bool {
|
||||
if obj, ok := obj.(*Circle); ok {
|
||||
return obj.Contains(g)
|
||||
}
|
||||
return obj.Spatial().IntersectsPoint(g.base)
|
||||
}
|
||||
|
||||
// WithinRect ...
|
||||
func (g *SimplePoint) WithinRect(rect geometry.Rect) bool {
|
||||
return rect.ContainsPoint(g.base)
|
||||
}
|
||||
|
||||
// WithinPoint ...
|
||||
func (g *SimplePoint) WithinPoint(point geometry.Point) bool {
|
||||
return point.ContainsPoint(g.base)
|
||||
}
|
||||
|
||||
// WithinLine ...
|
||||
func (g *SimplePoint) WithinLine(line *geometry.Line) bool {
|
||||
return line.ContainsPoint(g.base)
|
||||
}
|
||||
|
||||
// WithinPoly ...
|
||||
func (g *SimplePoint) WithinPoly(poly *geometry.Poly) bool {
|
||||
return poly.ContainsPoint(g.base)
|
||||
}
|
||||
|
||||
// IntersectsPoint ...
|
||||
func (g *SimplePoint) IntersectsPoint(point geometry.Point) bool {
|
||||
return g.base.IntersectsPoint(point)
|
||||
}
|
||||
|
||||
// IntersectsRect ...
|
||||
func (g *SimplePoint) IntersectsRect(rect geometry.Rect) bool {
|
||||
return g.base.IntersectsRect(rect)
|
||||
}
|
||||
|
||||
// IntersectsLine ...
|
||||
func (g *SimplePoint) IntersectsLine(line *geometry.Line) bool {
|
||||
return g.base.IntersectsLine(line)
|
||||
}
|
||||
|
||||
// IntersectsPoly ...
|
||||
func (g *SimplePoint) IntersectsPoly(poly *geometry.Poly) bool {
|
||||
return g.base.IntersectsPoly(poly)
|
||||
}
|
||||
|
||||
// NumPoints ...
|
||||
func (g *SimplePoint) NumPoints() int {
|
||||
return 1
|
||||
}
|
||||
|
||||
// Distance ...
|
||||
func (g *SimplePoint) Distance(obj Object) float64 {
|
||||
return obj.Spatial().DistancePoint(g.base)
|
||||
}
|
||||
|
||||
// DistancePoint ...
|
||||
func (g *SimplePoint) DistancePoint(point geometry.Point) float64 {
|
||||
return geoDistancePoints(g.Center(), point)
|
||||
}
|
||||
|
||||
// DistanceRect ...
|
||||
func (g *SimplePoint) DistanceRect(rect geometry.Rect) float64 {
|
||||
return geoDistancePoints(g.Center(), rect.Center())
|
||||
}
|
||||
|
||||
// DistanceLine ...
|
||||
func (g *SimplePoint) DistanceLine(line *geometry.Line) float64 {
|
||||
return geoDistancePoints(g.Center(), line.Rect().Center())
|
||||
}
|
||||
|
||||
// DistancePoly ...
|
||||
func (g *SimplePoint) DistancePoly(poly *geometry.Poly) float64 {
|
||||
return geoDistancePoints(g.Center(), poly.Rect().Center())
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
package geojson
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestSimplePointNotSimple(t *testing.T) {
|
||||
p := expectJSONOpts(t, `{"type":"Point","coordinates":[1,2,3]}`, nil, &ParseOptions{AllowSimplePoints: true})
|
||||
expect(t, p.Center() == P(1, 2))
|
||||
expectJSONOpts(t, `{"type":"Point","coordinates":[1,null]}`, errCoordinatesInvalid, &ParseOptions{AllowSimplePoints: true})
|
||||
expectJSONOpts(t, `{"type":"Point","coordinates":[1,2],"bbox":null}`, nil, &ParseOptions{AllowSimplePoints: true})
|
||||
expectJSONOpts(t, `{"type":"Point"}`, errCoordinatesMissing, &ParseOptions{AllowSimplePoints: true})
|
||||
expectJSONOpts(t, `{"type":"Point","coordinates":null}`, errCoordinatesInvalid, &ParseOptions{AllowSimplePoints: true})
|
||||
expectJSONOpts(t, `{"type":"Point","coordinates":[1,2,3,4,5]}`, `{"type":"Point","coordinates":[1,2,3,4]}`, &ParseOptions{AllowSimplePoints: true})
|
||||
expectJSONOpts(t, `{"type":"Point","coordinates":[1]}`, errCoordinatesInvalid, &ParseOptions{AllowSimplePoints: true})
|
||||
expectJSONOpts(t, `{"type":"Point","coordinates":[1,2,3],"bbox":[1,2,3,4]}`, `{"type":"Point","coordinates":[1,2,3],"bbox":[1,2,3,4]}`, &ParseOptions{AllowSimplePoints: true})
|
||||
}
|
||||
|
||||
func TestSimplePointParseValid(t *testing.T) {
|
||||
json := `{"type":"Point","coordinates":[190,90]}`
|
||||
p := expectJSONOpts(t, json, nil, &ParseOptions{AllowSimplePoints: true})
|
||||
expect(t, !p.(*SimplePoint).Empty())
|
||||
p = expectJSONOpts(t, json, nil, &ParseOptions{AllowSimplePoints: false})
|
||||
expect(t, !p.(*Point).Empty())
|
||||
p = expectJSONOpts(t, json, errCoordinatesInvalid, &ParseOptions{RequireValid: true, AllowSimplePoints: true})
|
||||
expect(t, p == nil)
|
||||
}
|
||||
|
||||
func TestSimplePointVarious(t *testing.T) {
|
||||
var g Object = PO(10, 20)
|
||||
expect(t, string(g.AppendJSON(nil)) == `{"type":"Point","coordinates":[10,20]}`)
|
||||
expect(t, g.Rect() == R(10, 20, 10, 20))
|
||||
expect(t, g.Center() == P(10, 20))
|
||||
expect(t, !g.Empty())
|
||||
}
|
||||
|
||||
func TestSimplePointValid(t *testing.T) {
|
||||
var g Object = PO(0, 20)
|
||||
expect(t, g.Valid())
|
||||
|
||||
var g1 Object = PO(10, 20)
|
||||
expect(t, g1.Valid())
|
||||
}
|
||||
|
||||
func TestSimplePointInvalidLargeX(t *testing.T) {
|
||||
var g Object = PO(10, 91)
|
||||
expect(t, !g.Valid())
|
||||
}
|
||||
|
||||
func TestSimplePointInvalidLargeY(t *testing.T) {
|
||||
var g Object = PO(181, 20)
|
||||
expect(t, !g.Valid())
|
||||
}
|
||||
|
||||
func TestSimplePointValidLargeX(t *testing.T) {
|
||||
var g Object = PO(180, 20)
|
||||
expect(t, g.Valid())
|
||||
}
|
||||
|
||||
func TestSimplePointValidLargeY(t *testing.T) {
|
||||
var g Object = PO(180, 90)
|
||||
expect(t, g.Valid())
|
||||
}
|
Loading…
Reference in New Issue