forked from allezViens/ridetheory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
customutilities.py
68 lines (53 loc) · 1.96 KB
/
customutilities.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
def formatResults(modelArray):
res = []
for i in range(len(modelArray)):
print 'in for loop'
res.append(objectify(modelArray[i]))
return res
def objectify(model):
print'objectify top'
obj = {
"id": model.email,
"origin": [float(model.oLat), float(model.oLon)],
"destination": [float(model.dLat), float(model.dLon)],
"date": model.date
}
return obj
def makeBuffer(lat,lon,miles,direction):
#This earth radius in miles may not be entirely accurate - there are various numbers and the earth is not a perfect sphere
#for the case of a buffer though, probably doesn't really matter
earthRadiusMiles = 3959
northwest = math.radians(315)
southeast = math.radians(135)
lat = math.radians(lat)
lon = math.radians(lon)
#cast as float or this breaks, because angular direction is a tiny tiny number
angularDirection = float(miles)/float(earthRadiusMiles)
print angularDirection
if(direction=="NW"):
bearing = northwest
if(direction=="SE"):
bearing = southeast
newLat = math.asin(math.sin(lat)*math.cos(angularDirection)) + math.cos(lat)*math.sin(angularDirection)*math.cos(bearing)
newLon = lon + math.atan2(math.sin(bearing)*math.sin(angularDirection)*math.cos(lat), math.cos(angularDirection)-math.sin(lat)*math.sin(newLat))
return math.degrees(newLat), math.degrees(newLon)
def detuplify(userDict):
oLat, oLon = userDict['origin'][0], userDict['origin'][1]
dLat, dLon = userDict['destination'][0], userDict['destination'][1]
userDict['oLat'] = oLat
userDict['oLon'] = oLon
userDict['dLat'] = dLat
userDict['dLon'] = dLon
if('id' in userDict):
userDict['email'] = userDict['id']
del userDict['origin']
del userDict['destination']
return userDict
def tuplify(userDict):
userDict['origin'] = [userDict['oLat'],userDict['oLon']]
userDict['destination'] = [userDict['dLat'],userDict['dLon']]
del userDict['oLat']
del userDict['oLon']
del userDict['dLat']
del userDict['dLon']
return userDict