tile38/internal/glob/glob.go
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

92 lines
1.6 KiB
Go

package glob
import "strings"
type Glob struct {
Pattern string
Desc bool
Limits []string
IsGlob bool
}
func Match(pattern, name string) (matched bool, err error) {
return wildcardMatch(pattern, name)
}
func IsGlob(pattern string) bool {
for i := 0; i < len(pattern); i++ {
switch pattern[i] {
case '[', '*', '?':
_, err := Match(pattern, "whatever")
return err == nil
}
}
return false
}
func Parse(pattern string, desc bool) *Glob {
g := &Glob{Pattern: pattern, Desc: desc, Limits: []string{"", ""}}
if strings.HasPrefix(pattern, "*") {
g.IsGlob = true
return g
}
if pattern == "" {
g.IsGlob = false
return g
}
n := 0
isGlob := false
outer:
for i := 0; i < len(pattern); i++ {
switch pattern[i] {
case '[', '*', '?':
_, err := Match(pattern, "whatever")
if err == nil {
isGlob = true
}
break outer
}
n++
}
if n == 0 {
g.Limits = []string{pattern, pattern}
g.IsGlob = false
return g
}
var a, b string
if desc {
a = pattern[:n]
b = a
if b[n-1] == 0x00 {
for len(b) > 0 && b[len(b)-1] == 0x00 {
if len(b) > 1 {
if b[len(b)-2] == 0x00 {
b = b[:len(b)-1]
} else {
b = string(append([]byte(b[:len(b)-2]), b[len(b)-2]-1, 0xFF))
}
} else {
b = ""
}
}
} else {
b = string(append([]byte(b[:n-1]), b[n-1]-1))
}
if a[n-1] == 0xFF {
a = string(append([]byte(a), 0x00))
} else {
a = string(append([]byte(a[:n-1]), a[n-1]+1))
}
} else {
a = pattern[:n]
if a[n-1] == 0xFF {
b = string(append([]byte(a), 0x00))
} else {
b = string(append([]byte(a[:n-1]), a[n-1]+1))
}
}
g.Limits = []string{a, b}
g.IsGlob = isGlob
return g
}