-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparking_viz_library.jl
299 lines (267 loc) · 10.9 KB
/
parking_viz_library.jl
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
struct InfractionNode
date::Int
code::Int
fine::Int
time::Int
loc::String
end
mutable struct StreetSegment
name::String
osm_id::String
boundingBox::Array{Float64}
infraction_nodes::Array{InfractionNode}
end
# We have a dictionary of StreetSegments (key is osm_id), so
# we make a custom merge function dispatched to StreetSegment dicts.
import Base.merge!
function merge!(d::Dict{String, StreetSegment},
others::Dict{String, StreetSegment})
for other in others
k = other.first
v = other.second
if haskey(d, k)
# concatenate infraction_nodes
d[k].infraction_nodes = vcat(d[k].infraction_nodes,
v.infraction_nodes)
else
d[k] = v
end
end
return d
end
function nominatim_query(qstring)
q = "http://localhost:7070/search/?format=json&q="
for word in split(qstring)
q *= word * "+"
end
return q[1:end-1]
end
function nominatim_reverse(lat,lon,zoom=16)
q = "http://localhost:7070/reverse?format=json&"
q *= "lat=" * lat * "&"
q *= "lon=" * lon * "&"
q *= "zoom=16"
return q
end
function nominatim_response(nom_query,reverse=false)
req = HTTP.request("GET", nom_query)
reqjson = JSON.parse(String(req.body))
if reverse==true
return reqjson
else
try
return reqjson[1]
catch
return reqjson
end
end
end
function getStreetSegment(qstring)
nom_query = nominatim_query(qstring)
qstring_req = nominatim_response(nom_query)
# reverse geolocate with
lat = qstring_req["lat"]
lon = qstring_req["lon"]
reversereq = nominatim_response(nominatim_reverse(lat,lon), true)
return reversereq
end
function allStreetSegments(df)
# make sure df has no missings, etc
listStreetSegments = Dict{String, StreetSegment}()
for i in 1:size(df)[1]
try
qstring = df[i,:].location2[1] * ", Toronto"
q_way = getStreetSegment(qstring)
qquery = nominatim_query(qstring)
qresponse = nominatim_response(qquery)
infnode = InfractionNode(df[i,:].date_of_infraction[1],
df[i,:].infraction_code[1],
df[i,:].set_fine_amount[1],
df[i,:].time_of_infraction[1],
df[i,:].location2[1])
if haskey(listStreetSegments, q_way["osm_id"])
push!(listStreetSegments[q_way["osm_id"]].infraction_nodes,
infnode)
else
seg = StreetSegment(q_way["display_name"],
q_way["osm_id"],
map(x -> parse(Float64, x), q_way["boundingbox"]),
[infnode])
listStreetSegments[q_way["osm_id"]] = seg
end
catch err
# probably going to be an error where nominatim can't find the query
print("ERROR: ")
println(err)
print("error at: ")
println(i)
end
end
return listStreetSegments
end
function make_db(i)
db = SQLite.DB(string("streetsegdb_", i))
SQLite.query(db,
"create table if not exists streetsegments(
osm_id int primary key,
name text,
slat real,
nlat real,
wlng real,
elng real)")
SQLite.query(db,
"create table if not exists streetsegmentinfraction(
osm_id int,
date int,
code int,
fine int,
time int,
loc text)")
return db
end
function amongst(num, workers)
quotient = Int(floor(num / workers))
rs = push!([(quotient*x + 1):(quotient*(x+1)) for x in 0:workers-2],
(quotient*(workers-1)+1):num)
return rs
end
function addToDB(df,db)
# println(string("worker: ", Threads.threadid()))
for j in 1:size(df)[1]
row = df[j,:]
try
#SQLite.execute!(db, "BEGIN TRANSACTION")
qstring = row.location2[1] * ", Toronto"
q_way = getStreetSegment(qstring)
qquery = nominatim_query(qstring)
qresponse = nominatim_response(qquery)
infnode = InfractionNode(row.date_of_infraction[1],
row.infraction_code[1],
row.set_fine_amount[1],
row.time_of_infraction[1],
row.location2[1])
if isempty(SQLite.query(db, "select * from streetsegments where osm_id=:osmid",
values = Dict(:osmid => q_way["osm_id"])))
SQLite.query(db, "insert into streetsegments values (:osmid, :name, :slat, :nlat, :wlng, :elng)",
values = Dict(:osmid => q_way["osm_id"],
:name => q_way["display_name"],
:slat => parse(Float64, q_way["boundingbox"][1]),
:nlat => parse(Float64, q_way["boundingbox"][2]),
:wlng => parse(Float64, q_way["boundingbox"][3]),
:elng => parse(Float64, q_way["boundingbox"][4])
))
end
SQLite.query(db, "insert into streetsegmentinfraction values (:osmid, :date, :code, :fine, :time, :loc)",
values = Dict(:osmid => q_way["osm_id"],
:date => row.date_of_infraction[1],
:code => row.infraction_code[1],
:fine => row.set_fine_amount[1],
:time => row.time_of_infraction[1],
:loc => row.location2[1]
))
#SQLite.execute!(db, "END TRANSACTION")
catch err
# probably going to be an error where nominatim can't find the query
println(string(err, "at: ", j))
end
end
end
function addToDB(row, db_procs, j)
db = db_procs[Threads.threadid()]
# println(string("worker: ", Threads.threadid()))
try
println("starto")
#SQLite.execute!(db, "BEGIN TRANSACTION")
qstring = row.location2[1] * ", Toronto"
q_way = getStreetSegment(qstring)
qquery = nominatim_query(qstring)
qresponse = nominatim_response(qquery)
infnode = InfractionNode(row.date_of_infraction[1],
row.infraction_code[1],
row.set_fine_amount[1],
row.time_of_infraction[1],
row.location2[1])
println("before check")
if isempty(SQLite.query(db, sr"select * from streetsegments where osm_id=:osmid",
values = Dict(:osmid => q_way["osm_id"])))
SQLite.query(db, sr"insert into streetsegments values (:osmid, :name, :slat, :nlat, :wlng, :elng)",
values = Dict(:osmid => q_way["osm_id"],
:name => q_way["display_name"],
:slat => parse(Float64, q_way["boundingbox"][1]),
:nlat => parse(Float64, q_way["boundingbox"][2]),
:wlng => parse(Float64, q_way["boundingbox"][3]),
:elng => parse(Float64, q_way["boundingbox"][4])
))
end
println("after check")
SQLite.query(db, sr"insert into streetsegmentinfraction values (:osmid, :date, :code, :fine, :time, :loc)",
values = Dict(:osmid => q_way["osm_id"],
:date => row.date_of_infraction[1],
:code => row.infraction_code[1],
:fine => row.set_fine_amount[1],
:time => row.time_of_infraction[1],
:loc => row.location2[1]
))
#SQLite.execute!(db, "END TRANSACTION")
catch err
# probably going to be an error where nominatim can't find the query
println(string(err, "at: ", j))
end
end
function multiSS2sqlite(df)
# make a vector of databases to accThreads.nthreads())
@everywhere db_procs = map(make_db, 1:Threads.nthreads())
# helper function
# warning: mutable!! mutates thedict
ranges = amongst(size(df)[1], Threads.nthreads())
streetsegs = @sync [@async addToDB(df[ranges[x], :], db_procs[x]) for x in 1:Threads.nthreads()]
return streetsegs
end
function multiSS2(df)
# helper function
# warning: mutable!! mutates thedict
@everywhere listStreetSegments = Dict{String, StreetSegment}()
function addToDict(row, thedict, j)
try
qstring = row.location2[1] * ", Toronto"
q_way = getStreetSegment(qstring)
qquery = nominatim_query(qstring)
qresponse = nominatim_response(qquery)
infnode = InfractionNode(row.date_of_infraction[1],
row.infraction_code[1],
row.set_fine_amount[1],
row.time_of_infraction[1],
row.location2[1])
if haskey(thedict, q_way["osm_id"])
push!(thedict[q_way["osm_id"]].infraction_nodes,
infnode)
else
seg = StreetSegment(q_way["display_name"],
q_way["osm_id"],
map(x -> parse(Float64, x), q_way["boundingbox"]),
[infnode])
thedict[q_way["osm_id"]] = seg
end
catch err
# probably going to be an error where nominatim can't find the query
println(err)
print("at: ")
println(j)
end
end
@sync for j in 1:size(df)[1]
@spawn addToDict(df[j,:], listStreetSegments, j)
end
return listStreetSegments
end
function multiStreetSegments(df)
function amongst(num, workers)
quotient = Int(floor(num / workers))
rs = push!([(quotient*x + 1):(quotient*(x+1)) for x in 0:workers-2],
(quotient*(workers-1)+1):num)
return rs
end
ranges = amongst(size(df)[1], nprocs())
streetsegs = @sync [@spawn allStreetSegments(df[x, :]) for x in ranges]
return foldl(merge!,[fetch(x) for x in streetsegs])
end