-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMap_Quest_Get.py
More file actions
104 lines (91 loc) · 3.46 KB
/
Map_Quest_Get.py
File metadata and controls
104 lines (91 loc) · 3.46 KB
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
import requests
import json
from pickle import dump , load
import sys
from routehome.settings import MapQuest_API
class Send_Data:
""" Class that creates the data to send to mapquestapi
and converts it with Json"""
def __init__(self, start, end, points):
"""
Arguuments:
start: tuple of latitude and longitude
end: tuple of latitude and longitude
points: list of latitude and longitude tuples
"""
self.start = start
self.end = end
self.points = points
self.Data()
self.data = json.dumps(self.data) #Converts Data into a json Object
self.Get_Directions()
def Data(self):
"""
Creates and populates dataset with data from website
"""
self.data = {}
self.data['locations'] = [ #defining the lat/long dictionaries for start and end locations
{
"latLng": {
"lat": self.start[0],
"lng": self.start[1]
}
},
{
"latLng": {
"lat": self.end[0],
"lng": self.end[1]
}
}
]
self.Options() #Calls the Options method
self.data['options'] = self.options
self.data['routeControlPointCollection'] = self.points # creates the dictionary filled with the routeControlPointCollection
def Options(self):
"""
Sets options for Mapquest API
"""
self.options = {}
self.options['tryAvoidLinkIds'] = []
if len(self.points) < 100:
for crimept in self.points:
try:
IDurl = 'http://www.mapquestapi.com/directions/v2/findlinkid?key=%s&lat=%f&lng=%f' % (
MapQuest_API, crimept['lat'], crimept['lng'])
idData = requests.get(IDurl)
linkId = str(idData.json()['linkId'])
self.options['tryAvoidLinkIds'].append(linkId)
except Exception as e:
continue
self.options['avoidTimedConditions'] = False
self.options['doReverseGeocode'] = True
self.options['shapeFormat'] = 'raw'
self.options['generalize'] = 0
self.options['routeType'] = 'pedestrian' #walking directions
self.options['timeType'] = 1
self.options['locale'] = 'en_US'
self.options['unit'] = 'm'
self.options['enhancedNarratives'] = False
self.options['drivingStyle'] = 2
self.options['highwayEfficiency'] = 21
self.options['highwayEfficiency'] = 21 #miles per galon (not relevant)
def Get_Directions(self):
"""
Gets directions from Mapquest
"""
url='http://www.mapquestapi.com/directions/v2/route?key=%s' % (MapQuest_API)
self.response = requests.post(url, data=self.data)
# Handle 500 Erros with pytho's request api
self.response = self.response.json()
return self.response
def Store_Directions(self):
"""
Pickles directions for use by the website
"""
Directions = open(Directions, 'wb')
dump(self.directions, Directions) # move this to a template, template displays stuff to user
Directions.close()
if __name__ == '__main__':
mydata = Send_Data("Clarendon Blvd, Arlington, VA", "2400 S Glebe Rd, Arlington, VA")
print(mydata.response['route']['legs'])
print(mydata.data)