-
Notifications
You must be signed in to change notification settings - Fork 1
/
geo.go
131 lines (114 loc) · 2.91 KB
/
geo.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
/*
* Author Linvon
*/
package geohelper
import (
"errors"
"fmt"
geo "github.com/kellydunn/golang-geo"
geojson "github.com/paulmach/go.geojson"
"io/ioutil"
)
const StrNotFound = "NotFound"
type GeoMap struct {
GMap map[string][]*geo.Polygon
}
func getPolyMap(data []byte, keys []string, ff func(ks []string) string) (map[string][]*geo.Polygon, error) {
fc, err := geojson.UnmarshalFeatureCollection(data)
if err != nil {
return nil, err
}
polysMap := make(map[string][]*geo.Polygon, 0)
for _, v := range fc.Features {
pKeys := make([]string, len(keys))
for i, key := range keys {
if _, ok := v.Properties[key]; !ok {
return nil, errors.New(fmt.Sprintf("file has no key:%v in some features", key))
}
pKeys[i] = v.Properties[key].(string)
}
key := ff(pKeys)
geometry := v.Geometry
mps := make([][][][]float64, 0)
if geometry.Type == "MultiPolygon" {
mps = geometry.MultiPolygon
} else if geometry.Type == "Polygon" {
mps = append(mps, geometry.Polygon)
}
for _, polygon := range mps {
tmpPointList := make([]*geo.Point, 0)
for _, points := range polygon {
for _, point := range points {
tmpPoint := geo.NewPoint(point[1], point[0])
tmpPointList = append(tmpPointList, tmpPoint)
}
}
polysMap[key] = append(polysMap[key], geo.NewPolygon(tmpPointList))
}
}
return polysMap, nil
}
func NewGeoMap(file, key string) (*GeoMap, error) {
data, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
g, e := getPolyMap(data, []string{key}, func(ks []string) string { return ks[0] })
if e != nil {
return nil, e
}
return &GeoMap{g}, nil
}
func NewGeoMapFromBytes(data []byte, key string) (*GeoMap, error) {
g, e := getPolyMap(data, []string{key}, func(ks []string) string { return ks[0] })
if e != nil {
return nil, e
}
return &GeoMap{g}, nil
}
func NewGeoMapFormat(file string, keys []string, ff func(ks []string) string) (*GeoMap, error) {
data, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
g, e := getPolyMap(data, keys, ff)
if e != nil {
return nil, e
}
return &GeoMap{g}, nil
}
func NewGeoMapFormatFromBytes(data []byte, keys []string, ff func(ks []string) string) (*GeoMap, error) {
g, e := getPolyMap(data, keys, ff)
if e != nil {
return nil, e
}
return &GeoMap{g}, nil
}
func (g *GeoMap) FindPoint(tmpPoint *geo.Point) string {
for name, polys := range g.GMap {
for _, poly := range polys {
if poly.Contains(tmpPoint) {
return name
}
}
}
return StrNotFound
}
func (g *GeoMap) ContainPoint(tmpPoint *geo.Point) bool {
for _, polys := range g.GMap {
for _, poly := range polys {
if poly.Contains(tmpPoint) {
return true
}
}
}
return false
}
func (g *GeoMap) FindLoc(lat, lng float64) string {
tmpPoint := geo.NewPoint(lat, lng)
return g.FindPoint(tmpPoint)
}
func (g *GeoMap) ContainLoc(lat, lng float64) bool {
tmpPoint := geo.NewPoint(lat, lng)
return g.ContainPoint(tmpPoint)
}