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 'float'>, <class 'numpy.float32'>, <class 'numpy.float64'>)
FLOAT64 = (<class 'numpy.float64'>,)
INT16 = (<class 'numpy.int16'>,)
INT32 = (<class 'int'>, <class 'int'>, <class 'numpy.int32'>, <class 'numpy.int64'>)
INT64 = (<class 'numpy.int64'>,)
INT8 = (<class 'numpy.int8'>,)
bands

Returns a tuple of raster bands

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
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
Returns:

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)[source]

Static method to load a raster file into the raster object

Parameters:raster (str) –
Returns:
Return type:Raster object
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

resample_to_grid(xc, yc, band, method='nearest')[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:
  • xc (np.ndarray or list) – an array of x-cell centers
  • yc (np.ndarray or list) – an array of y-cell centers
  • band (int) – raster band to re-sample
  • method (str) –

    scipy interpolation method options

    ”linear” for bi-linear interpolation “nearest” for nearest neighbor “cubic” for bi-cubic interpolation

Returns:

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)[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
Returns:

Return type:

np.ndarray of unordered raster values

write(name)[source]

Method to write raster data to a .tif file

Parameters:name (str) – output raster .tif file name
xcenters

Returns a np.ndarray of raster x cell centers

ycenters

Returns a np.ndarray of raster y cell centers