GeoPakTile
For writing geopackage XYZ raster tile layers, with support for using arbitrary grid systems, and for converting them to Cloud Optimized GeoTIFFs (COGs).
Geopackage XYZ files can use any tile grid system, this is often defined by a server's GetCapabilities response where a TileMatrixSet defines the data co-ordinate system, the extent, the number of rows and columns in each zoom lever, and the corner co-ordinates for each zoom level. This package contains a tool for parsing the GetCapabilities XML into a morecantile TileMatrixSet, and then for writing tiles to a new or existing geopackage.
Examples
Write tiles to a new geopackage using web mercator (similar to mbtiles), where tiles is a list of (z, x, y, bytes). tms is a morecantile TileMatrixSet - the standard web mercator can be obtained with morecantile.tms.get('WebMercatorQuad')
from geopaktile import GeoPakTile
# Here, a list of tiles are read from a local path, the bytes and co-ordinates are stored as tuples in a list:
# You might get these tiles from a tile server or anywhere else. Adjust accordingly.
tiles = [
(z, x, y, path.read_bytes()),
(z2, x2, y2, path2.read_bytes()),
(z3, x3, y3, path3.read_bytes()),
...
]
with GeoPakTile(test_gpkg, tms=morecantile.tms.get('WebMercatorQuad'), mode='rwc') as gpkg:
gpkg.add_tiles(tiles)
Open a geopackage in read only mode and see if it contains certain tiles. A TileMatrixSet isn't required for read-only mode.
from geopaktile import GeoPakTile
with GeoPakTile(test_gpkg, mode='ro') as gpkg:
assert gpkg.has_tile(z, x, y)
result = gpkg.has_tiles([(z, x, y), (z1, x1, y1)])
Parse a GetCapabilties xml to provide the TileMatrixSet for writing a geopackage:
from geopaktile import parse_capabilities
tms_set = parse_capabilities(xml)
tms = tms_set[0] # you'll need to know which one you want, or use:
tms = get_tile_matrix_set(xml, identifier = '1km') # an identifier that you know is defined in the xml
Convert a geopackage tile layer to a Cloud Optimized GeoTIFF. The highest zoom level becomes the full resolution image, and the lower zoom levels become the COG's overviews:
from geopaktile import gpkg_tiles_to_cog
gpkg_tiles_to_cog("tiles.gpkg", "tiles.tif")
# Clip to a bounding box, given here in longitude/latitude
gpkg_tiles_to_cog(
"tiles.gpkg",
"clipped.tif",
bbox=(-0.1, 51.45, 0.05, 51.52),
bbox_crs="EPSG:4326",
)
TileMatrixSet models
Tile Matrix definitions use the morecantile package (morecantile.TileMatrixSet, TileMatrix, etc.) so any morecantile TileMatrixSet (built-in or custom-loaded) can be passed to GeoPakTile. The XML parser returns morecantile TileMatrixSet objects.
XML Parser
The xml parser has been tested on NASA GetCapabilties XML and some others, if you come across any that doesn't work then please raise an issue or fix and pull request.
Converting to COG
gpkg_tiles_to_cog copies each zoom level's own pixels into the COG as an overview, rather than letting GDAL resample new overviews from the full resolution image, so every overview keeps the scale of the zoom level it came from. It works with any tile grid where each zoom level halves the pixel size of the one below, such as WebMercatorQuad and most grids from GetCapabilities documents.
The output extent defaults to the tiles present at the highest zoom level, or can be set with bbox (in the layer's CRS, or in bbox_crs if given). The extent is expanded slightly so that it lines up with whole pixels at the zoom levels used as overviews.
A zoom level can only be copied directly if its pixel grid lines up with the output extent. For small extents, the coarsest zoom levels often don't line up, and are left out by default. Pass resample_unaligned=True to include them by resampling them onto the extent, down to an overview 1 pixel across. A warning lists any zoom levels that were resampled. The method is set with resampling ("bilinear" by default; any rasterio Resampling name such as "nearest" or "average" works).
Other options:
| Argument | Default | Description |
|---|---|---|
table |
the only tile table | Tile table to convert, needed if the geopackage has more than one. |
min_zoom, max_zoom |
all zoom levels with tiles | Zoom levels to include. max_zoom sets the full resolution. Without resample_unaligned, setting min_zoom expands the extent to line up with that zoom level's pixels, which can make the output much larger. |
bands |
"RGBA" |
"RGBA" makes missing tiles transparent. "RGB" makes them black, and is required for JPEG compression. |
compress |
"DEFLATE" |
Any GDAL COG compression, such as "ZSTD", "LZW", "WEBP" or "JPEG". |
blocksize |
the geopackage tile width | Internal tile size of the COG. |
overwrite |
False |
Replace an existing output file. |
COG conversion requires rasterio, which includes GDAL's COG driver in its wheels.
Metadata
Release files for GeoPakTile 2.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| geopaktile-2.1.0.tar.gz | 16.5 kB | Without provenance | Details for geopaktile-2.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| geopaktile-2.1.0-py3-none-any.whl | Python 3 | none | any | Details for geopaktile-2.1.0-py3-none-any.whl |
Total release size: 35.4 kB
Release files / geopaktile-2.1.0.tar.gz
| Download URL | geopaktile-2.1.0.tar.gz |
|---|---|
| Size | 16.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ff5d079a0663a797e07396592a757e147f987b6b98b39e0a5eaee76142b27c26
|
|
BLAKE2b-256 checksum How to use checksums |
a3772782840c0492e16271efd7644aa0ed02fb5c7e76c217c3e8d50a314f12da
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / geopaktile-2.1.0-py3-none-any.whl
| Download URL | geopaktile-2.1.0-py3-none-any.whl |
|---|---|
| Size | 18.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
272ac7c6d3df62c6f3267ec88eeb5cece4df9b1e2a5999661326e45a9a69147b
|
|
BLAKE2b-256 checksum How to use checksums |
977b977adfe61228d17cfa32dafc61770317efc2e91fc50c57af272ff630a346
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for GeoPakTile 2.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.