Welcome to StreamStats’s documentation!¶
StreamStats¶
Python package for interfacing with the USGS StreamStats API.
Free software: MIT license
Documentation: https://streamstats-python.readthedocs.io/en/latest/
Features¶
Plot the GeoJSON of a watershed containing a spatial point in the United States
Find available basin characteristics of an identified watershed
Find the hydrologic unit code (HUC) of an identified watershed
View Example StreamStats Applications in Our Documentation Gallery¶
Check out our vignette gallery for applied examples of using StreamStats.
Installation¶
Stable release¶
To install StreamStats via pip
use:
$ pip install streamstats
This is the preferred method to install StreamStats, as it will always install the most recent stable release. If you don’t have pip installed, this Python installation guide can guide you through the process.
Alternatively, StreamStats can be installed from the conda-forge
repository
using Conda:
$ conda install -c conda-forge streamstats
From sources¶
The sources for StreamStats can be downloaded from the GitHub repository .
You can either clone the public repository:
$ git clone git://github.com/earthlab/streamstats
Once you have a copy of the source, you can install it with:
$ python setup.py install
How to Contribute¶
The steps to set up StreamStats for local development are as follows:
Fork the streastats repo on GitHub
Clone your fork locally:
$ git clone git://github.com:your_name_here/streamstats.git
Install your local copy into a new environment
If you have virutalenvwrapper installed:
$ mkvirtualenv streamstats
If you are using conda:
$ conda create -n streamstats python=3
$ conda activate streamstats
Then install StreamStats:
$ cd streamstats/
$ pip install -r requirements.txt
$ pip install -r requirements_dev.txt
$ install -e .
Create a branch for local development:
$ git checkout -b name-of-your-bugfix/feature
Now you can make your changes locally
5. When your changes are complete, check that your changes pass flake8 and the tests, including other Python versions with tox:
$ pytest
$ tox
Commit your changes and push your branch to GitHub:
$ git add
$ git commit -m "Your detailed description of your changes"
$ git push origin name-of-your-bugfix/feature
Submit a pull request through the GitHub website
We welcome and greatly appreciate contributions to StreamStats! The best way to send feedback is to file an issue at https://github.com/earthlab/streamstats/issues. To read more on ways to contribute and pull requests, click here.
Credits¶
Development Lead¶
Contributors¶
This package was created with Cookiecutter.
Installation¶
Stable release¶
To install StreamStats, run this command in your terminal:
$ pip install streamstats
This is the preferred method to install StreamStats, as it will always install the most recent stable release.
If you don’t have pip installed, this Python installation guide can guide you through the process.
From sources¶
The sources for StreamStats can be downloaded from the Github repo.
You can either clone the public repository:
$ git clone git://github.com/earthlab/streamstats
Once you have a copy of the source, you can install it with:
$ python setup.py install
Usage¶
Finding the watershed that contains a point¶
Given a spatial point in the U.S. defined by a lat/lon location and a state, the Watershed class can be used to find the watershed that contains that point using the USGS StreamStats API.
>>> from streamstats import Watershed
>>> Watershed(lat=43.939, lon=-74.524)
Watershed object with HUC8: 04150305, containing lat/lon: (43.939, -74.524)
StreamStats Vignette Gallery¶
Below you will find a set of example applications for using StreamStats.
Note
Click here to download the full example code
View Watershed Characterisitics Using StreamStats¶
Import Packages¶
To get started, download the necessary Python packages. The GeoPandas
package is an open source project that assists in working with geospatial data
in Python. Learn more about
GeoPandas.
import streamstats
import geopandas as gpd
Indentify watershed¶
To identify a HUC, use a latitude and longitude value to select a specific watershed.
Assign cordinates to variables lat
and lon
. Using StreamStat’s data,
assign location to a variable that will represent the
delineated watershed using the USGS StreamStats API.
lat, lon = 39.966256, -105.482227
ws = streamstats.Watershed(lat=lat, lon=lon)
Find the Hydrologic Unit Code (HUC) of the watershed¶
The USGS delineates watershed using a series of numbers based a hierarchal
region system. Every watershed is assigned a series of numbers called the
hydrological unit code (HUC). StreamStats uses HUC to identify and delineate
watersheds. Learn more about
Hydrologic Units . The ws.huc
function will return the HUC of the
identified watershed.
ws.huc
Out:
'10190005'
Find Characteristics of the watershed¶
The function ws.characteristics()
will return the available basin
characteristics for the identified watershed.In order to return information
on a specific characteristic, use function
ws.get_characteristic('StatLabel')
. Learn more
about StreamStats Basin Characteristic Definitions.
# Available characteristics
ws.characteristics
# Specific characteristics
ws.get_characteristic('DRNAREA')
Out:
{'ID': 0, 'name': 'Drainage Area', 'description': 'Area that drains to a point on a stream', 'code': 'DRNAREA', 'unit': 'square miles', 'value': 38.6}
Total running time of the script: ( 1 minutes 1.674 seconds)
Note
Click here to download the full example code
Download Data with StreamStats¶
Learn how to use StreamStats python library to download watershed boundary data available in USGS StreamStats API. StreamStats provides information including HUC code, a GeoJSON representation of the polygon associated with the watershed, and basin characteristics.
Import Packages¶
Download the necessary Python packages. The GeoPandas
package is an open
source project that assists in working with geospatial data in Python. Learn
more about GeoPandas.
import streamstats
import geopandas as gpd
Identify watershed¶
To identify a HUC, use a latitude and longitude value to select a specific watershed.
Assign cordinates to variables lat
and lon
. Using StreamStat’s data,
assign location to a variable that will represent the
delineated watershed using the USGS StreamStats API.
lat, lon = 39.966256, -105.482227
ws = streamstats.Watershed(lat=lat, lon=lon)
Find boundary properties of the watershed¶
ws.boundary
is a stored variable in the watershed object and will return
the full watershed GeoJSON as a dictionary. You can access the CRS through
the GeoJSON object.
ws.boundary
ws.boundary['crs']
Out:
{'type': 'ESPG', 'properties': {'code': 4326}}
Create plot of the Watershed¶
Open the GeoJSON with GeoPandas and plot the data.
ws.boundary
poly = gpd.GeoDataFrame.from_features(ws.boundary["features"], crs="EPSG:4326")
ax = poly.plot(figsize=(20, 10), edgecolor='k')
ax.set_title("Single Watershed", fontsize=30, fontweight = 'bold')
ax.set_axis_off()

