Package: centerline-width
Centerline-WidthFind the centerline and width of rivers based on the latitude and longitude positions from the right and left bankConvert raw data from Google Earth Pro to CSVextractPointsToTextFile()convertColumnsToCSV()Find centerline and width of riverplotCenterline()plotCenterlineWidth()riverWidthFromCenterline()centerlineVoronoicenterlineEqualDistancecenterlineEvenlySpacedcenterlineSmoothedcenterlineLengthrightBankLengthleftBankLengthExport centerline to .CSV and .MAT filessaveCenterlineCSV()saveCenterlineMAT()River Outlined in Google Earth ProGenerated Centerline for the RiverbankNOTE: This is Beta quality software that is being actively developed, use at your own risk. This project is not supported or endorsed by either JPL or NASA. The code is provided “as is”, use at your own risk.InstallPyPi pip install atpypi.org/project/centerline-width/pip install centerline-widthQuickstart: centerline-widthThe core of centerline-width works with a .csv file of the left and right bank latitude/longitudes. So, if starting from Google Earth Pro, two .kml must first be translated to a single .csv fileimportcenterline_widthcenterline_width.extractPointsToTextFile(left_kml="left_bank.kml",right_kml="right_bank.kml",text_output_name="river_coordinates_output.txt")centerline_width.convertColumnsToCSV(text_file="river_coordinates_output.txt")Then once the .csv file is created, to run the centerline-width functions, generate a river object from theriver_coordinates_output.csvriver_object=centerline_width.riverCenterline(csv_data="river_coordinates_output.csv")To plot the centerline, run theplotCenterline()function fromriver_objectcreated. By default, will display withDecimal Degrees(latitude/longitude) coordinatesriver_object.plotCenterline()To plot the width of the river at intervals along the bank, runplotCenterlineWidthWhileapply_smoothing,remove_intersections, anddisplay_true_centerlineare optional, they are recommended to generate a minimal width diagramriver_object.plotCenterlineWidth(apply_smoothing=True,remove_intersections=True,display_true_centerline=False)It is possible to also display all the coordinates as aRelative Distance, where all the coordinates are converted to a relative distance (in meters) from the first point on the left bankriver_object.plotCenterline(coordinate_unit="Relative Distance")For more details to fix unexpected behavior or error code:Debugging, Error Handling, and Edge CasesFor a complete example script to run centerline-width:centerline_width_example_script.pywithexample outputsPreprocessingGenerating KML files from Google Earth ProRiverbanks can be defined here as the high-contrast boundary between active/recently-active flow and the surrounding landscape. We will be mapping the right and left bank separatelyStep 1Map the left bank using the path tool. You can zoom into the river using the scroll-wheel. Flatten the mapping projection by pressingu. Leave the pop-up window open while you are mapping. You can erase the last point placed in a path by left-clicking. To erase a different point on the path, select the point with a right-click and then erase with a left-click and then select the last point to continue mapping downstream. To move a point, select it with a right-click. The selected point is highlighted blue, other points in the current path are highlighted red. The mapped distance downstream is shown in the measurements tab in the path tool pop-up window. When done mapping the left bank, close the pop-up window for the pathStep 2Save the path to a .kml file by right clicking on the path in Places and selecting 'save as'. Be sure to save as a .kml and not .kmz file. It is generally good practice to do this for each new path to ensure that no work is lost if Google Earth Pro crashesStep 3Repeat (1) and (2) for the right bank by starting a new pathConvert KML files to Text FileConvert two .kml files from Google Earth Pro (for the left and right bank) and export the coordinates into a text fileextractPointsToTextFile(left_kml=None,
			right_kml=None,
			text_output_name=None)[REQUIRED]left_kml (string): File location of the kml file for left bank[REQUIRED]right_kml (string): File location of the kml file for right bank[REQUIRED]text_output_name (string): Output file name (and location)importcenterline_widthcenterline_width.extractPointsToTextFile(left_kml="leftbank.kml",right_kml="rightbank.kml",text_output_name="data/river_coords_output.txt")Output: The text filedata/river_coords_output.txtwith the headersllat, llon, rlat, rlon(for the left latitude, left longitude, right latitude, and right longitude)Example:llat       llon      rlat       rlon
