A Python package for analyzing upstream basins and resampling DEM data
Project description
User Guide for Analyzing Upstream Basin
This guide provides instructions for using the tool to delineate upstream catchment areas from Digital Elevation Model (DEM) data. The tool processes DEM data, visualizes catchment areas, and optionally resamples the DEM for improved analysis.
Requirements
To use this tool, ensure you have the following Python packages installed:
- numpy:
pip install numpy
- pysheds:
pip install pysheds
- matplotlib:
pip install matplotlib
- scipy:
pip install scipy
- pyproj:
pip install pyproj
Function Overview
Analyze Upstream Basin
from upstream_analyzer.analyze_basin import analyze_upstream_basin
analyze_upstream_basin(asc_file_path, col, row, threshold, xytype='index', crs='EPSG:4326', clip_to=False, new_asc_file_path=None)
Parameters
- asc_file_path (str): Path to the DEM ASCII file.
- col (int/float): Column coordinate or longitude value.
- row (int/float): Row coordinate or latitude value.
- threshold (int): Minimum flow accumulation threshold for identifying high-accumulation cells.
- xytype (str, optional): Type of coordinates used. Set to 'index' for grid cell indices or 'coordinate' for geographic coordinates. Default is 'index'.
- crs (str, optional): EPSG code of the coordinate reference system (e.g., "EPSG:32610"). Default is "EPSG:4326".
- clip_to (bool, optional): If set to True, the catchment area will be clipped for visualization.
- new_asc_file_path (str, optional): Path to save the processed DEM file in ASCII format. If specified, the filled and resolved DEM will be saved to this path.
Output
The function displays a plot of the upstream catchment area. The plot includes a color-coded representation of the catchment with flow accumulation. If new_asc_file_path is provided, the processed DEM will be saved to the specified file.
Resample DEM
from upstream_analyzer.analyze_basin import resample_dem
resample_dem(input_asc, resample_asc, crs="EPSG:4326", scale_factor=0.5, interpolation_order=1)
Parameters
- input_asc (str): Path to the input DEM ASCII file.
- resample_asc (str): Path to save the resampled DEM ASCII file.
- crs (str, optional): EPSG code of the coordinate reference system (e.g., "EPSG:32610"). Default is "EPSG:4326".
- scale_factor (float, optional): Scaling factor for resampling. Default is 0.5 (reduces resolution by half).
- interpolation_order (int, optional): Interpolation method to use (0 - nearest, 1 - bilinear, etc.). Default is 1.
Output
The function saves the resampled DEM to the specified output file.
Instructions
1. Analyze Upstream Basin
The function requires a DEM ASCII file path and coordinates for the outlet point. You have two options for specifying the coordinates:
Option 1: Using Grid Index Coordinates
Specify the outlet point using grid index coordinates (col and row).
# Define the input parameters
asc_file_path = 'WA_Samish/Data_Inputs90m/m_1_DEM/Samish_DredgeMask_EEX.asc'
col, row = 108, 207
threshold = 500
# Call the function
analyze_upstream_basin(asc_file_path, col, row, threshold, xytype='index')
Option 2: Using Geographic Coordinates
Specify the outlet point using geographic coordinates (latitude and longitude) and transform them to the target CRS.
# Define the input parameters
asc_file_path = 'WA_Samish/Data_Inputs90m/m_1_DEM/Samish_DredgeMask_EEX.asc'
lat, long = 48.54594127, -122.3382169
threshold = 500
# Call the function with coordinate transformation and save processed DEM to a new file
target_crs = "EPSG:32610"
new_asc_file_path = 'New_Samish.asc'
analyze_upstream_basin(asc_file_path, long, lat, threshold, xytype='coordinate', crs=target_crs, new_asc_file_path=new_asc_file_path)
2. Resample DEM
The resample_dem
function allows you to resample the resolution of a DEM file to a new ASCII file.
# Define the input parameters
input_asc = 'WA_Samish/Data_Inputs90m/m_1_DEM/Samish_DredgeMask_EEX.asc'
resample_asc = 'Resample_dem.asc'
# Resample the DEM to half the original resolution
resample_dem(input_asc, resample_asc, crs="EPSG:32610", scale_factor=0.5)
3. Explanation of Processing Steps
- Initialize Grid and Read DEM: The DEM data is read using PySheds.
- Fill Pits in DEM: Pits (local depressions) in the DEM are filled to ensure proper flow direction calculations.
- Fill Depressions and Resolve Flats: Depressions are filled, and flat areas are resolved for accurate flow calculations.
- Compute Flow Direction: Flow direction is calculated based on the processed DEM.
- Extract Upstream Basin:
- For xytype = 'coordinate', the specified outlet point is snapped to the nearest high-accumulation cell, with optional CRS transformation.
- For xytype = 'index', the outlet point is treated as a grid cell index.
- Visualize Catchment Area: The catchment area and flow accumulation data are visualized.
4. Visualization
The function will produce a plot displaying the upstream basin and flow accumulation values.
- Blues Color Map: Indicates the extent of the catchment area.
- Binary Color Map: Represents flow accumulation values greater than the specified threshold.
The visualization provides a clear indication of the upstream area that contributes to the specified outlet point.
Example Outputs
The following images represent typical outputs from the function:
Figure: Upstream Basin from Specified Outlet
Notes
- Ensure that the DEM ASCII file is accessible and correctly formatted.
- The threshold value affects the snapping process for geographic coordinates; a higher value will snap the outlet point to cells with greater flow accumulation.
- The coordinate transformation is performed using the PyProj library.
- If new_asc_file_path is provided, the processed DEM file will be saved for further use.
Troubleshooting
- FileNotFoundError: Verify that the provided DEM file path is correct.
- IndexError: Ensure the provided coordinates are within the grid bounds.
- Coordinate Transformation Issues: Ensure that the crs value is a valid EPSG code.
Additional Resources
Feel free to customize the parameters as needed to explore different areas of interest within your DEM data!
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file upstream_analyzer-0.1.0.tar.gz
.
File metadata
- Download URL: upstream_analyzer-0.1.0.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 998c14ba9bf93e099e2d5c3a35b6db17491750cfa8e3cda39f95df2a0e787ca6 |
|
MD5 | 982a92f9b56762dfa1d49619ac8e2e93 |
|
BLAKE2b-256 | 8230f437266f29d933ced7277bd6c6b9cc225619790252657c9cebb35e2cb542 |
File details
Details for the file upstream_analyzer-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: upstream_analyzer-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5296b4130b2ad5679d8271a7818a3f2ee704be1ce404f7a441d6659cd64d7c5d |
|
MD5 | 1a2ace0f5fe44f3ee26446fdc9968830 |
|
BLAKE2b-256 | fe511d631ba494436142c956550c0e0156cf62d477efeba0b4f863b96866a27e |