
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.
196 lines
4.3 KiB
Go
196 lines
4.3 KiB
Go
// Copyright 2018 Joshua J Baker. All rights reserved.
|
|
// Use of this source code is governed by an MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package geometry
|
|
|
|
// Poly ...
|
|
type Poly struct {
|
|
Exterior Ring
|
|
Holes []Ring
|
|
}
|
|
|
|
// NewPoly ...
|
|
func NewPoly(exterior []Point, holes [][]Point, index int) *Poly {
|
|
poly := new(Poly)
|
|
poly.Exterior = newRing(exterior, index)
|
|
if len(holes) > 0 {
|
|
poly.Holes = make([]Ring, len(holes))
|
|
for i := range holes {
|
|
poly.Holes[i] = newRing(holes[i], index)
|
|
}
|
|
}
|
|
return poly
|
|
}
|
|
|
|
// Clockwise ...
|
|
func (poly *Poly) Clockwise() bool {
|
|
if poly == nil || poly.Exterior == nil {
|
|
return false
|
|
}
|
|
return poly.Exterior.Clockwise()
|
|
}
|
|
|
|
// Empty ...
|
|
func (poly *Poly) Empty() bool {
|
|
if poly == nil || poly.Exterior == nil {
|
|
return true
|
|
}
|
|
return poly.Exterior.Empty()
|
|
}
|
|
|
|
// Rect ...
|
|
func (poly *Poly) Rect() Rect {
|
|
if poly == nil || poly.Exterior == nil {
|
|
return Rect{}
|
|
}
|
|
return poly.Exterior.Rect()
|
|
}
|
|
|
|
// Move the polygon by delta. Returns a new polygon
|
|
func (poly *Poly) Move(deltaX, deltaY float64) *Poly {
|
|
if poly == nil {
|
|
return nil
|
|
}
|
|
if poly.Exterior == nil {
|
|
return new(Poly)
|
|
}
|
|
npoly := new(Poly)
|
|
if series, ok := poly.Exterior.(*baseSeries); ok {
|
|
npoly.Exterior = Ring(series.Move(deltaX, deltaY))
|
|
} else {
|
|
nseries := makeSeries(
|
|
seriesCopyPoints(poly.Exterior), false, true, DefaultIndex)
|
|
npoly.Exterior = Ring(nseries.Move(deltaX, deltaY))
|
|
}
|
|
if len(poly.Holes) > 0 {
|
|
npoly.Holes = make([]Ring, len(poly.Holes))
|
|
for i, hole := range poly.Holes {
|
|
if series, ok := hole.(*baseSeries); ok {
|
|
npoly.Holes[i] = Ring(series.Move(deltaX, deltaY))
|
|
} else {
|
|
nseries := makeSeries(
|
|
seriesCopyPoints(hole), false, true, DefaultIndex)
|
|
npoly.Holes[i] = Ring(nseries.Move(deltaX, deltaY))
|
|
}
|
|
}
|
|
}
|
|
return npoly
|
|
}
|
|
|
|
// ContainsPoint ...
|
|
func (poly *Poly) ContainsPoint(point Point) bool {
|
|
if poly == nil || poly.Exterior == nil {
|
|
return false
|
|
}
|
|
if !ringContainsPoint(poly.Exterior, point, true).hit {
|
|
return false
|
|
}
|
|
contains := true
|
|
for _, hole := range poly.Holes {
|
|
if ringContainsPoint(hole, point, false).hit {
|
|
contains = false
|
|
break
|
|
}
|
|
}
|
|
return contains
|
|
}
|
|
|
|
// IntersectsPoint ...
|
|
func (poly *Poly) IntersectsPoint(point Point) bool {
|
|
if poly == nil {
|
|
return false
|
|
}
|
|
return poly.ContainsPoint(point)
|
|
}
|
|
|
|
// ContainsRect ...
|
|
func (poly *Poly) ContainsRect(rect Rect) bool {
|
|
if poly == nil {
|
|
return false
|
|
}
|
|
// convert rect into a polygon
|
|
return poly.ContainsPoly(&Poly{Exterior: rect})
|
|
}
|
|
|
|
// IntersectsRect ...
|
|
func (poly *Poly) IntersectsRect(rect Rect) bool {
|
|
if poly == nil {
|
|
return false
|
|
}
|
|
// convert rect into a polygon
|
|
return poly.IntersectsPoly(&Poly{Exterior: rect})
|
|
}
|
|
|
|
// ContainsLine ...
|
|
func (poly *Poly) ContainsLine(line *Line) bool {
|
|
if poly == nil || poly.Exterior == nil || line == nil {
|
|
return false
|
|
}
|
|
if !ringContainsLine(poly.Exterior, line, true) {
|
|
return false
|
|
}
|
|
for _, polyHole := range poly.Holes {
|
|
if ringIntersectsLine(polyHole, line, false) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// IntersectsLine ...
|
|
func (poly *Poly) IntersectsLine(line *Line) bool {
|
|
if poly == nil || poly.Exterior == nil || line == nil {
|
|
return false
|
|
}
|
|
return ringIntersectsLine(poly.Exterior, line, true)
|
|
}
|
|
|
|
// ContainsPoly ...
|
|
func (poly *Poly) ContainsPoly(other *Poly) bool {
|
|
if poly == nil || poly.Exterior == nil ||
|
|
other == nil || other.Exterior == nil {
|
|
return false
|
|
}
|
|
// 1) other exterior must be fully contained inside of the poly exterior.
|
|
if !ringContainsRing(poly.Exterior, other.Exterior, true) {
|
|
return false
|
|
}
|
|
// 2) ring cannot intersect poly holes
|
|
contains := true
|
|
for _, polyHole := range poly.Holes {
|
|
if ringIntersectsRing(polyHole, other.Exterior, false) {
|
|
contains = false
|
|
// 3) unless the poly hole is contain inside of a other hole
|
|
for _, otherHole := range other.Holes {
|
|
if ringContainsRing(otherHole, polyHole, true) {
|
|
contains = true
|
|
// println(4)
|
|
break
|
|
}
|
|
}
|
|
if !contains {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
return contains
|
|
}
|
|
|
|
// IntersectsPoly ...
|
|
func (poly *Poly) IntersectsPoly(other *Poly) bool {
|
|
if poly == nil || poly.Exterior == nil ||
|
|
other == nil || other.Exterior == nil {
|
|
return false
|
|
}
|
|
if !ringIntersectsRing(other.Exterior, poly.Exterior, true) {
|
|
return false
|
|
}
|
|
for _, hole := range poly.Holes {
|
|
if ringContainsRing(hole, other.Exterior, false) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|