-
Notifications
You must be signed in to change notification settings - Fork 0
/
geocoder_handler.py
91 lines (85 loc) · 3.39 KB
/
geocoder_handler.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
from google_geocoder import GoogleGeocoder
from here_geocoder import HereGeocoder
import argparse
import logging
import sys
class GeocoderHandler(object):
'''
inputAddress is a string including spaces
mode specifies how the program should work
- firstResult mode uses the first result from Here/Google as authority
- bestMatch is a speculative mode that attempts to find common results
from several sources
...
'''
def __init__(self):
self.modeList = ['firstResult', 'bestMatch']
self.address = None
self.mode = self.modeList[0]
self.hereResults = None
self.googleResults = None
def callHereGeocode(self, formatAddress=True):
hereGeocoder = HereGeocoder(self.address)
if formatAddress:
hereGeocoder.formatAddress()
else:
hereGeocoder.formattedAddress = self.address
hereGeocoder.formatRequest()
hereGeocoder.getLatLon()
self.hereResults = [hereGeocoder.lat, hereGeocoder.lon]
logging.info("Results from Here API lat: {} lon: {}".format(
str(self.hereResults[0]), str(self.hereResults[1])))
def callGoogleGeocode(self, formatAddress=True):
googleGeocoder = GoogleGeocoder(self.address)
if formatAddress:
googleGeocoder.formatAddress()
else:
googleGeocoder.formattedAddress = self.address
googleGeocoder.formatRequest()
googleGeocoder.getLatLon()
self.googleResults = [googleGeocoder.lat, googleGeocoder.lon]
logging.info("Results from Google API lat: {} lon: {}".format(
str(self.googleResults[0]), str(self.googleResults[1])))
def compareAndPickResults(self):
if self.mode == 'firstResult':
if self.hereResults is not None:
return self.hereResults
elif self.googleResults is not None:
return self.googleResults
else:
return None
elif self.mode == 'bestMatch':
# Concept mode. Would take ALL results from API returns from Here
# and Google and return the closest match
pass
def parseArgs(self):
parser = argparse.ArgumentParser()
parser.add_argument(
'-s',
'--server',
help='Boolean parameter specifying if the program should be run as \
a server',
action='store_true'
)
parser.add_argument(
'-a',
'--address',
help='The address to get latitude and longitude for',
type=str
)
parser.add_argument(
'-m',
'--mode',
help='The mode used for picking the result latitude and longitude',
choices=self.modeList,
default=self.modeList[0]
)
args = parser.parse_args()
self.runAsServer = args.server
self.address = args.address
if (self.address is None or self.address == "") and not self.runAsServer:
sys.exit("Please provide a valid address")
self.mode = args.mode
logging.info("Parsed address: {}".format(self.address))
logging.info("Parsed mode: {}".format(self.mode))
logging.info("Parsed run as server {}".format(self.runAsServer))