Skip to content

morecantile.models

morecantile.models

Pydantic modules for OGC TileMatrixSets (www.ogc.org/standards/tms)

CRS

Bases: RootModel[Union[str, Union[CRSUri, CRSWKT, CRSRef]]]

CRS model.

Ref: github.com/opengeospatial/ogcapi-tiles/blob/master/openapi/schemas/common-geodata/crs.yaml

Code generated using koxudaxi/datamodel-code-generator

srs property

srs: str

return the string form of the user input used to create the CRS.

model_post_init

model_post_init(__context: Any) -> None

Post Init: Set private attr.

to_dict

to_dict() -> Dict

return DICT version of the CRS.

to_epsg

to_epsg(*args: Any, **kwargs: Any) -> Optional[int]

return EPSG number of the CRS.

to_json

to_json(*args: Any, **kwargs: Any) -> str

return JSON version of the CRS.

to_proj4

to_proj4(*args: Any, **kwargs: Any) -> str

return PROJ4 version of the CRS.

to_wkt

to_wkt(*args: Any, **kwargs: Any) -> str

return WKT version of the CRS.

CRSRef

Bases: BaseModel

CRS from referenceSystem.

CRSUri

Bases: BaseModel

Coordinate Reference System (CRS) from URI.

CRSWKT

Bases: BaseModel

Coordinate Reference System (CRS) from WKT encoded as PROJJSON Object.

TileMatrix

Bases: BaseModel

Tile Matrix Definition

A tile matrix, usually corresponding to a particular zoom level of a TileMatrixSet.

ref: github.com/opengeospatial/2D-Tile-Matrix-Set/blob/master/schemas/tms/2.0/json/tileMatrix.json

get_coalesce_factor

get_coalesce_factor(row: int) -> int

Get Coalesce value for TileMatrix.

TileMatrixSet

Bases: BaseModel

Tile Matrix Set Definition

A definition of a tile matrix set following the Tile Matrix Set standard. For tileset metadata, such a description (in tileMatrixSet property) is only required for offline use, as an alternative to a link with a http://www.opengis.net/def/rel/ogc/1.0/tiling-scheme relation type.

ref: github.com/opengeospatial/2D-Tile-Matrix-Set/blob/master/schemas/tms/2.0/json/tileMatrixSet.json

_invert_axis cached property

_invert_axis: bool

Check if CRS has inverted AXIS (lat,lon) instead of (lon,lat).

bbox cached property

bbox

Return TMS bounding box in geographic coordinate reference system.

geographic_crs cached property

geographic_crs: CRS

Return the TMS's geographic CRS.

is_quadtree cached property

is_quadtree: bool

Check for quadtree support.

is_variable cached property

is_variable: bool

Check if TMS has variable width matrix.

maxzoom property

maxzoom: int

TileMatrixSet maximum TileMatrix identifier

minzoom property

minzoom: int

TileMatrixSet minimum TileMatrix identifier

rasterio_crs cached property

rasterio_crs

Return rasterio CRS.

rasterio_geographic_crs cached property

rasterio_geographic_crs

Return the geographic CRS as a rasterio CRS.

xy_bbox property

xy_bbox

Return TMS bounding box in TileMatrixSet's CRS.

__init__

__init__(**data)

Set private attributes.

__iter__

__iter__()

Iterate over matrices

__repr__

__repr__()

Simplify default pydantic model repr.

_lr

_lr(*tile: Tile) -> Coords

Return the lower right coordinate of the tile in TMS coordinate reference system.

Attributes

tile: (x, y, z) tile coordinates or a Tile object we want the lower right coordinates of.

Returns

Coords: The lower right coordinates of the input tile.

_matrix_origin

_matrix_origin(matrix: TileMatrix) -> Coords

Return the Origin coordinates of the matrix.

_tile

_tile(xcoord: float, ycoord: float, zoom: int, ignore_coalescence: bool = True) -> Tile

Get the tile containing a Point (in TMS CRS).

Parameters

xcoord, ycoord : float A X and Y pair in TMS coordinate reference system. zoom : int The zoom level.

Returns

Tile

_ul

_ul(*tile: Tile) -> Coords

Return the upper left coordinate of the tile in TMS coordinate reference system.

Attributes

tile: (x, y, z) tile coordinates or a Tile object we want the upper left coordinates of.

Returns

Coords: The upper left coordinates of the input tile.

bounds

bounds(*tile: Tile) -> BoundingBox

Return the bounding box of the tile in geographic coordinate reference system.

Attributes

tile (tuple or Tile): A tuple of (x, y, z) tile coordinates or a Tile object we want the bounding box of.

Returns

BoundingBox: The bounding box of the input tile.

