-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolygon.go
More file actions
200 lines (139 loc) · 3.36 KB
/
polygon.go
File metadata and controls
200 lines (139 loc) · 3.36 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package geov
import (
"errors"
"fmt"
geo "github.com/kellydunn/golang-geo"
geojson "github.com/paulmach/go.geojson"
)
const (
PolygonIDentifierKey = "ISO3166-2"
)
var (
ErrMalformedPolygon = errors.New("polygon is not formatted correctly")
ErrMalformedPolygonPoint = errors.New("polygon point is not formatted correctly")
ErrPolygonIdKeyNotFound = errors.New(fmt.Sprintf("polygon cannot be attached to %s identifier", PolygonIDentifierKey))
ErrPolygonKeyNotFound = errors.New("there is no registered city for provided polygon identifier")
)
func NewMultiPolygon(ps []*geo.Polygon) MultiPolygon {
mp := make(MultiPolygon)
for index, p := range ps {
mp[index] = p
}
return mp
}
type MultiPolygon map[int]*geo.Polygon
func (mp MultiPolygon) BBox() *boundingBox {
var bb boundingBox
for _, polygon := range mp {
for _, p := range polygon.Points() {
bb.Expand(p)
}
}
return &bb
}
type ArcMap map[string]Arc
type Arc struct {
Points []geo.Point
}
func (a *Arc) Reverse() {
n := make([]geo.Point, len(a.Points))
for index, p := range a.Points {
n[len(a.Points)-1-index] = p
}
a.Points = n
}
func (a *Arc) Identifier() string {
l := len(a.Points)
if l == 0 {
return "nil"
}
first := a.Points[0]
last := a.Points[l-1]
middleIndex := l / 2
// swap to a deterministic order
if last.Lat() < first.Lat() {
first, last = last, first
middleIndex = l - 1 - middleIndex
}
if last.Lat() == first.Lat() {
if last.Lng() < last.Lng() {
first, last = last, first
middleIndex = l - 1 - middleIndex
}
}
return fmt.Sprintf("%v-%v-%v", first, a.Points[middleIndex], last)
}
func (a *Arc) AddPoint(p geo.Point) {
a.Points = append(a.Points, p)
}
func serialize(p *geo.Point) string {
return fmt.Sprintf("%0.8f-%0.8f", p.Lat(), p.Lng())
}
type Hashmap map[string]map[string]bool
func (mp MultiPolygon) Map() Hashmap {
// (point, neighbors)
hashmap := make(Hashmap)
for _, polygon := range mp {
points := polygon.Points()
l := len(points)
// polygon of less than 3 points in 2d plane is ignored
if l < 3 {
continue
}
for index, point := range points {
var prev *geo.Point
var next *geo.Point
switch index {
case 0:
prev = points[l-1]
next = points[1]
case l - 1:
prev = points[index-1]
next = points[0]
default:
prev = points[index-1]
next = points[index+1]
}
p := serialize(point)
if _, ok := hashmap[p]; ok {
hashmap[p][serialize(prev)] = true
hashmap[p][serialize(next)] = true
continue
}
hashmap[p] = map[string]bool{serialize(prev): true, serialize(next): true}
}
}
return hashmap
}
func Unmarshal(data []byte) (MultiPolygon, error) {
pm := make(MultiPolygon)
featureCollection, err := geojson.UnmarshalFeatureCollection(data)
if err != nil {
return nil, err
}
for index, feature := range featureCollection.Features {
if g := feature.Geometry; g.IsPolygon() {
polygon := geo.NewPolygon(nil)
for _, points := range g.Polygon {
if len(points) == 0 {
continue
}
for index, point := range points {
if len(point) != 2 {
continue
}
if index == len(points)-1 {
continue
}
// geojson points are lon/lat
polygon.Add(geo.NewPoint(point[1], point[0]))
}
}
if !polygon.IsClosed() {
return nil, ErrMalformedPolygon
}
pm[index] = polygon
}
}
return pm, nil
}