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'>, <class 'numpy.float64'>)
- 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
- 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
- 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
- resample_to_grid(modelgrid, band, method='nearest', multithread=False, thread_pool=2, 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 interpolationnearest
for nearest neighborcubic
for bi-cubic interpolationmean
for mean samplingmedian
for median samplingmin
for minimum samplingmax
for maximum sampling’mode’ for majority sampling
multithread (bool) – DEPRECATED boolean flag indicating if multithreading should be used with the
mean
andmedian
sampling methodsthread_pool (int) – DEPRECATED number of threads to use for mean and median 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 thenearest
interpolation method.
- Return type:
np.array
- 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
- 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