-
Notifications
You must be signed in to change notification settings - Fork 3
/
mapitem_geojson.go
72 lines (59 loc) · 1.54 KB
/
mapitem_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
package mapplz
import (
"encoding/json"
"strings"
)
type geojsonPoint struct {
Coordinates []float64
}
type geojsonLine struct {
Coordinates [][]float64
}
type geojsonPolygon struct {
Coordinates [][][]float64
}
type geojsonGeometry struct {
Type string
Coordinates json.RawMessage
Point geojsonPoint
Line geojsonLine
Polygon geojsonPolygon
}
type GeojsonFeature struct {
Type string
Geometry geojsonGeometry
Properties map[string]interface{}
}
type GeojsonFeatureCollection struct {
Type string
Features []json.RawMessage
}
func (mp *MapPLZ) ToGeoJson() string {
if mp.Database != nil {
// fetch all items from the DB
mp.MapItems = mp.Query("")
}
var features = []string{}
for i := 0; i < len(mp.MapItems); i++ {
if !mp.MapItems[i].Deleted() {
features = append(features, mp.MapItems[i].ToGeoJson())
}
}
return `{"type":"FeatureCollection","features":[` + strings.Join(features, ",") + `]}`
}
func (mp *MapPLZ) Add_Geojson_Feature(geojson string) MapItem {
mip := ConvertGeojsonFeature(geojson, mp.Database)
mp.MapItems = append(mp.MapItems, mip)
return mip
}
func (mp *MapPLZ) Add_Geojson_Collection(geojson string) []MapItem {
// GeoJSON parsing based on http://stackoverflow.com/a/15728702
var geojsonData GeojsonFeatureCollection
var featureList []MapItem
json.Unmarshal([]byte(geojson), &geojsonData)
for i := range geojsonData.Features {
t := geojsonData.Features[i]
featureList = append(featureList, ConvertGeojsonFeature(string(t), mp.Database))
}
return featureList
}