Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

centerline-width

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

centerline-width

A Python package to find the centerline and width of rivers based on the latitude and longitude of the right and left bank

  • 2.0.0
  • PyPI
  • Socket score

Maintainers
1

Centerline-Width

PyPi license repo-status NSF-2141064 PyPi-Versions pytests pre-commit codecov

Find the centerline and width of rivers based on the latitude and longitude positions from the right and left bank

  • Convert raw data from Google Earth Pro to CSV
    • kml_to_csv()
    • txt_to_csv()
  • Find centerline and width of river
    • plot_centerline()
    • plot_centerline_width()
    • width()
    • centerline_voronoi
    • centerline_equal_distance
    • centerline_evenly_spaced
    • centerline_smoothed
  • Return river features
    • centerline_length
    • right_bank_length
    • left_bank_length
    • area
    • sinuosity
    • incremental_sinuosity()
  • Export centerline to .CSV and .MAT files
    • save_centerline_csv()
    • save_centerline_mat()
River Outlined in Google Earth ProGenerated Centerline for the Riverbank
river_google_earth+pngriver_centerline+png

[!NOTE] 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.

Install

PyPi pip install at pypi.org/project/centerline-width/

pip install centerline-width

Quickstart: centerline-width

The 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 file

import centerline_width
centerline_width.kml_to_csv(left_kml="left_bank.kml",
                    right_kml="right_bank.kml",
                    text_output_name="river_coordinates_output.csv")

Then once the .csv file is created, to run the centerline-width functions, generate a river object from the river_coordinates_output.csv

river_object = centerline_width.CenterlineWidth(csv_data="river_coordinates_output.csv")

To plot the centerline, run the plot_centerline() function from river_object created. By default, will display with Decimal Degrees (latitude/longitude) coordinates

river_object.plot_centerline()

river_coords_centerline+png

To plot the width of the river at intervals along the bank, run plot_centerline_width()

While apply_smoothing, remove_intersections, and display_true_centerline are optional, they are recommended to generate a minimal width diagram

river_object.plot_centerline_width(apply_smoothing=True, remove_intersections=True, display_true_centerline=False)

river_coords_centerline+png

It is possible to also display all the coordinates as a Relative Distance, where all the coordinates are converted to a relative distance (in meters) from the first point on the left bank

river_object.plot_centerline(coordinate_unit="Relative Distance")

river_coords_centerline+png

For more details to fix unexpected behavior or error code: Debugging, Error Handling, and Edge Cases

For a complete example script to run centerline-width: centerline_width_example_script.py with example outputs

Preprocessing

Generating KML files from Google Earth Pro

Riverbanks 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 separately

Step 1

Map the left bank using the path tool. You can zoom into the river using the scroll-wheel. Flatten the mapping projection by pressing u. 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 path example+png

Step 2

Save 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 crashes

Step 3

Repeat (1) and (2) for the right bank by starting a new path

Convert KML files to CSV File

Convert two .kml files from Google Earth Pro (for the left and right bank) and export the coordinates into a csv file

kml_to_csv(left_kml: str = None,
           right_kml: str = None,
           flip_direction: bool = False,
           csv_output: str = 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] csv_output (string): Output file name (and location)
  • [OPTIONAL] flip_direction (boolean): If the latitude/longitude of the banks are generated in reverse order, flip the final values so left/right bank are in order

Scripts expects data as a list of point for left and right banks:

  • Header: llat, llon, rlat, rlon
import centerline_width
centerline_width.kml_to_csv(left_kml="leftbank.kml",
                    right_kml="rightbank.kml",
                    csv_output="data/river_coords_output.csv")

Example:

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.867430

Output: A csv file data/river_coords.csv with the headers llat, llon, rlat, rlon

Converted Text File to CSV

Convert 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)

