diff --git a/README.md b/README.md index f8ae3e6..edd9370 100644 --- a/README.md +++ b/README.md @@ -385,6 +385,7 @@ plotCenterline(centerline_type="Voronoi", plot_title=None, save_plot_name=None, display_voronoi=False, + show_plot=True, coordinate_unit="Decimal Degrees") ``` * [OPTIONAL] centerline_type (string): Centerline type graph within river (not case-sensitive), options: ["Voronoi", "Evenly Spaced", "Smoothed", "Equal Distance"], defaults to "Voronoi" @@ -396,6 +397,7 @@ plotCenterline(centerline_type="Voronoi", * [OPTIONAL] plot_title (string): Change plot title, defaults to "River Coordinates: Valid Centerline = True/False, Valid Polygon = True/False" * [OPTIONAL] save_plot_name (string): Save the plot with a given name and location * [OPTIONAL] display_voronoi (boolean): Overlay Voronoi diagram used to generate centerline, defaults to False +* [OPTIONAL] show_plot (boolean): display and open plots (plt.show() in Matplotlib), defaults to True * [OPTIONAL] coordinate_unit (string): Coordinates of the river are return as "Decimal Degrees" (latitude/longitude) or converted to a distance from the first point on the left bank as "Relative Distance", defaults to "Decimal Degrees" ```python @@ -421,6 +423,7 @@ plotCenterlineWidth(plot_title=None, remove_intersections=False, dark_mode=False, equal_axis=False, + show_plot=True, coordinate_unit="Decimal Degrees") ``` * [OPTIONAL] plot_title (string): Change plot title, defaults to "River Coordinates: Valid Centerline = True/False, Valid Polygon = True/False" @@ -433,6 +436,7 @@ plotCenterlineWidth(plot_title=None, * [OPTIONAL] remove_intersections (boolean): Remove intersecting lines (but maintain the most width lines as possible) and only return non-intersecting width lines, defaults to False * [OPTIONAL] dark_mode (bool): Change plot to a black ground (and override if `centerline_color="black"` to `centerline_color="white"`), defaults to False * [OPTIONAL] equal_axis (bool): Set x/y axes in plot to be equal, defaults to False +* [OPTIONAL] show_plot (boolean): display and open plots (plt.show() in Matplotlib), defaults to True * [OPTIONAL] coordinate_unit (string): Coordinates of the river are return as "Decimal Degrees" (latitude/longitude) or converted to a distance from the first point on the left bank as "Relative Distance", defaults to "Decimal Degrees" **display_true_centerline** diff --git a/centerline_width/error_handling.py b/centerline_width/error_handling.py index 8616a75..90fc853 100644 --- a/centerline_width/error_handling.py +++ b/centerline_width/error_handling.py @@ -44,6 +44,7 @@ def errorHandlingPlotCenterline(river_object=None, plot_title=None, save_plot_name=None, display_voronoi=None, + show_plot=None, coordinate_unit=None): # Error handling for plotCenterline() if river_object is None: @@ -86,6 +87,9 @@ def errorHandlingPlotCenterline(river_object=None, if type(display_voronoi) != bool: raise ValueError(f"[display_voronoi]: Must be a bool, current type = '{type(display_voronoi)}'") + if type(show_plot) != bool: + raise ValueError(f"[show_plot]: Must be a bool, current type = '{type(show_plot)}'") + if type(coordinate_unit) != str: raise ValueError(f"[coordinate_unit]: Must be a str, current type = '{type(coordinate_unit)}'") else: @@ -104,6 +108,7 @@ def errorHandlingPlotCenterlineWidth(river_object=None, remove_intersections=None, dark_mode=None, equal_axis=None, + show_plot=None, coordinate_unit=None): # Error handling for plotCenterlineWidth() if river_object is None: @@ -150,6 +155,9 @@ def errorHandlingPlotCenterlineWidth(river_object=None, if type(equal_axis) != bool: raise ValueError(f"[equal_axis]: Must be a bool, current type = '{type(equal_axis)}'") + if type(show_plot) != bool: + raise ValueError(f"[show_plot]: Must be a bool, current type = '{type(show_plot)}'") + if type(coordinate_unit) != str: raise ValueError(f"[coordinate_unit]: Must be a str, current type = '{type(coordinate_unit)}'") else: diff --git a/centerline_width/plotDiagrams.py b/centerline_width/plotDiagrams.py index 04d21de..90e7291 100644 --- a/centerline_width/plotDiagrams.py +++ b/centerline_width/plotDiagrams.py @@ -132,6 +132,7 @@ def plotCenterline(river_object=None, plot_title=None, save_plot_name=None, display_voronoi=False, + show_plot=True, coordinate_unit="Decimal Degrees"): # Plot Centerline of River centerline_width.errorHandlingPlotCenterline(river_object=river_object, @@ -144,6 +145,7 @@ def plotCenterline(river_object=None, plot_title=plot_title, save_plot_name=save_plot_name, display_voronoi=display_voronoi, + show_plot=show_plot, coordinate_unit=coordinate_unit) fig, ax, valid_path_through = plotCenterlineBackend(river_object=river_object, @@ -187,7 +189,7 @@ def plotCenterline(river_object=None, plt.ylabel("Relative Distance Y (m)") plt.legend(loc="upper right") - plt.show() + if show_plot: plt.show() if save_plot_name: fig.savefig(save_plot_name) def plotCenterlineWidth(river_object=None, @@ -201,6 +203,7 @@ def plotCenterlineWidth(river_object=None, remove_intersections=False, dark_mode=False, equal_axis=False, + show_plot=True, coordinate_unit="Decimal Degrees"): # Plot Width Lines based on Centerline centerline_width.errorHandlingPlotCenterlineWidth(river_object=river_object, @@ -214,6 +217,7 @@ def plotCenterlineWidth(river_object=None, remove_intersections=remove_intersections, dark_mode=dark_mode, equal_axis=equal_axis, + show_plot=show_plot, coordinate_unit=coordinate_unit) fig, ax, valid_path_through = plotCenterlineBackend(river_object=river_object, @@ -305,5 +309,5 @@ def plotCenterlineWidth(river_object=None, plt.ylabel("Distance Distance Y (m)") plt.legend(loc="upper right") - plt.show() + if show_plot: plt.show() if save_plot_name: fig.savefig(save_plot_name) diff --git a/centerline_width/pytests/test_plotDiagrams.py b/centerline_width/pytests/test_plotDiagrams.py index 6c6af47..369712c 100644 --- a/centerline_width/pytests/test_plotDiagrams.py +++ b/centerline_width/pytests/test_plotDiagrams.py @@ -116,6 +116,12 @@ def test_plotCenterline_displayVoronoiInvalidTypes(invalid_input, error_output): centerline_width.plotCenterline(river_object=river_class_example, display_voronoi=invalid_input) +@pytest.mark.parametrize("invalid_input, error_output", invalid_non_bool_options) +def test_plotCenterline_showPlotInvalidTypes(invalid_input, error_output): + with pytest.raises(ValueError, match=re.escape(f"[show_plot]: Must be a bool, current type = '{error_output}'")): + centerline_width.plotCenterline(river_object=river_class_example, + show_plot=invalid_input) + def test_plotCenterline_coordinateUnitInvalidOption(): with pytest.raises(ValueError, match=re.escape("[coordinate_unit]: Must be an available option in ['Decimal Degrees', 'Relative Distance'], current option = 'Invalid Option'")): centerline_width.plotCenterline(river_object=river_class_example, @@ -199,16 +205,22 @@ def test_plotCenterlineWidth_darkModeInvalidTypes(invalid_input, error_output): @pytest.mark.parametrize("invalid_input, error_output", invalid_non_bool_options) def test_plotCenterline_equalAxisInvalidTypes(invalid_input, error_output): with pytest.raises(ValueError, match=re.escape(f"[equal_axis]: Must be a bool, current type = '{error_output}'")): - centerline_width.plotCenterline(river_object=river_class_example, - equal_axis=invalid_input) + centerline_width.plotCenterlineWidth(river_object=river_class_example, + equal_axis=invalid_input) + +@pytest.mark.parametrize("invalid_input, error_output", invalid_non_bool_options) +def test_plotCenterline_showPlotInvalidTypes(invalid_input, error_output): + with pytest.raises(ValueError, match=re.escape(f"[show_plot]: Must be a bool, current type = '{error_output}'")): + centerline_width.plotCenterlineWidth(river_object=river_class_example, + show_plot=invalid_input) def test_plotCenterlineWidth_coordinateUnitInvalidOption(): with pytest.raises(ValueError, match=re.escape("[coordinate_unit]: Must be an available option in ['Decimal Degrees', 'Relative Distance'], current option = 'Invalid Option'")): centerline_width.plotCenterlineWidth(river_object=river_class_example, - coordinate_unit="Invalid Option") + coordinate_unit="Invalid Option") @pytest.mark.parametrize("invalid_input, error_output", invalid_non_str_options) def test_plotCenterlineWidth_coordinateUnitInvalidTypes(invalid_input, error_output): with pytest.raises(ValueError, match=re.escape(f"[coordinate_unit]: Must be a str, current type = '{error_output}'")): centerline_width.plotCenterlineWidth(river_object=river_class_example, - coordinate_unit=invalid_input) + coordinate_unit=invalid_input) diff --git a/centerline_width/riverCenterlineClass.py b/centerline_width/riverCenterlineClass.py index 563e6ff..5f0338e 100644 --- a/centerline_width/riverCenterlineClass.py +++ b/centerline_width/riverCenterlineClass.py @@ -112,6 +112,7 @@ def plotCenterline(self, display_all_possible_paths=False, plot_title=None, save_plot_name=None, + show_plot=True, display_voronoi=False, coordinate_unit="Decimal Degrees"): centerline_width.plotCenterline(river_object=self, @@ -124,6 +125,7 @@ def plotCenterline(self, plot_title=plot_title, save_plot_name=save_plot_name, display_voronoi=display_voronoi, + show_plot=show_plot, coordinate_unit=coordinate_unit) def plotCenterlineWidth(self, @@ -137,6 +139,7 @@ def plotCenterlineWidth(self, remove_intersections=False, dark_mode=False, equal_axis=False, + show_plot=True, coordinate_unit="Decimal Degrees"): centerline_width.plotCenterlineWidth(river_object=self, plot_title=plot_title, @@ -149,6 +152,7 @@ def plotCenterlineWidth(self, remove_intersections=remove_intersections, dark_mode=dark_mode, equal_axis=equal_axis, + show_plot=show_plot, coordinate_unit=coordinate_unit) def riverWidthFromCenterline(self, diff --git a/river_centerline_width_example.py b/river_centerline_width_example.py index 047dc42..79fda43 100644 --- a/river_centerline_width_example.py +++ b/river_centerline_width_example.py @@ -70,6 +70,7 @@ plot_title=None, dark_mode=True, equal_axis=True, + show_plot=True, coordinate_unit=coord_type) transect = 2 @@ -86,8 +87,9 @@ remove_intersections=True, dark_mode=True, equal_axis=False, + show_plot=True, coordinate_unit=coord_type) - exit() + # Return width line for each centerline coordinates river_width_dict = river.riverWidthFromCenterline(transect_span_distance=transect, transect_slope=slope_type,