-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathbuild_polygon.go
255 lines (206 loc) · 5.28 KB
/
build_polygon.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package osmgeojson
import (
"fmt"
"github.com/paulmach/orb"
"github.com/paulmach/orb/geojson"
"github.com/paulmach/osm"
"github.com/paulmach/osm/internal/mputil"
)
func (ctx *context) buildPolygon(relation *osm.Relation) *geojson.Feature {
tags := relation.Tags.Map()
var outer []mputil.Segment
var inner []mputil.Segment
tainted := false
outerCount := 0
var outerWay *osm.Way // used to get featureID if only one outer way
for _, m := range relation.Members {
if m.Type != osm.TypeWay {
continue
}
if m.Role != "inner" && m.Role != "outer" {
continue
}
if m.Role == "outer" {
outerCount++
}
way := ctx.wayMap[osm.WayID(m.Ref)]
if way == nil {
if len(m.Nodes) != 0 {
way = &osm.Way{
ID: osm.WayID(m.Ref),
Nodes: m.Nodes,
}
} else {
tainted = true
continue
}
}
if m.Role == "outer" {
if !hasInterestingTags(way.Tags, tags) {
ctx.skippable[way.ID] = struct{}{}
}
} else {
if !hasInterestingTags(way.Tags, nil) {
ctx.skippable[way.ID] = struct{}{}
}
}
ls, t := ctx.wayToLineString(way)
if t {
tainted = true
}
if len(ls) == 0 {
// we have the way but none of the node members
continue
}
segment := mputil.Segment{
Orientation: m.Orientation,
Line: ls,
}
if m.Role == "outer" {
outerWay = way
if segment.Orientation == orb.CW {
segment.Reverse()
}
outer = append(outer, segment)
} else {
if segment.Orientation == orb.CCW {
segment.Reverse()
}
inner = append(inner, segment)
}
}
var geometry orb.Geometry
// If there is only one outer way, and the relation doesn't have any interesting tags
// use the way to define this polygon. ie. use the way's type, id and tags.
tagObject := osm.Element(relation)
if len(outer) == 0 && !ctx.includeInvalidPolygons {
// no outer polygon, skip this relation
return nil
} else if len(outer) == 1 && outerCount == 1 {
// This section handles "old style" multipolygons that don't/shouldn't
// exist anymore. In the past tags were set on the outer ring way and
// the relation was used to add holes to the way.
outerRing := mputil.MultiSegment(outer).Ring(orb.CCW)
if len(outerRing) < 4 || !outerRing.Closed() {
// not a valid outer ring
return nil
}
innerSections := mputil.Join(inner)
polygon := make(orb.Polygon, 0, len(inner)+1)
polygon = append(polygon, outerRing)
for _, is := range innerSections {
polygon = append(polygon, is.Ring(orb.CW))
}
geometry = polygon
if !hasInterestingTags(relation.Tags, map[string]string{"type": "true"}) {
ctx.skippable[outerWay.ID] = struct{}{}
tags = outerWay.Tags.Map()
tagObject = outerWay
}
} else {
// more than one outer, need to map inner polygons to
// the outer that contains them.
outerSections := mputil.Join(outer)
mp := make(orb.MultiPolygon, 0, len(outer))
for _, os := range outerSections {
ring := os.Ring(orb.CCW)
if !ctx.includeInvalidPolygons && (len(ring) < 4 || !ring.Closed()) {
// needs at least 4 points and matching endpoints
continue
}
mp = append(mp, orb.Polygon{ring})
}
if len(mp) == 0 && !ctx.includeInvalidPolygons {
// no valid outer ways.
return nil
}
innerSections := mputil.Join(inner)
for _, is := range innerSections {
ring := is.Ring(orb.CW)
mp = addToMultiPolygon(mp, ring, ctx.includeInvalidPolygons)
}
if len(mp) == 0 {
return nil
}
geometry = mp
if len(mp) == 1 {
geometry = mp[0]
}
}
featureID := tagObject.FeatureID()
f := geojson.NewFeature(geometry)
if !ctx.noID {
f.ID = fmt.Sprintf("%s/%d", featureID.Type(), featureID.Ref())
}
f.Properties["id"] = int(featureID.Ref())
f.Properties["type"] = string(featureID.Type())
if tainted {
f.Properties["tainted"] = true
}
f.Properties["tags"] = tags
ctx.addMetaProperties(f.Properties, tagObject)
return f
}
func addToMultiPolygon(mp orb.MultiPolygon, ring orb.Ring, includeInvalidPolygons bool) orb.MultiPolygon {
for i := range mp {
if polygonContains(mp[i][0], ring) {
mp[i] = append(mp[i], ring)
return mp
}
}
if !includeInvalidPolygons {
// inner without its outer
return mp
}
if len(mp) > 0 {
// if the outer ring of the first polygon is not closed,
// we don't really know if this inner should be part of it.
// But... we assume yes.
fr := mp[0][0]
if len(fr) != 0 && fr[0] != fr[len(fr)-1] {
mp[0] = append(mp[0], ring)
return mp
}
// trying to find an existing "without outer" polygon to add this to.
for i := range mp {
if len(mp[i][0]) == 0 {
mp[i] = append(mp[i], ring)
return mp
}
}
}
// no polygons with empty outer, so create one.
// create another polygon with empty outer.
return append(mp, orb.Polygon{nil, ring})
}
func polygonContains(outer orb.Ring, r orb.Ring) bool {
for _, p := range r {
inside := false
x, y := p[0], p[1]
i, j := 0, len(outer)-1
for i < len(outer) {
xi, yi := outer[i][0], outer[i][1]
xj, yj := outer[j][0], outer[j][1]
if ((yi > y) != (yj > y)) &&
(x < (xj-xi)*(y-yi)/(yj-yi)+xi) {
inside = !inside
}
j = i
i++
}
if inside {
return true
}
}
return false
}
func reorient(p orb.Polygon) {
if p[0].Orientation() != orb.CCW {
p[0].Reverse()
}
for i := 1; i < len(p); i++ {
if p[i].Orientation() != orb.CW {
p[i].Reverse()
}
}
}