30.037581 -92.868569 30.119804 -92.907933
30.037613 -92.868549 30.119772 -92.907924
30.037648 -92.868546 30.119746 -92.907917
30.037674 -92.868536 30.119721 -92.907909
30.037702 -92.868533 30.119706 -92.907905Converted Text File to CSVConvert a text file with coordinates for a left and right bank's latitude and longitude to a csv file with columns for the left bank latitude (llat), left bank longitude (llon), right bank latitude (rlat), right bank longitude (rlon)convertColumnsToCSV(text_file=None, flipBankDirection=False)[REQUIRED]text_file (string): File location of the text file to convert[OPTIONAL] flipBankDirection (boolean): If the latitude/longitude of the banks are generated in reverse order, flip the final values so left/right bank are in orderScripts expects data as a list of point for left and right banks:Header: llat, llon, rlat, rlonimportcenterline_widthcenterline_width.convertColumnsToCSV(text_file="data/river_coords.txt",flipBankDirection=True)Converts text file:llat       llon      rlat       rlon
30.037581 -92.868569 30.037441 -92.867476
30.037613 -92.868549 30.037448 -92.867474
30.037648 -92.868546 30.037482 -92.867449
30.037674 -92.868536 30.037506 -92.867432
30.037702 -92.868533 30.037525 -92.867430To a CSV file:llat,llon,rlat,rlon
30.037581,-92.868569,30.037441,-92.867476
30.037613,-92.868549,30.037448,-92.867474
30.037648,-92.868546,30.037482,-92.867449
30.037674,-92.868536,30.037506,-92.867432
30.037702,-92.868533,30.037525,-92.867430Output: A csv filedata/river_coords.csvwith the headersllat, llon, rlat, rlonCenterline and WidthRiver ObjectFirst, generate a river object to contain river data and available transformationscenterline_width.riverCenterline(csv_data=None,
				optional_cutoff=None,
				interpolate_data=False,
				interpolate_n=5,
				interpolate_n_centerpoints=None,
				equal_distance=10,
				ellipsoid="WGS84")[REQUIRED]csv_data (string): File location of the text file to convert[OPTIONAL] optional_cutoff (int): Include only the first x number of the data to chart (useful for debugging)[OPTIONAL] interpolate_data (boolean): Interpolate between existing data by adding additional points[OPTIONAL] interpolate_n (int): Number of additional points to add between existing data, defaults to 5 (note: larger numbers will take exponentially longer to run, recommends less than 15)[OPTIONAL] interpolate_n_centerpoints (int): Number of points used to interpolate the Voronoi centerline, defaults to the the length of the data frame (df_len)[OPTIONAL] equal_distance (int): Equal distance between points (in meters) used to interpolate the Voronoi centerline, defaults 10 meters[OPTIONAL] ellipsoid (string): Ellipsoid definition of Earth to provide size and shape for built-in functions to convert degrees to meters, options include (is sensitive to case): ["GRS80", "airy", "bessel", "clrk66", "intl", "WGS60", "WGS66", "WGS72", "WGS84", "sphere"] for more details:"Built-in ellipsoid defintions", defaults to "WGS84"Equal Distance - Equal linear distance between pointsequal_distancewill generate points along the centerline that are an equal linear distance from one another in meters. Whenequal_distance=5each point will be 5 meters apartequal_distance=5equal_distance=20The red pins represent the equal distance centerline coordinates produced by centerline-width. The yellow line is the distance measured in Google Earth Pro between the points. The mapped river banks are in purple.Interpolation - A solution for sparse datainterpolate_datais an option that can be used to find a centerline when the existing data generates a Voronoi graph that is jagged or contains gaps due to the combination of sparse data and a narrow river (See: Debugging, Error Handling, and Edge Cases - Fix Gaps and Jagged Centerlines). By default,interpolate_data=Truewill add 5 additional points between each existing point but can be increased or decreased by modifying theinterpolate_noptioninterpolate_n_centerpointsis an option that can be used to increase the resolution (number of points) of the centerline found by the Voronoi vertices. By default, will evenly space out to the size of the dataframe. Can artificially increase the amount of width lines generated by increasing the number of center points. Wheninterpolate_n_centerpointsincreases, the number of width lines generated will increase (and visa versa)interpolate_n_centerpoints=75interpolate_n_centerpoints=200Object (class) useful attributes:centerlineVoronoi (list of tuples): List of the latitude and longitude coordinates of the centerline generated by Voronoi diagramscenterlineEqualDistance (list of tuples): List of the latitude and longitude coordinates of the centerline generated by equal distances between coordinates from the Voronoi diagramscenterlineEvenlySpaced (list of tuples): List of the latitude and longitude coordinates of the centerline generated by evenly spacing out points generated by the Voronoi diagramscenterlineSmoothed (list of tuples): List of the latitude and longitude coordinates of the centerline generated by smoothing out the evenly spaced-out points generated by the Voronoi diagramscenterlineLength (float): Length of the centerline of the river (in km)rightBankLength (float): Length of the right bank of the river (in km)leftBankLength (float): Length of the left bank of the river (in km)centerlineVoronoiRelative (list of tuples): List of the relative distance coordinates of the centerline generated by Voronoi diagramscenterlineEqualDistanceRelative (list of tuples): List of the relative distance coordinates of the centerline generated by equal distances between coordinates from the Voronoi diagramscenterlineEvenlySpacedRelative (list of tuples): List of the relative distance coordinates of the centerline generated by evenly spacing out points generated by the Voronoi diagramscenterlineSmoothedRelative (list of tuples): List of the relative distance coordinates of the centerline generated by smoothing out the evenly spaced out points generated by the Voronoi diagramsObject (class) additional attributes:river_name (string): name of object, set to the csv_data stringleft_bank_coordinates (list of tuples): list of latitude/longitude coordinates of the left bank generated from the csv file ([(x, y), (x, y)])right_bank_coordinates (list of tuples) list of latitude/longitude coordinates of the right bank generated from the csv file ([(x, y), (x, y)])left_bank_relative_coordinates (list of tuples): list of relative distances coordinates of the left bank, measured as the distance in meters from the first point on the left bank ([(x, y), (x, y)])right_bank_relative_coordinates (list of tuples): list of relative distances coordinates of the right bank, measured as the distance in meters from the first point on the left bank ([(x, y), (x, y)])df_len (int): Length of the data frame of the csv data (spliced by the optional_cutoff)equal_distance (int): Distance between points (in meters) used in centerlineEqualDistance, defaults to points every 10 metersellipsoid (string): Built-in ellipsoid definition of Earth to determine how degrees are converted to meters used by centerlineEqualDistance, defaults to "WGS84"bank_polygon (Shapley Polygon): Multi-sided polygon generated to encapsulate the latitude/longitude coordinate riverbank (used to define an inside and an outside of the river)bank_polygon_relative (Shapley Polygon): Multi-sided polygon generated to encapsulate the relative distance coordinate riverbank (used to define an inside and an outside of the river)top_bank (Shapley Linestring): Linestring that represents the top of the river/polygon for the latitude/longitude coordinate systemtop_bank_relative (Shapley Linestring): Linestring that represents the top of the river/polygon for the relative distance coordinate systembottom_bank (Shapley Linestring): Linestring that represents the bottom of the river/polygon for the latitude/longitude coordinate systembottom_bank_relative (Shapley Linestring): Linestring that represents the bottom of the river/polygon for the relative distance coordinate systemstarting_node (tuple): Tuple of the starting position (latitude and longitude) of the centerline pathstarting_node_relative (tuple): Tuple of the starting position (relative distance x and relative distance y) of the centerline pathending_node (tuple): Tuple of the end position (latitude and longitude) of the centerline pathending_node_relative (tuple): Tuple of the end position (relative distance x and relative distance y) of the centerline pathbank_voronoi (scipy Voronoi object): Voronoi generated by left/right banks of the latitude/longitude coordinate systembank_voronoi_relative (scipy Voronoi object): Voronoi generated by left/right banks of the relative distance coordinate systemx_voronoi_ridge_point (list of tuples): X positions on Voronoi ridge (starting Latitude position to ending Latitude position)y_voronoi_ridge_point (list of tuples): Y position on Voronoi ridge (starting Longitude position to ending Longitude position)x_voronoi_ridge_point_relative (list of tuples): X positions on Voronoi ridge (starting Relative Distance X position to ending Relative Distance X position)y_voronoi_ridge_point_relative (list of tuples): Y position on Voronoi ridge (starting Relative Distance Y position to ending Relative Distance Y position)interpolate_data (boolean): if interpolating between existing data, defaults to Falseinterpolate_n (int): specifies how many additional points will be added between points along the riverbank when interpolating data, defaults to 5interpolate_n_centerpoints (int): specifies how many points will be used to interpolate the Voronoi centerline, defaults to the length of the data frame (df_len)importcenterline_widthriver_object=centerline_width.riverCenterline(csv_data="data/river_coords.csv")Coordinates of CenterlineReturn the coordinates of the centerline based on the left and right banks with eitherDecimal Degree(latitude/longitude) orRelative Distance(meters)Types of CenterlinesThere are four types of centerline coordinates formed from the riverbank dataVoronoi centerline: centerline generated from where Voronoi vertices intersect within the riverEqual Distance Centerline: centerline based on Voronoi centerline but each point is equally spaced out from the previous (in meters) and takes into account the radius of the Earth to convert degrees to meters (example below:equal_distance=10)Evenly Spaced Centerline: centerline based on Voronoi centerline but evenly spaced with a fixed number of points (example:interpolate_n_centerpoints=200)Smoothed Centerline: centerline generated from the evenly spaced centerline but smoothed by a b-splineBy default, coordinates are formed inDecimal Degrees, but can be set toRelative Distance. Relative Distance measures the distance (in meters) of a point from the first point on the left bankCenterline coordinates are formed by the Voronoi verticesriver_object.centerlineVoronoiriver_object.centerlineVoronoiriver_object.centerlineVoronoiRelativeCenterline coordinates are formed by Equally Distanced vertices, set byequal_distanceriver_object.centerlineEqualDistanceriver_object.centerlineEqualDistanceriver_object.centerlineEqualDistanceRelativeCenterline coordinates are formed by Evenly Spaced vertices, set byinterpolate_n_centerpointsriver_object.centerlineEvenlySpacedriver_object.centerlineEvenlySpacedriver_object.centerlineEvenlySpacedRelativeCenterline coordinates are formed from Smoothed verticesriver_object.centerlineSmoothedriver_object.centerlineSmoothedriver_object.centerlineSmoothedRelativeExample:importcenterline_widthriver_object=centerline_width.riverCenterline(csv_data="data/river_coords.csv")river_centerline_coordinates=river_object.centerlineVoronoiOutput is a list of tuples: (example)[(-92.86788596499872, 30.03786596717931), (-92.86789573751797, 30.037834641974108), (-92.8679141386283, 30.037789636848878), (-92.8679251193248, 30.037756853899904), (-92.86796903819089, 30.03765423778148), (-92.86797335733262, 30.037643336049054), (-92.8679920356456, 30.037592224469797), (-92.86800576063828, 30.037555441489403), (-92.86800841510367, 30.037546512833107), (-92.8680119498663, 30.03753043193875)]Save Centerline Coordinates to a .CSV FileSave the centerline coordinates to a csv file with columns for latitude and longitude. This is the file format for a table of (latitude,longitude) pairs accepted to import back into Google Earth Pro.saveCenterlineCSV(save_to_csv=None, centerline_type="Voronoi", coordinate_unit="Decimal Degrees")[REQUIRED]save_to_csv (string): CSV filename, requires a .csv extension[OPTIONAL] centerline_type (string): Centerline type to save to CSV (not case-sensitive), options: ["Voronoi", "Evenly Spaced", "Smoothed", "Equal Distance"], defaults to "Voronoi"[OPTIONAL] latitude_header (string): Column header for latitude values, defaults to<centerline_type> Centerline Latitude (Deg)or<centerline_type> Relative Distance Y (from Latitude) (m)[OPTIONAL] longitude_header (string): Column header for longitude values, defaults to<centerline_type> Centerline Longitude (Deg)or<centerline_type> Relative Distance X (from Longitude) (m)[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"importcenterline_widthriver_object=centerline_width.riverCenterline(csv_data="data/river_coords.csv")river_object.saveCenterlineCSV(save_to_csv="centerline_coordinates.csv",centerline_type="Smoothed")Returns a csv with the Latitude and Longitude coordinates of the specified centerline with column headers with centerline type:Smoothed Centerline Latitude (Deg), Smoothed Centerline Longitude (Deg)Note: it is best practice to plot the centerline withplotCenterline()to ensure that the results saved are as expectedSave Centerline Coordinates to a .MAT FileSave the centerline coordinates to a .mat file with columns for latitude and longitudesaveCenterlineMAT(save_to_mat=None, centerline_type="Voronoi", coordinate_unit="Decimal Degrees")[REQUIRED]save_to_mat (string): MAT filename, requires a .mat extension[OPTIONAL] centerline_type (string): Centerline type to save to MAT (not case-sensitive), options: ["Voronoi", "Evenly Spaced", "Smoothed", "Equal Distance"], defaults to "Voronoi"[OPTIONAL] latitude_header (string): Column header for latitude values, defaults to<centerline_type>_Centerline_Latitude_(Deg)or<centerline_type>_Relative_Distance_Y_From_Latitude_m(cannot include spaces or special characters)[OPTIONAL] longitude_header (string): Column header for Longitude values, defaults to<centerline_type>_Centerline_Longitude_(Deg)or<centerline_type>_Relative_Distance_X_From_Longitude_m(cannot include spaces or special characters)[OPTIONAL] coordinate_unit (string): Coordinates of the river are return as "Decimal Degrees" (latitude/longtidue) or converted to a distance from the first point on the left bank as "Relative Distance", defaults to "Decimal Degrees"importcenterline_widthriver_object=centerline_width.riverCenterline(csv_data="data/river_coords.csv")river_object.saveCenterlineMAT(save_to_mat="centerline_coordinates.mat",centerline_type="Smoothed")Returns a .mat file with the Latitude and Longitude coordinates of the specified centerline with column headers with centerline type:Smoothed_Centerline_Latitude_(Deg), Smoothed_Centerline_Longitude_(Deg)Note: it is best practice to plot the centerline withplotCenterline()to ensure that the results saved are as expectedLength of CenterlineReturn the length of the centerline found between the left and right bank generated by the Voronoi diagramriver_object.centerlineLengthLength returned in kilometersimportcenterline_widthriver_object=centerline_width.riverCenterline(csv_data="data/river_coords.csv")river_centerline_length=river_object.centerlineLengthThe length of the river centerline returns215.34700589636674kmPlot Centerline in MatplotlibPlot the centerline created from a list of right and left banksplotCenterline(centerline_type="Voronoi",
		marker_type="line",
		centerline_color="black",
		dark_mode=False,
		equal_axis=False,
		display_all_possible_paths=False, 
		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"[OPTIONAL] marker_type (string): Graph type (not case-sensitive), options: ["Line", "Scatter"], defaults to "Line"[OPTIONAL] centerline_color (string): Color of centerline coordinates on graph (not case-sensitive), options:matplotlib named colors, defaults to "black"[OPTIONAL] dark_mode (bool): Change plot to a black ground (and override ifcenterline_color="black"tocenterline_color="white"), defaults to False[OPTIONAL] equal_axis (bool): Set x/y axes in plot to be equal, defaults to False[OPTIONAL] display_all_possible_paths (boolean): Display all possible paths, not just the centerline (useful for debugging), defaults to False[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"importcenterline_widthriver_object=centerline_width.riverCenterline(csv_data="data/river_coords.csv")river_object.plotCenterline()Output:Plot Centerline Width Lines in MatplotlibPlot the Centerline Width LinesPlot the width of the river based on the centerlineplotCenterlineWidth(plot_title=None, 
		save_plot_name=None, 
		display_true_centerline=True,
		transect_span_distance=3,
		transect_slope="Average",
		apply_smoothing=False,
		flag_intersections=True,
		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"[OPTIONAL] save_plot_name (string): Save the plot with a given name and location[OPTIONAL] display_true_centerline (boolean): Display generated true centerline based on Voronoi diagrams, defaults to True[OPTIONAL] transect_span_distance (int): Number n points around a center point to determine the slope (increase to decrease the impact of sudden changes), defaults to 3, must be greater than 1 (since the slope is found from the difference in position between two points)[OPTIONAL] transect_slope (str): Determine how the width lines are generated, either by averaging all slopes "Average" or directly from the first to last point in the span distance as "Direct", defaults to "Average"[OPTIONAL] apply_smoothing (boolean): Apply a B-spline smoothing to centerline[OPTIONAL] flag_intersections (boolean): Display intersecting width lines as red in graph, defaults to True[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 ifcenterline_color="black"tocenterline_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_centerlineThe width lines are generated from the evenly spaced coordinate (by default) or with the smoothed coordinates (whenapply_smoothing=True), but display_true_centerline will overlay the Voronoi centerline on top of the plotdisplay_true_centerline=Truedisplay_true_centerline=Falseapply_smoothingapply_smoothing applies a spline to smooth the centerline points created by the Voronoi vertices. This reduces the noise of the slopes and can create width lines that are less susceptible to small changes in the bankapply_smoothing=Falseapply_smoothing=Truetransect_span_distanceTransect span describes the number of points that are averaged to generate the slope of the width line (example: transect_span_distance=3, average of three slopes). The slope of the width line is  orthogonal to the average slopes measured along the transect spanIf the span is odd then the width line will be generated at the position of the middle of the span on n/2. If the span is even, then the width line will be generated at 1 + n/2, for example,[A, B, C, D]will generated a width line at pointCtransect_span_distance=6transect_span_distance=30transect_slopeThe width lines are generated as perpendicular to the slopes of the points acrosstransect_span_distanceBy default,transect_slope="Average"where the width lines are perpendicular to the average slopes of the across span distance. For example:[A, B, C, D] = avg( slope([A, B]) + slope([B, C]) + slope([C+D]) )Optionally, iftransect_slope="Direct"then the width lines will be perpendicular to slope of the first and last point. For example:[A, B, C, D] = slope([A, D])to avoid being susceptible to rapid small changes along the centerlinetransect_slope="Average"transect_slope="Direct"remove_intersectionsremove_intersections will remove the width lines that intersect other lines (that could be creating unrepresentative long width lines). Intersections are removed first in order from most to least number of intersections and then based on the longer of two intersecting lines. This ensures that the most width lines as possible are keptIntersecting lines are flagged in red by default (flag_intersections=True)remove_intersections=Falseremove_intersections=Truedark_modedark_mode will change the default Matplotlib background black and swapcenterline_colorfrom black to whitedark_mode=Falsedark_mode=Trueequal_axisequal_axis will set the x and y axis of the plot to be equal. Useful to show the perpendicular width lines as perpendicular since it can appear distorted by default in Matplotlibequal_axis=Falseequal_axis=Truecoordinate_unitTwo 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 bankcoordinate_unit="Decimal Degrees"remove_intersections="Relative Distance"importcenterline_widthriver_object=centerline_width.riverCenterline(csv_data="data/river_coords.csv")river_object.plotCenterlineWidth(apply_smoothing=True,remove_intersections=True,display_true_centerline=False)Return Width of RiverReturn the width of the river at each (evenly spaced or smoothed) with coordinates where width line intersects either the centerline,(Centerline Longitude, Centerline Latitude) : width, or riverbanks,((Right Bank Longitude, Right Bank Latitude), (Left Bank Longitude, Left Bank Latitude)) : widthin kilometersriverWidthFromCenterline(transect_span_distance=3,
			transect_slope="Average",
			apply_smoothing=True,
			remove_intersections=False,
			coordinate_unit="Decimal Degrees",
			coordinate_reference="Centerline",
			save_to_csv=None)[OPTIONAL] transect_span_distance (int): Number n points around a center point to determine the slope (increase to decrease the impact of sudden changes), defaults to 3, must be greater than 1 (since the slope is found from the difference in position between two points)[OPTIONAL] transect_slope (str): Determine how the width lines are generated, either by averaging all slopes "Average" or directly from the first to last point in the span distance as "Direct", defaults to "Average"[OPTIONAL] apply_smoothing (boolean): Apply a B-spline smoothing to centerline[OPTIONAL] remove_intersections (boolean): Iterative remove intersecting lines, to maintain the most width lines, but return only non-intersecting width lines, defaults to True[OPTIONAL] coordinate_unit (string): Coordinates of the river are return as "Decimal Degrees" (latitude/longtidue) or converted to a distance from the first point on the left bank as "Relative Distance", defaults to "Decimal Degrees"[OPTIONAL] coordinate_reference (string): Reference where the width line intersects the river, either along the centerline ("Centerline") as(Centerline Longitude, Centerline Latitude) : width, or riverbanks ("Banks") as((Right Bank Longitude, Right Bank Latitude), (Left Bank Longitude, Left Bank Latitude)) : width[OPTIONAL] save_to_csv (string): CSV filename to output width dictionary, defaults to None (no file is saved), requires a .csv extension (Column Headers automatically generated based oncoordinate_unitandcoordinate_reference)Important note, when usingapply_smoothing=True, the centerline generated is the result of evenly spaced coordinates generated from the original Voronoi coordinates, so the smoothed coordinates may not match exactly to the original centerline coordinates. Whenapply_smoothing=False, width lines are generated from the evenly spaced centerline coordinatesimportcenterline_widthriver_object=centerline_width.riverCenterline(csv_data="data/river_coords.csv")river_width_dict=river_object.riverWidthFromCenterline(transect_span_distance=3,apply_smoothing=True,coordinate_reference="Centerline",remove_intersections=True)Width dictionary ={(-92.86792084788995, 30.037769672351182): 0.10969163557087018, (-92.86795038641004, 30.03769867854198): 0.10794219579997719}Note: it is best practice to plot the centerline and width with same arguments inplotCenterlineWidth()to ensure that the results whensave_to_csv=Trueare as expectedDocumentation and Algorithm to Determine CenterlineThe 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 centerlineRight and Left bank points are plotted (X-Axis for Latitude, Y-Axis for Longitude)Generate a polygon to encapsulate the river between the right and left banks to define in and outside of riverGenerate a Voronoi diagram based on the points along the riverbanksDisplay Voronoi ridge vertices that lie within the polygon (within the riverbanks)Filter out any point pairs that only have one connection to filter out the short dead end pathsWith 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 outFind the starting and ending node based on distance from the top and bottom of polygonThe starting/ending node is defined by the vertex closest to the top/bottom of the polygon along the longest pathFind the shortest path from the starting node to the ending node (Dijkstra's Algorithm)Points on RiverbankNetworkX Graph of Points on RiverbankDisplay the centerline found by connecting the starting/ending node with the shortest pathThis 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 centerlinePoints 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:Vertices that have at least two connections (that would create gaps):Debugging, Error Handling, and Edge CasesWide Start/End of RiverIf the data starts or ends with a large width, it is possible for the starting/ending nodes to end up in the wrong positionCurrently, the starting node is determined by the closest node on the path to the top of the bank (in green) and the ending node is determined by the closest node on the path to the bottom of the bank (in red) that sits along the longest pathInvalid PolygonA polygon is formed to encapsulate the river with the given data (to determine the inside and outside of the river). The top and bottom are connected by a straight line from the start/end of the available data. As a result, it is possible for this straight line to overlap and create an invalid polygon.A polygon is invalid if it overlaps within itself:In this example, the polygon is invalid, but with such a small overlap it is still able to find a valid pathWith limited data, the polygon will overlap more dramatically and will struggle to find a valid centerline:Invalid CenterlineIf the data is too small, a centerline and its coordinates cannot not be found (since only a single Voronoi vertex exists within the polygon and after dead ends are filtered)CRITICAL ERROR, Polygon too short for the Voronoi diagram generated (no starting node found), unable to plot centerline. Set displayVoronoi=True to view vertices. Can typically be fixed by adding more data to expand range.Can be fixed by expanding the data until the polygon is large enough to contain at least two different vertex pointsInvalid Top and Bottom Bank Positions (flipBankDirection = True)Error:WARNING: Invalid Polygon Due to Flipped Banks, fix recommendation: rerun convertColumnsToCSV() and set flipBankDirection=True (or reset to default 'False' if currently set to flipBankDirection=True)If the data for the left and right riverbanks are generated in reverse order, they will be read in the incorrect order and the graph will find the invalid top and bottom of the bankIf the latitude/longitude of the banks are generated in reverse order, flip the final values so left/right bank are in orderThis can be fixed by using the flipBankDirection optional argumentcenterline_width.convertColumnsToCSV(text_file="data_example.txt", flipBankDirection=True)Invalid Smoothed CenterlineThe smoothed centerline (river_object.centerlineSmoothed) can end up lying outside the river if the centerline data points are sparse in a narrow river. If more than two points in the smoothed centerline lie outside the river, a warning will be thrownExample Error:WARNING: Partially invalid smoothed centerline due to sparse centerline data (6 points lie outside the polygon), fix recommendation: rerun riverCenterline to create river object with interpolate_n_centerpoints set to 62+By default,interpolate_n_centerpointsis set to None and not additional points will be added between the existing points along the centerline. By adding additional points between the existing centerline, the smoothed centerline can be fixed to stay within the polygon. This fix is set by creating a river object,centerline_width.riverCenterline, withinterpolate_n_centerpoints=65(with the recommended 62+) to fix for centerline coordinates that lie outside the polygoninterpolate_n_centerpoints = Nonedoes not interpolate data points, so the size will be set to the number of fixed points when creating the evenly spaced coordinates (equal to the size of the data frame)interpolate_n_centerpoints = Noneinterpolate_n_centerpoints = 65For very narrow rivers, this problem can become extreme and pronouncedBy increasing the interpolation between the centerline points, the smoothed centerlines will be forced within the polygon and reduce the number of points outside of the polygon. By default, this warning will be thrown if more than 2 points are outside of polygon, so as long as more than 2 points lie outside the polygon, the warning will recommend doubling the amount of centerline pointsFix Gaps and Jagged CenterlinesGaps formed can cause part of the centerline to be skipped due to sparse data. As a result, the start and end of the centerline can skip parts at the beginning or end of a riverSet river object created bycenterline_width.riverCenterlinetointerpolate_data=Trueto fix for jagged edges or gaps formed by the interaction of sparse data and narrow banksriver_object=centerline_width.riverCenterline(csv_data="data/river_coords.csv",interpolate_data=True)interpolate_data = Falseinterpolate_data = TrueThe number of additional points added by interpolating can be adjusted withinterpolate_n, but defaults to add 5 additional points between valuesDevelopment EnvironmentTo run or test againstcenterline-widthgithub repo/fork, a development environment can be created via conda/minicondaFirst,install MinicondaThen, using the existingenvironment.yml, a new conda environment can be create to run/test scripts againstconda env create --file environment.ymlOnce the environment has been built, activate the environment:conda activate centerline_widthTo run existing and new tests from the root directory:python -m pytestCitationsOriginally a Python implementation ofR-Code CMGO(Golly et al. 2017) but has been extensively expanded:Golly, A. and Turowski, J. M.: Deriving principal channel metrics from bank and long-profile geometry with the R package cmgo, Earth Surf. Dynam., 5, 557-570,https://doi.org/10.5194/esurf-5-557-2017, 2017.Acknowledging SoftwarePlease acknowledge the use of this software in any publications:"River centerline/width extraction software was provided by C. Y. Schneck and U. G. Schneck, and is available at URL: https://github.com/cyschneck/centerline-width."Please send a copy of such publications to:cyschneck@gmail.comandugschneck@gmail.comThis material is based upon work supported by the National Science Foundation Graduate Fellowship under Grant No. 2141064. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the authors and do not necessarily reflect the views of the National Science Foundation.Bug and Feature RequestSubmit a bug fix, question, or feature request as aGithub Issueor tougschneck@gmail.com/cyschneck@gmail.com