Skip to main content

Access USGS 3DEP database and get data such as elevation in the US

Project description

https://raw.githubusercontent.com/cheginit/HyRiver-examples/main/notebooks/_static/py3dep_logo.png

JOSS

Package

Description

Status

PyNHD

Navigate and subset NHDPlus (MR and HR) using web services

Github Actions

Py3DEP

Access topographic data through National Map’s 3DEP web service

Github Actions

PyGeoHydro

Access NWIS, NID, HCDN 2009, NLCD, and SSEBop databases

Github Actions

PyDaymet

Access Daymet for daily climate data both single pixel and gridded

Github Actions

AsyncRetriever

High-level API for asynchronous requests with persistent caching

Github Actions

PyGeoOGC

Send queries to any ArcGIS RESTful-, WMS-, and WFS-based services

Github Actions

PyGeoUtils

Convert responses from PyGeoOGC’s supported web services to datasets

Github Actions

Py3DEP: Topographic data through 3DEP

PyPi Conda Version CodeCov Python Versions Downloads

CodeFactor black pre-commit Binder

Features

Py3DEP is a part of HyRiver software stack that is designed to aid in watershed analysis through web services. This package provides access to the 3DEP database which is a part of the National Map services. The 3DEP service has multi-resolution sources and depending on the user provided resolution, the data is resampled on the server-side based on all the available data sources. Py3DEP returns the requests as xarray dataset. Moreover, under-the-hood, this package uses requests-cache for persistent caching that can improve the performance significantly. The 3DEP web service includes the following layers:

  • DEM

  • Hillshade Gray

  • Aspect Degrees

  • Aspect Map

  • GreyHillshade Elevation Fill

  • Hillshade Multidirectional

  • Slope Map

  • Slope Degrees

  • Hillshade Elevation Tinted

  • Height Ellipsoidal

  • Contour 25

  • Contour Smoothed 25

Moreover, Py3DEP offers some additional utilities:

  • elevation_bygrid: For getting elevations of all the grid points in a 2D grid.

  • elevation_bycoords: For getting elevation of a list of x and y coordinates.

  • deg2mpm: For converting slope dataset from degree to meter per meter.

You can try using Py3DEP without installing it on you system by clicking on the binder badge below the Py3DEP banner. A Jupyter notebook instance with the stack pre-installed will be launched in your web browser and you can start coding!

Please note that since this project is in early development stages, while the provided functionalities should be stable, changes in APIs are possible in new releases. But we appreciate it if you give this project a try and provide feedback. Contributions are most welcome.

Moreover, requests for additional functionalities can be submitted via issue tracker.

Installation

You can install Py3DEP using pip after installing libgdal on your system (for example, in Ubuntu run sudo apt install libgdal-dev). Moreover, Py3DEP has an optional dependency for using persistent caching, requests-cache. We highly recommend to install this package as it can significantly speedup send/receive queries. You don’t have to change anything in your code, since Py3DEP under-the-hood looks for requests-cache and if available, it will automatically use persistent caching:

$ pip install py3dep

Alternatively, Py3DEP can be installed from the conda-forge repository using Conda:

$ conda install -c conda-forge py3dep

Quick start

You can use Py3DEP using command-line or as a Python library. The commanda-line provides access to two functionality:

  • Getting topographic data: You must create a geopandas.GeoDataFrame that contains the geometries of the target locations. This dataframe must have at least three columns: id, res, and geometry. The id column is used as filenames for saving the obtained topographic data to a NetCDF (.nc) file. The res column must be the target resolution in meter. Then, you must save the dataframe to a file with extensions such as .shp or .gpkg (whatever that geopandas.read_file can read).

  • Getting elevation: You must create a pandas.DataFrame that contains coordinates of the target locations. This dataframe must have at least two columns: x and y. The elevations are obtained using airmap service in meters. The data are saved as a csv file with the same filename as the input file with an _elevation appended, e.g., coords_elevation.csv.

$ py3dep --help
Usage: py3dep [OPTIONS] TARGET [geometry|coords] CRS

  Retrieve topographic data within geometries or elevations for a list of coordinates.

  TARGET: Path to a geospatial file (any file that geopandas.read_file can open) or a csv file.

  The geospatial file should have three columns:

      - id: Feature identifiers that py3dep uses as the output netcdf/csv filenames.
      - res: Target resolution in meters.
      - geometry: A Polygon or MultiPloygon.

  The csv file should have two column: x and y.

  TARGET_TYPE: Type of input file: "coords" for csv, and "geometry" for geospatial.

  CRS: CRS of the input data.

  Examples:
      $ py3dep ny_coords.csv coords epsg:4326
      $ py3dep ny_geom.gpkg geometry epsg:3857 --layer "Slope Map"

