Raster

The georeferenced raster returned by mirrorworld.services, mirrorworld.elevation, mirrorworld.satellite and mirrorworld.aerial.pdok.

class Raster(data, crs, transform, nodata=None, band_names=None, metadata=<factory>)[source]

A georeferenced raster.

transform uses the affine convention of rasterio/GDAL: a pixel’s top left corner at column c and row r is at x = a*c + b*r + xoff and y = d*c + e*r + yoff, stored as (a, b, xoff, d, e, yoff). For north-up rasters b = d = 0 and e is negative.

Parameters:
data: ndarray

Pixel values with shape (bands, rows, columns).

crs: str

Coordinate reference system, e.g. EPSG:28992.

transform: Tuple[float, float, float, float, float, float]

Affine transform from pixel to CRS coordinates.

nodata: float | None = None

Value marking pixels without data, if any.

band_names: List[str] | None = None

Name of each band, e.g. ["red", "green", "blue", "nir"].

metadata: dict

Source information, e.g. service URL, layer, scene ID and acquisition date.

property count: int

Number of bands.

property resolution: Tuple[float, float]

Pixel width and height in CRS units.

property bounds: Tuple[float, float, float, float]

Left, bottom, right, top in CRS units.

band(name)[source]

Returns one band by name.

Parameters:

name (str)

Return type:

ndarray

to_image(bands=None, percentiles=(2, 98))[source]

Renders one or three bands as an 8-bit PIL image, e.g. for previews or RGB inputs to a model.

8-bit data is used as is; other data is stretched between the given percentiles.

Parameters:
  • bands (List[str] | None) – (optional) Names of one or three bands. Defaults to the first three bands, or the first.

  • percentiles (Tuple[float, float]) – (optional) Low and high percentile of the contrast stretch. Defaults to (2, 98).

Returns:

An L or RGB image.

Return type:

Image

save(path, pil_args=None)[source]

Saves the raster with its georeferencing.

.tif/.tiff writes a GeoTIFF with all bands, data type, CRS and nodata (needs rasterio). .png/.jpg writes an 8-bit rendering (see to_image()) with a world file and a GDAL .aux.xml CRS sidecar. Either way a .json file with metadata and band names is written beside it.

Parameters:
  • path (str) – Output path.

  • pil_args (dict) – (optional) Additional arguments for PIL’s Image.save (PNG/JPEG only).

Return type:

None

utm_crs(lat, lon)[source]

Returns the WGS 84 / UTM zone CRS containing a point, a good default metric CRS for local work.

Parameters:
  • lat (float) – Latitude.

  • lon (float) – Longitude.

Returns:

e.g. EPSG:32610 for Seattle.

Return type:

str

bbox_to_crs(north, west, south, east, crs)[source]

Transforms a WGS84 bounding box to another CRS, densifying the edges so the result encloses it.

Returns:

minx, miny, maxx, maxy in the target CRS (in its x/y order, i.e. easting/northing for projected systems and longitude/latitude for geographic ones).

Parameters:
Return type:

Tuple[float, float, float, float]

read_geotiff(data)[source]

Decodes a GeoTIFF (e.g. a WCS or ArcGIS exportImage response).

Parameters:

data (bytes) – The file contents.

Returns:

The raster.

Return type:

Raster

read_cog(url, north, west, south, east, resolution=None, crs=None, resampling='nearest', band_names=None)[source]

Reads a bounding box from any cloud-optimized GeoTIFF over HTTP, fetching only the bytes needed, e.g. the whole-country LiDAR models that national agencies publish as single COGs.

Parameters:
  • url (str) – HTTP(S) URL of the GeoTIFF.

  • north (float) – Latitude of the top edge.

  • west (float) – Longitude of the left edge.

  • south (float) – Latitude of the bottom edge.

  • east (float) – Longitude of the right edge.

  • resolution (float | None) – (optional) Pixel size in units of crs. Defaults to the file’s own.

  • crs (str | None) – (optional) Output CRS. Defaults to the file’s own.

  • resampling (str) – (optional) Resampling method when the grid differs. Defaults to nearest.

  • band_names (List[str] | None) – (optional) Names for the bands.

Returns:

The raster.

Return type:

Raster