Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@theogf
Created June 2, 2020 09:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theogf/9795ccded3741c776e0d9f3024179266 to your computer and use it in GitHub Desktop.
Save theogf/9795ccded3741c776e0d9f3024179266 to your computer and use it in GitHub Desktop.
using HTTP
using Dates
using Printf
using JSON
using DataFrames
using FileIO
using CSV
work_location = (52.516747,13.323553)
topleft = (52.549563, 13.273303)
bottomright = (52.464485, 13.478868)
center = (topleft .+ bottomright) ./ 2
google_transitmatrix_url = "https://maps.googleapis.com/maps/api/distancematrix/json?"
google_staticmap_url = "https://maps.googleapis.com/maps/api/staticmap?"
mapquest_staticmap_url = "https://open.mapquestapi.com/staticmap/v5/map?"
googlemap_key = ## PUT YOUR KEY HERE ##
mapquest_key = ## PUT YOUR KEY HERE ##
function url_string(options)
join(
(string(key) * "=" * string(value) for (key, value) in zip(keys(options), options)),
"&"
)
end
function coordtuple_to_string(o; join = ",")
@sprintf("%.6f", o[1]) * join * @sprintf("%.6f", o[2])
end
function build_transitmatrix_query_url(origins)
origins_string = join((coordtuple_to_string(o) for o in origins), "|")
options = (
origins = origins_string,
destinations = coordtuple_to_string(work_location),
units = "metric",
mode = "transit",
departure_time = string(Int(datetime2unix(DateTime("2020-06-02T08:00:00")))),
key = googlemapkey,
)
options_string = url_string(options)
google_transitmatrix_url * options_string
end
function build_mapquest_query_url(topleft,bottomright)
boundingbox_string = join((coordtuple_to_string(corner) for corner in [topleft,bottomright]), ",")
options = (
format= "png",
key= mapquest_key,
# size = "300,800",
boundingBox = boundingbox_string
)
options_string = url_string(options)
mapquest_staticmap_url * options_string
end
function build_staticmap_query_url(topleft,bottomright, center)
visible_corners = join((coordtuple_to_string(corner, join = "%2C") for corner in [topleft,bottomright]), "%7C")
options = (
format= "png",
key= googlemap_key,
center = coordtuple_to_string(center, join = "%2C"),
size = "800x600",
visible = visible_corners,
)
options_string = url_string(options)
google_staticmap_url * options_string
end
function extract_result(json)
rows = json["rows"]
addresses = String.(json["origin_addresses"])
distances = [try r["elements"][1]["distance"]["value"]; catch err; NaN end for r in rows]
durations = [try r["elements"][1]["duration"]["value"]; catch err; NaN end for r in rows]
(address = addresses, distance = distances, duration = durations)
end
function run_coordinates(coordinates)
query_url = build_transitmatrix_query_url(coordinates)
response = HTTP.get(query_url)
global response_json = JSON.parse(String(response.body))
data = extract_result(response_json)
merged_data = merge((lat = first.(coordinates), long = last.(coordinates)), data)
end
function get_grid(coordinates)
data = []
nrows = size(coordinates, 1)
for irow in 1:nrows
println("Row $irow of $nrows")
subset_data = run_coordinates(coordinates[irow, :])
push!(data, subset_data)
end
data
end
sidelength = 100
coordinates = [(x, y)
for x in LinRange(topleft[1], bottomright[1], sidelength),
y in LinRange(topleft[2], bottomright[2], sidelength)]
## Query the distance matrix from Google Maps (just run it once and save the result !)
# data_named_tuples = get_grid(coordinates)
# data_df = vcat(DataFrame.(data_named_tuples)...)
# CSV.write("grid_distance_to_work.csv", data_df)
## Query the PNG map from Map Quest
# query = build_mapquest_query_url(topleft,bottomright)
query = build_staticmap_query_url(topleft,bottomright, center)
res = HTTP.get(query)
write(joinpath(@__DIR__, "googlestaticmap.png"), res.body)
## Plotting
using Makie
using MakieLayout
using ImageMagick
using Measures
datagrid = CSV.read("grid_distance_to_work.csv")
lat_range = [topleft[1], bottomright[1]]
long_range = [topleft[2], bottomright[2]]
minutesgrid = reshape(datagrid.duration ./ 60, 100, 100)
minutesgrid[minutesgrid .> 55] .= NaN
latitudes = reshape(datagrid.lat, 100, 100)
longitudes = reshape(datagrid.long, 100, 100)
function rect2rect(r1, r2, p)
frac = (p .- r1.origin) ./ r1.widths
frac .* r2.widths .+ r2.origin
end
## Print scene
scene, layout = layoutscene()
ax1 = layout[1, 1] = LAxis(scene, aspect = DataAspect())
ax2 = layout[2, 1] = LAxis(scene, aspect = DataAspect())
hidexdecorations!.([ax1, ax2])
hideydecorations!.([ax1, ax2])
linkaxes!(ax1, ax2)
tightlimits!(ax1)
tightlimits!(ax2)
# image = load(joinpath(@__DIR__, "staticmap.png"))
image = load(joinpath(@__DIR__, "googlestaticmap.png"))
hm = Makie.heatmap!(ax1, long_range, lat_range, reverse(minutesgrid, dims=2), colormap = :rainbow)
im = Makie.image!(ax2, long_range, lat_range, rotr90(image))
contour!(ax1, LinRange(long_range..., 100), LinRange(lat_range..., 100), minutesgrid, linewidth = 4, colormap = :rainbow)
contour!(ax2, LinRange(long_range..., 100), LinRange(lat_range..., 100), minutesgrid, linewidth = 4, colormap = :rainbow)
scatpos = Node([Point2f0(0, 0)])
scatter!(ax1, scatpos, xautolimits = false, yautolimits = false, color = :red, markersize = 10px)
scatter!(ax2, scatpos, xautolimits = false, yautolimits = false, color = :red, markersize = 10px)
scatter!(ax1, [Point2f0(reverse(work_location))], xautolimits = false, yautolimits = false, marker = '★', color = :white, markersize = 10px)
scatter!(ax2, [Point2f0(reverse(work_location))], xautolimits = false, yautolimits = false, marker = '★', color = :black, markersize = 10px)
on(events(ax2.scene).mouseposition) do pos
scatpos[] = [rect2rect(ax2.scene.px_area[], ax2.limits[], pos)]
end
cb = layout[1:2, 2] = LColorbar(scene, hm, width = 30, label = "Minutes")
layout[0, :] = LText(scene, "Transit time to TU Berlin CS department", textsize = 35)
scene
## Saving
save(joinpath(@__DIR__, "berlingrid.png"), scene)
scene
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment