-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransformTrails.py
180 lines (164 loc) · 5.86 KB
/
transformTrails.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
import pandas as pd
pd.set_option("display.max_rows", None, "display.max_columns", None)
import numpy as np
import sys
if __name__ == '__main__':
df = pd.read_csv("alltrails.csv")
# print (df["_geoloc"])
featureSet = {}
activitySet = {}
updatedLines = []
rowCount = 0
for i, data in df.iterrows():
if rowCount == 0 or rowCount == 1 or rowCount == 2:
rowCount +=1
continue
features = data["features"]
features = features.split(",")
activities = data["activities"]
activities = activities.split(",")
for feature in features:
feature = feature.strip(" ,\'\"[]")
if feature == '':
continue
if feature in featureSet:
featureSet[feature] += 1
else:
featureSet[feature] = 1
for activity in activities:
activity = activity.strip(" ,\'\"[]")
if feature == '':
continue
if activity in activitySet:
activitySet[activity] += 1
else:
activitySet[activity] = 1
binnedDf = pd.read_csv("trails_binned6_with_visitors.csv")
rowCount = 0
visitorSet = {}
for i, data in binnedDf.iterrows():
if i == 0:
continue
visitors = data["visitors"]
if data["area_name"] in visitorSet:
continue
else:
visitorSet[data["area_name"]] = visitors
visitorSet['Clayton Co International Park, Jonesboro GA'] = 100000
visitorSet['Fort Hunt National Park'] = 100000
visitorSet['Wolf Trap National Park for the Performing Arts'] = 100000
# print(visitorSet)
# first 3 lines
header = []
restrictions = []
fileReader = open("alltrails.csv", 'r')
firstLine = fileReader.readline().strip("\n")
firstLine = firstLine.split(",")
secondLine = fileReader.readline().strip("\n")
secondLine = secondLine.split(",")
for idx, var in enumerate(firstLine):
if var == "_geoloc":
header.append("latitude")
header.append("longitude")
restrictions.append("0")
restrictions.append("0")
continue
if var == "features":
for count, val in enumerate(featureSet):
header.append(val)
restrictions.append("2")
continue
if var == "activities":
for count, val in enumerate(activitySet):
header.append(val)
restrictions.append("2")
continue
header.append(var)
restrictions.append("-1")
header.append("visitors")
restrictions.append("0")
# print(len(restrictions))
updatedLines.append(header)
updatedLines.append(restrictions)
thirdLine = fileReader.readline().strip("\n")
updatedLines.append(thirdLine)
rowCount = 0
for i, data in df.iterrows():
if rowCount == 0 or rowCount == 1 or rowCount == 2:
rowCount +=1
continue
newLine = []
newLine.append(data["trail_id"])
name = data["name"].replace(',', '')
newLine.append(name)
newLine.append(data["area_name"])
newLine.append(data["city_name"])
newLine.append(data["state_name"])
newLine.append(data["country_name"])
# geoloc
geoloc = data["_geoloc"].split(",")
lat = geoloc[0]
lon = geoloc[1]
lat = lat.strip(" ,\'\"[]{}lat:")
lon = lon.strip(" ,\'\"[]{}lng:")
newLine.append(lat)
newLine.append(lon)
# newLine.append(data["popularity"])
popularity = float(data["popularity"])
if(popularity < 4.231):
popBin = "unpopular"
if(popularity >= 4.231 and popularity < 8.462):
popBin = "less popular"
if(popularity >= 8.462 and popularity < 12.693):
popBin = "mid popular"
if(popularity >= 12.693 and popularity < 16.925):
popBin = "more popular"
if(popularity >= 16.925 and popularity < 25.387):
popBin = "very popular"
if(popularity >= 25.387):
popBin = "extremely popular"
newLine.append(popBin)
newLine.append(data["length"])
newLine.append(data["elevation_gain"])
newLine.append(data["difficulty_rating"])
newLine.append(data["route_type"])
# if data["route_type"] != "out and back" and data["route_type"] != "loop" and data["route_type"] != "point to point":
# print(data["route_type"])
newLine.append(data["visitor_usage"])
newLine.append(data["avg_rating"])
newLine.append(data["num_reviews"])
# features
features = data["features"]
features = features.split(",")
activities = data["activities"]
activities = activities.split(",")
for count, val in enumerate(featureSet):
myFeatures = []
for feature in features:
feature = feature.strip(" ,\'\"[]")
myFeatures.append(feature)
if val in myFeatures:
newLine.append(1)
else:
newLine.append(0)
# activities
for count, val in enumerate(activitySet):
myActivities = []
for activity in activities:
activity = activity.strip(" ,\'\"[]")
myActivities.append(activity)
if val in myActivities:
newLine.append(1)
else:
newLine.append(0)
newLine.append(data["units"])
# visitors
newLine.append(visitorSet[data["area_name"]])
updatedLines.append(newLine)
# print(updatedLines)
with open ('newTrails.csv', 'w') as f:
for line in updatedLines:
for var in line:
f.write(str(var))
f.write(',')
f.write('\n')