pygeoutils.pygeoutils
Contents
pygeoutils.pygeoutils
#
Some utilities for manipulating GeoSpatial data.
Module Contents#
- class pygeoutils.pygeoutils.Coordinates#
Generate validated and normalized coordinates in WGS84.
- Parameters
Examples
>>> from pygeoutils import Coordinates >>> c = Coordinates([460, 20, -30], [80, 200, 10]) >>> c.points.x.tolist() [100.0, -30.0]
- property points: geopandas.GeoSeries#
Get validate coordinate as a
geopandas.GeoSeries
.
- class pygeoutils.pygeoutils.GeoBSpline(points, npts_sp, degree=3)#
Create B-spline from a geo-dataframe of points.
- Parameters
points (
geopandas.GeoDataFrame
orgeopandas.GeoSeries
) – Input points as aGeoDataFrame
orGeoSeries
in a projected CRS.npts_sp (
int
) – Number of points in the output spline curve.degree (
int
, optional) – Degree of the spline. Should be less than the number of points and greater than 1. Default is 3.
Examples
>>> from pygeoutils import GeoBSpline >>> import geopandas as gpd >>> xl, yl = zip( ... *[ ... (-97.06138, 32.837), ... (-97.06133, 32.836), ... (-97.06124, 32.834), ... (-97.06127, 32.832), ... ] ... ) >>> pts = gpd.GeoSeries(gpd.points_from_xy(xl, yl, crs=4326)) >>> sp = GeoBSpline(pts.to_crs("epsg:3857"), 5).spline >>> pts_sp = gpd.GeoSeries(gpd.points_from_xy(sp.x, sp.y, crs="epsg:3857")) >>> pts_sp = pts_sp.to_crs("epsg:4326") >>> list(zip(pts_sp.x, pts_sp.y)) [(-97.06138, 32.837), (-97.06135, 32.83629), (-97.06131, 32.83538), (-97.06128, 32.83434), (-97.06127, 32.83319)]
- property spline: Spline#
Get the spline as a
Spline
object.
- pygeoutils.pygeoutils.arcgis2geojson(arcgis, id_attr=None)#
Convert ESRIGeoJSON format to GeoJSON.
Notes
Based on arcgis2geojson.
- pygeoutils.pygeoutils.break_lines(lines, points, tol=0.0)#
Break lines at specified points at given direction.
- Parameters
lines (
geopandas.GeoDataFrame
) – Lines to break at intersection points.points (
geopandas.GeoDataFrame
) – Points to break lines at. It must contain a column nameddirection
with valuesup
ordown
. This column is used to determine which part of the lines to keep, i.e., upstream or downstream of points.tol (
float
, optional) – Tolerance for snapping points to the nearest lines in meters. The default is 0.0.
- Returns
geopandas.GeoDataFrame
– Original lines except for the parts that have been broken at the specified points.
- pygeoutils.pygeoutils.geo2polygon(geometry, geo_crs=None, crs=None)#
Convert a geometry to a Shapely’s Polygon and transform to any CRS.
- Parameters
- Returns
shapely.Polygon
orshapely.MultiPolygon
– A (Multi)Polygon in the target CRS, if different from the input CRS.
- pygeoutils.pygeoutils.geodf2xarray(geodf, resolution, attr_col=None, fill=0, projected_crs=5070)#
Rasterize a
geopandas.GeoDataFrame
toxarray.DataArray
.- Parameters
geodf (
geopandas.GeoDataFrame
orgeopandas.GeoSeries
) – GeoDataFrame or GeoSeries to rasterize.resolution (
float
) – Target resolution of the output raster in theprojected_crs
unit. Since the defaultprojected_crs
isEPSG:5070
, the default unit for the resolution is meters.attr_col (
str
, optional) – Column name of the attribute to use as variable., defaults toNone
, i.e., the variable will be a boolean mask where 1 indicates the presence of a geometry. Also, note that the attribute must be numeric and have one of the followingnumpy
types:int16
,int32
,uint8
,uint16
,uint32
,float32
, andfloat64
.fill (
int
orfloat
, optional) – Value to use for filling the missing values (mask) of the output raster, defaults to0
.projected_crs (
int
,str
, orpyproj.CRS
, optional) – A projected CRS to use for the output raster, defaults toEPSG:5070
.
- Returns
xarray.Dataset
– The xarray Dataset with a single variable.
- pygeoutils.pygeoutils.geometry_list(geometry)#
Get a list of polygons, points, and lines from a geometry.
- pygeoutils.pygeoutils.get_transform(ds, ds_dims=('y', 'x'))#
Get transform of a
xarray.Dataset
orxarray.DataArray
.- Parameters
ds (
xarray.Dataset
orxarray.DataArray
) – The dataset(array) to be maskedds_dims (
tuple
, optional) – Names of the coordinames in the dataset, defaults to("y", "x")
. The order of the dimension names must be (vertical, horizontal).
- Returns
rasterio.Affine
,int
,int
– The affine transform, width, and height
- pygeoutils.pygeoutils.gtiff2xarray(r_dict, geometry=None, geo_crs=None, ds_dims=None, driver=None, all_touched=False, nodata=None, drop=True)#
Convert (Geo)Tiff byte responses to
xarray.Dataset
.- Parameters
r_dict (
dict
) – Dictionary of (Geo)Tiff byte responses where keys are some names that are used for naming each responses, and values are bytes.geometry (
Polygon
,MultiPolygon
, ortuple
, optional) – The geometry to mask the data that should be in the same CRS as the r_dict. Defaults toNone
.geo_crs (
int
,str
, orpyproj.CRS
, optional) – The spatial reference of the input geometry, defaults toNone
. This argument should be given whengeometry
is given.ds_dims (
tuple
ofstr
, optional) – The names of the vertical and horizontal dimensions (in that order) of the target dataset, default to None. If None, dimension names are determined from a list of common names.driver (
str
, optional) – A GDAL driver for reading the content, defaults to automatic detection. A list of the drivers can be found here: https://gdal.org/drivers/raster/index.htmlall_touched (
bool
, optional) – Include a pixel in the mask if it touches any of the shapes. If False (default), include a pixel only if its center is within one of the shapes, or if it is selected by Bresenham’s line algorithm.nodata (
float
orint
, optional) – The nodata value of the raster, defaults to None, i.e., is determined from the raster.drop (
bool
, optional) – If True, drop the data outside of the extent of the mask geometries. Otherwise, it will return the same raster with the data masked. Default is True.
- Returns
xarray.Dataset
orxarray.DataAraay
– Parallel (with dask) dataset or dataarray.
- pygeoutils.pygeoutils.json2geodf(content, in_crs=4326, crs=4326)#
Create GeoDataFrame from (Geo)JSON.
- Parameters
- Returns
geopandas.GeoDataFrame
– Generated geo-data frame from a GeoJSON
- pygeoutils.pygeoutils.nested_polygons(gdf)#
Get nested polygons in a GeoDataFrame.
- Parameters
gdf (
geopandas.GeoDataFrame
orgeopandas.GeoSeries
) – A GeoDataFrame or GeoSeries with (multi)polygons.- Returns
dict
– A dictionary where keys are indices of larger ploygons and values are a list of indices of smaller polygons that are contained within the larger polygons.
- pygeoutils.pygeoutils.snap2nearest(lines, points, tol)#
Find the nearest points on a line to a set of points.
- Parameters
lines (
geopandas.GeoDataFrame
orgeopandas.GeoSeries
) – Lines.points (
geopandas.GeoDataFrame
orgeopandas.GeoSeries
) – Points to snap to lines.tol (
float
, optional) – Tolerance for snapping points to the nearest lines in meters. It must be greater than 0.0.
- Returns
geopandas.GeoDataFrame
orgeopandas.GeoSeries
– Points snapped to lines.
- pygeoutils.pygeoutils.xarray2geodf(da, dtype, mask_da=None, connectivity=8)#
Vectorize a
xarray.DataArray
to ageopandas.GeoDataFrame
.- Parameters
da (
xarray.DataArray
) – The dataarray to vectorize.dtype (
type
) – The data type of the dataarray. Valid types areint16
,int32
,uint8
,uint16
, andfloat32
.mask_da (
xarray.DataArray
, optional) – The dataarray to use as a mask, defaults toNone
.connectivity (
int
, optional) – Use 4 or 8 pixel connectivity for grouping pixels into features, defaults to 8.
- Returns
geopandas.GeoDataFrame
– The vectorized dataarray.
- pygeoutils.pygeoutils.xarray_geomask(ds, geometry, crs, all_touched=False, drop=True, from_disk=False)#
Mask a
xarray.Dataset
based on a geometry.- Parameters
ds (
xarray.Dataset
orxarray.DataArray
) – The dataset(array) to be maskedgeometry (
Polygon
,MultiPolygon
, ortuple
oflength 4
) – The geometry to mask the datacrs (
int
,str
, orpyproj.CRS
) – The spatial reference of the input geometryall_touched (
bool
, optional) – Include a pixel in the mask if it touches any of the shapes. If False (default), include a pixel only if its center is within one of the shapes, or if it is selected by Bresenham’s line algorithm.drop (
bool
, optional) – If True, drop the data outside of the extent of the mask geometries. Otherwise, it will return the same raster with the data masked. Default is True.from_disk (
bool
, optional) – If True, it will clip from disk using rasterio.mask.mask if possible. This is beneficial when the size of the data is larger than memory. Default is False.
- Returns
xarray.Dataset
orxarray.DataArray
– The input dataset with a mask applied (np.nan)