Skip to content

Instantly share code, notes, and snippets.

@jarnaldich
Created September 6, 2022 08:52
Show Gist options
  • Save jarnaldich/e462ff8da5f3cb7f02f77416fe25f451 to your computer and use it in GitHub Desktop.
Save jarnaldich/e462ff8da5f3cb7f02f77416fe25f451 to your computer and use it in GitHub Desktop.
[Raster GeoPackage with Custom Tile Schema] Sample on generating a Raster GeoPackage with a Custom Tile Schema with GDAL 3 #gdal #geopackage #raster
function GetInfo {
param([string]$InputPath)
gdalinfo -json $InputPath | ConvertFrom-JSon -AsHashTable
}
function Urbana {
param(
[string]$InputPath,
[string]$OutputPath,
[String[]]$Bands=(1, 2, 3))
$scale = ""
$bandParam = $Bands -join " -b "
$vrt = ($OutputPath -replace ".gpkg","") + ".vrt"
$warped_alpha_vrt = ($OutputPath -replace ".gpkg","") + "_warped_alpha.vrt"
$info = GetInfo $InputPath
if(
(($info.bands.type[0] -eq "Byte") -and ($InputPath -match '_16[bB]')) -or
(($info.bands.type[0] -ne "Byte") -and ($InputPath -match '_8[bB]'))
) {
Write-Host "WARNING: Name does not match Byte info in $InputPath"
}
if($info.bands.type[0] -ne "Byte") {
$scale = "-scale 1 65535 1 255 -ot Byte"
}
# Build a VRT with band selection and datatype scaling
"gdal_translate -of VRT $scale -b $bandParam $InputPath $vrt"
# Use gdalwarp to turn NoData values/mask to an alpha channel
"gdalwarp -tap -tr 0.1 0.1 -srcnodata 0 -dstalpha -of VRT $vrt $warped_alpha_vrt"
# Create GPKG
# Tiling Scheme can be used because there is a tms_OF10C.json file in the GDAL_DATA dir.
"gdal_translate -of GPKG -co RASTER_TABLE=tiles -co TILING_SCHEME=OF10C $warped_alpha_vrt $OutputPath"
# Add Overviews
"gdaladdo -r average $OutputPath"
}
{
"title": "Esquema per Orto 25cm ICGC",
"abstract": "",
"boundingBox": {
"lowerCorner": [
258840,
4225392
],
"upperCorner": [
783128,
4749680
],
"crs": "http://www.opengis.net/def/crs/EPSG/0/25831",
"type": "BoundingBoxType"
},
"type": "TileMatrixSetType",
"identifier": "OF25C",
"tileMatrix": [
{
"type": "TileMatrixType",
"identifier": 0,
"tileWidth": 256,
"tileHeight": 256,
"matrixWidth": 1,
"matrixHeight": 1,
"topLeftCorner": [
258840,
4749680
],
"scaleDenominator": 14628571.4285714
},
{
"type": "TileMatrixType",
"identifier": 1,
"tileWidth": 256,
"tileHeight": 256,
"matrixWidth": 1,
"matrixHeight": 1,
"topLeftCorner": [
258840,
4749680
],
"scaleDenominator": 7314285.71428571
},
{
"type": "TileMatrixType",
"identifier": 2,
"tileWidth": 256,
"tileHeight": 256,
"matrixWidth": 2,
"matrixHeight": 2,
"topLeftCorner": [
258840,
4749680
],
"scaleDenominator": 3657142.85714286
},
{
"type": "TileMatrixType",
"identifier": 3,
"tileWidth": 256,
"tileHeight": 256,
"matrixWidth": 4,
"matrixHeight": 4,
"topLeftCorner": [
258840,
4749680
],
"scaleDenominator": 1828571.42857143
},
{
"type": "TileMatrixType",
"identifier": 4,
"tileWidth": 256,
"tileHeight": 256,
"matrixWidth": 8,
"matrixHeight": 8,
"topLeftCorner": [
258840,
4749680
],
"scaleDenominator": 914285.714285714
},
{
"type": "TileMatrixType",
"identifier": 5,
"tileWidth": 256,
"tileHeight": 256,
"matrixWidth": 16,
"matrixHeight": 16,
"topLeftCorner": [
258840,
4749680
],
"scaleDenominator": 457142.857142857
},
{
"type": "TileMatrixType",
"identifier": 6,
"tileWidth": 256,
"tileHeight": 256,
"matrixWidth": 32,
"matrixHeight": 32,
"topLeftCorner": [
258840,
4749680
],
"scaleDenominator": 228571.428571429
},
{
"type": "TileMatrixType",
"identifier": 7,
"tileWidth": 256,
"tileHeight": 256,
"matrixWidth": 64,
"matrixHeight": 64,
"topLeftCorner": [
258840,
4749680
],
"scaleDenominator": 114285.714285714
},
{
"type": "TileMatrixType",
"identifier": 8,
"tileWidth": 256,
"tileHeight": 256,
"matrixWidth": 128,
"matrixHeight": 128,
"topLeftCorner": [
258840,
4749680
],
"scaleDenominator": 57142.8571428571
},
{
"type": "TileMatrixType",
"identifier": 9,
"tileWidth": 256,
"tileHeight": 256,
"matrixWidth": 256,
"matrixHeight": 256,
"topLeftCorner": [
258840,
4749680
],
"scaleDenominator": 28571.4285714286
},
{
"type": "TileMatrixType",
"identifier": 10,
"tileWidth": 256,
"tileHeight": 256,
"matrixWidth": 512,
"matrixHeight": 512,
"topLeftCorner": [
258840,
4749680
],
"scaleDenominator": 14285.7142857143
},
{
"type": "TileMatrixType",
"identifier": 11,
"tileWidth": 256,
"tileHeight": 256,
"matrixWidth": 1024,
"matrixHeight": 1024,
"topLeftCorner": [
258840,
4749680
],
"scaleDenominator": 7142.85714285714
},
{
"type": "TileMatrixType",
"identifier": 12,
"tileWidth": 256,
"tileHeight": 256,
"matrixWidth": 2048,
"matrixHeight": 2048,
"topLeftCorner": [
258840,
4749680
],
"scaleDenominator": 3571.42857142857
},
{
"type": "TileMatrixType",
"identifier": 13,
"tileWidth": 256,
"tileHeight": 256,
"matrixWidth": 4096,
"matrixHeight": 4096,
"topLeftCorner": [
258840,
4749680
],
"scaleDenominator": 1785.71428571429
},
{
"type": "TileMatrixType",
"identifier": 14,
"tileWidth": 256,
"tileHeight": 256,
"matrixWidth": 8192,
"matrixHeight": 8192,
"topLeftCorner": [
258840,
4749680
],
"scaleDenominator": 892.857142857143
},
{
"type": "TileMatrixType",
"identifier": 15,
"tileWidth": 256,
"tileHeight": 256,
"matrixWidth": 16384,
"matrixHeight": 16384,
"topLeftCorner": [
258840,
4749680
],
"scaleDenominator": 446.428571428572
},
{
"type": "TileMatrixType",
"identifier": 16,
"tileWidth": 256,
"tileHeight": 256,
"matrixWidth": 32768,
"matrixHeight": 32768,
"topLeftCorner": [
258840,
4749680
],
"scaleDenominator": 223.214285714286
}
],
"supportedCRS": "http://www.opengis.net/def/crs/EPSG/0/25831"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment