NumpyRaster

class buzzard.ASource(<implementation detail>)[source]

Base abstract class defining the common behavior of all sources opened in the Dataset.

Features Defined

  • Has a stored spatial reference

  • Has a virtual spatial reference that is influenced by the Dataset’s opening mode

  • Can be closed

property wkt_stored

The spatial reference that can be found in the metadata of a source, in wkt format.

string or None

property proj4_stored

The spatial reference that can be found in the metadata of a source, in proj4 format.

string or None

property wkt_virtual

The spatial reference considered to be written in the metadata of a source, in wkt format.

string or None

property proj4_virtual

The spatial reference considered to be written in the metadata of a source, in proj4 format.

string or None

get_keys()[source]

Get the list of keys under which this source is registered to in the Dataset

property close

Close a source with a call or a context management. The close attribute returns an object that can be both called and used in a with statement

Examples

>>> ds.dem.close()
>>> with ds.dem.close:
        # code...
>>> with ds.acreate_raster('result.tif', fp, float, 1).close as result:
        # code...
>>> with ds.acreate_vector('results.shp', 'linestring').close as roofs:
        # code...
__del__()[source]
class buzzard.ASourceRaster(<implementation detail>)[source]

Base abstract class defining the common behavior of all rasters.

Features Defined

  • Has a stored Footprint that defines the location of the raster

  • Has a Footprint that is influenced by the Dataset’s opening mode

  • Has a length that defines how many channels are available

  • Has a channels_schema that defines per channel attributes (e.g. nodata)

  • Has a dtype (like np.float32)

  • Has a get_data method that allows to read pixels in their current state to numpy arrays

property fp_stored
property fp
property channels_schema
property dtype
property nodata

Accessor for first channel’s nodata value

get_nodata(channel=0)[source]

Accessor for nodata value

__len__()[source]

Return the number of channels

get_data(fp=None, channels=None, dst_nodata=None, interpolation='cv_area', **kwargs)[source]

Read a rectangle of data on several channels from the source raster.

If fp is not fully within the source raster, the external pixels are set to nodata. If nodata is missing, 0 is used. If fp is not on the same grid as the source raster, remapping is performed using interpolation algorithm. (It fails if the allow_interpolation parameter is set to False in Dataset (default)). When remapping, the nodata values are not interpolated, they are correctly spread to the output.

If dst_nodata is provided, nodata pixels are set to dst_nodata.

Warning

The alpha channels are currently resampled like any other channels, this behavior may change in the future. To normalize an rgba array after a resampling operation, use this piece of code:

>>> arr = np.where(arr[..., -1] == 255, arr, 0)

Warning

Bands in GDAL are indexed from 1. Channels in buzzard are indexed from 0.

Parameters

fp: Footprint of shape (Y, X) or None

If None: return the full source raster

If Footprint: return this window from the raster

channels: None or int or slice or sequence of int (see Channels Parameter below)

The channels to be read

dst_nodata: nbr or None

nodata value in output array If None and raster.nodata is not None: raster.nodata is used If None and raster.nodata is None: 0 is used

interpolation: one of {‘cv_area’, ‘cv_nearest’, ‘cv_linear’, ‘cv_cubic’, ‘cv_lanczos4’} or None

OpenCV method used if intepolation is necessary

Returns

array: numpy.ndarray of shape (Y, X) or (Y, X, C)
  • If the channels parameter is -1, the returned array is of shape (Y, X) when C=1, (Y, X, C) otherwise.

  • If the channels parameter is an integer >=0, the returned array is of shape (Y, X).

  • If the channels parameter is a sequence or a slice, the returned array is always of shape (Y, X, C), no matter the size of C.

(see Channels Parameter below)

Channels Parameter

type

value

meaning

output shape

NoneType

None (default)

All channels

(Y, X) or (Y, X, C)

slice

slice(None), slice(1), slice(0, 2), slice(2, 0, -1)

Those channels

(Y, X, C)

int

0, 1, 2, -1, -2, -3

Channel idx

(Y, X)

(int, …)

[0], [1], [2], [-1], [-2], [-3], [0, 1], [-1, 2, 1]

Those channels

(Y, X, C)

class buzzard.AStored(<implementation detail>)[source]

Base abstract class defining the common behavior of all sources that are stored somewhere (like RAM or disk).

Features Defined

  • Has an opening mode

property mode

Open mode, one of {‘r’, ‘w’}

class buzzard.AStoredRaster(<implementation detail>)[source]

Base abstract class defining the common behavior of all rasters that are stored somewhere (like RAM or disk).

Features Defined

  • Has a set_data method that allows to write pixels to storage

set_data(array, fp=None, channels=None, interpolation='cv_area', mask=None, **kwargs)[source]

Write a rectangle of data to the destination raster. Each channel in array is written to one channel in raster in the same order as described by the channels parameter. An optional mask may be provided to only write certain pixels of array.

If fp is not fully within the destination raster, only the overlapping pixels are written. If fp is not on the same grid as the destination raster, remapping is automatically performed using the interpolation algorithm. (It fails if the allow_interpolation parameter is set to False in Dataset (default)). When interpolating:

  • The nodata values are not interpolated, they are correctly spread to the output.

  • At most one pixel may be lost at edges due to interpolation. Provide more context in array to compensate this loss.

  • The mask parameter is also interpolated.

The alpha bands are currently resampled like any other band, this behavior may change in the future.

This method is not thread-safe.

Parameters

array: numpy.ndarray of shape (Y, X) or (Y, X, C)

The values to be written

fp: Footprint of shape (Y, X) or None

If None: write the full source raster If Footprint: write this window to the raster

channels: None or int or slice or sequence of int (see Channels Parameter below)

The channels to be written.

interpolation: one of {‘cv_area’, ‘cv_nearest’, ‘cv_linear’, ‘cv_cubic’, ‘cv_lanczos4’} or None

OpenCV method used if intepolation is necessary

mask: numpy array of shape (Y, X) and dtype bool OR inputs accepted by Footprint.burn_polygons

Channels Parameter

type

value

meaning

NoneType

None (default)

All channels

slice

slice(None), slice(1), slice(0, 2), slice(2, 0, -1)

Those channels

int

0, 1, 2, -1, -2, -3

Channel idx

(int, …)

[0], [1], [2], [-1], [-2], [-3], [0, 1], [-1, 2, 1]

Those channels

Caveat

When using a Raster backed by a driver (like a GDAL driver), the data might be flushed to disk only after the garbage collection of the driver object. To be absolutely sure that the driver cache is flushed to disk, call .close or .deactivate on this Raster.

fill(value, channels=None, **kwargs)[source]

Fill raster with value.

This method is not thread-safe.

Parameters

value: nbr
channels: int or sequence of int (see Channels Parameter below)

The channels to be written

Channels Parameter

type

value

meaning

NoneType

None (default)

All channels

slice

slice(None), slice(1), slice(0, 2), slice(2, 0, -1)

Those channels

int

0, 1, 2, -1, -2, -3

Channel idx

(int, …)

[0], [1], [2], [-1], [-2], [-3], [0, 1], [-1, 2, 1]

Those channels

Caveat

When using a Raster backed by a driver (like a GDAL driver), the data might be flushed to disk only after the garbage collection of the driver object. To be absolutely sure that the driver cache is flushed to disk, call .close or .deactivate on this Raster.

class buzzard.NumpyRaster(<implementation detail>)[source]

Concrete class defining the behavior of a wrapped numpy array

>>> help(Dataset.wrap_numpy_raster)

Features Defined

  • Has an array property that points to the numpy array provided at construction.

property array

Returns the Raster’s full input data as a Numpy array