-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeojson.go
106 lines (91 loc) · 2.06 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
package govector
import (
"errors"
"io"
"io/ioutil"
"path/filepath"
"github.com/flywave/go-geom"
"github.com/flywave/go-geom/general"
)
type GeoJSONProvider struct {
fc *geom.FeatureCollection
index int
}
func (p *GeoJSONProvider) Open(filename string, file io.Reader) error {
data, err := ioutil.ReadAll(file)
if err != nil {
return err
}
p.fc, err = general.UnmarshalFeatureCollection(data)
if err != nil {
return err
}
return nil
}
func (p *GeoJSONProvider) Match(filename string, file io.Reader) bool {
ext := filepath.Ext(filename)
if ext != ".geojson" && ext != ".json" {
return false
}
data, err := ioutil.ReadAll(file)
if err != nil {
return false
}
p.fc, err = general.UnmarshalFeatureCollection(data)
if err != nil {
return false
}
return len(p.fc.Features) > 0
}
func (p *GeoJSONProvider) Reset() error {
p.index = 0
return nil
}
func (p *GeoJSONProvider) Close() error {
return nil
}
func (p *GeoJSONProvider) Next() bool {
if p.index < (len(p.fc.Features)) {
p.index++
return true
}
return false
}
func (p *GeoJSONProvider) Read() *geom.Feature {
if p.fc == nil {
return nil
}
return p.fc.Features[p.index-1]
}
type GeoJSONExporter struct {
cache *geom.FeatureCollection
writer io.WriteCloser
}
func newGeoJSONExporter(writer io.WriteCloser) Exporter {
return &GeoJSONExporter{cache: &geom.FeatureCollection{Features: make([]*geom.Feature, 0, 1024)}, writer: writer}
}
func (e *GeoJSONExporter) WriteFeature(feature *geom.Feature) error {
if e.cache == nil {
return errors.New("export not init")
}
e.cache.Features = append(e.cache.Features, feature)
return nil
}
func (e *GeoJSONExporter) WriteFeatureCollection(feature *geom.FeatureCollection) error {
if e.cache == nil {
return errors.New("export not init")
}
e.cache.Features = append(e.cache.Features, feature.Features...)
return nil
}
func (e *GeoJSONExporter) Flush() error {
return nil
}
func (e *GeoJSONExporter) Close() error {
json, err := e.cache.MarshalJSON()
if err != nil {
return err
}
_, err = e.writer.Write(json)
return e.writer.Close()
}