-
Notifications
You must be signed in to change notification settings - Fork 156
/
ops.go
103 lines (93 loc) · 2.31 KB
/
ops.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
101
102
103
package geos
/*
#cgo LDFLAGS: -lgeos_c
#include "geos_c.h"
#include <stdlib.h>
*/
import "C"
func (this *Geos) Contains(a, b *Geom) bool {
result := C.GEOSContains_r(this.v, a.v, b.v)
if result == 1 {
return true
}
// result == 2 -> exception (already logged to console)
return false
}
func (this *Geos) Intersects(a, b *Geom) bool {
result := C.GEOSIntersects_r(this.v, a.v, b.v)
if result == 1 {
return true
}
// result == 2 -> exception (already logged to console)
return false
}
func (this *Geos) Intersection(a, b *Geom) *Geom {
result := C.GEOSIntersection_r(this.v, a.v, b.v)
if result == nil {
return nil
}
geom := &Geom{result}
return geom
}
func (this *Geos) Buffer(geom *Geom, size float64) *Geom {
buffered := C.GEOSBuffer_r(this.v, geom.v, C.double(size), 50)
if buffered == nil {
return nil
}
return &Geom{buffered}
}
func (this *Geos) SimplifyPreserveTopology(geom *Geom, tolerance float64) *Geom {
simplified := C.GEOSTopologyPreserveSimplify_r(this.v, geom.v, C.double(tolerance))
if simplified == nil {
return nil
}
return &Geom{simplified}
}
// UnionPolygons tries to merge polygons.
// Returns a single (Multi)Polygon.
// Destroys polygons and returns new allocated (Multi)Polygon as necessary.
func (this *Geos) UnionPolygons(polygons []*Geom) *Geom {
if len(polygons) == 0 {
return nil
}
if len(polygons) == 1 {
return polygons[0]
}
multiPolygon := this.MultiPolygon(polygons)
if multiPolygon == nil {
return nil
}
defer this.Destroy(multiPolygon)
result := C.GEOSUnaryUnion_r(this.v, multiPolygon.v)
if result == nil {
return nil
}
return &Geom{result}
}
// LineMerge tries to merge lines. Returns slice of LineStrings.
// Destroys lines and returns new allocated LineString Geoms.
func (this *Geos) LineMerge(lines []*Geom) []*Geom {
if len(lines) <= 1 {
return lines
}
multiLineString := this.MultiLineString(lines)
if multiLineString == nil {
return nil
}
defer this.Destroy(multiLineString)
merged := C.GEOSLineMerge_r(this.v, multiLineString.v)
if merged == nil {
return nil
}
geom := &Geom{merged}
if this.Type(geom) == "LineString" {
return []*Geom{geom}
}
// extract MultiLineString
lines = make([]*Geom, 0, this.NumGeoms(geom))
for _, line := range this.Geoms(geom) {
lines = append(lines, this.Clone(line))
}
this.Destroy(geom)
return lines
}