GeoContext

Back to Scenes

Datasets in the Descartes Labs catalog have many different resolutions and projections. In two different images, even covering the same place on Earth, the pixels (i, j) usually correspond to two different points on the ground.

GeoContexts are a way to ensure multiple images from different sources are spatially compatible—that is, they all have the same shape (same width and height, in pixels), and the same pixel in each image corresponds to the same area on Earth.

They do this by simply capturing all the spatial parameters that affect how imagery is rasterized—namely output resolution, coordinate reference system, and bounding box—in one object that can be passed into different method calls. In typical use, these contexts are created for you with reasonable defaults, so you only need to understand the different parameters when you need more control.

The different subclasses of GeoContext implement different functionality.

  • AOI clips to arbitrary geometry, and lets you specify any output resolution and projection.
  • DLTile helps you split large regions up into a grid of any spacing and resolution, and represents a single tile in that grid, in UTM projection.

Tutorial

Often, you don’t have to create GeoContexts yourself—an AOI with default parameters is created for you by scenes.search and Scene.from_id.

In [1]: import descarteslabs as dl

In [2]: scene, default_ctx = dl.scenes.Scene.from_id('landsat:LC08:PRE:TOAR:meta_LC80260322013188_v1')

In [3]: default_ctx
Out[3]: 
AOI(geometry=None,
    resolution=15,
    crs=u'EPSG:32615',
    align_pixels=True,
    bounds=(-94.7328, 39.2786658, -92.0791951, 41.3717108),
    shape=None)

GeoContexts are immutable; instead, create copies with new values using AOI.assign. (Assigning new values to DLTiles is not yet supported.)

# let's use a lower resolution to load images faster
In [4]: lowres = default_ctx.assign(resolution=75)

In [5]: lowres_arr = scene.ndarray("red green blue", lowres)

In [6]: dl.scenes.display(lowres_arr, size=4, title="Default GeoContext, 75-meter resolution")
https://cdn.descarteslabs.com/docs/_images/geocontext1.png

You can also create GeoContexts explicitly:

In [7]: import shapely.affinity

# make a new polygon half the size of the scene's full extent
In [8]: new_cutline = shapely.affinity.scale(scene.geometry, xfact=0.5, yfact=0.5)

In [9]: webmerc_cutline_aoi = dl.scenes.AOI(
   ...:     geometry=new_cutline,
   ...:     resolution=75,
   ...:     crs="EPSG:3857"  # "EPSG:3857" is the code for the Web Mercator
   ...: )                    # coordinate reference system, see http://epsg.io/3857
   ...: 

In [10]: webmerc_cutline_arr = scene.ndarray("red green blue", webmerc_cutline_aoi)

In [11]: dl.scenes.display(webmerc_cutline_arr, size=4, title="Same scene, with cutline and Web Mercator")
https://cdn.descarteslabs.com/docs/_images/geocontext2.png

Let’s assign our new cutline to the default GeoContext to see the difference between the coordinate reference systems:

In [12]: with_cutline = lowres.assign(geometry=new_cutline)

In [13]: with_cutline_arr = scene.ndarray("red green blue", with_cutline)

In [14]: dl.scenes.display(with_cutline_arr, size=4, title="Original GeoContext with new cutline")
https://cdn.descarteslabs.com/docs/_images/geocontext3.png

Why is there all that empty space around the sides? We assigned a new geometry, but we didn’t change the bounds. Bounds determine the x-y extent that’s rasterized; geometry just clips within that. You can pass bounds="update" to compute new bounds when assinging a new geometry.

In [15]: cutline_bounds = lowres.assign(geometry=new_cutline, bounds="update")

In [16]: cutline_bounds_arr = scene.ndarray("red green blue", cutline_bounds)

In [17]: dl.scenes.display(cutline_bounds_arr, size=4, title="Original GeoContext, new cutline and bounds")
https://cdn.descarteslabs.com/docs/_images/geocontext4.png

You can also use DLTiles to split up regions along a grid:

In [1]: tiles = dl.scenes.DLTile.from_shape(
   ...:     new_cutline, resolution=75, tilesize=256, pad=16
   ...: )
   ...: 

In [2]: len(tiles)
Out[2]: 38

In [3]: tile0_arr = scene.ndarray("red green blue", tiles[0])

In [4]: tile1_arr = scene.ndarray("red green blue", tiles[1])

In [5]: dl.scenes.display(tile0_arr, tile1_arr, title=[tiles[0].key, tiles[1].key], size=3)
https://cdn.descarteslabs.com/docs/_images/geocontext5.png
class AOI(geometry=None, resolution=None, crs=None, align_pixels=True, bounds=None, shape=None)[source]

A GeoContext that clips scenes to a geometry, and/or to square bounds, with any output resolution and CRS.

Examples

cutline_aoi = dl.scenes.AOI(my_geometry, resolution=40)
aoi_with_cutline_disabled = cutline_aoi.assign(geometry=None)
no_cutline_aoi = dl.scenes.AOI(geometry=None, resolution=15, bounds=(-40, 35, -39, 36))
aoi_without_auto_bounds = dl.scenes.AOI(geometry=my_geometry, resolution=15, bounds=(-40, 35, -39, 36))
aoi_with_specific_pixel_dimensions = dl.scenes.AOI(geometry=my_geometry, shape=(200, 400))
Parameters:
  • geometry (GeoJSON-like dict, object with __geo_interface__; optional) – Clip scenes to this geometry. Coordinates must be WGS84 (lat-lon). If None, scenes will just be clipped to bounds.
  • resolution (float, optional) – Distance, in units of the CRS, that the edge of each pixel represents on the ground. Can only specify one of resolution and shape.
  • crs (str, optional) – Coordinate Reference System into which scenes will be projected, expressed as an EPSG code (like "EPSG:4326"), a PROJ.4 definition, or an OGC CRS Well-Known Text string
  • align_pixels (bool, optional, default True) –

    If True, this ensures that, in different Scenes rasterized with this same AOI GeoContext, pixels (i, j) correspond to the same area in space. This is accomplished by snapping the coordinates of the origin (top-left corner of top-left pixel) to a non-fractional interval of resolution.

    If align_pixels is False, when using scenes with different native resolutions and/or projections, pixels at the same indicies can be misaligned by a fraction of resolution (i.e. correspond to slighly different coordinates in space).

    However, this requires warping of the original image, which can be undesireable when you want to work with the original data in its native resolution and projection.

  • bounds (4-tuple, optional) – Clip scenes to these (min_x, min_y, max_x, max_y) bounds, expressed in WGS84 (lat-lon) coordinates. bounds are automatically computed from geometry if not specified. Otherwise, bounds are required.
  • shape (2-tuple, optional) – (rows, columns), in pixels, the output raster should fit within; the longer side of the raster will be min(shape). Can only specify one of resolution and shape.
assign(geometry='unchanged', resolution='unchanged', crs='unchanged', align_pixels='unchanged', bounds='unchanged', shape='unchanged')[source]

Return a copy of the AOI with the given values assigned.

Note

If you are assigning a new geometry and want bounds to updated as well, use bounds="update".

If you assign geometry without changing bounds, the new AOI GeoContext will produce rasters with the same shape and covering the same spatial area as the old one, just with pixels masked out that fall outside your new geometry.

Returns:new
Return type:AOI
align_pixels

bool – If True, this ensures that, in different Scenes rasterized with this same AOI GeoContext, pixels (i, j) correspond to the same area in space. This is accomplished by snapping the coordinates of the origin (top-left corner of top-left pixel) to a non-fractional interval of resolution.

If align_pixels is False, when using scenes with different native resolutions and/or projections, pixels at the same indicies can be misaligned by a fraction of resolution (i.e. correspond to slighly different coordinates in space).

However, this requires warping of the original image, which can be undesireable when you want to work with the original data in its native resolution and projection.

bounds

tuple – Clip scenes to these (min_x, min_y, max_x, max_y) bounds, expressed in WGS84 (lat-lon) coordinates.

crs

str – Coordinate Reference System into which scenes will be projected, expressed as an EPSG code (like "EPSG:4326"), a PROJ.4 definition, or an OGC CRS Well-Known Text string

geometry

shapely geometry – Clip scenes to this geometry Coordinates must be WGS84 (lat-lon) If None, scenes will just be clipped to bounds

raster_params

dict – The properties of this AOI, as keyword arguments to use for Raster.ndarray or Raster.raster.

Raises ValueError if self.bounds, self.crs, self.resolution, or self.align_pixels is None, or values are invalid.

resolution

float – Distance, in units of the CRS, that the edge of each pixel represents on the ground.

shape

tuple(rows, columns), in pixels, the output raster should fit within; the longer side of the raster will be min(shape).

class DLTile(dltile_dict)[source]

A GeoContext that clips and projects Scenes to a single DLTile.

DLTiles allow you to define a grid of arbitrary spacing, resolution, and overlap that can cover the globe. DLTiles are always in a UTM projection.

__init__ instantiates a DLTile from a dict returned by Raster.dltile.

It’s preferred to use the DLTile.from_latlon, DLTile.from_shape, or DLTile.from_key class methods to construct a DLTile GeoContext.

classmethod from_key(dltile_key, raster_client=None)[source]

Return a DLTile GeoContext from a DLTile key.

Parameters:
  • dltile_key (str) – DLTile key, e.g. ‘128:16:960.0:15:-1:37’
  • raster_client (descarteslabs.client.services.Raster, optional, default None) – Unneeded in general use; lets you use a specific client instance with non-default auth and parameters.
Returns:

tile

Return type:

DLTile

classmethod from_latlon(lat, lon, resolution, tilesize, pad, raster_client=None)[source]

Return a DLTile GeoContext that covers a latitude/longitude

Where the point falls within the tile will vary, depending on the point and tiling parameters.

Parameters:
  • lat (float) – Latitude (WGS84)
  • lon (float) – Longitude (WGS84)
  • resolution (float) – Distance, in meters, that the edge of each pixel represents on the ground
  • tilesize (int) – Length of each side of the tile, in pixels
  • pad (int) – Number of extra pixels by which each side of the tile is buffered. This determines the number of pixels by which two tiles overlap.
  • raster_client (descarteslabs.client.services.Raster, optional, default None) – Unneeded in general use; lets you use a specific client instance with non-default auth and parameters.
Returns:

tile

Return type:

DLTile

classmethod from_shape(shape, resolution, tilesize, pad, raster_client=None)[source]

Return a list of DLTiles that intersect the given geometry

Parameters:
  • shape (GeoJSON-like) – A GeoJSON dict, or object with a __geo_interface__. Must be in EPSG:4326 (WGS84 lat-lon) projection.
  • resolution (float) – Distance, in meters, that the edge of each pixel represents on the ground
  • tilesize (int) – Length of each side of the tile, in pixels
  • pad (int) – Number of extra pixels by which each side of the tile is buffered. This determines the number of pixels by which two tiles overlap.
  • raster_client (descarteslabs.client.services.Raster, optional, default None) – Unneeded in general use; lets you use a specific client instance with non-default auth and parameters.
Returns:

tiles

Return type:

List[DLTile]

bounds

tuple – The (min_x, min_y, max_x, max_y) of the area covered by this DLTile, in UTM coordinates

crs

str – Coordinate Reference System into which scenes will be projected. For DLTiles, this is always a UTM projection, given as an EPSG code.

geometry

shapely.geometry.Polygon – The polygon covered by this DLTile in WGS84 (lat-lon) coordinates

key

str – The DLTile’s key, which encodes the tiling parameters, and which number in the grid this tile is.

pad

int – Number of extra pixels by which each side of the tile is buffered. This determines the number of pixels by which two tiles overlap.

raster_params

dict – The properties of this DLTile, as keyword arguments to use for Raster.ndarray or Raster.raster.

resolution

float – Distance, in meters, that the edge of each pixel represents on the ground

ti

int – The y-index of this tile in its grid

tilesize

int – Length of each side of the tile, in pixels. Note that the total number of pixels along each side of an image is tile_size + 2*padding

tj

int – The x-index of this tile in its grid

zone

int – The UTM zone of this tile

class GeoContext[source]

Specifies spatial parameters to use when loading a raster from the Descartes Labs catalog.

Two Scenes loaded with the same GeoContext will result in images with the same shape (in pixels), covering the same spatial extent, regardless of the dimensions or projection of the original data.

Specifically, a fully-defined GeoContext specifies:

  • geometry to use as a cutline (WGS84), and/or bounds (WGS84)
  • resolution (m)
  • EPSG code of the output coordinate reference system
  • whether to align pixels to the output CRS (see docstring for AOI.align_pixels for more information)

GeoContexts are immutable.

raster_params

dict – The properties of this GeoContext, as keyword arguments to use for Raster.ndarray or Raster.raster.