txt_to_csv(txt_input: str = None,
           flip_direction: bool = False
  • [REQUIRED] txt_input (string): File location of the text file to convert
  • [OPTIONAL] flip_direction (boolean): If the latitude/longitude of the banks are generated in reverse order, flip the final values so left/right bank are in order

Scripts expects data as a list of point for left and right banks:

  • Header: llat, llon, rlat, rlon
import centerline_width
centerline_width.txt_to_csv(txt_input="data/river_coords.txt",
                flip_direction=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.867430

To 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.867430

Output: A csv file data/river_coords.csv with the headers llat, llon, rlat, rlon

Centerline and Width

River Object

First, generate a river object to contain river data and available transformations

centerline_width.CenterlineWidth(csv_data=None,
                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] 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 definitions", defaults to "WGS84"

Equal Distance - Equal linear distance between points

equal_distance will generate points along the centerline that are an equal linear distance from one another in meters. When equal_distance=5 each point will be 5 meters apart

equal_distance=5equal_distance=20
example+pngexample+png

The 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 data

interpolate_data is 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=True will add 5 additional points between each existing point but can be increased or decreased by modifying the interpolate_n option

interpolate_n_centerpoints is 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. When interpolate_n_centerpoints increases, the number of width lines generated will increase (and visa versa)

interpolate_n_centerpoints=75interpolate_n_centerpoints=200
example+pngexample+png

Object (class) useful attributes:

  • centerline_voronoi (list of tuples): List of the latitude and longitude coordinates of the centerline generated by Voronoi diagrams
  • centerline_equal_distance (list of tuples): List of the latitude and longitude coordinates of the centerline generated by equal distances between coordinates from the Voronoi diagrams
  • centerline_evenly_spaced (list of tuples): List of the latitude and longitude coordinates of the centerline generated by evenly spacing out points generated by the Voronoi diagrams
  • centerline_smoothed (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 diagrams
  • centerline_length (float): Length of the centerline of the river (in km)
  • right_bank_length (float): Length of the right bank of the river (in km)
  • left_bank_length (float): Length of the left bank of the river (in km)
  • area (float): Area contained within river bank polygon (in km^2)
  • sinuosity (float): Sinuosity of the river based on evenly spaced centerline coordinates
  • centerline_voronoi_relative (list of tuples): List of the relative distance coordinates of the centerline generated by Voronoi diagrams
  • centerline_equal_distance_relative (list of tuples): List of the relative distance coordinates of the centerline generated by equal distances between coordinates from the Voronoi diagrams
  • centerline_evenly_spaced_relative (list of tuples): List of the relative distance coordinates of the centerline generated by evenly spacing out points generated by the Voronoi diagrams
  • centerline_smoothed_relative (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 diagrams
Object (class) additional attributes: (Click to view all)
  • river_name (string): name of object, set to the csv_data string
  • left_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 cutoff)
  • equal_distance (int): Distance between points (in meters) used in centerline_equal_distance, defaults to points every 10 meters
  • ellipsoid (string): Built-in ellipsoid definition of Earth to determine how degrees are converted to meters used by centerline_equal_distance, 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 system
  • top_bank_relative (Shapley Linestring): Linestring that represents the top of the river/polygon for the relative distance coordinate system
  • bottom_bank (Shapley Linestring): Linestring that represents the bottom of the river/polygon for the latitude/longitude coordinate system
  • bottom_bank_relative (Shapley Linestring): Linestring that represents the bottom of the river/polygon for the relative distance coordinate system
  • starting_node (tuple): Tuple of the starting position (latitude and longitude) of the centerline path
  • starting_node_relative (tuple): Tuple of the starting position (relative distance x and relative distance y) of the centerline path
  • ending_node (tuple): Tuple of the end position (latitude and longitude) of the centerline path
  • ending_node_relative (tuple): Tuple of the end position (relative distance x and relative distance y) of the centerline path
  • bank_voronoi (scipy Voronoi object): Voronoi generated by left/right banks of the latitude/longitude coordinate system
  • bank_voronoi_relative (scipy Voronoi object): Voronoi generated by left/right banks of the relative distance coordinate system
  • x_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 False
  • interpolate_n (int): specifies how many additional points will be added between points along the riverbank when interpolating data, defaults to 5
  • interpolate_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)
import centerline_width
river_object = centerline_width.CenterlineWidth(csv_data="data/river_coords.csv")

Coordinates of Centerline

Return the coordinates of the centerline based on the left and right banks with either Decimal Degree (latitude/longitude) or Relative Distance (meters)

Types of Centerlines

There are four types of centerline coordinates formed from the riverbank data

  • Voronoi centerline: centerline generated from where Voronoi vertices intersect within the river example+png
  • Equal 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) example+png
  • Evenly Spaced Centerline: centerline based on Voronoi centerline but evenly spaced with a fixed number of points (example: interpolate_n_centerpoints=200) example+png
  • Smoothed Centerline: centerline generated from the evenly spaced centerline but smoothed by a b-spline example+png

By default, coordinates are formed in Decimal Degrees, but can be set to Relative Distance. Relative Distance measures the distance (in meters) of a point from the first point on the left bank

Centerline coordinates are formed by the Voronoi vertices

river_object.centerline_voronoi
river_object.centerline_voronoiriver_object.centerline_voronoi_relative
centerlineVoronoi+pngcenterlineVoronoiRelative+png

Centerline coordinates are formed by Equally Distanced vertices, set by equal_distance

river_object.centerline_equal_distance
river_object.centerline_equal_distanceriver_object.centerline_equal_distance_relative
centerlineEqualDistance+pngcenterlineEqualDistanceRelative+png

Centerline coordinates are formed by Evenly Spaced vertices, set by interpolate_n_centerpoints

river_object.centerline_evenly_spaced
river_object.centerline_evenly_spacedriver_object.centerline_evenly_spaced_relative
centerlineEvenlySpaced+pngcenterlineEvenlySpacedRelative+png

Centerline coordinates are formed from Smoothed vertices

river_object.centerline_smoothed
river_object.centerline_smoothedriver_object.centerline_smoothed_relative
centerlineEvenlySpaced+pngcenterlineEvenlySpacedRelative+png

Example:

import centerline_width
river_object = centerline_width.CenterlineWidth(csv_data="data/river_coords.csv")
river_centerline_coordinates = river_object.centerline_voronoi

Output 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 File

Save 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.

save_centerline_csv(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"
import centerline_width
river_object = centerline_width.CenterlineWidth(csv_data="data/river_coords.csv")
river_object.save_centerline_csv(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)

[!TIP] It is best practice to plot the centerline with plot_centerline() to ensure that the results saved are as expected

Save Centerline Coordinates to a .MAT File

Save the centerline coordinates to a .mat file with columns for latitude and longitude

save_centerline_mat(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"
import centerline_width
river_object = centerline_width.CenterlineWidth(csv_data="data/river_coords.csv")
river_object.save_centerline_mat(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)

[!TIP] It is best practice to plot the centerline with plot_centerline() to ensure that the results saved are as expected

Length of Centerline

Return the length of the centerline found between the left and right bank generated by the Voronoi diagram

river_object.centerline_length

Length returned in kilometers

import centerline_width
river_object = centerline_width.CenterlineWidth(csv_data="data/river_coords.csv")
river_centerline_length = river_object.centerline_length

The length of the river centerline returns 215.34700589636674 km

Area of River

Return the area contained within the polygon generated the left and right bank latitude/longitudes

river_object.area

Area returned in kilometers^2

import centerline_width
river_object = centerline_width.CenterlineWidth(csv_data="data/river_coords.csv")
river_area = river_object.area

The area of the river returns 334.0398585246558 km^2

Total Sinuosity of River

Return the total sinuosity of the river

river_object.sinuosity

The sinuosity (or sinuosity index) is the ratio of the curved centerline and the straight distance of the river

Sinuosity = centerline length / straight distance from first/last point
Sinuosity = river length / straight line length of the river

Where sinuosity is broken in types:

  • SI < 1.05: Almost straight river
  • 1.05 <= SI < 1.25: Winding river
  • 1.25 <= SI < 1.5: Twisty river
  • 1.5 <= Meandering river

Sinuosity of river based on the evenly spaced centerline coordinates

import centerline_width
river_object = centerline_width.CenterlineWidth(csv_data="data/river_coords.csv")
river_area = river_object.sinuosity

The sinuosity of the river returns as a float 1.4593141841039725

Incremental Sinuosity of River

Return the incremental sinuosity of the river at evenly spaced increments

incremental_sinuosity(
        incremental_points=100,
        save_to_csv=None)
  • [REQUIRED] incremental_points: grouping of centerline coordinates, defaults to 100 (must be less than the length of the evenly spaced centerline coordinates)
  • [OPTIONAL] save_to_csv (string): CSV filename, requires a .csv extension with headers ["Centerline Latitude Start (Deg)", "Centerline Longitude Start (Deg)", "Centerline Latitude End (Deg)", "Centerline Longitude End (Deg)", "Sinuosity"]
import centerline_width
river_object = centerline_width.CenterlineWidth(csv_data="data/river_coords.csv")
river_object.incremental_sinuosity()

Returns a dictionary with the start and end centerline coordinates and associated sinuosity {((-92.87803465419134, 30.04494734395193), (-92.87718084516158, 30.03944640478984)): 0.8164574107802118, ((-92.87714797109666, 30.03944945940497), (-92.87020323809925, 30.039886265891074)): 0.9810773013508994}

Plot Centerline in Matplotlib

Plot the centerline created from a list of right and left banks

plot_centerline(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=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 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] 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, Interpolated = True/False"
  • [OPTIONAL] save_plot (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"
import centerline_width
river_object = centerline_width.CenterlineWidth(csv_data="data/river_coords.csv")
river_object.plot_centerline()

river_coords_centerline+png

centerline_type

Display different centerline types in plot ("Voronoi", "Evenly Spaced", "Smoothed", "Equal Distance"), but defaults to "Voronoi"

centerline_type="Voronoi"centerline_type="Smoothed"
centerline_type_voronoi+pngcenterline_type_equal_distance+png

marker_type

Display centerline plot as either line or scatter

marker_type="Line"marker_type="Scatter"
marker_type_line+pngmarker_type_scatter+png

centerline_color

Change the color of the centerline, defaults to black

centerline_color="black"centerline_color="palegreen"
centerline_color_black+pngcenterline_color_purple+png

dark_mode

dark_mode will change the default Matplotlib background black and swap centerline_color from default black to white

dark_mode=Falsedark_mode=True
dark_mode_false+pngdark_mode_true+png

equal_axis

equal_axis will set the x and y axis of the plot to be equal

equal_axis=Falseequal_axis=True
river_not_equal+pngriver_equal+png

display_all_possible_paths

Display all possible paths generated by Voronoi edge ridges, defaults to False

display_all_possible_paths=Falsedisplay_all_possible_paths=True
display_all_possible_paths_false+pngdisplay_all_possible_paths_true+png

display_voronoi

Overlay Voronoi diagram used to generate centerline, defaults to False

display_voronoi=Falsedisplay_voronoi=True
display_voronoi_default+pngdisplay_voronoi_true+png

coordinate_unit

Plot as either "Decimal Degrees" and "Relative Distance". defaults to "Decimal Degrees"

coordinate_unit="Decimal Degrees"coordinate_unit="Relative Distance"
coordinate_unit_dd+pngcoordinate_unit_rd+png

Plot Centerline Width Lines in Matplotlib

Plot the Centerline Width Lines

Plot the width of the river based on the centerline

plot_centerline_width(plot_title=None, 
        save_plot=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 Width Coordinates: Valid Centerline = True/False, Valid Polygon = True/False, Centerline made of <interpolate_n_centerpoints> Fixed Points, width lines generated every <transect_span_distance> points, Interpolated = True/False"
  • [OPTIONAL] save_plot (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 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"
import centerline_width
river_object = centerline_width.CenterlineWidth(csv_data="data/river_coords.csv")
river_object.plot_centerline_width(apply_smoothing=True, remove_intersections=True, display_true_centerline=False)

river_coords_centerline+png

display_true_centerline

The width lines are generated from the evenly spaced coordinate (by default) or with the smoothed coordinates (when apply_smoothing=True), but display_true_centerline will overlay the Voronoi centerline on top of the plot

display_true_centerline=Truedisplay_true_centerline=False
river_with_centerline+pngriver_no_centerline+png

apply_smoothing

apply_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 bank

apply_smoothing=Falseapply_smoothing=True
river_without_smoothing+pngriver_with_smoothing+png

transect_span_distance

Transect 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 span

If 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 point C

transect_span_distance

transect_span_distance=6transect_span_distance=30
river_transect_6+pngriver_transect_30+png

transect_slope

The width lines are generated as perpendicular to the slopes of the points across transect_span_distance

By 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, if transect_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 centerline

transect_span_distance

transect_slope="Average"transect_slope="Direct"
river_transect_avg+pngriver_transect_direct+png

remove_intersections

remove_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 kept

Intersecting lines are flagged in red by default (flag_intersections=True)

remove_intersections=Falseremove_intersections=True
river_keep+pngriver_remove+png

dark_mode

dark_mode will change the default Matplotlib background black and swap centerline_color from default black to white

dark_mode=Falsedark_mode=True
river_white+pngriver_black+png

equal_axis

equal_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 Matplotlib

equal_axis=Falseequal_axis=True
river_not_equal+pngriver_equal+png

coordinate_unit

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"coordinate_unit="Relative Distance"
dd_coords+pngrd_coords+png

Return Width of River

Return 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)) : width in kilometers

width(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 on coordinate_unit and coordinate_reference)

[!IMPORTANT] When using apply_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. When apply_smoothing=False, width lines are generated from the evenly spaced centerline coordinates

import centerline_width
river_object = centerline_width.CenterlineWidth(csv_data="data/river_coords.csv")
river_width_dict = river_object.width(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}

[!TIP] It is best practice to plot the centerline and width with same arguments in plot_centerline_width() to ensure that the results when save_to_csv=True are as expected

Documentation and Algorithm to Determine Centerline

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)

algorithm_step1+png

Generate a polygon to encapsulate the river between the right and left banks to define in and outside of river

algorithm_step2+png

Generate a Voronoi diagram based on the points along the riverbanks

algorithm_step3+png

Display 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 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 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

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 algorithm_step6+png

Find the shortest path from the starting node to the ending node (Dijkstra's Algorithm)

Points on RiverbankNetworkX Graph of Points on Riverbank
algorithm_step7+pngalgorithm_step8+png

Display the centerline found by connecting the starting/ending node with the shortest path

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: algorithm_step4+png

Vertices that have at least two connections (that would create gaps): algorithm_step10+png

Debugging, Error Handling, and Edge Cases

Wide Start/End of River

If the data starts or ends with a large width, it is possible for the starting/ending nodes to end up in the wrong position invalid_too_wide+png Currently, 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 path

Invalid Polygon

A 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: invalid_minor_polygon+png In this example, the polygon is invalid, but with such a small overlap it is still able to find a valid path

With limited data, the polygon will overlap more dramatically and will struggle to find a valid centerline: invalid_major_polygon+png

Invalid Centerline

If the data is too small, a centerline and its coordinates cannot 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 display_voronoi=True to view vertices. Can typically be fixed by adding more data to expand range invalid_too_small+png Can be fixed by expanding the data until the polygon is large enough to contain at least two different vertex points

Invalid Top and Bottom Bank Positions (flip_direction = True)

Error: WARNING: Invalid Polygon Due to Flipped Banks, fix recommendation: rerun convertColumnsToCSV() and set flip_direction=True (or reset to default 'False' if currently set to flip_direction=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 bank

If the latitude/longitude of the banks are generated in reverse order, flip the final values so left/right bank are in order

This can be fixed by using the flip_direction optional argument centerline_width.convertColumnsToCSV(text_file="data_example.txt", flip_direction=True) invalid_flipped_banks+png

Invalid Smoothed Centerline

The smoothed centerline (river_object.centerline_smoothed) 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 thrown

Example Error: WARNING: Partially invalid smoothed centerline due to sparse centerline data (6 points lie outside the polygon), fix recommendation: rerun CenterlineWidth to create river object with interpolate_n_centerpoints set to 62+

By default, interpolate_n_centerpoints is set to None and no 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.CenterlineWidth, with interpolate_n_centerpoints=65 (with the recommended 62+) to fix for centerline coordinates that lie outside the polygon

interpolate_n_centerpoints = None does 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 = 65
example+pngriver_centerline+png

For very narrow rivers, this problem can become extreme and pronounced example+png

By 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 points, with diminishing returns

Fixing Gaps and Jagged Centerlines

Gaps 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 river example+png Set river object created by centerline_width.CenterlineWidth to interpolate_data=True to fix for jagged edges or gaps formed by the interaction of sparse data and narrow banks

river_object = centerline_width.CenterlineWidth(csv_data="data/river_coords.csv", interpolate_data=True)
interpolate_data = Falseinterpolate_data = True
example+pngriver_centerline+png
example+pngriver_centerline+png

The number of additional points added by interpolating can be adjusted with interpolate_n, but defaults to add 5 additional points between values

Development Environment

To run or test against centerline-width github repo/fork, a development environment can be created via conda/miniconda

First, install Miniconda

Then, using the existing environment.yml, a new conda environment can be create to run/test scripts against

conda env create --file environment.yml

Once the environment has been built, activate the environment:

conda activate centerline_width

Set up pre-commit hooks to ensure standard code formatting and spelling:

pre-commit install

Pre-commit hooks can be manually run before commits:

pre-commit run --all-files

To run existing and new tests from the root directory:

python -m pytest

Beta :test_tube: Features

These features are not included in pip install because they are still experimental and being tested/debugged. For more information and getting them up and running, contact cyschneck@gmail.com or ugschneck@gmail.com or post a question as a Github Issue

  • Calculate the dominant meander wavelength amd its variance

  • Research and Contribute to OpenStreetMap for existing mapped rivers with generated centerlines

  • Calculate the asymmetry of the meanders

  • Calculate the dominant submeander and supermeander scales

  • Extract elevation/slope from river profiles (.kml files)

  • Overlay plots with images via geopandas

  • Calculate centerline migration rate

Citations

Originally, centerline-width was developed as a Python implementation of R-Code CMGO (Golly et al. 2017) but has since been extensively expanded and changed:

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 Software

Please 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."

We are interested in expanding this software based on river needs, so please send a copy of such publications to: cyschneck@gmail.com and ugschneck@gmail.com (we'd love to see them!)

This 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 :bug: and Feature Requests

Submit a bug fix, question, or feature request as a Github Issue or to ugschneck@gmail.com/cyschneck@gmail.com

Keywords

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc