flopy.utils.rasters module

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

Bases: object

The Raster object is used for cropping, sampling raster values, and re-sampling raster values to grids, and provides methods to plot rasters and histograms of raster digital numbers for visualization and analysis purposes.

Parameters:
  • array (np.ndarray) – a three dimensional array of raster values with dimensions defined by (raster band, nrow, ncol)

  • bands (tuple) – a tuple of raster bands

  • crs (int, string, rasterio.crs.CRS object) – either a epsg code, a proj4 string, or a CRS object

  • transform (affine.Affine object) – affine object, which is used to define geometry

  • nodataval (float) – raster no data value

  • rio_ds (DatasetReader object) – rasterIO dataset Reader object

Notes

Examples

>>> from flopy.utils import Raster
>>>
>>> rio = Raster.load("myraster.tif")
FLOAT32 = (<class 'float'>, <class 'numpy.float32'>)
FLOAT64 = (<class 'numpy.float64'>,)
INT16 = (<class 'numpy.int16'>, <class 'numpy.uint16'>)
INT32 = (<class 'int'>, <class 'numpy.int32'>, <class 'numpy.uint32'>, <class 'numpy.uint64'>, <class 'numpy.uint32'>, <class 'numpy.uint32'>)
INT64 = (<class 'numpy.int64'>, <class 'numpy.uint64'>)
INT8 = (<class 'numpy.int8'>, <class 'numpy.uint8'>)
property bands

Returns a tuple of raster bands

property bounds

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

crop(polygon, invert=False)[source]

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

Parameters:
  • polygon (list, 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

  • invert (bool) – Default value is False. If invert is True then the area inside the shapes will be masked out

property crs

Returns a rasterio CRS object

get_array(band, masked=True)[source]

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

Parameters:
  • band (int) – band number from the raster

  • masked (bool) – determines if nodatavals will be returned as np.nan to the user

Return type:

np.ndarray

histogram(ax=None, **kwargs)[source]

Method to plot a histogram of digital numbers

Parameters:
  • ax (matplotlib.pyplot.axes) – optional matplotlib axes for plotting

  • **kwargs – matplotlib keyword arguments see matplotlib documentation for valid arguments for histogram

Returns:

ax

Return type:

matplotlib.pyplot.axes

static load(raster: str | PathLike)[source]

Static method to load a raster file into the raster object

Parameters:

raster (str or PathLike) – The path to the raster file

Return type:

A Raster object

property nodatavals

Returns a Tuple of values used to define no data

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

Method to plot raster layers or contours.

Parameters:
  • ax (matplotlib.pyplot.axes) – optional matplotlib axes for plotting

  • contour (bool) – flag to indicate creation of contour plot

  • **kwargs – matplotlib keyword arguments see matplotlib documentation for valid arguments for plot and contour.

Returns:

ax

Return type:

matplotlib.pyplot.axes

static raster_from_array(array, modelgrid=None, nodataval=1e-10, crs=None, transform=None)[source]

Method to create a raster from an array. When using a modelgrid to define the transform, delc and delr must be uniform in each dimension. Otherwise, the user can define their own transform using the affine package.

Parameters:
  • array (np.ndarray) – array of (n-bands, nrows, ncols) for the raster

  • modelgrid (flopy.discretization.StructuredGrid) – StructuredGrid object (optional), but transform must be defined if a StructuredGrid is not supplied

  • nodataval ((int, float)) – Null value

  • crs (coordinate reference system input of many types) –

  • transform (affine.Affine) – optional affine transform that defines the spatial parameters of the raster. This must be supplied if a modelgrid is not used to define the transform

Return type:

Raster object

resample_to_grid(modelgrid, band, method='nearest', extrapolate_edges=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:
  • modelgrid (flopy.Grid object) – model grid to sample data from

  • band (int) – raster band to re-sample

  • method (str) –

    resampling 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

    ’mode’ for majority sampling

  • extrapolate_edges (bool) – 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.

Return type:

np.array

sample_point(*point, band=1)[source]

Method to get nearest raster value at a user provided point

Parameters:
  • *point (point 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

  • band (int) – raster band to re-sample

Returns:

value

Return type:

float

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

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

Parameters:
  • polygon (list, 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

  • band (int) – raster band to re-sample

  • invert (bool) – Default value is False. If invert is True then the area inside the shapes will be masked out

Return type:

np.ndarray of unordered raster values

to_crs(crs=None, epsg=None, inplace=False)[source]

Method for re-projecting rasters from an existing CRS to a new CRS

Parameters:
  • crs (CRS user input of many different kinds) –

  • epsg (int) – epsg code input that defines the coordinate system

  • inplace (bool) – Boolean flag to indicate if the operation takes place “in place” which reprojects the raster within the current object or the default (False) to_crs() returns a reprojected Raster object

Returns:

Raster or None – inplace=False, otherwise the reprojected information overwrites the current Raster object

Return type:

returns a reprojected raster object if

property transform

Returns the affine transform for the raster

write(name)[source]

Method to write raster data to a .tif file

Parameters:

name (str) – 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