Skip to content

Commit

Permalink
upd (*) style, cleaning, tests, hint
Browse files Browse the repository at this point in the history
  • Loading branch information
amauryval committed Aug 29, 2020
1 parent bc24329 commit b848f8a
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 155 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ OsmGT
[![codecov](https://codecov.io/gh/amauryval/osmgt/branch/master/graph/badge.svg)](https://codecov.io/gh/wiralyki/osmgt)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)

OpenStreetMap network analysis based on graph-tools:
OpenStreetMap (OSM) network analysis based on graph-tools (GT):
* Find data (roads and pois)
* graph creation (and topology processing)
* isochrone builder
Expand Down
4 changes: 2 additions & 2 deletions example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,7 @@
" \"input_gdf\": isochrones_polygons_from_location,\n",
" \"legend\": \"iso_name\",\n",
" \"fill_color\": \"color\",\n",
" \"line_color\": \"white\",\n",
" \"line_color\": \"color\",\n",
" \"fill_alpha\": 0.7\n",
" },\n",
" {\n",
Expand Down Expand Up @@ -1139,4 +1139,4 @@
},
"nbformat": 4,
"nbformat_minor": 1
}
}
201 changes: 101 additions & 100 deletions index.html

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions osmgt/compoments/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class OsmGtCore(Logger):

_QUERY_ELEMENTS_FIELD: str = "elements"
__USELESS_COLUMNS: List = []
_location_id: None = None
_location_id: Optional[int] = None

_NOMINATIM_DEFAULT_ID: int = 3600000000 # this is it
_NOMINATIM_OSM_ID_FIELD: str = "osm_id"
Expand All @@ -59,13 +59,13 @@ class OsmGtCore(Logger):
def __init__(self) -> None:
super().__init__()

self.study_area_geom: None
self.study_area_geom: Optional[Polygon] = None

self._output_data: Optional[Union[gpd.geodataframe, List[Dict]]]
self._output_data: Optional[Union[gpd.geodataframe, List[Dict]]] = None
self._bbox_value: Optional[Tuple[float, float, float, float]] = None
self._bbox_mode: bool = False

def from_location(self, location_name: str) -> None:
def from_location(self, location_name: str, *args) -> None:
self.logger.info(f"From location: {location_name}")
self.logger.info("Loading data...")

Expand All @@ -92,7 +92,7 @@ def from_bbox(self, bbox_value: Tuple[float, float, float, float]) -> None:
self._bbox_mode: bool = True
self.logger.info(f"From bbox: {bbox_value}")
self.logger.info("Loading data...")
self.study_area_geom: Polygon = box(*bbox_value, ccw=True)
self.study_area_geom = box(*bbox_value, ccw=True)
# reordered because of nominatim
self._bbox_value = (bbox_value[1], bbox_value[0], bbox_value[3], bbox_value[2])

Expand Down
20 changes: 13 additions & 7 deletions osmgt/compoments/roads.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import List
from typing import Optional
from typing import Dict
from typing import Iterator


from osmgt.compoments.core import OsmGtCore
Expand Down Expand Up @@ -79,17 +80,22 @@ def get_graph(self) -> GraphHelpers:
self.logger.info("Prepare graph")
self._check_build_input_data()

graph = GraphHelpers(is_directed=network_queries[self._mode]["directed_graph"])
graph = GraphHelpers(self.logger, is_directed=network_queries[self._mode]["directed_graph"])

for feature in self._output_data:
graph.add_edge(
Point(feature[self._GEOMETRY_FIELD].coords[0]).wkt,
Point(feature[self._GEOMETRY_FIELD].coords[-1]).wkt,
feature[self._TOPO_FIELD],
compute_wg84_line_length(shape(feature[self._GEOMETRY_FIELD])),
)
graph.add_edge(*self.__compute_edges(feature))

return graph

def __compute_edges(self, feature: Dict) -> Tuple[str, str, str, float]:
coordinates = feature[self._GEOMETRY_FIELD]
return (
Point(coordinates.coords[0]).wkt,
Point(coordinates.coords[-1]).wkt,
feature[self._TOPO_FIELD],
compute_wg84_line_length(shape(coordinates))
)

