Skip to content

Commit

Permalink
Merge branch 'master' into fix/192-fix-traffic-analysis-for-nodes-att…
Browse files Browse the repository at this point in the history
…ached-to-multiple-nodes
  • Loading branch information
Carsopre authored Nov 8, 2023
2 parents 6c2db78 + c510377 commit 20df818
Show file tree
Hide file tree
Showing 35 changed files with 77 additions and 80 deletions.
14 changes: 7 additions & 7 deletions docs/user_guide/user_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ Initialization file templates
polygon = map.geojson # <name + file extension of the geojson polygon file in the static/network folder> / None
network_type = drive # drive / walk / bike / drive_service / all
road_types = motorway,motorway_link,trunk,trunk_link,primary, primary_link,secondary,secondary_link,tertiary,tertiary_link
save_shp = True # True / False
save_gpkg = True # True / False

[origins_destinations]
origins = origins.shp # <file name> / None
Expand Down Expand Up @@ -159,7 +159,7 @@ Initialization file templates
name = single link redundancy test
analysis = single_link_redundancy
weighing = distance
save_shp = True
save_gpkg = True
save_csv = True

[analysis2]
Expand All @@ -168,14 +168,14 @@ Initialization file templates
aggregate_wl = max
threshold = 0.5
weighing = distance
save_shp = True
save_gpkg = True
save_csv = True

[analysis3]
name = optimal origin dest test
analysis = optimal_route_origin_destination
weighing = distance
save_shp = True
save_gpkg = True
save_csv = True

[analysis4]
Expand All @@ -184,7 +184,7 @@ Initialization file templates
aggregate_wl = max
threshold = 0.5
weighing = distance
save_shp = True
save_gpkg = True
save_csv = False

[analysis5]
Expand All @@ -193,7 +193,7 @@ Initialization file templates
aggregate_wl = max
threshold = 0.5
weighing = distance
save_shp = True
save_gpkg = True
save_csv = True

[analysis6]
Expand All @@ -204,6 +204,6 @@ Initialization file templates
weighing = length
buffer_meters = 40
category_field_name = category
save_shp = True
save_gpkg = True
save_csv = True

2 changes: 1 addition & 1 deletion examples/example_origin_destination_analysis.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4133,7 +4133,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
"version": "3.10.12"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion examples/example_ra2ce_basics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
"version": "3.10.12"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion examples/example_single_link_redundancy.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2067,7 +2067,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
"version": "3.10.12"
}
},
"nbformat": 4,
Expand Down
4 changes: 2 additions & 2 deletions examples/pizza_course_material_DIY.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
"**polygon = ...<br>**\n",
"network_type = drive<br>\n",
"**road_types = ...<br>**\n",
"save_shp = True<br>\n",
"save_gpkg = True<br>\n",
"<br>"
]
},
Expand Down Expand Up @@ -301,7 +301,7 @@
"name = beira_redundancy <br>\n",
"analysis = single_link_redundancy <br>\n",
"weighing = distance <br>\n",
"save_shp = True <br>\n",
"save_gpkg = True <br>\n",
"save_csv = True <br>"
]
},
Expand Down
6 changes: 3 additions & 3 deletions ra2ce/analyses/direct/analyses_direct.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ def execute(self):
gdf = []

output_path = self.config["output"] / analysis["analysis"]
if analysis["save_shp"]:
shp_path = output_path / (analysis["name"].replace(" ", "_") + ".shp")
if analysis["save_gpkg"]:
shp_path = output_path / (analysis["name"].replace(" ", "_") + ".gpkg")
save_gdf(gdf, shp_path)
if analysis["save_csv"]:
csv_path = output_path / (analysis["name"].replace(" ", "_") + ".csv")
Expand Down Expand Up @@ -187,7 +187,7 @@ def effectiveness_measures(self, analysis):
else:
df = em.load_table(
self.config["input"] / "direct",
analysis["file_name"].replace(".shp", ".csv"),
analysis["file_name"].replace(".gpkg", ".csv"),
)

