gsflow.utils.rasters Module

rasters.py includes code to work with raster datasets, particularly to sample raster using a PrmsDiscretization object and then use it as input data

class gsflow.utils.rasters.Raster(array, bands, crs, transform, nodataval, driver='GTiff', rio_ds=None)[source]

Overloaded flopy.utils.Raster class. This class is used to resample rasters and the overload allows for multiprocessing using the “ray” python library

Parameters
arraynp.ndarray

a three dimensional array of raster values with dimensions defined by (raster band, nrow, ncol)

bandstuple

a tuple of raster bands

crsint, string, rasterio.crs.CRS object

either a epsg code, a proj4 string, or a CRS object

transformaffine.Affine object

affine object, which is used to define geometry

nodatavalfloat

raster no data value

rio_dsDatasetReader object

rasterIO dataset Reader object

Examples

>>> from gsflow.utils import Raster
>>>
>>> rio = Raster.load("myraster.tif")
Attributes
bands

Returns a tuple of raster bands

bounds

Returns a tuple of xmin, xmax, ymin, ymax boundaries

nodatavals

Returns a Tuple of values used to define no data

xcenters

Returns a np.ndarray of raster x cell centers

ycenters

Returns a np.ndarray of raster y cell centers

Methods

crop(self, polygon[, invert])

Method to crop a new raster object from the current raster object

get_array(self, band[, masked])

Method to get a numpy array corresponding to the provided raster band.

histogram(self[, ax])

Method to plot a histogram of digital numbers

load(raster)

Static method to load a raster file into the raster object

plot(self[, ax, contour])

Method to plot raster layers or contours.

resample_to_grid(self, modelgrid, band[, …])

Method to resample the raster data to a user supplied grid of x, y coordinates.

sample_point(self, \*point[, band])

Method to get nearest raster value at a user provided point

sample_polygon(self, polygon, band[, invert])

Method to get an unordered list of raster values that are located within a arbitrary polygon

structured_downscale(self, band, delr, delc, …)

Fast method for structured grid downscaling.

write(self, name)

Method to write raster data to a .tif file

property bands

Returns a tuple of raster bands

property bounds

Returns a tuple of xmin, xmax, ymin, ymax boundaries

crop(self, polygon, invert=False)[source]

Method to crop a new raster object from the current raster object

Parameters
polygonlist, geojson, shapely.geometry, shapefile.Shape

crop method accepts any of these geometries:

a list of (x, y) points, ex. [(x1, y1), …] geojson Polygon object shapely Polygon object shapefile Polygon shape flopy Polygon shape

invertbool

Default value is False. If invert is True then the area inside the shapes will be masked out

get_array(self, band, masked=True)[source]

Method to get a numpy array corresponding to the provided raster band. Nodata vals are set to np.NaN

Parameters
bandint

band number from the raster

maskedbool

determines if nodatavals will be returned as np.nan to the user

Returns
np.ndarray
histogram(self, ax=None, **kwargs)[source]

Method to plot a histogram of digital numbers

Parameters
axmatplotlib.pyplot.axes

optional matplotlib axes for plotting

**kwargs :

matplotlib keyword arguments see matplotlib documentation for valid arguments for histogram

Returns
axmatplotlib.pyplot.axes
static load(raster)[source]

Static method to load a raster file into the raster object

Parameters
rasterstr
Returns
Raster object
property nodatavals

Returns a Tuple of values used to define no data

plot(self, ax=None, contour=False, **kwargs)[source]

Method to plot raster layers or contours.

Parameters
axmatplotlib.pyplot.axes

optional matplotlib axes for plotting

contourbool

flag to indicate creation of contour plot

**kwargs :

matplotlib keyword arguments see matplotlib documentation for valid arguments for plot and contour.

Returns
axmatplotlib.pyplot.axes
resample_to_grid(self, modelgrid, band, method='nearest', multithread=False, thread_pool=2, extrapolate_edges=False, no_numba=False, use_oldstyle=False)[source]

Method to resample the raster data to a user supplied grid of x, y coordinates.

x, y coordinate arrays should correspond to grid vertices

Parameters
modelgridflopy.Grid object

model grid to sample data from

bandint

raster band to re-sample

methodstr

scipy interpolation methods

linear for bi-linear interpolation

nearest for nearest neighbor

cubic for bi-cubic interpolation

mean for mean sampling

median for median sampling

min for minimum sampling

max for maximum sampling

multithreadbool

boolean flag indicating if multithreading should be used with the mean and median sampling methods

thread_poolint

number of threads to use for mean and median sampling

extrapolate_edgesbool

boolean flag indicating if areas without data should be filled using the nearest interpolation method. This option has no effect when using the nearest interpolation method.

no_numbabool

method to turn off numba based resampling, default is False

use_oldstylebool

method to force point in polygon intersection routine, default is fast resampling based on raster warp sampling methods

Returns
np.array
sample_point(self, *point, band=1)[source]

Method to get nearest raster value at a user provided point

Parameters
*pointpoint geometry representation

accepted data types: x, y values : ex. sample_point(1, 3, band=1) tuple of x, y: ex sample_point((1, 3), band=1) shapely.geometry.Point geojson.Point flopy.geometry.Point

bandint

raster band to re-sample

Returns
valuefloat
sample_polygon(self, polygon, band, invert=False, **kwargs)[source]

Method to get an unordered list of raster values that are located within a arbitrary polygon

Parameters
polygonlist, geojson, shapely.geometry, shapefile.Shape

sample_polygon method accepts any of these geometries:

a list of (x, y) points, ex. [(x1, y1), …] geojson Polygon object shapely Polygon object shapefile Polygon shape flopy Polygon shape

bandint

raster band to re-sample

invertbool

Default value is False. If invert is True then the area inside the shapes will be masked out

Returns
np.ndarray of unordered raster values
structured_downscale(self, band, delr, delc, nrow, ncol, resampler)[source]

Fast method for structured grid downscaling. Requires 0 rotation and that delc and delr are constant for accurate downscaling.

Parameters
bandint

raster band

delrint

x resolution

delcint

y resolution

resamplingrasterio Resampling object
Returns
np.ndarray
write(self, name)[source]

Method to write raster data to a .tif file

Parameters
namestr

output raster .tif file name

property xcenters

Returns a np.ndarray of raster x cell centers

property ycenters

Returns a np.ndarray of raster y cell centers

gsflow.utils.rasters.compiled_resampling(polygons, method, arr, xc, yc, nodatavals, ray_count, mask, data, ncpl)[source]

Multiprocessing method to resample to grid using mean, min, max, mode, or median resampling. Method uses numba compiled python to take advantage of parallel compiled methods for performing point in polygon calculations. Code is ~ 3 - 4 times faster than python multithreading and multiprocessing.

Parameters
polygonsnp.ndarray

3 dimensional array of polygon vertices for the model

methodstr

sampling methods

mean for mean sampling

median for median sampling

min for minimum sampling

max for maximum sampling

mode for most frequent sampling

arrnp.ndarray

array of raster data

xcnp.ndarray

raster cell xcenter points

ycnp.ndarray

raster cell ycenter points

nodatavalsnp.ndarray

1d array of no data values for raster

ray_countnp.ndarray

2d zero array of shape nrow, ncol

masknp.ndarray

2d boolean array (True) of shape nrow, ncol

datanp.ndarray

2d zero array of shape nrow, ncol to set resampled data to

ncplint

number of cells per layer

Returns
datanp.ndarray of resampled raster data
gsflow.utils.rasters.numba_mode(a)[source]

Numba method to calculate most frequent value in an array

Parameters
anp.ndarray

1d array of values

Returns
mode, count