diff --git a/README.md b/README.md index d8059c7..0597d45 100644 --- a/README.md +++ b/README.md @@ -537,7 +537,7 @@ equal_axis will set the x and y axis of the plot to be equal. Useful to show the Two options for measuring and displaying coordinates. The two options are "Decimal Degrees" and "Relative Distance". "Decimal Degrees" is the default option that uses the original data coordinate system with latitude/longitude. "Relative Distance" changes the coordinates of each point to be the distance (in meters) from the first point on the left bank -| coordinate_unit="Decimal Degrees" | remove_intersections="Relative Distance" | +| coordinate_unit="Decimal Degrees" | coordinate_unit="Relative Distance" | | ------------- | ------------- | | ![dd_coords+png](https://raw.githubusercontent.com/cyschneck/centerline-width/main/data/doc_examples/river_coords_width_decimal_degrees.png) | ![rd_coords+png](https://raw.githubusercontent.com/cyschneck/centerline-width/main/data/doc_examples/river_coords_width_relative_distance.png)| @@ -580,42 +580,45 @@ Note: it is best practice to plot the centerline and width with same arguments i The centerline is defined by the greatest distance from the right and left bank, created from a Voronoi Diagram. The remaining paths within the river are filtered through Dijkstra's algorithm to find the shortest path that is the centerline ### Right and Left bank points are plotted (X-Axis for Latitude, Y-Axis for Longitude) -![example+png](https://raw.githubusercontent.com/cyschneck/centerline-width/main/data/doc_examples/example1.png) +![algorithm_step1+png](https://raw.githubusercontent.com/cyschneck/centerline-width/main/data/doc_examples/algorithm_step1.png) ### Generate a polygon to encapsulate the river between the right and left banks to define in and outside of river -![example+png](https://raw.githubusercontent.com/cyschneck/centerline-width/main/data/doc_examples/example2.png) +![algorithm_step2+png](https://raw.githubusercontent.com/cyschneck/centerline-width/main/data/doc_examples/algorithm_step2.png) ### Generate a Voronoi diagram based on the points along the riverbanks -![example+png](https://raw.githubusercontent.com/cyschneck/centerline-width/main/data/doc_examples/example3.png) +![algorithm_step3+png](https://raw.githubusercontent.com/cyschneck/centerline-width/main/data/doc_examples/algorithm_step3.png) ### Display Voronoi ridge vertices that lie within the polygon (within the riverbanks) -![example+png](https://raw.githubusercontent.com/cyschneck/centerline-width/main/data/doc_examples/example4.png) +Filter out any point pairs that only have one connection to filter out the short dead end paths -### Filter out any point pairs that only have one connection to filter out the short dead end paths With the vertices removed, it is possible to form multiple unconnected graphs within the polygon. The largest subgraph is assumed to contain the centerline and the other subgraphs are filtered out -![example+png](https://raw.githubusercontent.com/cyschneck/centerline-width/main/data/doc_examples/example6.png) +![algorithm_step4+png](https://raw.githubusercontent.com/cyschneck/centerline-width/main/data/doc_examples/algorithm_step4.png) + +### Define Top and Bottom of Polygon +The top of the river is defined as the last plotted points in the data, while the bottom of the river is the first plotted points +![algorithm_step5+png](https://raw.githubusercontent.com/cyschneck/centerline-width/main/data/doc_examples/algorithm_step5.png) ### Find the starting and ending node based on distance from the top and bottom of polygon The starting/ending node is defined by the vertex closest to the top/bottom of the polygon along the longest path -![example+png](https://raw.githubusercontent.com/cyschneck/centerline-width/main/data/doc_examples/example7.png) +![algorithm_step6+png](https://raw.githubusercontent.com/cyschneck/centerline-width/main/data/doc_examples/algorithm_step6.png) ### Find the shortest path from the starting node to the ending node ([Dijkstra's Algorithm](https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.shortest_paths.generic.shortest_path.html#networkx.algorithms.shortest_paths.generic.shortest_path)) | Points on Riverbank | NetworkX Graph of Points on Riverbank | | ------------- | ------------- | -| ![example+png](https://raw.githubusercontent.com/cyschneck/centerline-width/main/data/doc_examples/example10.png) | ![example+png](https://raw.githubusercontent.com/cyschneck/centerline-width/main/data/doc_examples/example9.png) | +| ![algorithm_step7+png](https://raw.githubusercontent.com/cyschneck/centerline-width/main/data/doc_examples/algorithm_step7.png) | ![algorithm_step8+png](https://raw.githubusercontent.com/cyschneck/centerline-width/main/data/doc_examples/algorithm_step8.png) | ### Display the centerline found by connecting the starting/ending node with the shortest path -![example+png](https://raw.githubusercontent.com/cyschneck/centerline-width/main/data/doc_examples/example8.png) +![algorithm_step9+png](https://raw.githubusercontent.com/cyschneck/centerline-width/main/data/doc_examples/algorithm_step9.png) This is an attempt at a more robust algorithm working from raw data to ensure that all dead ends are removed, and no gaps exist in the centerline Points that only have one connection are removed, but limiting the number of connections for a point to just two will create gaps. The Voronoi vertices connect to other vertex values, but some connect to more and some only connect to one other point. Removing additional values will create gaps, so this is avoided in this code by not applying additional filters. **All vertices:** -![example+png](https://raw.githubusercontent.com/cyschneck/centerline-width/main/data/doc_examples/example4.png) +![algorithm_step4+png](https://raw.githubusercontent.com/cyschneck/centerline-width/main/data/doc_examples/algorithm_step4.png) **Vertices that have at least two connections (that would create gaps):** -![example+png](https://raw.githubusercontent.com/cyschneck/centerline-width/main/data/doc_examples/example5.png) +![algorithm_step10+png](https://raw.githubusercontent.com/cyschneck/centerline-width/main/data/doc_examples/algorithm_step10.png) ## Debugging, Error Handling, and Edge Cases ### Wide Start/End of River diff --git a/centerline_width/__init__.py b/centerline_width/__init__.py index 461339f..c6d2f72 100644 --- a/centerline_width/__init__.py +++ b/centerline_width/__init__.py @@ -21,6 +21,7 @@ # centerline.py function calls from .centerline import centerlinePath +from .centerline import generateNXGraph from .centerline import networkXGraphShortestPath from .centerline import equalDistanceCenterline from .centerline import evenlySpacedCenterline diff --git a/centerline_width/centerline.py b/centerline_width/centerline.py index ef25d6b..6633407 100644 --- a/centerline_width/centerline.py +++ b/centerline_width/centerline.py @@ -7,7 +7,7 @@ import networkx as nx from scipy import interpolate from shapely.geometry import Point, LineString -import pyproj +from pyproj import Geod import geopy.distance # Internal centerline_width reference to access functions, global variables, and error handling @@ -79,7 +79,7 @@ def networkXGraphShortestPath(nx_graph=None, "[FAILED] No direct path found from starting node to ending node. To view gaps, plotCenterline(display_all_possible_paths=True). Recommended fix, rerun riverCenterline: set interpolate_data=True or (if interpolate_data=True) increase interpolate_n" ) return None - #nx.draw(graph_connections, with_labels=True, font_size=10) + #nx.draw(nx_graph, with_labels=True, font_size=10) return shortest_path else: return None @@ -88,7 +88,8 @@ def networkXGraphShortestPath(nx_graph=None, def centerlinePath(river_voronoi=None, river_polygon=None, top_polygon_line: LineString = None, - bottom_polygon_line: LineString = None): + bottom_polygon_line: LineString = None, + multiple_connections: int = 0): # Return the starting node, ending node, all possible paths positions, and all paths starting/end position as a dictionary start_end_points_dict = centerline_width.pointsFromVoronoi( river_voronoi, @@ -102,7 +103,7 @@ def centerlinePath(river_voronoi=None, for start_point, end_point_list in start_end_points_dict.items(): if len( end_point_list - ) > 0: # TESTING TESTING: Show only the end points that have multiple connections (set to 0 during production) + ) > multiple_connections: # TESTING TESTING: Show only the end points that have multiple connections (set to 0 during production) # Find the starting and ending node based on distance from the top and bottom of the polygon if starting_node is None: starting_node = start_point @@ -158,7 +159,7 @@ def equalDistanceCenterline(centerline_coordinates: list = None, equal_distance_between_centerline_coordinates = [] - geodesic = pyproj.Geod(ellps=ellipsoid) + geodesic = Geod(ellps=ellipsoid) # Iterate through coordinates based on a set distance (distance_m) lon_start, lat_start = centerline_coordinates[0] diff --git a/centerline_width/width.py b/centerline_width/width.py index 582e533..2c02384 100644 --- a/centerline_width/width.py +++ b/centerline_width/width.py @@ -359,6 +359,8 @@ def riverWidthFromCenterline( width_dict = {} + geodesic = Geod(ellps=river_object.ellipsoid) + for centerline_coord, _ in right_width_coordinates.items(): # store the distance between the lat/lon position of the right/left bank lon1, lat1 = right_width_coordinates[centerline_coord] diff --git a/data/doc_examples/algorithm_step1.png b/data/doc_examples/algorithm_step1.png new file mode 100644 index 0000000..9c2b245 Binary files /dev/null and b/data/doc_examples/algorithm_step1.png differ diff --git a/data/doc_examples/algorithm_step10.png b/data/doc_examples/algorithm_step10.png new file mode 100644 index 0000000..d661c1d Binary files /dev/null and b/data/doc_examples/algorithm_step10.png differ diff --git a/data/doc_examples/algorithm_step2.png b/data/doc_examples/algorithm_step2.png new file mode 100644 index 0000000..df05605 Binary files /dev/null and b/data/doc_examples/algorithm_step2.png differ diff --git a/data/doc_examples/algorithm_step3.png b/data/doc_examples/algorithm_step3.png new file mode 100644 index 0000000..87780de Binary files /dev/null and b/data/doc_examples/algorithm_step3.png differ diff --git a/data/doc_examples/algorithm_step4.png b/data/doc_examples/algorithm_step4.png new file mode 100644 index 0000000..94bcd89 Binary files /dev/null and b/data/doc_examples/algorithm_step4.png differ diff --git a/data/doc_examples/algorithm_step5.png b/data/doc_examples/algorithm_step5.png new file mode 100644 index 0000000..491e2d2 Binary files /dev/null and b/data/doc_examples/algorithm_step5.png differ diff --git a/data/doc_examples/algorithm_step6.png b/data/doc_examples/algorithm_step6.png new file mode 100644 index 0000000..4acff38 Binary files /dev/null and b/data/doc_examples/algorithm_step6.png differ diff --git a/data/doc_examples/algorithm_step7.png b/data/doc_examples/algorithm_step7.png new file mode 100644 index 0000000..fb154c2 Binary files /dev/null and b/data/doc_examples/algorithm_step7.png differ diff --git a/data/doc_examples/algorithm_step8.png b/data/doc_examples/algorithm_step8.png new file mode 100644 index 0000000..4f9cac0 Binary files /dev/null and b/data/doc_examples/algorithm_step8.png differ diff --git a/data/doc_examples/algorithm_step9.png b/data/doc_examples/algorithm_step9.png new file mode 100644 index 0000000..67f630a Binary files /dev/null and b/data/doc_examples/algorithm_step9.png differ diff --git a/data/doc_examples/equal_distance_centerline.png b/data/doc_examples/equal_distance_centerline.png index fb2b0fc..497d128 100644 Binary files a/data/doc_examples/equal_distance_centerline.png and b/data/doc_examples/equal_distance_centerline.png differ diff --git a/data/doc_examples/equal_distance_centerline_relative.png b/data/doc_examples/equal_distance_centerline_relative.png index 8a1053e..c0bd2a4 100644 Binary files a/data/doc_examples/equal_distance_centerline_relative.png and b/data/doc_examples/equal_distance_centerline_relative.png differ diff --git a/data/doc_examples/evenly_spaced_centerline.png b/data/doc_examples/evenly_spaced_centerline.png index c060ce5..8c16138 100644 Binary files a/data/doc_examples/evenly_spaced_centerline.png and b/data/doc_examples/evenly_spaced_centerline.png differ diff --git a/data/doc_examples/evenly_spaced_centerline_relative.png b/data/doc_examples/evenly_spaced_centerline_relative.png index 7a8b6a3..7d853b3 100644 Binary files a/data/doc_examples/evenly_spaced_centerline_relative.png and b/data/doc_examples/evenly_spaced_centerline_relative.png differ diff --git a/data/doc_examples/example1.png b/data/doc_examples/example1.png deleted file mode 100644 index 8b093b6..0000000 Binary files a/data/doc_examples/example1.png and /dev/null differ diff --git a/data/doc_examples/example10.png b/data/doc_examples/example10.png deleted file mode 100644 index c530311..0000000 Binary files a/data/doc_examples/example10.png and /dev/null differ diff --git a/data/doc_examples/example2.png b/data/doc_examples/example2.png deleted file mode 100644 index 41ee595..0000000 Binary files a/data/doc_examples/example2.png and /dev/null differ diff --git a/data/doc_examples/example3.png b/data/doc_examples/example3.png deleted file mode 100644 index b1f665d..0000000 Binary files a/data/doc_examples/example3.png and /dev/null differ diff --git a/data/doc_examples/example4.png b/data/doc_examples/example4.png deleted file mode 100644 index 5f1edbc..0000000 Binary files a/data/doc_examples/example4.png and /dev/null differ diff --git a/data/doc_examples/example5.png b/data/doc_examples/example5.png deleted file mode 100644 index fbd4339..0000000 Binary files a/data/doc_examples/example5.png and /dev/null differ diff --git a/data/doc_examples/example6.png b/data/doc_examples/example6.png deleted file mode 100644 index bc8d381..0000000 Binary files a/data/doc_examples/example6.png and /dev/null differ diff --git a/data/doc_examples/example7.png b/data/doc_examples/example7.png deleted file mode 100644 index d675712..0000000 Binary files a/data/doc_examples/example7.png and /dev/null differ diff --git a/data/doc_examples/example8.png b/data/doc_examples/example8.png deleted file mode 100644 index 2647acc..0000000 Binary files a/data/doc_examples/example8.png and /dev/null differ diff --git a/data/doc_examples/example9.png b/data/doc_examples/example9.png deleted file mode 100644 index 1254a1a..0000000 Binary files a/data/doc_examples/example9.png and /dev/null differ diff --git a/data/doc_examples/interpolate_n_centerpoints_200.png b/data/doc_examples/interpolate_n_centerpoints_200.png index bdd3517..a2b743e 100644 Binary files a/data/doc_examples/interpolate_n_centerpoints_200.png and b/data/doc_examples/interpolate_n_centerpoints_200.png differ diff --git a/data/doc_examples/interpolate_n_centerpoints_75.png b/data/doc_examples/interpolate_n_centerpoints_75.png index 8dddcaf..c31beac 100644 Binary files a/data/doc_examples/interpolate_n_centerpoints_75.png and b/data/doc_examples/interpolate_n_centerpoints_75.png differ diff --git a/data/doc_examples/river_coords_centerline.png b/data/doc_examples/river_coords_centerline.png index d8d11ff..bc1796c 100644 Binary files a/data/doc_examples/river_coords_centerline.png and b/data/doc_examples/river_coords_centerline.png differ diff --git a/data/doc_examples/river_coords_equal_ax.png b/data/doc_examples/river_coords_equal_ax.png index 55bcc55..6ebd1e8 100644 Binary files a/data/doc_examples/river_coords_equal_ax.png and b/data/doc_examples/river_coords_equal_ax.png differ diff --git a/data/doc_examples/river_coords_not_equal_default_ax.png b/data/doc_examples/river_coords_not_equal_default_ax.png index 5375492..cf35aa9 100644 Binary files a/data/doc_examples/river_coords_not_equal_default_ax.png and b/data/doc_examples/river_coords_not_equal_default_ax.png differ diff --git a/data/doc_examples/river_coords_transect_avg.png b/data/doc_examples/river_coords_transect_avg.png index aabc439..24ff7ab 100644 Binary files a/data/doc_examples/river_coords_transect_avg.png and b/data/doc_examples/river_coords_transect_avg.png differ diff --git a/data/doc_examples/river_coords_transect_direct.png b/data/doc_examples/river_coords_transect_direct.png index 2286ef3..7367047 100644 Binary files a/data/doc_examples/river_coords_transect_direct.png and b/data/doc_examples/river_coords_transect_direct.png differ diff --git a/data/doc_examples/river_coords_width.png b/data/doc_examples/river_coords_width.png index c220609..24ff7ab 100644 Binary files a/data/doc_examples/river_coords_width.png and b/data/doc_examples/river_coords_width.png differ diff --git a/data/doc_examples/river_coords_width_dark_mode_false.png b/data/doc_examples/river_coords_width_dark_mode_false.png index 165763a..389ddc9 100644 Binary files a/data/doc_examples/river_coords_width_dark_mode_false.png and b/data/doc_examples/river_coords_width_dark_mode_false.png differ diff --git a/data/doc_examples/river_coords_width_dark_mode_true.png b/data/doc_examples/river_coords_width_dark_mode_true.png index 3d9018f..66d07f4 100644 Binary files a/data/doc_examples/river_coords_width_dark_mode_true.png and b/data/doc_examples/river_coords_width_dark_mode_true.png differ diff --git a/data/doc_examples/river_coords_width_decimal_degrees.png b/data/doc_examples/river_coords_width_decimal_degrees.png index 28030e4..24ff7ab 100644 Binary files a/data/doc_examples/river_coords_width_decimal_degrees.png and b/data/doc_examples/river_coords_width_decimal_degrees.png differ diff --git a/data/doc_examples/river_coords_width_keep_intersections.png b/data/doc_examples/river_coords_width_keep_intersections.png index e519cd0..8b84d61 100644 Binary files a/data/doc_examples/river_coords_width_keep_intersections.png and b/data/doc_examples/river_coords_width_keep_intersections.png differ diff --git a/data/doc_examples/river_coords_width_relative_distance.png b/data/doc_examples/river_coords_width_relative_distance.png index 00948e2..4192fdd 100644 Binary files a/data/doc_examples/river_coords_width_relative_distance.png and b/data/doc_examples/river_coords_width_relative_distance.png differ diff --git a/data/doc_examples/river_coords_width_remove_intersections.png b/data/doc_examples/river_coords_width_remove_intersections.png index c220609..24ff7ab 100644 Binary files a/data/doc_examples/river_coords_width_remove_intersections.png and b/data/doc_examples/river_coords_width_remove_intersections.png differ diff --git a/data/doc_examples/river_coords_width_transect_30.png b/data/doc_examples/river_coords_width_transect_30.png index 0a7e715..c3ce06f 100644 Binary files a/data/doc_examples/river_coords_width_transect_30.png and b/data/doc_examples/river_coords_width_transect_30.png differ diff --git a/data/doc_examples/river_coords_width_transect_6.png b/data/doc_examples/river_coords_width_transect_6.png index 5bb9bfb..956a06a 100644 Binary files a/data/doc_examples/river_coords_width_transect_6.png and b/data/doc_examples/river_coords_width_transect_6.png differ diff --git a/data/doc_examples/river_coords_width_with_smoothing.png b/data/doc_examples/river_coords_width_with_smoothing.png index c220609..24ff7ab 100644 Binary files a/data/doc_examples/river_coords_width_with_smoothing.png and b/data/doc_examples/river_coords_width_with_smoothing.png differ diff --git a/data/doc_examples/river_coords_width_without_smoothing.png b/data/doc_examples/river_coords_width_without_smoothing.png index cca3ae7..e375b53 100644 Binary files a/data/doc_examples/river_coords_width_without_smoothing.png and b/data/doc_examples/river_coords_width_without_smoothing.png differ diff --git a/data/doc_examples/river_coords_with_centerline.png b/data/doc_examples/river_coords_with_centerline.png index b6d504b..df3e806 100644 Binary files a/data/doc_examples/river_coords_with_centerline.png and b/data/doc_examples/river_coords_with_centerline.png differ diff --git a/data/doc_examples/river_coords_without_centerline.png b/data/doc_examples/river_coords_without_centerline.png index a559fa0..24ff7ab 100644 Binary files a/data/doc_examples/river_coords_without_centerline.png and b/data/doc_examples/river_coords_without_centerline.png differ diff --git a/data/doc_examples/river_example.png b/data/doc_examples/river_example.png index cc3cc74..72eedf0 100644 Binary files a/data/doc_examples/river_example.png and b/data/doc_examples/river_example.png differ diff --git a/data/doc_examples/river_relative_distance_coords_centerline.png b/data/doc_examples/river_relative_distance_coords_centerline.png index 10e92a2..bebf067 100644 Binary files a/data/doc_examples/river_relative_distance_coords_centerline.png and b/data/doc_examples/river_relative_distance_coords_centerline.png differ diff --git a/data/doc_examples/smoothed_centerline.png b/data/doc_examples/smoothed_centerline.png index 467dd8f..6c63457 100644 Binary files a/data/doc_examples/smoothed_centerline.png and b/data/doc_examples/smoothed_centerline.png differ diff --git a/data/doc_examples/smoothed_centerline_relative.png b/data/doc_examples/smoothed_centerline_relative.png index 0d2b3ea..0f09f6d 100644 Binary files a/data/doc_examples/smoothed_centerline_relative.png and b/data/doc_examples/smoothed_centerline_relative.png differ diff --git a/data/doc_examples/voronoi_centerline.png b/data/doc_examples/voronoi_centerline.png index 53cb7e9..2e66c8f 100644 Binary files a/data/doc_examples/voronoi_centerline.png and b/data/doc_examples/voronoi_centerline.png differ diff --git a/data/doc_examples/voronoi_centerline_relative.png b/data/doc_examples/voronoi_centerline_relative.png index 65cb28f..f1c001d 100644 Binary files a/data/doc_examples/voronoi_centerline_relative.png and b/data/doc_examples/voronoi_centerline_relative.png differ diff --git a/generate_readme_diagrams.py b/generate_readme_diagrams.py new file mode 100644 index 0000000..b7dc121 --- /dev/null +++ b/generate_readme_diagrams.py @@ -0,0 +1,404 @@ +# generates plots for README.md documentation +import centerline_width +import matplotlib.pyplot as plt +from scipy.spatial import voronoi_plot_2d +import networkx as nx + +if __name__ == "__main__": + centerline_width.extractPointsToTextFile( + left_kml="data/leftbank.kml", + right_kml="data/rightbank.kml", + text_output_name="data/river_coords.txt") + centerline_width.convertColumnsToCSV(text_file="data/river_coords.txt", + flipBankDirection=True) + + is_debug = False # set to False when generating, True when debugging to view all plots + + ################### Introduction and Quickstart ########################################## + + river_object = centerline_width.riverCenterline( + csv_data="data/river_coords.csv") + river_object.plotCenterline( + plot_title="Centerline with Riverbanks", + save_plot_name="data/doc_examples/river_example.png", + show_plot=is_debug) + + ro_550 = centerline_width.riverCenterline(csv_data="data/river_coords.csv", + optional_cutoff=550) + ro_550.plotCenterline( + save_plot_name="data/doc_examples/river_coords_centerline.png", + show_plot=is_debug) + ro_550.plotCenterlineWidth( + save_plot_name="data/doc_examples/river_coords_width.png", + apply_smoothing=True, + remove_intersections=True, + display_true_centerline=False, + show_plot=is_debug) + ro_550.plotCenterline( + save_plot_name= + "data/doc_examples/river_relative_distance_coords_centerline.png", + coordinate_unit="Relative Distance", + show_plot=is_debug) + + ################### Centerline and Width ################################################ + + ro_550_interpolate_centerline_75 = centerline_width.riverCenterline( + csv_data="data/river_coords.csv", + optional_cutoff=550, + interpolate_n_centerpoints=75) + ro_550_interpolate_centerline_75.plotCenterline( + save_plot_name="data/doc_examples/interpolate_n_centerpoints_75.png", + centerline_type="Evenly Spaced", + centerline_color="fuchsia", + marker_type="scatter", + show_plot=is_debug) + ro_550_interpolate_centerline_200 = centerline_width.riverCenterline( + csv_data="data/river_coords.csv", + optional_cutoff=550, + interpolate_n_centerpoints=200) + ro_550_interpolate_centerline_200.plotCenterline( + save_plot_name="data/doc_examples/interpolate_n_centerpoints_200.png", + centerline_type="Evenly Spaced", + centerline_color="fuchsia", + marker_type="scatter", + show_plot=is_debug) + + ################### Types of Centerlines ################################################ + ro_550.plotCenterline( + save_plot_name="data/doc_examples/voronoi_centerline.png", + centerline_type="Voronoi", + centerline_color="black", + marker_type="scatter", + plot_title="Centerline Formed by Voronoi Diagram", + show_plot=is_debug) + ro_550.plotCenterline( + save_plot_name="data/doc_examples/voronoi_centerline_relative.png", + centerline_type="Voronoi", + centerline_color="black", + marker_type="scatter", + coordinate_unit="Relative Distance", + plot_title= + "Centerline Formed by Voronoi Diagram with Relative Distance", + show_plot=is_debug) + + ro_550.plotCenterline( + save_plot_name="data/doc_examples/equal_distance_centerline.png", + centerline_type="Equal Distance", + centerline_color="mediumorchid", + marker_type="scatter", + plot_title= + f"Centerline Formed by Points Equally Distanced Apart Every {ro_550.equal_distance} Meters", + show_plot=is_debug) + ro_550.plotCenterline( + save_plot_name= + "data/doc_examples/equal_distance_centerline_relative.png", + centerline_type="Equal Distance", + centerline_color="mediumorchid", + marker_type="scatter", + coordinate_unit="Relative Distance", + plot_title= + f"Centerline Formed by Points Equally Distanced Apart Every {ro_550.equal_distance} Meters with Relative Distance", + show_plot=is_debug) + + ro_550_interpolate_centerline_200.plotCenterline( + save_plot_name="data/doc_examples/evenly_spaced_centerline.png", + centerline_type="Evenly Spaced", + centerline_color="fuchsia", + marker_type="scatter", + plot_title= + f"Centerline Formed by {ro_550_interpolate_centerline_200.interpolate_n_centerpoints} Evenly Spaced Centerline Points", + show_plot=is_debug) + ro_550_interpolate_centerline_200.plotCenterline( + save_plot_name= + "data/doc_examples/evenly_spaced_centerline_relative.png", + centerline_type="Evenly Spaced", + centerline_color="fuchsia", + marker_type="scatter", + coordinate_unit="Relative Distance", + plot_title= + f"Centerline Formed by {ro_550_interpolate_centerline_200.interpolate_n_centerpoints} Evenly Spaced Centerline Points with Relative Distance", + show_plot=is_debug) + + ro_550_interpolate_centerline_200.plotCenterline( + save_plot_name="data/doc_examples/smoothed_centerline.png", + centerline_type="Smoothed", + centerline_color="blue", + marker_type="scatter", + plot_title= + f"Centerline Formed by {ro_550_interpolate_centerline_200.interpolate_n_centerpoints} Centerline Coordinates", + show_plot=is_debug) + ro_550_interpolate_centerline_200.plotCenterline( + save_plot_name="data/doc_examples/smoothed_centerline_relative.png", + centerline_type="Smoothed", + centerline_color="blue", + marker_type="scatter", + coordinate_unit="Relative Distance", + plot_title= + f"Centerline Formed by {ro_550_interpolate_centerline_200.interpolate_n_centerpoints} Centerline Coordinates with Relative Distance", + show_plot=is_debug) + + ################### Plot Centerline Width Lines in Matplotlib ############################### + ro_550.plotCenterlineWidth( + save_plot_name="data/doc_examples/river_coords_with_centerline.png", + display_true_centerline=True, + apply_smoothing=True, + remove_intersections=True, + show_plot=is_debug) + ro_550.plotCenterlineWidth( + save_plot_name="data/doc_examples/river_coords_without_centerline.png", + display_true_centerline=False, + apply_smoothing=True, + remove_intersections=True, + show_plot=is_debug) + ro_550.plotCenterlineWidth( + save_plot_name= + "data/doc_examples/river_coords_width_without_smoothing.png", + display_true_centerline=True, + apply_smoothing=False, + remove_intersections=True, + show_plot=is_debug) + ro_550.plotCenterlineWidth( + save_plot_name= + "data/doc_examples/river_coords_width_with_smoothing.png", + display_true_centerline=False, + apply_smoothing=True, + remove_intersections=True, + show_plot=is_debug) + ro_550.plotCenterlineWidth( + save_plot_name="data/doc_examples/river_coords_width_transect_6.png", + display_true_centerline=False, + transect_span_distance=6, + apply_smoothing=True, + remove_intersections=True, + show_plot=is_debug) + ro_550.plotCenterlineWidth( + save_plot_name="data/doc_examples/river_coords_width_transect_30.png", + display_true_centerline=False, + transect_span_distance=30, + apply_smoothing=True, + remove_intersections=True, + show_plot=is_debug) + ro_550.plotCenterlineWidth( + save_plot_name="data/doc_examples/river_coords_transect_avg.png", + display_true_centerline=False, + transect_slope="Average", + apply_smoothing=True, + remove_intersections=True, + show_plot=is_debug) + ro_550.plotCenterlineWidth( + save_plot_name="data/doc_examples/river_coords_transect_direct.png", + display_true_centerline=False, + transect_slope="Direct", + apply_smoothing=True, + remove_intersections=True, + show_plot=is_debug) + ro_550.plotCenterlineWidth( + save_plot_name= + "data/doc_examples/river_coords_width_keep_intersections.png", + display_true_centerline=False, + remove_intersections=False, + apply_smoothing=True, + show_plot=is_debug) + ro_550.plotCenterlineWidth( + save_plot_name= + "data/doc_examples/river_coords_width_remove_intersections.png", + display_true_centerline=False, + remove_intersections=True, + apply_smoothing=True, + show_plot=is_debug) + ro_550.plotCenterlineWidth( + save_plot_name= + "data/doc_examples/river_coords_width_dark_mode_false.png", + display_true_centerline=True, + remove_intersections=False, + dark_mode=False, + show_plot=is_debug) + ro_550.plotCenterlineWidth( + save_plot_name= + "data/doc_examples/river_coords_width_dark_mode_true.png", + display_true_centerline=True, + remove_intersections=False, + dark_mode=True, + show_plot=is_debug) + ro_10 = centerline_width.riverCenterline(csv_data="data/river_coords.csv", + optional_cutoff=10) + ro_10.plotCenterlineWidth( + save_plot_name= + "data/doc_examples/river_coords_not_equal_default_ax.png", + display_true_centerline=True, + remove_intersections=True, + equal_axis=False, + show_plot=is_debug) + ro_10.plotCenterlineWidth( + save_plot_name="data/doc_examples/river_coords_equal_ax.png", + display_true_centerline=True, + remove_intersections=True, + equal_axis=True, + show_plot=is_debug) + ro_550.plotCenterlineWidth( + save_plot_name= + "data/doc_examples/river_coords_width_decimal_degrees.png", + display_true_centerline=False, + apply_smoothing=True, + coordinate_unit="Decimal Degrees", + remove_intersections=True, + show_plot=is_debug) + ro_550.plotCenterlineWidth( + save_plot_name= + "data/doc_examples/river_coords_width_relative_distance.png", + display_true_centerline=False, + apply_smoothing=True, + coordinate_unit="Relative Distance", + remove_intersections=True, + show_plot=is_debug) + + ################### Algorithm to Determine Centerline ############################### + ro_15 = centerline_width.riverCenterline(csv_data="data/river_coords.csv", + optional_cutoff=15) + + def plotAlgorithm(fig_save_name=None, + plot_polygon=False, + plot_voronoi=False, + plot_all_possible_paths=False, + total_number_of_connections=0, + plot_top_bottom_banks=False, + plot_start_end_node=False, + plot_point_labels=False, + plot_centerline=False): + left_bank = ro_15.left_bank_coordinates + right_bank = ro_15.right_bank_coordinates + + fig = plt.figure(figsize=(10, 10)) + ax = fig.add_subplot(111) + scatter_plot_size = 4 + + right_coords = ro_15.right_bank_coordinates + left_coords = ro_15.left_bank_coordinates + + if plot_polygon: + plt.plot(*ro_15.bank_polygon.exterior.xy, c="gainsboro") + + x = [i[0] for i in right_coords] + y = [i[1] for i in right_coords] + plt.scatter(x, + y, + c="dodgerblue", + s=scatter_plot_size, + label="Right Bank") + x = [i[0] for i in left_coords] + y = [i[1] for i in left_coords] + plt.scatter(x, y, c="orange", s=scatter_plot_size, label="Left Bank") + + if plot_voronoi: + voronoi_plot_2d(ro_15.bank_voronoi, + show_points=True, + point_size=1, + ax=ax) + if plot_all_possible_paths: + starting_node, ending_node, x_ridge_point, y_ridge_point, shortest_path_coordinates = centerline_width.centerlinePath( + ro_15.bank_voronoi, ro_15.bank_polygon, ro_15.top_bank, + ro_15.bottom_bank, total_number_of_connections) + for i in range(len(x_ridge_point)): + plt.plot(x_ridge_point[i], + y_ridge_point[i], + 'cyan', + linewidth=1, + zorder=1) + + if plot_top_bottom_banks: + plt.plot(*ro_15.top_bank.xy, c="forestgreen") + plt.plot(*ro_15.bottom_bank.xy, c="lightcoral") + + if plot_start_end_node: + plt.scatter(ro_15.starting_node[0], + ro_15.starting_node[1], + c="green", + label="Starting Node", + s=45) + plt.scatter(ro_15.ending_node[0], + ro_15.ending_node[1], + c="red", + label="Ending Node", + s=45) + + if plot_point_labels: + values_plotted = [] + for i in range(len(x_ridge_point)): + first_connection = f"({x_ridge_point[i][0]}, {y_ridge_point[i][0]})" + if first_connection not in values_plotted: + ax.annotate(first_connection, + (x_ridge_point[i][0], y_ridge_point[i][0]), + fontsize=6) + values_plotted.append(first_connection) + second_connection = f"({x_ridge_point[i][1]}, {y_ridge_point[i][1]})" + if second_connection not in values_plotted: + ax.annotate(second_connection, + (x_ridge_point[i][1], y_ridge_point[i][1]), + fontsize=6) + values_plotted.append(second_connection) + + if plot_centerline: + plt.plot(*zip(*shortest_path_coordinates), + c="black", + label="Centerline", + zorder=10) + + plt.title("River Coordinates") + plt.xlabel("Longitude (°)") + plt.ylabel("Latitude (°)") + + if is_debug: plt.show() + if not is_debug: plt.close() + fig.savefig(fig_save_name) + + return fig, ax + + def plotNetworkXGraph(fig_save_name=None): + # Draw NetworkX Graph + fig = plt.figure(figsize=(10, 10)) + ax = fig.add_subplot(111) + start_end_points_dict = centerline_width.pointsFromVoronoi( + ro_15.bank_voronoi, ro_15.bank_polygon) + nx_graphs, largest_subgraph_nodes = centerline_width.generateNXGraph( + start_end_points_dict) + nx.draw(nx_graphs, with_labels=True, font_size=10) + plt.draw() + if is_debug: plt.show() + if not is_debug: plt.close() + fig.savefig(fig_save_name) + + plotAlgorithm(fig_save_name="data/doc_examples/algorithm_step1.png") + plotAlgorithm(fig_save_name="data/doc_examples/algorithm_step2.png", + plot_polygon=True) + plotAlgorithm(fig_save_name="data/doc_examples/algorithm_step3.png", + plot_polygon=True, + plot_voronoi=True) + plotAlgorithm(fig_save_name="data/doc_examples/algorithm_step4.png", + plot_polygon=True, + plot_all_possible_paths=True) + plotAlgorithm(fig_save_name="data/doc_examples/algorithm_step5.png", + plot_polygon=True, + plot_all_possible_paths=True, + plot_top_bottom_banks=True) + plotAlgorithm(fig_save_name="data/doc_examples/algorithm_step6.png", + plot_polygon=True, + plot_all_possible_paths=True, + plot_top_bottom_banks=True, + plot_start_end_node=True) + plotAlgorithm(fig_save_name="data/doc_examples/algorithm_step7.png", + plot_polygon=True, + plot_all_possible_paths=True, + plot_top_bottom_banks=True, + plot_point_labels=True) + plotNetworkXGraph(fig_save_name="data/doc_examples/algorithm_step8.png") + plotAlgorithm(fig_save_name="data/doc_examples/algorithm_step9.png", + plot_polygon=True, + plot_all_possible_paths=True, + plot_top_bottom_banks=True, + plot_start_end_node=True, + plot_centerline=True) + plotAlgorithm(fig_save_name="data/doc_examples/algorithm_step10.png", + plot_polygon=True, + plot_all_possible_paths=True, + plot_top_bottom_banks=True, + total_number_of_connections=1)