df = em.calculate_strategy_effectiveness(df, effectiveness_dict)
Expand Down
2 changes: 1 addition & 1 deletion ra2ce/analyses/direct/cost_benefit_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def create_feature_table(file_path):

# save as csv
path, file = os.path.split(file_path)
df.to_csv(os.path.join(path, file.replace(".shp", ".csv")), index=False)
df.to_csv(os.path.join(path, file.replace(".gpkg", ".csv")), index=False)
return df

@staticmethod
Expand Down
25 changes: 11 additions & 14 deletions ra2ce/analyses/indirect/analyses_indirect.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,10 +779,8 @@ def multi_link_origin_destination_regional_impact(self, gdf_ori):
gdf_ori_ = gdf_ori.copy()

# read origin points
origin_fn = (
Path(self.config["static"])
/ "output_graph"
/ "origin_destination_table.gpkg"
origin_fn = Path(self.config["static"]).joinpath(
"output_graph", "origin_destination_table.gpkg"
)
origin = gpd.read_file(origin_fn)
index = [type(x) == str for x in origin["o_id"]]
Expand Down Expand Up @@ -917,7 +915,7 @@ def multi_link_isolated_locations(
results_hz_roads.to_file(
self.config["output"]
/ analysis["analysis"]
/ f"flooded_and_isolated_roads_{hazard_name}.shp"
/ f"flooded_and_isolated_roads_{hazard_name}.gpkg"
)

# relate the locations to network disruption due to hazard by spatial overlay
Expand Down Expand Up @@ -1036,7 +1034,7 @@ def execute(self):
opt_routes = None
output_path = self.config["output"] / analysis["analysis"]

def _save_shp_analysis(
def _save_gpkg_analysis(
base_graph,
to_save_gdf: List[gpd.GeoDataFrame],
to_save_gdf_names: List[str],
Expand Down Expand Up @@ -1165,11 +1163,11 @@ def _save_shp_analysis(
opt_routes,
destinations,
) = analyzer.optimal_route_origin_closest_destination()
if analysis["save_shp"]:
if analysis["save_gpkg"]:
# Save the GeoDataFrames
to_save_gdf = [destinations, opt_routes]
to_save_gdf_names = ["destinations", "optimal_routes"]
_save_shp_analysis(base_graph, to_save_gdf, to_save_gdf_names)
_save_gpkg_analysis(base_graph, to_save_gdf, to_save_gdf_names)

if analysis["save_csv"]:
csv_path = output_path / (
Expand Down Expand Up @@ -1219,7 +1217,7 @@ def _save_shp_analysis(
) = analyzer.multi_link_origin_closest_destination()
opt_routes_without_hazard = gpd.GeoDataFrame()

if analysis["save_shp"]:
if analysis["save_gpkg"]:
# Save the GeoDataFrames
to_save_gdf = [
origins,
Expand All @@ -1233,7 +1231,7 @@ def _save_shp_analysis(
"optimal_routes_without_hazard",
"optimal_routes_with_hazard",
]
_save_shp_analysis(base_graph, to_save_gdf, to_save_gdf_names)
_save_gpkg_analysis(base_graph, to_save_gdf, to_save_gdf_names)
if analysis["save_csv"]:
csv_path = output_path / (
analysis["name"].replace(" ", "_") + "_destinations.csv"
Expand Down Expand Up @@ -1281,7 +1279,7 @@ def _save_shp_analysis(

if not gdf.empty:
# Not for all analyses a gdf is created as output.
if analysis["save_shp"]:
if analysis["save_gpkg"]:
gpkg_path = output_path.joinpath(
analysis["name"].replace(" ", "_") + ".gpkg"
)
Expand All @@ -1308,13 +1306,12 @@ def _save_shp_analysis(
)


def save_gdf(gdf, save_path):
def save_gdf(gdf: gpd.GeoDataFrame, save_path: Path):
"""Takes in a geodataframe object and outputs shapefiles at the paths indicated by edge_shp and node_shp
Arguments:
gdf [geodataframe]: geodataframe object to be converted
edge_shp [str]: output path including extension for edges shapefile
node_shp [str]: output path including extension for nodes shapefile
save_path [str]: output path including extension for edges shapefile
Returns:
None
"""
Expand Down
4 changes: 2 additions & 2 deletions ra2ce/graph/exporters/geodataframe_network_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@


class GeoDataFrameNetworkExporter(NetworkExporterBase):
def export_to_shp(self, output_dir: Path, export_data: gpd.GeoDataFrame) -> None:
_output_shp_path = output_dir / (self._basename + ".shp")
def export_to_gpkg(self, output_dir: Path, export_data: gpd.GeoDataFrame) -> None:
_output_shp_path = output_dir / (self._basename + ".gpkg")
export_data.to_file(
_output_shp_path, index=False
) # , encoding='utf-8' -Removed the encoding type because this causes some shapefiles not to save.
Expand Down
4 changes: 2 additions & 2 deletions ra2ce/graph/exporters/multi_graph_network_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
class MultiGraphNetworkExporter(NetworkExporterBase):
pickle_path: Optional[Path]

def export_to_shp(self, output_dir: Path, export_data: MULTIGRAPH_TYPE) -> None:
def export_to_gpkg(self, output_dir: Path, export_data: MULTIGRAPH_TYPE) -> None:
if not output_dir.is_dir():
output_dir.mkdir(parents=True)

Expand All @@ -46,7 +46,7 @@ def export_to_shp(self, output_dir: Path, export_data: MULTIGRAPH_TYPE) -> None:
output_dir / (self._basename + "_nodes.gpkg"),
)
logging.info(
f"Saved {self._basename + '_edges.shp'} and {self._basename + '_nodes.shp'} in {output_dir}."
f"Saved {self._basename + '_edges.gpkg'} and {self._basename + '_nodes.gpkg'} in {output_dir}."
)

def export_to_pickle(self, output_dir: Path, export_data: MULTIGRAPH_TYPE) -> None:
Expand Down
6 changes: 3 additions & 3 deletions ra2ce/graph/exporters/network_exporter_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ def __init__(self, basename: str, export_types: List[str]) -> None:
self._export_types = export_types
self.pickle_path = None

def export_to_shp(self, output_dir: Path, export_data: NETWORK_TYPE) -> None:
def export_to_gpkg(self, output_dir: Path, export_data: NETWORK_TYPE) -> None:
"""
Exports the given data into a `*.shp` file.
Exports the given data into a `*.gpkg` file.
Args:
output_dir (Path): Output directory where the save the exported data.
Expand Down Expand Up @@ -75,4 +75,4 @@ def export(self, export_path: Path, export_data: Any) -> None:
self.export_to_pickle(export_path, export_data)

if "shp" in self._export_types:
self.export_to_shp(export_path, export_data)
self.export_to_gpkg(export_path, export_data)
10 changes: 5 additions & 5 deletions ra2ce/graph/hazard/hazard_overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def __init__(self, config: NetworkConfigData, graphs: dict, files: dict):
self._output_dir = config.output_path
self._origins = config.origins_destinations.origins
self._destinations = config.origins_destinations.destinations
self._save_shp = config.network.save_shp
self._save_gpkg = config.network.save_gpkg
self._isolation_locations = config.static_path.joinpath(
"network", config.isolation.locations
)
Expand Down Expand Up @@ -427,7 +427,7 @@ def get_filtered_files(*suffix) -> list[Path]:

_hazard_files = {}
_hazard_files["tif"] = get_filtered_files(".tif")
_hazard_files["shp"] = get_filtered_files(".shp")
_hazard_files["shp"] = get_filtered_files(".gpkg")
_hazard_files["table"] = get_filtered_files(".csv", ".json")
return _hazard_files

Expand Down Expand Up @@ -552,7 +552,7 @@ def create(self):
write all the objects
"""
types_to_export = ["pickle"] if not self._save_shp else ["pickle", "shp"]
types_to_export = ["pickle"] if not self._save_gpkg else ["pickle", "shp"]

if (
not self.files["base_graph"]
Expand Down Expand Up @@ -712,9 +712,9 @@ def create(self):
)

# Save the OD pairs (GeoDataFrame) as shapefile
if self._save_shp:
if self._save_gpkg:
ods_path = self._output_graph_dir.joinpath(
"origin_destination_table.shp"
"origin_destination_table.gpkg"
)
ods.to_file(ods_path, index=False)
logging.info(f"Saved {ods_path.stem} in {ods_path.resolve().parent}.")
Expand Down
2 changes: 1 addition & 1 deletion ra2ce/graph/network_config_data/network_config_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class NetworkSection:
polygon: Optional[Path] = None
network_type: str = "" # Should be enum
road_types: list[str] = field(default_factory=list)
save_shp: bool = False
save_gpkg: bool = False


@dataclass
Expand Down
4 changes: 2 additions & 2 deletions ra2ce/graph/network_config_data/network_config_data_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ def get_network_section(self) -> NetworkSection:
_network_section.directed = self._parser.getboolean(
_section, "directed", fallback=_network_section.directed
)
_network_section.save_shp = self._parser.getboolean(
_section, "save_shp", fallback=_network_section.save_shp
_network_section.save_gpkg = self._parser.getboolean(
_section, "save_gpkg", fallback=_network_section.save_gpkg
)
_network_section.road_types = self._parser.getlist(
_section, "road_types", fallback=_network_section.road_types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
],
"origins": ["file", None],
"destinations": ["file", None],
"save_shp": [True, False, None],
"save_gpkg": [True, False, None],
"save_csv": [True, False, None],
"hazard_map": ["file", None],
"aggregate_wl": ["max", "min", "mean", None],
Expand Down
2 changes: 1 addition & 1 deletion ra2ce/graph/network_wrappers/shp_network_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def get_network(
col="geometry", inplace=True
) # To ensure the object is a GeoDataFrame and not a Series
_emerged_lines_file = self.output_graph_dir.joinpath(
f"{self.project_name}_lines_that_merged.shp"
f"{self.project_name}_lines_that_merged.gpkg"
)
lines_merged.to_file(_emerged_lines_file)
logging.info(
Expand Down
6 changes: 3 additions & 3 deletions ra2ce/graph/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ def add_od_nodes(
logging.info(f"Saved {name + '.feather'} in {self.output_graph_dir}.")

# Save the OD pairs (GeoDataFrame) as shapefile
if self._network_config.save_shp:
ods_path = self.output_graph_dir.joinpath(name + ".shp")
if self._network_config.save_gpkg:
ods_path = self.output_graph_dir.joinpath(name + ".gpkg")
ods.to_file(ods_path, index=False)
logging.info(f"Saved {ods_path.stem} in {ods_path.resolve().parent}.")

Expand Down Expand Up @@ -209,7 +209,7 @@ def create(self) -> dict:
(dict): A dict of a network (GeoDataFrame) and 1 (base NetworkX graph) or 2 graphs (base NetworkX and OD graph)
"""
# Save the 'base' network as gpickle and if the user requested, also as shapefile.
to_save = ["pickle"] if not self._network_config.save_shp else ["pickle", "shp"]
to_save = ["pickle"] if not self._network_config.save_gpkg else ["pickle", "shp"]
od_graph = None
base_graph = None
network_gdf = None
Expand Down
2 changes: 1 addition & 1 deletion ra2ce/graph/origins_destinations.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ def origins_from_raster(output_folder: Path, mask_fn, raster_fn) -> Path:
+ " origin points."
)

out_fn = output_folder / "origins_points.shp"
out_fn = output_folder / "origins_points.gpkg"
out_fn = generate_points_from_raster(outputfile, out_fn)

return out_fn
2 changes: 1 addition & 1 deletion tests/analyses/direct/test_analyses_direct.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test_execute(self):
{
"name": "DummyExecute",
"analysis": "",
"save_shp": False,
"save_gpkg": False,
"save_csv": False,
}
],
Expand Down
Loading

0 comments on commit 20df818

Please sign in to comment.