Total running time of the script: ( 0 minutes 44.197 seconds)
streamstats¶
streamstats package¶
Submodules¶
streamstats.utils module¶
Utility functions for streamstats.
- streamstats.utils.find_address(lat, lon)[source]¶
Find the address associated with a lat/lon pair.
- Parameters
lat (float) – Latitude of point in decimal degrees
lon (float) – Longitude of point in decimal degrees
- Return type
dictionary containing address data
- streamstats.utils.find_state(address)[source]¶
Find the U.S. state that contains an address
- Parameters
address (dict) – An address found by
find_address
- Return type
string of the state code (e.g., “CO” for Colorado)
- streamstats.utils.requests_retry_session(retries=3, backoff=0.3, status_forcelist=(500, 502, 504))[source]¶
Make a session that backs off automatically.
- Parameters
retries (int) – Number of times to retry a request
backoff (float) – Interval: {backoff} * (2^({number of total retries} - 1))
status_forcelist (tuple of ints) – Status codes that prompt a retry
streamstats.watershed module¶
Functionality for finding watershed information for specific locations.
- class streamstats.watershed.Watershed(lat, lon)[source]¶
Bases:
object
Watershed covering a spatial region, with associated information.
The USGS StreamStats API is built around watersheds as organizational units. Watersheds in the 50 U.S. states can be found using lat/lon lookups, along with information about the watershed including its HUC code and a GeoJSON representation of the polygon of a watershed. Basin characteristics can also be extracted from watersheds.
- base_url = 'https://streamstats.usgs.gov/streamstatsservices/'¶
- property boundary¶
Return the full watershed GeoJSON as a dictionary.
:rtype dict containing GeoJSON watershed boundary
- property characteristics¶
List the available watershed characteristics.
Details about these characteristics can be found in the StreamStats docs: https://streamstatsags.cr.usgs.gov/ss_defs/basin_char_defs.aspx
:rtype OrderedDict with characteristic codes and descriptions
- get_characteristic(code=None)[source]¶
Retrieve a specified watershed characteristic
- Parameters
code (string) – Watershed characteristic code to extract.
get_characteristic() requires a characteristic code as an argument. Valid codes can be seen as keys in the dictionary returned by the characteristics() method.
:rtype dict containing specified characteristic’s data and metadata
- property huc¶
Find the Hydrologic Unit Code (HUC) of the watershed.
Module contents¶
Top-level package for StreamStats.
Contributing¶
We welcome and greatly appreciate contributions to streamstats! Every bit helps, and credit will always be given.
You can contribute in many ways:
Types of Contributions¶
Report Bugs¶
Report bugs at https://github.com/earthlab/streamstats/issues.
If you are reporting a bug, please include:
Your operating system name and version.
Any details about your local setup that might be helpful in troubleshooting.
Detailed steps to reproduce the bug.
Fix Bugs¶
Look through the GitHub issues for bugs. Anything tagged with “bug” and “help wanted” is open to whoever wants to implement it.
Implement Features¶
Look through the GitHub issues for features. Anything tagged with “enhancement” and “help wanted” is open to whoever wants to implement it.
Write Documentation¶
StreamStats could always use more documentation, whether as part of the official StreamStats docs, in docstrings, or even on the web in blog posts, articles, and such.
Submit Feedback¶
The best way to send feedback is to file an issue at https://github.com/earthlab/streamstats/issues.
If you are proposing a feature:
Explain in detail how it would work.
Keep the scope as narrow as possible, to make it easier to implement.
Remember that this is a volunteer-driven project, and that contributions are welcome :)
Get Started!¶
Ready to contribute? Here’s how to set up StreamStats for local development.
Fork the streamstats repo on GitHub.
Clone your fork locally:
$ git clone git@github.com:your_name_here/streamstats.git
Install your local copy into a new environment.
If you have virtualenvwrapper installed:
$ mkvirtualenv streamstats
If you are a conda user:
$ conda create -n streamstats python=3
$ conda activate streamstats
Then install StreamStats:
$ cd streamstats/
$ pip install -r requirements.txt
$ pip install -r requirements_dev.txt
$ pip install -e .
Create a branch for local development:
$ git checkout -b name-of-your-bugfix-or-feature
Now you can make your changes locally.
When you’re done making changes, check that your changes pass flake8 and the tests, including testing other Python versions with tox:
$ pytest $ tox
Commit your changes and push your branch to GitHub:
$ git add . $ git commit -m "Your detailed description of your changes." $ git push origin name-of-your-bugfix-or-feature
Submit a pull request through the GitHub website.
Pull Request Guidelines¶
Before you submit a pull request, check that it meets these guidelines:
The pull request should include tests.
If the pull request adds functionality, the docs should be updated. Put your new functionality into a function with a docstring, and add the feature to the list in README.rst.
The pull request should pass checks for all supported Python versions.
Tips¶
To run a subset of tests:
$ py.test tests.test_streamstats
Deploying¶
A reminder for the maintainers on how to deploy. Make sure all your changes are committed (including an entry in HISTORY.rst). Then run:
$ bumpversion patch # possible: major / minor / patch
$ git push
$ git push --tags
Travis will then deploy to PyPI if tests pass.
Credits¶
Development Lead¶
Maxwell B. Joseph <maxwell.b.joseph@colorado.edu>
Contributors¶
Scott Eilerman
Leah Wasser
Jeremy Diaz
Nate Mietkiewicz
History¶
0.1.3¶
remove get_flow_stats() which is down server side
0.1.0 (2018-10-22)¶
First release on PyPI.