Skip to content

Commit

Permalink
add trim_to_connected_graph for map parser and cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
pszufe committed Jul 4, 2020
1 parent 1723d25 commit 5c7c898
Show file tree
Hide file tree
Showing 14 changed files with 301 additions and 166 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "OpenStreetMapX"
uuid = "86cd37e6-c0ff-550b-95fe-21d72c8d4fc9"
authors = ["Przemyslaw Szufel <[email protected]>", "Bartosz Pankratz <[email protected]>", "Anna Szczurek <[email protected]>", "Bogumil Kaminski <[email protected]>", "Pawel Pralat <[email protected]>"]
version = "0.1.12"
version = "0.1.13"

[deps]
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
Expand Down
1 change: 1 addition & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[deps]
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
OpenStreetMapX = "86cd37e6-c0ff-550b-95fe-21d72c8d4fc9"
2 changes: 2 additions & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Documenter
using Pkg

try
using OpenStreetMapX
catch
Expand Down
1 change: 1 addition & 0 deletions docs/src/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Bounds
center
inbounds
onbounds
latlon
```


Expand Down
59 changes: 59 additions & 0 deletions samples/plotting_with_folium.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using OpenStreetMapX, LightGraphs, PyCall

# This code assumes that folium has benn installed


function plot_map(m::MapData, filename::AbstractString; tiles="Stamen Toner" )
MAP_BOUNDS = [ ( m.bounds.min_y, m.bounds.min_x), ( m.bounds.max_y, m.bounds.max_x) ]
flm = pyimport("folium")
m_plot = flm.Map(tiles=tiles)
for e in edges(m.g)
info = "Edge from: $(e.src) to $(e.dst)<br>[information from the <pre>.e</pre> and <pre>.w</pre> fields] "
flm.PolyLine( (latlon(m,e.src), latlon(m,e.dst)),
color="brown", weight=4, opacity=1).add_to(m_plot)
end

for n in keys(m.nodes)
lla = LLA(m.nodes[n],m.bounds)
info = "Node: $(n)\n<br>Lattitude: $(lla.lat)\n<br>Longitude: $(lla.lon)<br>[information from the <pre>.node</pre> field] "
flm.Circle(
(lla.lat, lla.lon),
popup=info,
tooltip=info,
radius=10,
color="orange",
weight=3,
fill=true,
fill_color="orange"
).add_to(m_plot)
end

for nn in keys(m.n)
n = m.n[nn]
lla = LLA(m.nodes[n],m.bounds)
info = "Graph: $nn <br>Node: $(n)\n<br>Lattitude: $(lla.lat)\n<br>Longitude: $(lla.lon)<br>
[The node identifiers are hold in the <pre>.n</pre> field and location in the <pre>.nodes</pre> field]"
flm.Rectangle(
[(lla.lat-0.00014, lla.lon-0.0002), (lla.lat+0.00014, lla.lon+0.0002)],
popup=info,
tooltip=info,
color="green",
weight=1.5,
fill=false,
fill_opacity=0.2,
fill_color="green",
).add_to(m_plot)
end


MAP_BOUNDS = [( m.bounds.min_y, m.bounds.min_x),( m.bounds.max_y, m.bounds.max_x)]
flm.Rectangle(MAP_BOUNDS, color="black",weight=4).add_to(m_plot)
m_plot.fit_bounds(MAP_BOUNDS)
m_plot.save(filename)
end

pth = joinpath(dirname(pathof(OpenStreetMapX)),"..","test","data","reno_east3.osm")

m2 = OpenStreetMapX.get_map_data(pth,use_cache = false, trim_to_connected_graph=true);

plot_map(m2, "mymap.html")
8 changes: 4 additions & 4 deletions samples/routing.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using OpenStreetMapX

mapfile = "reno_east3.osm"; # This file can be found in test/data folder
datapath = "/home/ubuntu/";
map_data = get_map_data(datapath, mapfile,use_cache=false);

pth = joinpath(dirname(pathof(OpenStreetMapX)),"..","test","data","reno_east3.osm")
map_data = OpenStreetMapX.get_map_data(pth,use_cache = false);

using Random
Random.seed!(0);
Expand All @@ -21,7 +21,7 @@ println("fastest route nodes: ",fastest_route1)

### Create this file if you want to test routing with Google API
### The file should only contain your Google API key
google_api_file = joinpath(datapath,"googleapi.key")
google_api_file = joinpath("some_folder","googleapi.key")

if isfile(google_api_file)
google_api_key = readlines(google_api_file)[1]
Expand Down
3 changes: 3 additions & 0 deletions src/OpenStreetMapX.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ export get_google_route
export encode, decode
export generate_point_in_bounds, point_to_nodes

export latlon

export ROAD_CLASSES, CYCLE_CLASSES, PED_CLASSES, SPEED_ROADS_URBAN, SPEED_ROADS_RURAL



include("types.jl") #types used in the package
include("classes.jl") #grouping highways into classes for routing and plotting
include("speeds.jl") # speed limits in kilometers per hour
Expand Down
12 changes: 12 additions & 0 deletions src/conversion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,15 @@ Converts a dictionary of `ENU` `nodes` into a dictionary of `LLA` values.
Uses the center of the given `bounds` for linearization.
"""
LLA(nodes::Dict{Int,ENU}, bounds::Bounds{LLA}, datum::OpenStreetMapX.Ellipsoid = OpenStreetMapX.WGS84) = LLA(nodes, OpenStreetMapX.center(bounds), datum)

"""
latlon(m::MapData,map_g_point_id::Int64)::Tuple{Float64, Float64}
Returns a tuple of lattitute and longitude for a given graph node identifier
`map_g_point_id` in graph `m.g` (i.e. `map_g_point_id ∈ 1:nv(m.g)`).
"""
function latlon(m::MapData,map_g_point_id::Int64)::Tuple{Float64, Float64}
osm_node_ix = m.n[map_g_point_id]
lla = LLA(m.nodes[osm_node_ix], m.bounds)
return (lla.lat, lla.lon)
end
29 changes: 16 additions & 13 deletions src/intersections.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
### Auxiliary Functions ###
###########################

### Check if Way is One - Way ###

"""
Check if Way is One - Way
"""
function oneway(w::OpenStreetMapX.Way)
v = get(w.tags,"oneway", "")
if v == "false" || v == "no" || v == "0"
Expand All @@ -16,23 +18,25 @@ function oneway(w::OpenStreetMapX.Way)
return (highway == "motorway" || highway == "motorway_link" || junction == "roundabout")
end

### Check if Way is Reverse ###

"""
Check if Way is Reverse
"""
reverseway(w::OpenStreetMapX.Way) = (get(w.tags,"oneway", "") == "-1")

### Compute the distance of a route ###

"""
Compute the distance of a route for some `nodes` data
"""
function distance(nodes::Dict{Int,T}, route::Vector{Int}) where T<:(Union{OpenStreetMapX.ENU,OpenStreetMapX.ECEF})
if length(route) == 0
return Inf
end
dist = sum(distance(nodes[route[i-1]],nodes[route[i]]) for i = 2:length(route))
end

######################################
### Find Intersections of Highways ###
######################################

"""
Find Intersections of Highways ###
"""
function find_intersections(highways::Vector{OpenStreetMapX.Way})
seen = Set{Int}()
intersections = Dict{Int,Set{Int}}()
Expand All @@ -55,10 +59,9 @@ function find_intersections(highways::Vector{OpenStreetMapX.Way})
return intersections
end

#################################
### Find Segments of Highways ###
#################################

"""
Find Segments of Highways ###
"""
function find_segments(nodes::Dict{Int,T}, highways::Vector{OpenStreetMapX.Way}, intersections::Dict{Int,Set{Int}}) where T<:Union{OpenStreetMapX.ENU,OpenStreetMapX.ECEF}
segments = OpenStreetMapX.Segment[]
intersect = Set(keys(intersections))
Expand All @@ -82,4 +85,4 @@ function find_segments(nodes::Dict{Int,T}, highways::Vector{OpenStreetMapX.Way},
end
end
return segments
end
end
45 changes: 24 additions & 21 deletions src/nodes.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
######################
### Add a New Node ###
######################

"""
Add a New Node
"""
function add_new_node!(nodes::Dict{Int,T},loc::T, start_id::Int = reinterpret((Int), hash(loc))) where T <: (Union{OpenStreetMapX.LLA,OpenStreetMapX.ENU})
id = start_id
while id <= typemax(Int)
Expand All @@ -16,12 +15,11 @@ function add_new_node!(nodes::Dict{Int,T},loc::T, start_id::Int = reinterpret((I
throw(error(msg))
end

#############################
### Find the Nearest Node ###
#############################


### Find the nearest node to a given location ###
"""
Find the nearest node to a given location `loc`
"""
function nearest_node(nodes::Dict{Int,T}, loc::T) where T<:(Union{OpenStreetMapX.ENU,OpenStreetMapX.ECEF})
min_dist = Inf
best_ind = 0
Expand All @@ -37,12 +35,16 @@ function nearest_node(nodes::Dict{Int,T}, loc::T) where T<:(Union{OpenStreetMapX
return best_ind
end

"""
Find the nearest node to a given location `loc`
"""
function nearest_node(m::MapData, loc::ENU, vs_only::Bool=true)
vs_only ? nearest_node(m.nodes,loc, keys(m.v)) : nearest_node(m.nodes,loc)
end


### Find nearest node in a list of nodes ###
"""
Find the nearest node in a list of nodes
"""
function nearest_node(nodes::Dict{Int,T}, loc::T, node_list::AbstractSet{Int}) where T<:(Union{OpenStreetMapX.ENU,OpenStreetMapX.ECEF})
min_dist = Inf
best_ind = 0
Expand All @@ -64,11 +66,9 @@ end
#nearest_node(loc::T, m::OpenStreetMapX.MapData) where T<:(Union{OpenStreetMapX.ENU,OpenStreetMapX.ECEF}) = OpenStreetMapX.nearest_node(m.nodes,loc,collect(keys(m.v)))


#############################
### Find Node Within Range###
#############################

### Find all nodes within range of a location ###
"""
Find all nodes within range of a location
"""
function nodes_within_range(nodes::Dict{Int,T}, loc::T, range::Float64 = Inf) where T<:(Union{OpenStreetMapX.ENU,OpenStreetMapX.ECEF})
if range == Inf
return keys(nodes)
Expand All @@ -83,7 +83,9 @@ function nodes_within_range(nodes::Dict{Int,T}, loc::T, range::Float64 = Inf) wh
return indices
end

### Find nodes within range of a location using a subset of nodes ###
"""
Find nodes within range of a location using a subset of nodes
"""
function nodes_within_range(nodes::Dict{Int,T}, loc::T, node_list::AbstractSet{Int}, range::Float64 = Inf) where T<:(Union{OpenStreetMapX.ENU,OpenStreetMapX.ECEF})
if range == Inf
return node_list
Expand All @@ -98,13 +100,14 @@ function nodes_within_range(nodes::Dict{Int,T}, loc::T, node_list::AbstractSet{I
return indices
end

### Find vertices of a routing network within range of a location ###
"""
Find vertices of a routing network within range of a location
"""
nodes_within_range(nodes::Dict{Int,T},loc::T, m::OpenStreetMapX.MapData, range::Float64 = Inf) where T <:(Union{OpenStreetMapX.ENU,OpenStreetMapX.ECEF}) = OpenStreetMapX.nodes_within_range(nodes,loc,collect(keys(m.v)),range)

#########################################
### Compute Centroid of List of Nodes ###
#########################################

"""
Compute Centroid of List of Nodes
"""
function centroid(nodes::Dict{Int,T}, node_list::Vector{Int}) where T<:(Union{OpenStreetMapX.LLA,OpenStreetMapX.ENU})
sum_1 = 0
sum_2 = 0
Expand Down
Loading

2 comments on commit 5c7c898

@pszufe
Copy link
Owner Author

@pszufe pszufe commented on 5c7c898 Jul 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/17418

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.13 -m "<description of version>" 5c7c8985c025567842a61015682bab8038f36d80
git push origin v0.1.13

Please sign in to comment.