pygeoutils.pygeoutils#
Some utilities for manipulating GeoSpatial data.
Module Contents#
- pygeoutils.pygeoutils.arcgis2geojson(arcgis, id_attr=None)#
Convert ESRIGeoJSON format to GeoJSON.
Notes
Based on arcgis2geojson.
- 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.- Return type:
- pygeoutils.pygeoutils.gtiff2vrt(file_list, vrt_path)#
Create a VRT file from a list of (Geo)Tiff files.
Note
This function requires
gdal
to be installed.
- 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 ther_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.all_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 toNone
, i.e., it 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
– Requested dataset or dataarray.- Return type:
- pygeoutils.pygeoutils.json2geodf(content, in_crs=4326, crs=4326)#
Create GeoDataFrame from (Geo)JSON.
- Parameters:
content (
dict
orlist
ofdict
) – A (Geo)JSON dictionary or a list of them.in_crs (
int
,str
, orpyproj.CRS
, optional) – CRS of the content, defaults toepsg:4326
. If the content has no CRS, it will be set to this CRS, otherwise,in_crs
will be ignored.crs (
int
,str
, orpyproj.CRS
, optional) – The target CRS of the output GeoDataFrame, defaults toepsg:4326
.
- Returns:
geopandas.GeoDataFrame
– Generated geo-data frame from a GeoJSON- Return type:
- pygeoutils.pygeoutils.sample_window(dataset, xy, window=5, indexes=None, masked=False, resampling=1)#
Interpolate pixel values at given coordinates by interpolation.
Note
This function is adapted from the
rasterio.sample.sample_gen
function of RasterIO.- Parameters:
dataset (
rasterio.DatasetReader
) – Opened in"r"
mode.xy (iterable) – Pairs of x, y coordinates in the dataset’s reference system.
window (
int
, optional) – Size of the window to read around each point. Must be odd. Default is 5.indexes (
int
orlist
ofint
, optional) – Indexes of dataset bands to sample, defaults to all bands.masked (
bool
, optional) – Whether to mask samples that fall outside the extent of the dataset. Default isFalse
.resampling (
int
, optional) – Resampling method to use. See rasterio.enums.Resampling for options. Default is 1, i.e.,Resampling.bilinear
.
- Yields:
numpy.array
– An array of length equal to the number of specified indexes containing the interpolated values for the bands corresponding to those indexes.
- 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.- Return type:
- 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)- Return type:
XD