Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
ba
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
markn92
ba
Commits
a37827ee
Commit
a37827ee
authored
4 years ago
by
markn92
Browse files
Options
Downloads
Patches
Plain Diff
add rtree to graph itself
parent
7c813ea7
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
evrouting/osm.py
+5
-16
5 additions, 16 deletions
evrouting/osm.py
tests/osm/test_osm_charge.py
+30
-20
30 additions, 20 deletions
tests/osm/test_osm_charge.py
with
35 additions
and
36 deletions
evrouting/osm.py
+
5
−
16
View file @
a37827ee
...
...
@@ -37,25 +37,11 @@ def query_url(service, coordinates, osrm_config: OsrmConf):
f
'
{
"
;
"
.
join
([
f
"
{
lon
}
,
{
lat
}
"
for lat, lon in coordinates])
}
'
def
insert_charging_stations
(
G
,
charging_stations
,
osrm_conf
=
None
):
osrm_conf
=
osrm_conf
or
OsrmConf
(
server
=
'
localhost
'
,
port
=
5000
)
index
=
rtree
.
index
.
Index
()
for
n
in
G
.
nodes
:
lon
=
G
.
nodes
[
n
][
'
lon
'
]
lat
=
G
.
nodes
[
n
][
'
lat
'
]
# insert point
index
.
insert
(
n
,
(
lon
,
lat
,
lon
,
lat
))
def
insert_charging_stations
(
G
,
charging_stations
):
for
s
in
charging_stations
:
lon
=
s
[
'
lon
'
]
lat
=
s
[
'
lat
'
]
n
=
list
(
index
.
nearest
((
lon
,
lat
,
lon
,
lat
),
1
))[
0
]
n
=
list
(
G
.
rtree
.
nearest
((
lon
,
lat
,
lon
,
lat
),
1
))[
0
]
G
.
nodes
[
n
][
CHARGING_COEFFICIENT_KEY
]
=
s
[
'
power
'
]
return
G
...
...
@@ -136,12 +122,15 @@ def read_osm(osm_xml_data, only_roads=True) -> nx.DiGraph:
nx
.
add_path
(
G
,
w
.
nds
,
id
=
w
.
id
)
nx
.
add_path
(
G
,
w
.
nds
[::
-
1
],
id
=
w
.
id
)
G
.
rtree
=
rtree
.
index
.
Index
()
# Complete the used nodes' information
for
n_id
in
G
.
nodes
():
n
=
osm
.
nodes
[
n_id
]
G
.
nodes
[
n_id
][
'
lat
'
]
=
n
.
lat
G
.
nodes
[
n_id
][
'
lon
'
]
=
n
.
lon
G
.
nodes
[
n_id
][
'
id
'
]
=
n
.
id
G
.
rtree
.
insert
(
n_id
,
(
n
.
lat
,
n
.
lon
,
n
.
lat
,
n
.
lon
))
return
G
...
...
This diff is collapsed.
Click to expand it.
tests/osm/test_osm_charge.py
+
30
−
20
View file @
a37827ee
import
os
import
pytest
import
networkx
as
nx
import
rtree
from
evrouting.osm
import
read_osm
,
insert_charging_stations
from
evrouting.graph_tools
import
CHARGING_COEFFICIENT_KEY
@pytest.fixture
def
graph
():
G
=
nx
.
DiGraph
()
G
.
rtree
=
rtree
.
index
.
Index
()
node_coordinates
=
[
(
51.7705832
,
7.0002595
),
(
51.7696529
,
6.9568520
)
]
for
n_id
,
coordinates
in
enumerate
(
node_coordinates
):
lat
,
lon
=
coordinates
# Add two nodes, that exist in osm test map
G
.
add_node
(
n_id
,
lat
=
lat
,
lon
=
lon
)
G
.
rtree
.
insert
(
n_id
,
(
lon
,
lat
,
lon
,
lat
))
yield
G
del
G
def
_test_read_osm
():
"""
Just check if it runs. Todo: Delete.
"""
assert
read_osm
(
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
'
static/map.osm
'
))
def
test_insert_charging_stations_close
():
G
=
nx
.
DiGraph
()
# Add two nodes, that exist in osm test map
G
.
add_node
(
0
,
lat
=
51.7705832
,
lon
=
7.0002595
)
G
.
add_node
(
1
,
lat
=
51.7696529
,
lon
=
6.9568520
)
def
test_insert_charging_stations_close
(
graph
):
# Close two node 1
S
=
[{
"
lon
"
:
7.0002593
,
"
lat
"
:
51.7705832
,
"
power
"
:
22.0
}]
G
=
insert_charging_stations
(
G
,
S
)
graph
=
insert_charging_stations
(
graph
,
S
)
assert
G
.
nodes
[
0
][
CHARGING_COEFFICIENT_KEY
]
==
22.0
assert
CHARGING_COEFFICIENT_KEY
not
in
G
.
nodes
[
1
]
def
test_insert_charging_stations_eq
():
G
=
nx
.
DiGraph
()
assert
graph
.
nodes
[
0
][
CHARGING_COEFFICIENT_KEY
]
==
22.0
assert
CHARGING_COEFFICIENT_KEY
not
in
graph
.
nodes
[
1
]
# Add two nodes, that exist in osm test map
G
.
add_node
(
0
,
lat
=
51.7705832
,
lon
=
7.0002595
)
G
.
add_node
(
1
,
lat
=
51.7696529
,
lon
=
6.9568520
)
def
test_insert_charging_stations_eq
(
graph
):
# Close exactly at node 1
S
=
[{
"
lon
"
:
7.0002595
,
"
lat
"
:
51.7705832
,
"
power
"
:
22.0
}]
G
=
insert_charging_stations
(
G
,
S
)
graph
=
insert_charging_stations
(
graph
,
S
)
assert
G
.
nodes
[
0
][
CHARGING_COEFFICIENT_KEY
]
==
22.0
assert
CHARGING_COEFFICIENT_KEY
not
in
G
.
nodes
[
1
]
assert
graph
.
nodes
[
0
][
CHARGING_COEFFICIENT_KEY
]
==
22.0
assert
CHARGING_COEFFICIENT_KEY
not
in
graph
.
nodes
[
1
]
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment