Python API¶
Get and set runtime settings¶
- terracotta.get_settings()[source]¶
Returns the current set of global runtime settings.
- Return type:
Example
>>> import terracotta as tc >>> tc.get_settings().DEBUG False
- terracotta.update_settings(**new_config)[source]¶
Update the global Terracotta runtime settings.
- Parameters:
new_config (
Any
) – Options to override. Have to be valid Terracotta settings.- Return type:
Example
>>> import terracotta as tc >>> tc.get_settings().DEFAULT_TILE_SIZE (256, 256) >>> tc.update_settings(DEFAULT_TILE_SIZE=[512, 512]) >>> tc.get_settings().DEFAULT_TILE_SIZE (512, 512)
Get a driver instance¶
- terracotta.get_driver(url_or_path, provider=None)[source]¶
Retrieve Terracotta driver instance for the given path.
This function always returns the same instance for identical inputs.
Warning
Always retrieve Driver instances through this function instead of instantiating them directly to prevent caching issues.
- Parameters:
- Return type:
Example
>>> import terracotta as tc >>> tc.get_driver('tc.sqlite') TerracottaDriver( meta_store=SQLiteDriver('/home/terracotta/tc.sqlite'), raster_store=GeoTiffRasterStore() ) >>> tc.get_driver('mysql://root@localhost/tc') TerracottaDriver( meta_store=MySQLDriver('mysql+pymysql://localhost:3306/tc'), raster_store=GeoTiffRasterStore() ) >>> # pass provider if path is given in a non-standard way >>> tc.get_driver('root@localhost/tc', provider='mysql') TerracottaDriver( meta_store=MySQLDriver('mysql+pymysql://localhost:3306/tc'), raster_store=GeoTiffRasterStore() )
TerracottaDriver¶
- class terracotta.drivers.TerracottaDriver(meta_store, raster_store)[source]¶
Terracotta driver object used to retrieve raster tiles and metadata.
Do not instantiate directly, use
terracotta.get_driver()
instead.- compute_metadata(path, *, extra_metadata=None, use_chunks=None, max_shape=None)[source]¶
Compute metadata for a dataset.
- Parameters:
path (
str
) – Path identifing the dataset.extra_metadata (
Optional
[Any
]) – Any additional metadata that will be returned as is in the result, under the metadata key.use_chunks (
Optional
[bool
]) – Whether to load the dataset in chunks, when computing. Useful if the dataset is too large to fit in memory. Mutually exclusive with max_shape.max_shape (
Optional
[Sequence
[int
]]) – If dataset is larger than this shape, it will be downsampled while loading. Useful if the dataset is too large to fit in memory. Mutually exclusive with use_chunks.
- Return type:
- Returns:
A
dict
with the valuesrange
: global minimum and maximum value in datasetbounds
: physical bounds covered by dataset in latitude-longitude projectionconvex_hull
: GeoJSON shape specifying total data coverage in latitude-longitude projectionpercentiles
: array of pre-computed percentiles from 1% through 99%mean
: global meanstdev
: global standard deviationmetadata
: any additional client-relevant metadata
- connect(verify=True)[source]¶
Context manager to connect to the metastore and clean up on exit.
This allows you to pool interactions with the metastore to prevent possibly expensive reconnects, or to roll back several interactions if one of them fails.
- Parameters:
verify (
bool
) – Whether to verify the metastore (primarily its version) when connecting. Should be true unless absolutely necessary, such as when instantiating the metastore during creation of it.- Return type:
Note
Make sure to call
create()
on a fresh metastore before using this method.Example
>>> import terracotta as tc >>> driver = tc.get_driver('tc.sqlite') >>> with driver.connect(): ... for keys, dataset in datasets.items(): ... # connection will be kept open between insert operations ... driver.insert(keys, dataset)
- property db_version: str¶
Terracotta version used to create the meta store.
- Returns:
A str specifying the version of Terracotta that was used to create the meta store.
- get_datasets(where=None, page=0, limit=None)[source]¶
Get all known dataset key combinations matching the given constraints, and a path to retrieve the data (dependent on the raster store).
- Parameters:
- Return type:
- Returns:
A
dict
mapping from key sequence tuple to dataset path.
- get_keys()[source]¶
Get all known keys and their fulltext descriptions.
- Return type:
- Returns:
An
OrderedDict
in the form{key_name: key_description}
- get_metadata(keys)[source]¶
Return all stored metadata for given keys.
- Parameters:
keys (
Union
[Sequence
[str
],Mapping
[str
,str
]]) – Keys of the requested dataset. Can either be given as a sequence of key values, or as a mapping{key_name: key_value}
.- Return type:
- Returns:
A
dict
with the valuesrange
: global minimum and maximum value in datasetbounds
: physical bounds covered by dataset in latitude-longitude projectionconvex_hull
: GeoJSON shape specifying total data coverage in latitude-longitude projectionpercentiles
: array of pre-computed percentiles from 1% through 99%mean
: global meanstdev
: global standard deviationmetadata
: any additional client-relevant metadata
- get_raster_tile(keys, *, tile_bounds=None, tile_size=(256, 256), preserve_values=False, asynchronous=False)[source]¶
Load a raster tile with given keys and bounds.
- Parameters:
keys (
Union
[Sequence
[str
],Mapping
[str
,str
]]) – Key sequence identifying the dataset to load tile from.tile_bounds (
Optional
[Sequence
[float
]]) – Physical bounds of the tile to read, in Web Mercator projection (EPSG3857). Reads the whole dataset if not given.tile_size (
Sequence
[int
]) – Shape of the output array to return. Must be two-dimensional. Defaults toDEFAULT_TILE_SIZE
.preserve_values (
bool
) – Whether to preserve exact numerical values (e.g. when reading categorical data). Sets all interpolation to nearest neighbor.asynchronous (
bool
) – If given, the tile will be read asynchronously in a separate thread. This function will return immediately with aFuture
that can be used to retrieve the result.
- Return type:
- Returns:
Requested tile as
MaskedArray
of shapetile_size
ifasynchronous=False
, otherwise aFuture
containing the result.
- insert(keys, path, *, override_path=None, metadata=None, skip_metadata=False)[source]¶
Register a new dataset. Used to populate meta store.
- Parameters:
keys (
Union
[Sequence
[str
],Mapping
[str
,str
]]) – Keys of the dataset. Can either be given as a sequence of key values, or as a mapping{key_name: key_value}
.path (
str
) – Path to access dataset (driver dependent).override_path (
Optional
[str
]) – If given, this path will be inserted into the meta store instead of the one used to load the dataset.metadata (
Optional
[Mapping
[str
,Any
]]) – Metadata dict for the dataset. If not given, metadata will be computed viacompute_metadata()
.skip_metadata (
bool
) – If True, will skip metadata computation (will be computed during first request instead). Has no effect ifmetadata
argument is given.
- Return type:
Supported metadata stores¶
SQLite metadata store¶
- class terracotta.drivers.sqlite_meta_store.SQLiteMetaStore(path)[source]¶
An SQLite-backed metadata driver.
Stores metadata and paths to raster files in SQLite.
This is the simplest Terracotta driver, as it requires no additional infrastructure. The SQLite database is simply a file that can e.g. be stored together with the actual raster files.
Note
This driver requires the SQLite database to be physically present on the server. For remote SQLite databases hosted on S3, use
RemoteSQLiteDriver
.The SQLite database consists of 4 different tables:
terracotta
: Metadata about the database itself.key_names
: Contains two columns holding all available keys and their description.datasets
: Maps key values to physical raster path.metadata
: Contains actual metadata as separate columns. Indexed via key values.
This driver caches key names, but not metadata.
Warning
This driver is not thread-safe. It is not possible to connect to the database outside the main thread.
Remote SQLite metadata store¶
- class terracotta.drivers.sqlite_remote_meta_store.RemoteSQLiteMetaStore(remote_path)[source]¶
An SQLite-backed metadata driver, where the database file is stored remotely on S3.
Stores metadata and paths to raster files in SQLite.
See also
SQLiteDriver
for the local version of this driver.The SQLite database is simply a file that can be stored e.g. together with the actual raster files on S3. Before handling the first request, this driver will download a temporary copy of the remote database file. It is thus not feasible for large databases.
The local database copy will be updated in regular intervals defined by
REMOTE_DB_CACHE_TTL
.Warning
This driver is read-only. Any attempts to use the create, insert, or delete methods will throw a DatabaseNotWritableError.
MySQL metadata store¶
- class terracotta.drivers.mysql_meta_store.MySQLMetaStore(mysql_path)[source]¶
A MySQL-backed metadata driver.
Stores metadata and paths to raster files in MySQL.
Requires a running MySQL server.
The MySQL database consists of 4 different tables:
terracotta
: Metadata about the database itself.key_names
: Contains two columns holding all available keys and their description.datasets
: Maps key values to physical raster path.metadata
: Contains actual metadata as separate columns. Indexed via key values.
This driver caches key names.