check_for_old_specification

check_for_old_specification(data)

Check for TMS V1.0 keywords.

children

children(*tile: Tile, zoom: int = None)

Get the children of a tile

The children are ordered: top-left, top-right, bottom-right, bottom-left.

Parameters

tile : Tile or sequence of int May be be either an instance of Tile or 3 ints, X, Y, Z. zoom : int, optional Determines the zoom level of the returned child tiles. This defaults to one higher than the tile (the immediate children).

Returns

list: list of Tile

custom classmethod

custom(extent: List[float], crs: CRS, tile_width: int = 256, tile_height: int = 256, matrix_scale: Optional[List] = None, extent_crs: Optional[CRS] = None, minzoom: int = 0, maxzoom: int = 24, title: Optional[str] = None, id: Optional[str] = None, ordered_axes: Optional[List[str]] = None, screen_pixel_size: float = 0.00028, decimation_base: int = 2, **kwargs: Any)

Construct a custom TileMatrixSet.

Attributes

crs: pyproj.CRS Tile Matrix Set coordinate reference system extent: list Bounding box of the Tile Matrix Set, (left, bottom, right, top). tile_width: int Width of each tile of this tile matrix in pixels (default is 256). tile_height: int Height of each tile of this tile matrix in pixels (default is 256). matrix_scale: list Tiling schema coalescence coefficient (default: [1, 1] for EPSG:3857). Should be set to [2, 1] for EPSG:4326. see: docs.opengeospatial.org/is/17-083r2/17-083r2.html#14 extent_crs: pyproj.CRS Extent's coordinate reference system, as a pyproj CRS object. (default: same as input crs) minzoom: int Tile Matrix Set minimum zoom level (default is 0). maxzoom: int Tile Matrix Set maximum zoom level (default is 24). title: str, optional Tile Matrix Set title id: str, optional Tile Matrix Set identifier ordered_axes: list of str, optional Override Axis order (e.g ["N", "S"]) else default to CRS's metadata screen_pixel_size: float, optional Rendering pixel size. 0.28 mm was the actual pixel size of a common display from 2005 and considered as standard by OGC. decimation_base: int, optional How tiles are divided at each zoom level (default is 2). Must be greater than 1. kwargs: Any Attributes to forward to the TileMatrixSet

Returns:

TileMatrixSet

feature

feature(tile: Tile, fid: Optional[str] = None, props: Optional[Dict] = None, buffer: Optional[NumType] = None, precision: Optional[int] = None, projected: bool = False) -> Dict

Get the GeoJSON feature corresponding to a tile.

Originally from github.com/mapbox/mercantile/blob/master/mercantile/__init__.py

Parameters

tile : Tile or sequence of int May be be either an instance of Tile or 3 ints, X, Y, Z. fid : str, optional A feature id. props : dict, optional Optional extra feature properties. buffer : float, optional Optional buffer distance for the GeoJSON polygon. precision: float If >= 0, geometry coordinates will be rounded to this number of decimal, otherwise original coordinate values will be preserved (default). projected : bool, optional Return coordinates in TMS projection. Default is false.

Returns

dict

from_v1 classmethod

from_v1(tms: Dict) -> TileMatrixSet

Makes a TMS from a v1 TMS definition

    Attributes

supportedCRS: CRS Tile Matrix Set coordinate reference system title: str Title of TMS abstract: str (optional) Abstract of CRS keywords: str (optional) Keywords identifier: str TMS Identifier wellKnownScaleSet: AnyHttpUrl (optional) WKSS URL boundingBox: TMSBoundingBox (optional) Bounding box of TMS tileMatrix: List[TileMatrix] List of Tile Matrices

Returns:

TileMatrixSet

intersect_tms

intersect_tms(bbox: BoundingBox) -> bool

Check if a bounds intersects with the TMS bounds.

is_valid

is_valid(*tile: Tile) -> bool

Check if a tile is valid.

lnglat

lnglat(x: float, y: float, truncate=False) -> Coords

Transform point(x,y) to geographic longitude and latitude.

lr

lr(*tile: Tile) -> Coords

Return the lower right coordinates of the tile in geographic coordinate reference system.

Attributes

tile (tuple or Tile): (x, y, z) tile coordinates or a Tile object we want the lower right geographic coordinates of.

Returns

Coords: The lower right geographic coordinates of the input tile.

matrix

matrix(zoom: int) -> TileMatrix

Return the TileMatrix for a specific zoom.

minmax

minmax(zoom: int) -> Dict

Return TileMatrix Extrema.

Parameters

zoom : int The zoom level.

Returns

Dict

neighbors

neighbors(*tile: Tile) -> List[Tile]

