This repository has been archived by the owner on Aug 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
geojson.go
250 lines (221 loc) · 5.8 KB
/
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package decoding
import (
"errors"
"fmt"
"io"
geojson "github.com/paulmach/go.geojson"
"github.com/reearth/reearth-backend/pkg/layer"
"github.com/reearth/reearth-backend/pkg/property"
)
type GeoStyle struct {
StrokeColor string `json:"stroke"`
StrokeWidth float64 `json:"stroke-width"`
FillColor string `json:"fill"`
}
type GeoJSONDecoder struct {
reader io.Reader
features []*geojson.Feature
sceneId layer.SceneID
groupName string
}
func NewGeoJSONDecoder(r io.Reader, s layer.SceneID) *GeoJSONDecoder {
return &GeoJSONDecoder{
reader: r,
sceneId: s,
groupName: "",
}
}
func validateFeatures(fc []*geojson.Feature) []*geojson.Feature {
var res []*geojson.Feature
for _, f := range fc {
if f.Geometry == nil {
continue
}
if f.Geometry.Type == geojson.GeometryMultiPolygon {
for _, p := range f.Geometry.MultiPolygon {
nf := geojson.NewPolygonFeature(p)
for k, v := range f.Properties {
nf.SetProperty(k, v)
}
res = append(res, nf)
}
} else {
res = append(res, f)
}
}
return res
}
func (d *GeoJSONDecoder) Decode() (Result, error) {
lg, err := layer.NewGroup().NewID().Scene(d.sceneId).Name("GeoJSON").Build()
if err != nil {
return Result{}, err
}
con, err := io.ReadAll(d.reader)
if err != nil {
return Result{}, errors.New("unable to parse file content")
}
fc, err := geojson.UnmarshalFeatureCollection(con)
if err != nil {
return Result{}, errors.New("unable to parse file content")
}
fl := validateFeatures(fc.Features)
// if feature collection > append it to features list, else try to decode a single feature (layer)
if len(fc.Features) > 0 {
d.features = fl
} else {
f, err := geojson.UnmarshalFeature(con)
if err != nil {
return Result{}, errors.New("unable to parse file content")
}
d.features = append(d.features, f)
}
var layers layer.Map
var properties property.Map
for range d.features {
li, p, err := d.decodeLayer()
if errors.Is(err, io.EOF) {
return resultFrom(lg, layers, properties)
}
if err != nil {
return Result{}, err
}
if li != nil {
var l layer.Layer = li
lg.Layers().AddLayer(l.ID(), -1)
layers = layers.Add(&l)
}
if p != nil {
properties = properties.Add(p)
}
}
return resultFrom(lg, layers, properties)
}
func (d *GeoJSONDecoder) decodeLayer() (*layer.Item, *property.Property, error) {
var feat *geojson.Feature
var p *property.Property
var l *layer.Item
var ex layer.PluginExtensionID
var err error
var stroke, fillColor string
var strokeWidth float64
var ok bool
var layerName string
if len(d.features) > 0 {
feat, d.features = d.features[0], d.features[1:]
} else {
return nil, nil, io.EOF
}
switch feat.Geometry.Type {
case "Point":
var latlng property.LatLng
var height float64
if len(feat.Geometry.Point) > 2 {
height = feat.Geometry.Point[2]
}
latlng = property.LatLng{
Lat: feat.Geometry.Point[1],
Lng: feat.Geometry.Point[0],
}
p, err = createProperty("Point", property.LatLngHeight{
Lat: latlng.Lat,
Lng: latlng.Lng,
Height: height,
}, d.sceneId, feat.Properties["marker-color"], "geojson")
if err != nil {
return nil, nil, err
}
ex = extensions["Point"]
layerName = "Point"
case "LineString":
var coords []property.LatLngHeight
for _, c := range feat.Geometry.LineString {
var height float64
if len(c) == 2 {
height = 0
} else if len(c) == 3 {
height = c[3]
} else {
return nil, nil, errors.New("unable to parse coordinates")
}
coords = append(coords, property.LatLngHeight{Lat: c[1], Lng: c[0], Height: height})
}
if feat.Properties["stroke"] != nil {
stroke, ok = feat.Properties["stroke"].(string)
if !ok {
return nil, nil, errors.New("unable to parse")
}
}
if feat.Properties["stroke-width"] != nil {
strokeWidth, ok = feat.Properties["stroke-width"].(float64)
if !ok {
return nil, nil, errors.New("unable to parse")
}
}
ex = extensions["Polyline"]
p, err = createProperty("Polyline", coords, d.sceneId, GeoStyle{StrokeColor: stroke, StrokeWidth: strokeWidth}, "geojson")
if err != nil {
return nil, nil, err
}
layerName = "Polyline"
case "Polygon":
var poly [][]property.LatLngHeight
for _, r := range feat.Geometry.Polygon {
var coords []property.LatLngHeight
for _, c := range r {
var height float64
if len(c) == 2 {
height = 0
} else if len(c) == 3 {
height = c[3]
} else {
return nil, nil, errors.New("unable to parse coordinates")
}
coords = append(coords, property.LatLngHeight{Lat: c[1], Lng: c[0], Height: height})
}
poly = append(poly, coords)
}
ex = extensions["Polygon"]
if feat.Properties["stroke"] != nil {
stroke, ok = feat.Properties["stroke"].(string)
if !ok {
return nil, nil, errors.New("unable to parse")
}
}
if feat.Properties["stroke-width"] != nil {
strokeWidth, ok = feat.Properties["stroke-width"].(float64)
if !ok {
return nil, nil, errors.New("unable to parse")
}
}
if feat.Properties["stroke-width"] != nil {
fillColor, ok = feat.Properties["fill"].(string)
if !ok {
return nil, nil, errors.New("unable to parse")
}
}
p, err = createProperty("Polygon", poly, d.sceneId, GeoStyle{StrokeColor: stroke, StrokeWidth: strokeWidth, FillColor: fillColor}, "geojson")
if err != nil {
return nil, nil, err
}
layerName = "Polygon"
default:
return nil, nil, fmt.Errorf("unsupported type %s", feat.Geometry.Type)
}
if feat.Properties["name"] != nil {
// name is not required, so no need to return error if name is not decoded
layerName, _ = feat.Properties["name"].(string)
}
l, err = layer.
NewItem().
NewID().
Name(layerName).
Scene(d.sceneId).
Property(p.IDRef()).
Extension(&ex).
Plugin(&layer.OfficialPluginID).
Build()
if err != nil {
return nil, nil, err
}
return l, p, nil
}