Skip to content

Commit 3920379

Browse files
RebZJjarodlam
authored andcommitted
[FEAT] Add download method for custom Overpass API query
1 parent 6442955 commit 3920379

File tree

4 files changed

+79
-7
lines changed

4 files changed

+79
-7
lines changed

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
88
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
99
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
1010
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
11+
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
1112
LightXML = "9c8b4983-aa76-5018-a973-4c85ecc9e179"
1213
MetaGraphs = "626554b9-1ddb-594c-aa3c-2596fe9399a5"
1314
NearestNeighbors = "b8a86587-4115-5ab1-83bc-aa920d37bbce"

src/download.jl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,36 @@ function overpass_query(filters::String,
102102
return query
103103
end
104104

105+
"""
106+
osm_network_from_custom_filters(custom_filters::String,
107+
metadata::Bool=false,
108+
download_format::Symbol=:json,
109+
bbox::Union{Vector{AbstractFloat},Nothing}=nothing
110+
)::String
111+
112+
To pass in a custom query string, filters and bbox
113+
114+
# Arguments
115+
- `custom_filters::String`: Custom filters for the query, e.g. polygon filter, highways only, traffic lights only, etc.
116+
- `metadata::Bool=false`: Set true to return metadata.
117+
- `download_format::Symbol=:json`: Download format, either `:osm`, `:xml` or `json`.
118+
- `bbox::Union{Vector{AbstractFloat},Nothing}=nothing`: Optional bounding box filter.
119+
- `network_type::Symbol=:drive`:
120+
121+
# Return
122+
- `String`: Overpass query string.
123+
"""
124+
function osm_network_from_custom_filters(;custom_filters::String,
125+
network_type::Symbol=:drive,
126+
metadata::Bool=false,
127+
download_format::Symbol=:json,
128+
bbox::Union{Vector{<:AbstractFloat},Nothing}=nothing)::String
129+
# return overpass_request(query)
130+
query = overpass_query(custom_filters, metadata, download_format, bbox)
131+
response = overpass_request(query)
132+
return response
133+
end
134+
105135
"""
106136
overpass_polygon_network_query(geojson_polygons::Vector{Vector{Any}},
107137
network_type::Symbol=:drive,
@@ -334,6 +364,8 @@ function osm_network_downloader(download_method::Symbol)::Function
334364
return osm_network_from_point
335365
elseif download_method == :polygon
336366
return osm_network_from_polygon
367+
elseif download_method == :custom_filters
368+
return osm_network_from_custom_filters
337369
else
338370
throw(ArgumentError("OSM network downloader $download_method does not exist"))
339371
end
@@ -375,6 +407,12 @@ Downloads an OpenStreetMap network by querying with a place name, bounding box,
375407
*`download_method=:polygon`*
376408
- `polygon::AbstractVector`: Vector of longitude-latitude pairs.
377409
410+
*`download_method=:custom_filters`*
411+
- `custom_filters::String`: Filters for the query, e.g. polygon filter, highways only, traffic lights only, etc.
412+
- `metadata::Bool=false`: Set true to return metadata.
413+
- `download_format::Symbol=:json`: Download format, either `:osm`, `:xml` or `json`.
414+
- `bbox::Union{Vector{AbstractFloat},Nothing}=nothing`: Optional bounding box filter.
415+
378416
# Network Types
379417
- `:drive`: Motorways excluding private and service ways.
380418
- `:drive_service`: Motorways including private and service ways.

test/download.jl

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
@test LightOSM.osm_network_downloader(:bbox) == LightOSM.osm_network_from_bbox
44
@test LightOSM.osm_network_downloader(:point) == LightOSM.osm_network_from_point
55
@test LightOSM.osm_network_downloader(:polygon) == LightOSM.osm_network_from_polygon
6+
@test LightOSM.osm_network_downloader(:custom_filters) == LightOSM.osm_network_from_custom_filters
67
@test_throws ArgumentError LightOSM.osm_network_downloader(:doesnt_exist)
78
end
89

@@ -23,22 +24,54 @@ end
2324
try
2425
wait_for_overpass()
2526
data = download_osm_network(:point,
26-
radius=0.5,
27-
point=GeoLocation(-37.8136, 144.9631),
28-
network_type=:drive,
29-
download_format=format,
30-
save_to_file_location=filename);
27+
radius=0.5,
28+
point=GeoLocation(-37.8136, 144.9631),
29+
network_type=:drive,
30+
download_format=format,
31+
save_to_file_location=filename)
3132
@test isfile(filename)
3233
g = graph_from_file(filename) # Check it doesn't error
3334
catch err
3435
# Sometimes gets HTTP.ExceptionRequest.StatusError in tests due to connection to overpass
3536
!isa(err, HTTP.ExceptionRequest.StatusError) && rethrow()
36-
@error "Test failed due to connection issue" exception=(err, catch_backtrace())
37+
@error "Test failed due to connection issue" exception = (err, catch_backtrace())
3738
end
3839

3940
try
4041
rm(filename)
4142
catch
4243
end
4344
end
45+
46+
#run test for custom filters
47+
try
48+
wait_for_overpass()
49+
filename = "map.json"
50+
format = :json
51+
custom_filters = """way
52+
["highway"]
53+
["motorcar"!~"no"]
54+
["area"!~"yes"]
55+
["highway"!~"elevator|steps|tertiary|construction|bridleway|proposed|track|pedestrian|secondary|path|living_street|cycleway|primary|footway|platform|abandoned|service|escalator|corridor|raceway"]
56+
["motor_vehicle"!~"no"]["access"!~"private"]
57+
["service"!~"parking|parking_aisle|driveway|private|emergency_access"]
58+
;
59+
>
60+
;
61+
62+
"""
63+
64+
bbox = [-37.816779513558274, 144.9590750877158, -37.81042034950731, 144.967124565619]
65+
66+
test_custom_query = download_osm_network(:custom_filters, download_format=format,
67+
save_to_file_location=filename,
68+
custom_filters = custom_filters, bbox = bbox)
69+
70+
71+
g = graph_from_file(filename)
72+
catch err
73+
74+
@warn "Could not build graph"
75+
rethrow()
76+
end
4477
end

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ include("stub.jl")
1111

1212
const TEST_OSM_URL = "https://raw.githubusercontent.com/DeloitteOptimalReality/LightOSMFiles.jl/main/maps/south-yarra.json"
1313

14-
@testset "LightOSM Tests" begin
14+
@testset "LightOSM Tests" begin
1515
@testset "Constants" begin include("constants.jl") end
1616
@testset "Utilities" begin include("utilities.jl") end
1717
@testset "Geometry" begin include("geometry.jl") end

0 commit comments

Comments
 (0)