def __build_network_topology(
self,
raw_data: List[Dict],
Expand Down
2 changes: 1 addition & 1 deletion osmgt/geometry/network_topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ def find_intersections_from_ways(self) -> Set[Tuple[float, float]]:

def __rtree_generator_func(
self,
) -> Generator[int, Tuple[float, float, float, float], None]:
) -> Iterator[Tuple[int, Tuple[str, str, str, float], None]]:
for fid, feature in self._network_data.items():
# fid is an integer
yield fid, feature[self.__GEOMETRY_FIELD].bounds, None
Expand Down
7 changes: 5 additions & 2 deletions osmgt/network/gt_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@ class GraphHelpers(Graph):
- find_vertex_names_from_edge_name()
"""

def __init__(self, is_directed: bool = True) -> None:
def __init__(self, logger, is_directed: bool = True) -> None:
"""
:param logger: logger
:type logger:
:param is_directed: is directed or not
:type is_directed: bool
"""
super(GraphHelpers, self).__init__(directed=is_directed)

self._logger = logger
self.vertex_names = self.new_vertex_property("string")
self.edge_names = self.new_edge_property("string")

Expand Down Expand Up @@ -246,7 +249,7 @@ def vertex_exists_from_name(self, vertex_name: str) -> bool:
return self.find_vertex_from_name(vertex_name) is not None

def plot(self, output_file_with_extension: Optional[str] = None):

self._logger.info("Graph to PNG file")
pos = sfdp_layout(self)
graph_draw(
self,
Expand Down
14 changes: 10 additions & 4 deletions osmgt/processing/isochrone.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
pass

from shapely.wkt import loads
from shapely.geometry import base
from shapely.geometry import Point

from osmgt.geometry.geom_helpers import Concave_hull
Expand Down Expand Up @@ -130,17 +131,22 @@ def get_gdf(self, verbose: bool = True) -> gpd.GeoDataFrame:
output = super().get_gdf()
# find iso index pair in order to create hole geom. isochrones are like russian dolls
iso_values = self._raw_isochrones[::-1]
iso_values_map = {x[0]: x[-1] for x in list(zip(iso_values, iso_values[1:]))}
iso_values_map: Dict = {x[0]: x[-1] for x in list(zip(iso_values, iso_values[1:]))}
output["geometry"] = output.apply(
lambda x: x["geometry"].difference(
lambda x: self.__compute_isochrone_difference(
x["geometry"],
output.loc[
output[self.__ISOCHRONE_NAME_FIELD]
== iso_values_map[x[self.__ISOCHRONE_NAME_FIELD]]
].iloc[0]["geometry"]
].iloc[0]['geometry']
)
if x[self.__ISOCHRONE_NAME_FIELD] in iso_values_map
else x["geometry"],
axis=1,
axis=1
)

return output

def __compute_isochrone_difference(self, first_geom: base, remove_part_geom: base) -> base:

return first_geom.difference(remove_part_geom)
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
author="amauryval",
author_email='amauryval@gmail.com',
url="https://github.com/amauryval/osmgt",
version='0.6.0',
description="A library to play with OSM roads and POIs data using graph tool network library",
version='0.6.1',
description="A library to play with OSM roads (and POIs) data using graph tool network library",
entry_points={},
install_requires=requirements,
license="GPL3",
Expand Down
7 changes: 3 additions & 4 deletions tests/fixtures/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ def some_line_features():
(4.07132541293213457, 46.03636692445581957),
(4.07195460627399886, 46.0366250550576126),
(4.07215358194621313, 46.03668152112675216),
(4.0724305345710512, 46.03630508066581228)
(4.0724305345710512, 46.03630508066581228),
]
),
"properties": {"uuid": 12, "id": "12", "oneway": "yes"},
}
},
]
output_gdf = build_geojson_features(all_features)

Expand Down Expand Up @@ -145,8 +145,7 @@ def some_point_features():
{
"geometry": Point((4.07101188185213569, 46.0373516329414727)),
"properties": {"uuid": 9, "id": "9"},
}

},
]
output_gdf = build_geojson_features(all_features)

Expand Down
48 changes: 27 additions & 21 deletions tests/test_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,40 @@

def test_connect_lines(some_line_features, some_point_features):
raw_data_topology_rebuild = NetworkTopology(
OsmGtCore().logger, some_line_features, some_point_features, "uuid", "pedestrian"
OsmGtCore().logger,
some_line_features,
some_point_features,
"uuid",
"pedestrian",
).run()
all_uuid = [feature["uuid"] for feature in raw_data_topology_rebuild]

assert len(raw_data_topology_rebuild) == 18
# check duplicated
assert len(all_uuid) == len(all_uuid)
assert len(all_uuid) == len(set(all_uuid))
assert sorted(all_uuid) == sorted([
'10_0',
'10_1',
'10_2',
'10_3',
'10_4',
'10_5',
'10_6',
'10_7',
'11_0',
'11_1',
'12',
'added_1',
'added_2',
'added_3',
'added_6',
'added_7',
'added_8',
'added_9'
])
assert sorted(all_uuid) == sorted(
[
"10_0",
"10_1",
"10_2",
"10_3",
"10_4",
"10_5",
"10_6",
"10_7",
"11_0",
"11_1",
"12",
"added_1",
"added_2",
"added_3",
"added_6",
"added_7",
"added_8",
"added_9",
]
)

for feature in raw_data_topology_rebuild:
if feature["topology"] == "unchanged":
Expand Down
11 changes: 8 additions & 3 deletions tests/test_graph.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import pytest

from osmgt.helpers.logger import Logger

from osmgt.network.gt_helper import GraphHelpers
from osmgt.network.gt_helper import ExistingVertex


def init_logger():
return Logger(logger_name="graph_test").logger

def create_undirected_graph(point_a, point_b, point_c):
graph = GraphHelpers()
graph = GraphHelpers(init_logger())
edge_1 = graph.add_edge(point_a.wkt, point_b.wkt, "edge_1")
edge_2 = graph.add_edge(point_b.wkt, point_c.wkt, "edge_2")
edge_3 = graph.add_edge(point_b.wkt, point_c.wkt, "edge_2")
Expand All @@ -14,7 +19,7 @@ def create_undirected_graph(point_a, point_b, point_c):


def create_weighted_undirected_graph(point_a, point_b, point_c):
graph = GraphHelpers()
graph = GraphHelpers(init_logger())
edge_1 = graph.add_edge(point_a.wkt, point_b.wkt, "edge_1", 10.2)
edge_2 = graph.add_edge(point_b.wkt, point_c.wkt, "edge_2", 15.9)
edge_3 = graph.add_edge(point_b.wkt, point_c.wkt, "edge_2", 25)
Expand All @@ -23,7 +28,7 @@ def create_weighted_undirected_graph(point_a, point_b, point_c):


def test_create_vertices(point_a, point_b):
graph = GraphHelpers()
graph = GraphHelpers(init_logger())

vertex_a = graph.add_vertex(point_a.wkt)
vertex_b = graph.add_vertex(point_b.wkt)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def test_if_isochrones_can_be_computed(location_point, isochrone_values):

assert isochrones_lines_from_location.shape[0] > 0
assert set(isochrones_lines_from_location["iso_name"].to_list()) == set(
["2 minutes" , "5 minutes" , "10 minutes"]
["2 minutes", "5 minutes", "10 minutes"]
)


Expand All @@ -195,8 +195,8 @@ def test_if_shortest_path_can_be_computed(start_node, end_node):
(start_node, end_node),
(start_node, end_node),
(start_node, end_node),
(start_node, end_node)
(start_node, end_node),
],
mode="pedestrian"
mode="pedestrian",
)
assert shortest_paths.shape[0] == 1

0 comments on commit b848f8a

Please sign in to comment.