-
Notifications
You must be signed in to change notification settings - Fork 8
/
map.go
293 lines (268 loc) · 7.21 KB
/
map.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package xutil
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"github.com/panjf2000/ants"
)
type Poi struct {
Status int
Message string
Lng float64
Lat float64
Addr string
Province string
City string
District string
CityCode string
Level string
Info map[string]string
}
type MapAPI struct {
AK string
LimitCity string
SM sync.Map
wg sync.WaitGroup
}
func NewMapAPI(ak string) MapAPI {
return MapAPI{AK: ak, LimitCity: "上海市"}
}
//---------------------------------------------------------------------------------------------------------------------
// AmapGeoCode 高德解析地址为经纬度
func (m MapAPI) AmapGeoCode(address string) (poi Poi) {
type AmapPoi struct {
Status string `json:"status"`
Info string `json:"info"`
Infocode string `json:"infocode"`
Count string `json:"count"`
Geocodes []struct {
FormattedAddress string `json:"formatted_address"`
Country string `json:"country"`
Province string `json:"province"`
Citycode string `json:"citycode"`
City string `json:"city"`
District string `json:"district"`
Adcode string `json:"adcode"`
Location string `json:"location"`
Level string `json:"level"`
} `json:"geocodes"`
}
// https://lbs.amap.com/api/webservice/guide/api/georegeo
address = strings.Join(strings.Fields(address), "")
address = url.QueryEscape(address)
m.LimitCity = url.QueryEscape(m.LimitCity)
APIURL := ""
if m.LimitCity != "" {
APIURL = fmt.Sprintf("http://restapi.amap.com/v3/geocode/geo?key=%s&address=%s&city=%s&citylimit=true", m.AK, address, m.LimitCity)
} else {
APIURL = fmt.Sprintf("http://restapi.amap.com/v3/geocode/geo?key=%s&address=%s", m.AK, address)
}
resp, err := http.Get(APIURL)
if err != nil {
poi.Status = -1
poi.Message = err.Error()
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
poi.Status = -1
poi.Message = err.Error()
return
}
mpoi := AmapPoi{}
err = json.Unmarshal(body, &mpoi)
if err != nil {
poi.Status = -1
poi.Message = err.Error()
return
}
if len(mpoi.Geocodes) > 0 {
poi.Addr = mpoi.Geocodes[0].FormattedAddress
poi.Province = mpoi.Geocodes[0].Province
poi.CityCode = mpoi.Geocodes[0].Citycode
poi.City = mpoi.Geocodes[0].City
poi.District = mpoi.Geocodes[0].District
poi.Level = mpoi.Geocodes[0].Level
loc := strings.Split(mpoi.Geocodes[0].Location, ",")
lng, err1 := strconv.ParseFloat(loc[0], 64)
lat, err2 := strconv.ParseFloat(loc[1], 64)
if err1 == nil && err2 == nil {
poi.Lng = lng
poi.Lat = lat
}
poi.Status = 0
poi.Message = ""
} else {
poi.Status = 1
poi.Message = "EmptyData"
}
return
}
//_codeAmapAddr 解析地址,多次失败尝试
func (m *MapAPI) _codeAmapAddr(addr interface{}, cnt int) {
unaddr := addr.(string)
if cnt > 5 {
return
}
poi := m.AmapGeoCode(unaddr)
if poi.Status != 0 {
cnt++
m._codeAmapAddr(addr, cnt)
} else {
m.SM.Store(unaddr, poi)
}
}
// AmapGeoCodeALL 高德解析地址为经纬度
func (m *MapAPI) AmapGeoCodeALL(addrsMap map[string]string, poolsize int) (addrsAll map[string]Poi) {
p, _ := ants.NewPoolWithFunc(poolsize, func(addr interface{}) {
m._codeAmapAddr(addr, 0)
m.wg.Done()
})
defer p.Release()
for k := range addrsMap {
m.wg.Add(1)
p.Invoke(k)
}
m.wg.Wait()
addrsAll = make(map[string]Poi, 0)
m.SM.Range(func(k, v interface{}) bool {
addr := k.(string)
addrsAll[addr] = v.(Poi)
return true
})
return addrsAll
}
//---------------------------------------------------------------------------------------------------------------------
// BdmapGeoCode 百度解析地址为经纬度
func (m MapAPI) BdmapGeoCode(address string) (poi Poi) {
type BdmapPOI struct {
Status int `json:"status"`
Message string `json:"msg"`
Result struct {
Location struct {
Lng float64 `json:"lng"`
Lat float64 `json:"lat"`
} `json:"location"`
Precise int `json:"precise"`
Confidence int `json:"confidence"`
Comprehension int `json:"comprehension"`
Level string `json:"level"`
} `json:"result"`
}
address = strings.Join(strings.Fields(address), "")
address = url.QueryEscape(address)
m.LimitCity = url.QueryEscape(m.LimitCity)
APIURL := ""
if m.LimitCity != "" {
APIURL = fmt.Sprintf("http://api.map.baidu.com/geocoder/v2/?output=json&ak=%s&address=%s&city=%s", m.AK, address, m.LimitCity)
} else {
APIURL = fmt.Sprintf("http://api.map.baidu.com/geocoder/v2/?output=json&ak=%s&address=%s", m.AK, address)
}
resp, err := http.Get(APIURL)
if err != nil {
poi.Status = -1
poi.Message = err.Error()
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
poi.Status = -1
poi.Message = err.Error()
return
}
mpoi := BdmapPOI{}
err = json.Unmarshal(body, &mpoi)
if err != nil {
poi.Status = -1
poi.Message = err.Error()
return
}
if mpoi.Status != 0 {
poi.Status = 1
poi.Message = mpoi.Message
return
}
// fmt.Println(mpoi)
poi.Status = mpoi.Status
poi.Message = mpoi.Message
poi.Lng = mpoi.Result.Location.Lng
poi.Lat = mpoi.Result.Location.Lat
poi.Level = mpoi.Result.Level
poi.Info = make(map[string]string, 0)
poi.Info["confidence"] = fmt.Sprintf("%d", mpoi.Result.Confidence)
poi.Info["comprehension"] = fmt.Sprintf("%d", mpoi.Result.Comprehension)
poi.Info["precise"] = fmt.Sprintf("%d", mpoi.Result.Precise)
return
}
//_codeBdmapAddr 解析地址,多次失败尝试
func (m *MapAPI) _codeBdmapAddr(addr interface{}, cnt int) {
unaddr := addr.(string)
if cnt > 5 {
return
}
poi := m.BdmapGeoCode(unaddr)
if poi.Status != 0 {
cnt++
m._codeBdmapAddr(addr, cnt)
} else {
m.SM.Store(unaddr, poi)
}
}
// BdmapGeoCodeALL 百度解析地址为经纬度
func (m *MapAPI) BdmapGeoCodeALL(addrsMap map[string]string, poolsize int) (addrsAll map[string]Poi) {
p, _ := ants.NewPoolWithFunc(poolsize, func(addr interface{}) {
m._codeBdmapAddr(addr, 0)
m.wg.Done()
})
defer p.Release()
for k := range addrsMap {
m.wg.Add(1)
p.Invoke(k)
}
m.wg.Wait()
addrsAll = make(map[string]Poi, 0)
m.SM.Range(func(k, v interface{}) bool {
addr := k.(string)
addrsAll[addr] = v.(Poi)
return true
})
return addrsAll
}
// mocator := "4|13534914.0122,3645387.5227;13535422.4951,3645834.93158|1-13534914.0122,3645542.22157,13534919.2537,3645555.85957,13535111.9804,3645672.21552,13535163.1497,3645713.32995;"
//BmapMocator 百度墨卡托解析
func (m MapAPI) BmapMocator(mocator string) string {
if len(mocator) <= 0 {
return ""
}
geos := strings.Split(mocator, "|")
plm := strings.Split(geos[2], ";")
geo := ""
if geos[0] == "4" {
for i := 0; i < len(plm); i++ {
geoPaths := strings.Split(plm[i], "-")
if geoPaths[0] == "1" {
geo = geoPaths[1]
}
}
}
// 墨卡托坐标解析
var sb bytes.Buffer
sb.WriteString("LINESTRING (")
geoPolyline := strings.Split(geo, ",")
for i := 0; i < len(geoPolyline); i = i + 2 {
x, _ := strconv.ParseFloat(geoPolyline[i], 64)
y, _ := strconv.ParseFloat(geoPolyline[i+1], 64)
lng, lat := MercatorToBd09(x, y)
sb.WriteString(fmt.Sprintf("%g %g,", lng, lat))
}
sb.Bytes()[sb.Len()-1] = ')'
return sb.String()
}