/
findBestAvailableAddress.py
200 lines (189 loc) · 7.08 KB
/
findBestAvailableAddress.py
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
# -*- coding: utf-8 -*-
"""
Created on Fri May 17 08:07:28 2019
Find best available address/coordinate
@author: aimin.fan
"""
# main_with_ini.py
import configparser
from geopy.geocoders import Nominatim
from geopy.geocoders import GeoNames
from geopy.geocoders import Here
from geopy.geocoders import TomTom
from geopy.geocoders import OpenMapQuest
from geopy.geocoders import Photon
from geopy.geocoders import GoogleV3
from geopy import distance
#import pandas as pd
import os
import ogr, osr
#import time
import requests
def replacefileExtension(f,ext):
if not ext.startswith('.'):
ext='.'+ext
return os.path.splitext(f)[0]+ext
def reprojectPoint(pt,frmEPSG=26917,toEPSG=4326):
# create coordinate transformation
inSpatialRef = osr.SpatialReference()
inSpatialRef.ImportFromEPSG(frmEPSG)
outSpatialRef = osr.SpatialReference()
outSpatialRef.ImportFromEPSG(toEPSG)
coordTransform = osr.CoordinateTransformation(inSpatialRef, outSpatialRef)
# transform point
pt.Transform(coordTransform)
return pt #[point.GetX(), point.GetY()]
def reprojectXY(_x,_y,frmEPSG=26917,toEPSG=4326):
# create a geometry from coordinates
point = ogr.Geometry(ogr.wkbPoint)
point.AddPoint(_x, _y)
return reprojectPoint(point,frmEPSG,toEPSG)
def reprojectXYgetjson(_x,_y,frmEPSG=26917,toEPSG=4326):
pt=reprojectXY(_x, _y,frmEPSG,toEPSG)
return {'x':pt.GetX(), 'y':pt.GetY(),"spatialReference": {
"wkid": toEPSG}}
def revGeocodingbyIQ(_lat,_lon):
url = "https://us1.locationiq.com/v1/reverse.php"
data = {'key': '91013ed0a98308','lat': '{}'.format(_lat),'lon': '{}'.format(_lon),'showdistance':'1','format': 'json'}
addrDict={'Geocoder':'revGeocodingbyIQ','address':'','accuracy':999}
try:
response = requests.get(url, params=data)
res=response.json()
if 'display_name' in res:
addrDict['address']= res['display_name']
if 'distance' in res:
addrDict['accuracy']= res['distance']
except:
print('reverse geocoding (revGeocodingbyIQ) filed:lat:{},Lon:{}'.format(_lat,_lon))
finally:
return addrDict
def revGeocodingGeopy(_lat,_lon,geolocator):
listaddrs=[]
addrDict={'Geocoder':geolocator['name'],'address':'','accuracy':999}
try:
gl=geolocator['locator']
location = gl.reverse('{},{}'.format(_lat,_lon),exactly_one=False)
if location:
#if len(location)>0:
for lc in location:
#print(lc)
if len(lc)>0:
addrDict['address']=lc[0]
addrDict['accuracy']=getAccuracy(_lat,_lon,addrDict['address'],gl)
listaddrs.append(addrDict)
except:
print(geolocator['name'] , 'Exception!')
return listaddrs
return listaddrs
def getAccuracy(_lat,_lon,addr,geolocator):
p1=(_lat,_lon)
p2=getLatLon(addr,geolocator)
if p2:
d=distance.great_circle
dv=d(p1,p2).m
#print('{} m offset for address: {}'.format(dv,addr))
return dv
else:
return 999
def getLatLon(addr,geolocator):
l=geolocator.geocode(addr)
if l:
if len(l)>=2:
return l[1]
return None
def collectGeocoders():
config = configparser.ConfigParser()
conf=r'..\conf\config.ini'
config.read(conf)
keys = {'Here_app_id':config['DEFAULT']['Here_app_id'],
'Here_app_code':config['DEFAULT']['Here_app_code'],
'TomTom':config['DEFAULT']['TomTom_api_key'],
'OpenMapQuest':config['DEFAULT']['OpenMapQuest_api_key'],
'GoogleV3':config['DEFAULT']['GoogleV3_api_key']
}
locators=[{'locator':Nominatim(user_agent="afan"),'name':'Nominatim','type':'Geopy'},
{'locator':GeoNames(username="aimfan"),'name':'GeoNames','type':'Geopy'},
{'locator':Here(app_id=keys['Here_app_id'], app_code=keys['Here_app_code']),'name':'Here','type':'Geopy'},
{'locator':TomTom(api_key=keys['TomTom']),'name':'TomTom','type':'Geopy'},
{'locator':OpenMapQuest(api_key=keys['OpenMapQuest']),'name':'OpenMapQuest','type':'Geopy'},
{'locator':Photon(),'name':'Photon','type':'Geopy'}
]
#locators.append({'locator':GoogleV3(api_key=keys['GoogleV3']),'name':'GoogleV3','type':'Geopy'})
locators.append({'locator':revGeocodingbyIQ,'name':'revGeocodingbyIQ','type':'Custom'})
return locators
def getBestAddress(_lat,_lon):
print('processing: Lon: {},Lat: {}'.format(_lon,_lat))
locators=collectGeocoders()
mindv=999
bestAddr=None
for l in locators:
if l['type']=='Geopy':
addrl=revGeocodingGeopy(_lat,_lon,l)
addr=addrl[0]
elif l['type']=='Custom':
g=l['locator']
addr=g(_lat,_lon)
else:
addr=None
print('Geocoder {} Not Implemented!'.format(l['name']))
if addr:
fdv=998.9
dv=addr['accuracy']
if type(dv)==float or type(dv)==int:
fdv=dv
elif type(dv)==str:
if dv.isdigit():
fdv=float(dv)
else:
print('{} is not number!'.format(dv))
else:
print('{} not implemented'.format(type(dv)))
if fdv < mindv:
mindv=dv
bestAddr=addr
return bestAddr
def getBestLocation(addressString):
print('processing:{}'.format(addressString))
locators=collectGeocoders()
for l in locators:
addrDict={'Geocoder':l['name'],'address':addressString,'location':None,'accuracy':999}
if l['type']=='Geopy':
try:
gl=l['locator']
location = gl.geocode('{}'.format(addressString),exactly_one=False)
if len(location)>0:
if len(location[0])>0:
addrDict['location']=location[0][1]
return addrDict
except:
print('Error when geocoding!')
continue
return None
def getAllAddress(_lat,_lon):
print('processing: Lon: {},Lat: {}'.format(_lon,_lat))
locators=collectGeocoders()
lAddr=[]
for l in locators:
if l['type']=='Geopy':
addr=revGeocodingGeopy(_lat,_lon,l)
lAddr=lAddr+addr
elif l['type']=='Custom':
g=l['locator']
addr=g(_lat,_lon)
lAddr.append(addr)
else:
addr=None
print('Geocoder {} Not Implemented!'.format(l['name']))
return lAddr
if __name__ == '__main__':
conf=r'c:\projects\conf\config.ini'
originaddress='320 Queen St. Chatham ON'
#originaddress='36 Birmingham Ln. Chatham ON'
b=getBestLocation(originaddress)
print(b['location'],'\n\n')
a=getBestAddress(b['location'][0], b['location'][1])
print(a)
## print(a)
# for item in a:
#
# print(item)