This repository has been archived by the owner on Sep 19, 2023. It is now read-only.
/
prepared.go
100 lines (83 loc) · 3 KB
/
prepared.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package geos
/*
#include "geos.h"
*/
import "C"
import (
"errors"
"runtime"
)
// PGeometry represents a "prepared geometry", a type of geometry object that is
// optimized for a limited set of operations.
type PGeometry struct {
p *C.GEOSPreparedGeometry
}
// PrepareGeometry constructs a prepared geometry from a normal geometry object.
func PrepareGeometry(g *Geometry) *PGeometry {
ptr := cGEOSPrepare(g.g)
p := &PGeometry{ptr}
runtime.SetFinalizer(p, (*PGeometry).destroy)
return p
}
func (p *PGeometry) destroy() {
cGEOSPreparedGeom_destroy(p.p)
p.p = nil
}
// Prepared geometry binary predicates
// Contains computes whether the prepared geometry contains the other prepared
// geometry.
func (p *PGeometry) Contains(other *Geometry) (bool, error) {
return p.predicate("contains", cGEOSPreparedContains, other)
}
// ContainsP computes whether the prepared geometry properly contains the other
// prepared geometry.
func (p *PGeometry) ContainsP(other *Geometry) (bool, error) {
return p.predicate("contains", cGEOSPreparedContainsProperly, other)
}
// CoveredBy computes whether the prepared geometry is covered by the other
// prepared geometry.
func (p *PGeometry) CoveredBy(other *Geometry) (bool, error) {
return p.predicate("covered by", cGEOSPreparedCoveredBy, other)
}
// Covers computes whether the prepared geometry covers the other prepared
// geometry.
func (p *PGeometry) Covers(other *Geometry) (bool, error) {
return p.predicate("covers", cGEOSPreparedCovers, other)
}
// Crosses computes whether the prepared geometry crosses the other prepared
// geometry.
func (p *PGeometry) Crosses(other *Geometry) (bool, error) {
return p.predicate("crosses", cGEOSPreparedCrosses, other)
}
// Disjoint computes whether the prepared geometry is disjoint from the other
// prepared geometry.
func (p *PGeometry) Disjoint(other *Geometry) (bool, error) {
return p.predicate("disjoint", cGEOSPreparedDisjoint, other)
}
// Intersects computes whether the prepared geometry intersects the other
// prepared geometry.
func (p *PGeometry) Intersects(other *Geometry) (bool, error) {
return p.predicate("intersects", cGEOSPreparedIntersects, other)
}
// Overlaps computes whether the prepared geometry overlaps the other
// prepared geometry.
func (p *PGeometry) Overlaps(other *Geometry) (bool, error) {
return p.predicate("overlaps", cGEOSPreparedOverlaps, other)
}
// Touches computes whether the prepared geometry touches the other
// prepared geometry.
func (p *PGeometry) Touches(other *Geometry) (bool, error) {
return p.predicate("touches", cGEOSPreparedTouches, other)
}
// Within computes whether the prepared geometry is within the other
// prepared geometry.
func (p *PGeometry) Within(other *Geometry) (bool, error) {
return p.predicate("within", cGEOSPreparedWithin, other)
}
func (p *PGeometry) predicate(name string, fn func(*C.GEOSPreparedGeometry, *C.GEOSGeometry) C.char, other *Geometry) (bool, error) {
i := fn(p.p, other.g)
if i == 2 {
return false, errors.New("geos: prepared " + name)
}
return i == 1, nil
}