rasterio package¶
Subpackages¶
- rasterio.rio package
- Submodules
- rasterio.rio.blocks module
- rasterio.rio.bounds module
- rasterio.rio.calc module
- rasterio.rio.clip module
- rasterio.rio.convert module
- rasterio.rio.edit_info module
- rasterio.rio.env module
- rasterio.rio.gcps module
- rasterio.rio.helpers module
- rasterio.rio.info module
- rasterio.rio.insp module
- rasterio.rio.main module
- rasterio.rio.mask module
- rasterio.rio.merge module
- rasterio.rio.options module
- rasterio.rio.overview module
- rasterio.rio.rasterize module
- rasterio.rio.rm module
- rasterio.rio.sample module
- rasterio.rio.shapes module
- rasterio.rio.stack module
- rasterio.rio.transform module
- rasterio.rio.warp module
- Module contents
- Submodules
Submodules¶
- rasterio._base module
- rasterio._crs module
- rasterio._env module
- rasterio._err module
- rasterio._example module
- rasterio._features module
- rasterio._fill module
- rasterio._io module
- rasterio._warp module
- rasterio.compat module
- rasterio.control module
- rasterio.coords module
- rasterio.crs module
- rasterio.drivers module
- rasterio.dtypes module
- rasterio.enums module
- rasterio.env module
- rasterio.errors module
- rasterio.features module
- rasterio.fill module
- rasterio.io module
- rasterio.mask module
- rasterio.merge module
- rasterio.path module
- rasterio.plot module
- rasterio.profiles module
- rasterio.sample module
- rasterio.shutil module
- rasterio.tools module
- rasterio.transform module
- rasterio.vrt module
- rasterio.warp module
- rasterio.windows module
Module contents¶
Rasterio
-
class
rasterio.
Env
(session=None, aws_unsigned=False, profile_name=None, session_class=<function Session.aws_or_dummy>, **options)¶ Bases:
object
Abstraction for GDAL and AWS configuration
The GDAL library is stateful: it has a registry of format drivers, an error stack, and dozens of configuration options.
Rasterio’s approach to working with GDAL is to wrap all the state up using a Python context manager (see PEP 343, https://www.python.org/dev/peps/pep-0343/). When the context is entered GDAL drivers are registered, error handlers are configured, and configuration options are set. When the context is exited, drivers are removed from the registry and other configurations are removed.
Example
with rasterio.Env(GDAL_CACHEMAX=128000000) as env: # All drivers are registered, GDAL's raster block cache # size is set to 128 MB. # Commence processing... ... # End of processing. # At this point, configuration options are set to their # previous (possible unset) values.
A boto3 session or boto3 session constructor arguments aws_access_key_id, aws_secret_access_key, aws_session_token may be passed to Env’s constructor. In the latter case, a session will be created as soon as needed. AWS credentials are configured for GDAL as needed.
-
credentialize
()¶ Get credentials and configure GDAL
Note well: this method is a no-op if the GDAL environment already has credentials, unless session is not None.
- Returns
- Return type
None
-
classmethod
default_options
()¶ Default configuration options
- Parameters
None –
- Returns
- Return type
dict
-
drivers
()¶ Return a mapping of registered drivers.
-
-
rasterio.
band
(ds, bidx)¶ A dataset and one or more of its bands
- Parameters
ds (dataset object) – An opened rasterio dataset object.
bidx (int or sequence of ints) – Band number(s), index starting at 1.
- Returns
- Return type
rasterio.Band
-
rasterio.
open
(fp, mode='r', driver=None, width=None, height=None, count=None, crs=None, transform=None, dtype=None, nodata=None, sharing=False, **kwargs)¶ Open a dataset for reading or writing.
The dataset may be located in a local file, in a resource located by a URL, or contained within a stream of bytes.
In read (‘r’) or read/write (‘r+’) mode, no keyword arguments are required: these attributes are supplied by the opened dataset.
In write (‘w’ or ‘w+’) mode, the driver, width, height, count, and dtype keywords are strictly required.
- Parameters
fp (str, file object or pathlib.Path object) – A filename or URL, a file object opened in binary (‘rb’) mode, or a Path object.
mode (str, optional) – ‘r’ (read, the default), ‘r+’ (read/write), ‘w’ (write), or ‘w+’ (write/read).
driver (str, optional) – A short format driver name (e.g. “GTiff” or “JPEG”) or a list of such names (see GDAL docs at http://www.gdal.org/formats_list.html). In ‘w’ or ‘w+’ modes a single name is required. In ‘r’ or ‘r+’ modes the driver can usually be omitted. Registered drivers will be tried sequentially until a match is found. When multiple drivers are available for a format such as JPEG2000, one of them can be selected by using this keyword argument.
width (int, optional) – The number of columns of the raster dataset. Required in ‘w’ or ‘w+’ modes, it is ignored in ‘r’ or ‘r+’ modes.
height (int, optional) – The number of rows of the raster dataset. Required in ‘w’ or ‘w+’ modes, it is ignored in ‘r’ or ‘r+’ modes.
count (int, optional) – The count of dataset bands. Required in ‘w’ or ‘w+’ modes, it is ignored in ‘r’ or ‘r+’ modes.
crs (str, dict, or CRS; optional) – The coordinate reference system. Required in ‘w’ or ‘w+’ modes, it is ignored in ‘r’ or ‘r+’ modes.
transform (Affine instance, optional) – Affine transformation mapping the pixel space to geographic space. Required in ‘w’ or ‘w+’ modes, it is ignored in ‘r’ or ‘r+’ modes.
dtype (str or numpy dtype) – The data type for bands. For example: ‘uint8’ or
rasterio.uint16
. Required in ‘w’ or ‘w+’ modes, it is ignored in ‘r’ or ‘r+’ modes.nodata (int, float, or nan; optional) – Defines the pixel value to be interpreted as not valid data. Required in ‘w’ or ‘w+’ modes, it is ignored in ‘r’ or ‘r+’ modes.
sharing (bool; optional) – To reduce overhead and prevent programs from running out of file descriptors, rasterio maintains a pool of shared low level dataset handles. When True this function will use a shared handle if one is available. Multithreaded programs must avoid sharing and should set sharing to False.
kwargs (optional) – These are passed to format drivers as directives for creating or interpreting datasets. For example: in ‘w’ or ‘w+’ modes a tiled=True keyword argument will direct the GeoTIFF format driver to create a tiled, rather than striped, TIFF.
- Returns
- Return type
A
DatasetReader
orDatasetWriter
object.
Examples
To open a GeoTIFF for reading using standard driver discovery and no directives:
>>> import rasterio >>> with rasterio.open('example.tif') as dataset: ... print(dataset.profile)
To open a JPEG2000 using only the JP2OpenJPEG driver:
>>> with rasterio.open( ... 'example.jp2', driver='JP2OpenJPEG') as dataset: ... print(dataset.profile)
To create a new 8-band, 16-bit unsigned, tiled, and LZW-compressed GeoTIFF with a global extent and 0.5 degree resolution:
>>> from rasterio.transform import from_origin >>> with rasterio.open( ... 'example.tif', 'w', driver='GTiff', dtype='uint16', ... width=720, height=360, count=8, crs='EPSG:4326', ... transform=from_origin(-180.0, 90.0, 0.5, 0.5), ... nodata=0, tiled=True, compress='lzw') as dataset: ... dataset.write(...)
-
rasterio.
pad
(array, transform, pad_width, mode=None, **kwargs)¶ pad array and adjust affine transform matrix.
- Parameters
array (ndarray) – Numpy ndarray, for best results a 2D array
transform (Affine transform) – transform object mapping pixel space to coordinates
pad_width (int) – number of pixels to pad array on all four
mode (str or function) – define the method for determining padded values
- Returns
(array, transform) – Tuple of new array and affine transform
- Return type
tuple
Notes
See numpy docs for details on mode and other kwargs: http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.pad.html