Options:
  -l, --layer [DEM|Hillshade Gray|Aspect Degrees|Aspect Map|GreyHillshade_elevationFill|Hillshade Multidirectional|Slope Map|Slope Degrees|Hillshade Elevation Tinted|Height Ellipsoidal|Contour 25|Contour Smoothed 25]
                                  Layer name when requesting for topographic data.
  -s, --save_dir PATH             Path to a directory to save the requested files. Extension
                                  for the outputs is .nc for geometry and .csv for coords.

Now, let’s see how we can use Py3DEP as a library.

Py3DEP accepts Shapely’s Polygon or a bounding box (a tuple of length four) as an input geometry. We can use PyNHD to get a watershed’s geometry, then use it to get the DEM and slope in meters/meters from Py3DEP using get_map function.

The get_map has a resolution argument that sets the target resolution in meters. Note that the highest available resolution throughout the CONUS is about 10 m, though higher resolutions are available in limited parts of the US. Note that the input geometry can be in any valid spatial reference (geo_crs argument). The crs argument, however, is limited to CRS:84, EPSG:4326, and EPSG:3857 since 3DEP only supports these spatial references.

import py3dep
from pynhd import NLDI

geom = NLDI().get_basins("01031500").geometry[0]
dem = py3dep.get_map("DEM", geom, resolution=30, geo_crs="epsg:4326", crs="epsg:3857")
slope = py3dep.get_map("Slope Degrees", geom, resolution=30)
slope = py3dep.deg2mpm(slope)
https://raw.githubusercontent.com/cheginit/HyRiver-examples/main/notebooks/_static/dem_slope.png

We can use rioxarray package to save the obtained dataset as a raster file:

import rioxarray

dem.rio.to_raster("dem_01031500.tif")

Moreover, we can get the elevations of set of x- and y- coordinates on a grid. For example, let’s get the minimum temperature data within this watershed from Daymet using PyDaymet then add the elevation as a new variable to the dataset:

import pydaymet as daymet
import xarray as xr
import numpy as np

clm = daymet.get_bygeom(geometry, ("2005-01-01", "2005-01-31"), variables="tmin")
elev = py3dep.elevation_bygrid(clm.x.values, clm.y.values, clm.crs, clm.res[0] * 1000)
attrs = clm.attrs
clm = xr.merge([clm, elev])
clm["elevation"] = clm.elevation.where(~np.isnan(clm.isel(time=0).tmin), drop=True)
clm.attrs.update(attrs)

Now, let’s get street network data using osmnx package and add elevation data for its nodes using elevation_bycoords function.

import osmnx as ox

G = ox.graph_from_place("Piedmont, California, USA", network_type="drive")
x, y = nx.get_node_attributes(G, "x").values(), nx.get_node_attributes(G, "y").values()
elevation = py3dep.elevation_bycoords(zip(x, y), crs="epsg:4326")
nx.set_node_attributes(G, dict(zip(G.nodes(), elevation)), "elevation")
https://raw.githubusercontent.com/cheginit/HyRiver-examples/main/notebooks/_static/street_elev.png

Contributing

Contributions are very welcomed. Please read CONTRIBUTING.rst file for instructions.

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

py3dep-0.11.2.tar.gz (33.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py3dep-0.11.2-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file py3dep-0.11.2.tar.gz.

File metadata

  • Download URL: py3dep-0.11.2.tar.gz
  • Upload date:
  • Size: 33.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.7

File hashes

Hashes for py3dep-0.11.2.tar.gz
Algorithm Hash digest
SHA256 e1f76c81c701abb09f58821d8843115762adcee2db9457831141e725951ec1d5
MD5 4df5408ec3a347ef35b7ad0adc37b175
BLAKE2b-256 147f4cb52289e196bbcd2c418f7a847630d4df1b7db3dc6101d52eae9d1a47e4

See more details on using hashes here.

File details

Details for the file py3dep-0.11.2-py3-none-any.whl.

File metadata

  • Download URL: py3dep-0.11.2-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.7

File hashes

Hashes for py3dep-0.11.2-py3-none-any.whl
Algorithm Hash digest
SHA256 bbd141347dfa0ca9bd464dae13aaa64723c85f84e8acfea858d9bc5ff3d6a353
MD5 7474b7ab2ef2a1abacb08156186043fe
BLAKE2b-256 1c25c0cd7fce7cee7b81a5d05092e4c2eab410f9ad5bf098d5e8afb73977dab8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page