-
Notifications
You must be signed in to change notification settings - Fork 1
/
element_geojson.go
226 lines (205 loc) · 6.21 KB
/
element_geojson.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
package osmpbfparser
import (
"github.com/paulmach/go.geojson"
"reflect"
"strconv"
)
// ToGeoJSON convery element to JSON bytes.
func (e *Element) ToGeoJSON() ([]byte, error) {
switch e.Type {
}
fc := geojson.NewFeatureCollection()
f := e.ToGeoJSONFeature()
fc.AddFeature(f)
rawJSON, err := fc.MarshalJSON()
return rawJSON, err
}
// ToGeoJSONFeature ...
func (e *Element) ToGeoJSONFeature() *geojson.Feature {
var f *geojson.Feature
switch e.Type {
case 0:
f = e.NodeToFeature()
case 1:
f = e.WayToFeature()
case 2:
f = e.RelationToFeature()
}
return f
}
// WayToFeature convert way element to geojson feature.
func (e *Element) WayToFeature() *geojson.Feature {
latLngs := [][]float64{}
for _, member := range e.Elements {
latLngs = append(latLngs, []float64{member.Node.Lon, member.Node.Lat})
}
var f *geojson.Feature
switch e.IsArea() {
case true:
f = geojson.NewPolygonFeature([][][]float64{latLngs})
default:
f = geojson.NewLineStringFeature(latLngs)
}
wayID := "way" + "/" + strconv.FormatInt(e.Way.ID, 10)
f.ID = wayID
f.SetProperty("osmid", wayID)
f.SetProperty("osmType", "way")
for k, v := range e.Way.Tags {
f.SetProperty(k, v)
}
return f
}
// NodeToFeature convert node element to geojson feature.
func (e *Element) NodeToFeature() *geojson.Feature {
f := geojson.NewPointFeature([]float64{e.Node.Lon, e.Node.Lat})
nodeID := "node/" + strconv.FormatInt(e.Node.ID, 10)
f.ID = nodeID
f.SetProperty("osmid", nodeID)
f.SetProperty("osmType", "node")
for k, v := range e.Node.Tags {
f.SetProperty(k, v)
}
return f
}
// IsMultiPolygon check if element is multi-polygon or not.
func (e *Element) IsMultiPolygon() bool {
if e.Type == 2 {
if v, ok := e.Relation.Tags["type"]; ok {
if v == "multipolygon" {
return true
}
}
}
return false
}
// RelationToFeature convert relation element to geojson feature.
func (e *Element) RelationToFeature() *geojson.Feature {
var f *geojson.Feature
if e.IsMultiPolygon() {
f = e.GetRelationAsMultipolygon()
} else {
f = e.GetRelationAsGeometries()
}
id := "relation/" + strconv.FormatInt(e.Relation.ID, 10)
f.ID = id
f.SetProperty("osmid", id)
f.SetProperty("osmType", "relation")
for k, v := range e.Relation.Tags {
f.SetProperty(k, v)
}
return f
}
// GetRelationAsMultipolygon return relation as geosjon multipolygon.
func (e *Element) GetRelationAsMultipolygon() *geojson.Feature {
multiPolygon := [][][][]float64{}
points := [][]float64{}
emtPoints := [][]float64{}
polygon := [][][]float64{}
innerPolygon := [][][]float64{}
for _, emt := range e.Elements {
emtFeature := emt.ToGeoJSONFeature()
switch emtFeature.Geometry.Type {
case geojson.GeometryPoint:
emtPoints = append(emtPoints, emtFeature.Geometry.Point)
case geojson.GeometryMultiPoint:
emtPoints = append(emtPoints, emtFeature.Geometry.MultiPoint...)
case geojson.GeometryLineString:
emtPoints = append(emtPoints, emtFeature.Geometry.LineString...)
case geojson.GeometryMultiLineString:
for _, lineString := range emtFeature.Geometry.MultiLineString {
emtPoints = append(emtPoints, lineString...)
}
case geojson.GeometryPolygon:
multiPolygon = append(multiPolygon, emtFeature.Geometry.Polygon)
case geojson.GeometryMultiPolygon:
multiPolygon = append(multiPolygon, emtFeature.Geometry.MultiPolygon...)
}
points = multiPolygonPointsAppendPoints(points, emtPoints)
multiPolygon, polygon, points, innerPolygon = multiAreaMultiPolygonAppend(multiPolygon, polygon, points, emt.Role, innerPolygon)
}
// Final flush
if len(polygon) > 0 {
if len(innerPolygon) > 0 {
polygon = append(polygon, innerPolygon...)
}
multiPolygon = append(multiPolygon, polygon)
}
return geojson.NewMultiPolygonFeature(multiPolygon...)
}
// Area multi-polygon append role.
// Check is area and append by different role(inner, outer) cases.
func multiAreaMultiPolygonAppend(multiPolygon [][][][]float64, polygon [][][]float64, points [][]float64, role int, innerPolygon [][][]float64) ([][][][]float64, [][][]float64, [][]float64, [][][]float64) {
// Is area?
// Is points length > 1 && first point equal to last point.
if len(points) > 1 && reflect.DeepEqual(points[0], points[len(points)-1]) {
switch role {
case 0: // outer
if len(polygon) > 0 {
multiPolygon = append(multiPolygon, polygon)
polygon = [][][]float64{}
}
polygon = append(polygon, points)
if len(innerPolygon) > 0 {
polygon = append(polygon, innerPolygon...)
innerPolygon = [][][]float64{}
}
case 1: // inner
if len(polygon) == 0 {
innerPolygon = append(innerPolygon, points)
} else {
polygon = append(polygon, points)
}
}
points = [][]float64{}
}
return multiPolygon, polygon, points, innerPolygon
}
// Checkint the graft point, different case will have different way to append.
// Case (AEnd, BStart), (AEnd, BEnd), (AStart, BEnd), (AStart, BStart)
func multiPolygonPointsAppendPoints(pointsA [][]float64, pointsB [][]float64) [][]float64 {
if len(pointsB) == 0 {
return pointsA
}
if len(pointsA) == 0 {
return pointsB
}
var bStart, bEnd []float64
if len(pointsB) > 0 {
bStart = pointsB[0]
bEnd = pointsB[len(pointsB)-1]
}
aStart := pointsA[0]
aEnd := pointsA[len(pointsA)-1]
if reflect.DeepEqual(aEnd, bStart) {
pointsA = append(pointsA, pointsB...)
} else if reflect.DeepEqual(aStart, bStart) {
newPoints := [][]float64{}
for i := len(pointsB) - 1; i >= 0; i-- {
newPoints = append(newPoints, pointsB[i])
}
pointsA = append(newPoints, pointsA...)
} else if reflect.DeepEqual(aEnd, bEnd) {
for i := len(pointsB) - 1; i >= 0; i-- {
pointsA = append(pointsA, pointsB[i])
}
} else {
pointsA = append(pointsB, pointsA...)
}
return pointsA
}
// GetRelationAsGeometries return relation as geojson geometries.
// Sometimes relation will missing type tag, so defualt we use collect feature.
func (e *Element) GetRelationAsGeometries() *geojson.Feature {
geometries := []*geojson.Geometry{}
for _, emt := range e.Elements {
switch emt.Type {
case 0:
geometries = append(geometries, emt.NodeToFeature().Geometry)
case 1:
geometries = append(geometries, emt.WayToFeature().Geometry)
case 2:
geometries = append(geometries, emt.RelationToFeature().Geometry)
}
}
return geojson.NewCollectionFeature(geometries...)
}