-
Notifications
You must be signed in to change notification settings - Fork 21
/
Trip.jl
215 lines (195 loc) · 6.12 KB
/
Trip.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
using HDF5, CSV, DataFrames, Dates, Sockets
using Distances: euclidean
using StatsBase:rle
import JSON
include("util.jl")
mutable struct Trip{T<:AbstractFloat}
lon::Vector{T}
lat::Vector{T}
tms::Vector{T}
devid
roads
end
Trip(lon, lat, tms) = Trip(lon, lat, tms, 0, nothing)
Trip(lon, lat, tms, devid) = Trip(lon, lat, tms, devid, nothing)
function Base.show(io::IO, t::Trip)
print(io, "Trip: $(length(t.lon)) points")
end
Base.length(t::Trip) = length(t.lon)
Base.reverse(trip::Trip) = Trip(reverse(trip.lon), reverse(trip.lat), trip.tms)
#trip = Trip([1, 2, 3.], [1, 3, 2.], [3, 4, 5.])
function readtripsh5(tripfile::String)
"""
Read trips from hdf5file
"""
trips = Trip[]
h5open(tripfile, "r") do f
ntrips = read(f["/meta/ntrips"])
for i = 1:ntrips
lon = read(f["/trip/$i/lon"])
lat = read(f["/trip/$i/lat"])
tms = read(f["/trip/$i/tms"])
trip = Trip(lon, lat, tms)
push!(trips, trip)
#i >= 10000 && break
end
end
trips
end
function readtripscsv(tripfile::String, header=nothing)
"""
Read trips from csvfile. If `header` is unspecified, the csvfile must contain
columns [:devid, :tripid, :tms, :lon, :lat].
"""
df = CSV.File(tripfile, header=header) |> DataFrame
hasdevid = :devid in names(df)
trips = Trip[]
for sdf in groupby(df, :tripid)
#sdf = df[df.tripid .== tripid[i], :]
sdf = DataFrame(sdf)
sort!(sdf, :tms)
lon = convert(Array{Float64}, sdf.lon)
lat = convert(Array{Float64}, sdf.lat)
tms = convert(Array{Float64}, sdf.tms)
devid = hasdevid ? first(sdf.devid) : nothing
trip = Trip(lon, lat, tms, devid)
push!(trips, trip)
end
trips
end
function pathdistance(trip::Trip)
"""
The trip distance (km) in the road network
"""
s = 0.0
for i = 2:length(trip)
px, py = gps2webmercator(trip.lon[i-1], trip.lat[i-1])
cx, cy = gps2webmercator(trip.lon[i], trip.lat[i])
s += euclidean([px, py], [cx, cy])
end
s / 1000.0
end
function isvalidtrip(trip::Trip)
"""
Return true if the trip is a valid trip which maximum speed cannnot exceed 35
otherwise return false.
"""
for i = 2:length(trip.tms)
px, py = gps2webmercator(trip.lon[i-1], trip.lat[i-1])
cx, cy = gps2webmercator(trip.lon[i], trip.lat[i])
euclidean([px, py], [cx, cy]) / (trip.tms[i] - trip.tms[i-1]) > 35 && return false
end
true
end
function timeslotsubtrips(trips::Vector{Trip}, stms::T, etms::T) where T
"""
Return the sub-trips falling into time slot [stms, etms].
"""
subtrips = Trip[]
for trip in trips
a, b = searchrange(trip.tms, stms, etms)
if a < b
subtrip = Trip(trip.lon[a:b], trip.lat[a:b], trip.tms[a:b], trip.devid)
push!(subtrips, subtrip)
end
end
subtrips
end
function timeslottrips(trips::Vector{Trip}, stms::T, etms::T, Δmins=5) where T
"""
Return the trips whose start time falling into time slot [stms, etms].
"""
filter(trips) do trip
stms < trip.tms[1] < etms && etms + Δmins*60 > trip.tms[end]
end
end
function trip2finetrip(trip::Trip, Δ=200)
"""
Interpolating a coarse trip into a fine-grained trip
Input:
trip (Trip)
Δ (Real): The minimum Euclidean distance between two consecutive points after interpolation.
Output:
A trip
"""
finepoints, tms, _ = linear_interpolate(gps2webmercator.(trip.lon, trip.lat), trip.tms, Δ)
x, y = map(first, finepoints), map(last, finepoints)
gpspoints = webmercator2gps.(x, y)
lon, lat = map(first, gpspoints), map(last, gpspoints)
Trip(lon, lat, tms, trip.devid)
end
function removeredundantpoints(trip::Trip, δ=10)
"""
Remove the redundant sampling points in a trip. A point is redundant if the timestamp gap
of it and its last point is smaller than δ. The function is applied to a trip before submitting
it to the map matcher in order to avoid unnecessary computation.
Input:
trip (Trip)
δ (Real): the minimum gap whose the unit is second
Output:
_ (Trip)
"""
ind = Int[1]
for i = 2:length(trip)
if trip.tms[i]-trip.tms[ind[end]] >= δ
push!(ind, i)
end
end
Trip(trip.lon[ind], trip.lat[ind], trip.tms[ind], trip.devid)
end
###############################################################################
function trip2geojson(trip::Trip)
points = [Dict("type"=>"Feature",
"geometry"=>Dict("type"=>"Point",
"coordinates"=>[trip.lon[i], trip.lat[i]]))
for i = 1:length(trip)]
Dict("type"=>"FeatureCollection",
"features"=>points)
end
function trip2json(trip::Trip)
"""
Mapping a trip to the json format required by the map matcher
Input:
trip (Trip)
Output:
_ (Dict)
"""
js = Dict{String, String}[]
for i = 1:length(trip)
lon, lat = trip.lon[i], trip.lat[i]
tms = Dates.format(Dates.unix2datetime(trip.tms[i]), "yyyy-mm-dd HH:MM:SS")
push!(js, Dict("point"=>"POINT($lon $lat)",
"time"=> "$(tms)+0800",
"id"=>"\\x0001"))
end
js
end
function matchtrip(trip::Trip)
"""
Matching a trip by submitting it to the match server.
Input:
trip (Trip)
Output:
result (Vector): an array of dict
"""
#js = trip2finetrip(trip, 200) |> removeredundantpoints |> trip2json
js = trip |> removeredundantpoints |> trip2json
request = Dict("format"=>"slimjson", "request"=>js) |> JSON.json
clientside = connect("localhost", 1234)
try
message = request * "\n"
write(clientside, message)
response = readline(clientside)
response == "SUCCESS" ? readline(clientside) |> JSON.parse : []
finally
close(clientside)
end
end
function trip2roads(trip::Trip)
"""
Mapping a trip to a sequence of road segments.
"""
result = matchtrip(trip)
roads, _ = map(d->get(d, "road", -1), result) |> rle
roads
end