The neighbors of a tile

The neighbors function makes no guarantees regarding neighbor tile ordering.

The neighbors function returns up to eight neighboring tiles, where tiles will be omitted when they are not valid.

Parameters

tile : Tile or sequence of int May be be either an instance of Tile or 3 ints, X, Y, Z.

Returns

list

parent

parent(*tile: Tile, zoom: int = None)

Get the parent of a tile

The parent is the tile of one zoom level lower that contains the given "child" tile.

Parameters

tile : Tile or sequence of int May be be either an instance of Tile or 3 ints, X, Y, Z. zoom : int, optional Determines the zoom level of the returned parent tile. This defaults to one lower than the tile (the immediate parent).

Returns

list: list of Tile

quadkey

quadkey(*tile: Tile) -> str

Get the quadkey of a tile

Parameters

tile : Tile or sequence of int May be be either an instance of Tile or 3 ints, X, Y, Z.

Returns

str

quadkey_to_tile

quadkey_to_tile(qk: str) -> Tile

Get the tile corresponding to a quadkey

Parameters

qk : str A quadkey string.

Returns

Tile

sort_tile_matrices

sort_tile_matrices(v)

Sort matrices by identifier

tile

tile(lng: float, lat: float, zoom: int, truncate=False, ignore_coalescence: bool = False) -> Tile

Get the tile for a given geographic longitude and latitude pair.

Parameters

lng, lat : float A longitude and latitude pair in geographic coordinate reference system. zoom : int The zoom level. truncate : bool Whether or not to truncate inputs to limits of TMS geographic bounds.

Returns

Tile

tiles

tiles(west: float, south: float, east: float, north: float, zooms: Sequence[int], truncate: bool = False) -> Iterator[Tile]

Get the tiles overlapped by a geographic bounding box

Original code from github.com/mapbox/mercantile/blob/master/mercantile/__init__.py#L424

Parameters

west, south, east, north : sequence of float Bounding values in decimal degrees (geographic CRS). zooms : int or sequence of int One or more zoom levels. truncate : bool, optional Whether or not to truncate inputs to TMS limits.

Yields

Tile

Notes

A small epsilon is used on the south and east parameters so that this function yields exactly one tile when given the bounds of that same tile.

truncate_lnglat

truncate_lnglat(lng: float, lat: float) -> Tuple[float, float]

Truncate geographic coordinates to TMS geographic bbox.

Adapted from github.com/mapbox/mercantile/blob/master/mercantile/__init__.py

ul

ul(*tile: Tile) -> Coords

Return the upper left coordinates of the tile in geographic coordinate reference system.

Attributes

tile (tuple or Tile): (x, y, z) tile coordinates or a Tile object we want the upper left geographic coordinates of.

Returns

Coords: The upper left geographic coordinates of the input tile.

xy

xy(lng: float, lat: float, truncate=False) -> Coords

Transform geographic longitude and latitude coordinates to TMS CRS.

xy_bounds

xy_bounds(*tile: Tile) -> BoundingBox

Return the bounding box of the tile in TMS coordinate reference system.

Attributes

tile: A tuple of (x, y, z) tile coordinates or a Tile object we want the bounding box of.

Returns

BoundingBox: The bounding box of the input tile.

zoom_for_res

zoom_for_res(res: float, max_z: Optional[int] = None, zoom_level_strategy: str = 'auto', min_z: Optional[int] = None) -> int

Get TMS zoom level corresponding to a specific resolution.

Parameters:

  • res (float) –

    Resolution in TMS unit.

  • max_z (int, default: None ) –

    Maximum zoom level (default is tms maxzoom).

  • zoom_level_strategy (str, default: 'auto' ) –

    Strategy to determine zoom level (same as in GDAL 3.2). LOWER will select the zoom level immediately below the theoretical computed non-integral zoom level. On the contrary, UPPER will select the immediately above zoom level. Defaults to AUTO which selects the closest zoom level. ref: gdal.org/drivers/raster/cog.html#raster-cog

  • min_z (int, default: None ) –

    Minimum zoom level (default is tms minzoom).

Returns:

  • int ( int ) –

    TMS zoom for a given resolution.

Examples:

>>> zoom_for_res(430.021)

CRS_to_uri

CRS_to_uri(crs: CRS) -> str

Convert CRS to URI.

crs_axis_inverted

crs_axis_inverted(crs: CRS) -> bool

Check if CRS has inverted AXIS (lat,lon) instead of (lon,lat).

ordered_axis_inverted

ordered_axis_inverted(ordered_axes: List[str]) -> bool

Check if ordered axes have inverted AXIS (lat,lon) instead of (lon,lat).