-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeoProcess.py
More file actions
107 lines (96 loc) · 2.51 KB
/
geoProcess.py
File metadata and controls
107 lines (96 loc) · 2.51 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
105
106
107
# -*-coding: utf-8 -*-
from NSE_Analytics_pilot_testing.process import *
import re
def dist(u,v):
if abs(Filter(u[0])-Filter(v[0]))+abs(Filter(u[1])-Filter(v[1]))>0.001:
return True
else:
return False
def geoProcess(geojson, home_loc, school_loc):
geoList = []
regPoint = re.compile(r'\[.*?\]')
Num = 0
for item in geojson:
points = regPoint.findall(item)
for term in points:
term = map(float,term.strip("[]").split(","))
if Num>0:
if (abs(term[0]-geoList[Num-1][1])+abs(term[1]-geoList[Num-1][0]))>0.001:
geoList.append(tuple([term[1],term[0]]))
Num += 1
else:
geoList.append(tuple([term[1],term[0]]))
Num += 1
#geoRaw = list(set(geoList))
geoRaw = geoList;
geoList = []
for item in geoRaw:
Count = 0
for checker in geoList:
if dist(item,checker):
Count += 1
if Count == len(geoList):
geoList.append(item)
mid = -1
for i in range(len(geoList)):
if not dist(geoList[i],home_loc):
mid = i
return [geoList[mid+1:-1],geoList[0:mid]]#AM, geoListPM
def getMainModeOfAM(data):
regAm_mode = re.compile(r"'am_mode':\s\[.*?\],")
matchMode = regAm_mode.search(str(data))
ModeList = []
try:
ModeList = map(int,matchMode.group().strip("'am_mode': [").rstrip("],").split(','))
except Exception:
print "No travel mode!"
return "Loss"
if 4 in ModeList:
return "Train"
if 5 in ModeList:
return "Bus"
if 6 in ModeList:
return "Car"
return "Walk"
#return matchMode.group()
def getMainModeOfPM(data):
regPm_mode = re.compile(r"'pm_mode':\s\[.*?\]")
matchMode = regPm_mode.search(str(data))
ModeList = []
#print matchMode.group().strip("'pm_mode': [").rstrip("]").split(',')
try:
ModeList = map(int,matchMode.group().strip("'pm_mode': [").rstrip("]").split(','))
except Exception:
print "No travel mode!"
return "Loss"
if 4 in ModeList:
return "Train"
if 5 in ModeList:
return "Bus"
if 6 in ModeList:
return "Car"
return "Walk"
#return matchMode.group()
def Filter(number):
if number == None:
return 0
else:
return number
if __name__=="__main__":
overview, geojson = main("https://data.nse.sg","nodeFile.csv","2015-09-29",testing = True)
#print geojson
print geojson
print overview
home_loc = overview['Home'][(510132, "2015-09-28")]
school_loc = overview['School'][(510132, "2015-09-28")]
geoList = geoProcess(geojson,home_loc,school_loc)
print "PM:"
for item in geoList[0]:
print item
print "AM:"
for item in geoList[1]:
print item
#print overview
AMMode = getMainModeOfAM(overview)
PMMode = getMainModeOfPM(overview)
print AMMode,PMMode