GDAL - Tipps und Tricks
Zurück zu GDAL.
How To
Wichtige Rastermanipulationen: Auf den Seiten des "GIS-Kompetenzzentrums Uster" gibt es verschiedene Tipps zur Rasterbilder-Konvertierung mit GDAL (siehe [1]):
- Rasterbild ausschneiden
- Projektion und Metadaten zuweisen
- Resampling
- Rasterkacheln zusammenfassen mit gdal_merge
- Pyramiden (Overviews) berechnen für schnellen Dateizugriff
- JPEG-Komprimierung (z.B. für Luftbilder oder Schattierungen)
- PDF-Dateien mit Ghostscript rasterisieren
- ASCII GRID zu TIFF-File konvertieren
How to "Get height given coords from a large DTM" (ST_Value):
- Siehe Diskussion in PostGIS-Users Mailing List (5.12.2011): [2]
Tiled Raster Files
Looking for raster map services? See Hintergrundkarten#Our_favourites.
See also GeoServer.
---
Convert TIFF (tif+tfw) to GeoTIFF with GDAL:
gdal_translate -of GTiff -co "TILED=YES" input.tif tiledGeotif.tif
By using the TILED=YES parameter we ask the command to prepare the file for tiles rendering. Now the size of the resulting file is larger.
Then create overviews for the GeoTIFF file:
gdaladdo -r average tiledGeotif.tif 2 4 8 16
Pyramiden-Bilder erstellen
Problem und Ausgangslage: Grosse Rasterbilder machen den Bildaufbau langsam oder übersteigen gar das Memory.
Lösung:
- Bilder kacheln und ggf. in verschiedener Auflösung speichern (sog. Pyramiden-Bilder).
- Die GIS-Software lädt dabei zunächst nicht das eigentliche Bild, sondern eine Index-Datei (je nach GIS im Format .vrt oder Shapefile).
- Das empfohlene Format ist TIF. ECW könnte ev. schneller sein, müsste man in einem zweiten Performance-Schritt anschauen.
Voraussetzungen:
- GDAL installiert (vgl. http://hub.qgis.org/projects/quantum-gis/wiki/DownloadDe#OSGeo4W-Installer)
- Genügend Diskplatz und CPU-Leistung.
- Dokumentation auf http://www.gdal.org/gdal_utilities.html
- Nützliche Infos: http://linfiniti.com/2009/09/image-mosaicking-with-gdal/
Vorgehen:
- Angenommen das grosse Bild heisst myimage.tif
- Ziel ist, eine Datei myimage2.vrt (oder .shp) zu erhalten, die vom GIS geladen wird.
Anleitung:
1. Originalbilder in kommprimiertes(!) TIFF wandeln (falls noch nicht der Fall):
% gdal_translate -of GTiff -co COMPRESS=DEFLATE -co TILED=YES myimage.tif myimage2.tif
oder:
% gdal_translate -expand rgb -of GTiff -co COMPRESS=DEFLATE -co TILED=YES myimage.tif myimage2.tif
2. Mosaik erzeugen: => Alle erzeugten Bilder in ein eigenes Verzeichnis verschieben. Dort:
% gdalbuildvrt myimage2.vrt *.tif
3. Pyramide erstellen:
% gdaladdo -ro --config INTERLEAVE_OVERVIEW PIXEL --config COMPRESS_OVERVIEW JPEG myimage2.vrt 2 4 8 16
4. Nun kann man die Datei myimage2.vrt (die sich mit den eigentlichen TIF-Bildern im gleichen Verzeichnis befindet) ins GIS laden.
From WMS to MBTiles
- Source https://vespucci.io/tutorials/custom_imagery_mbtiles/#gdal
- Credits: Manfred Stock
# Fetch available subdatasets: gdalinfo "WMS:http://wms.zh.ch/OGDOrthoZH" # Generate description file (I left out the BBOX from the output of the # above command and changed the SRS to EPSG:4326 though, otherwise, I got # errors later): gdal_translate "WMS:http://wms.zh.ch/OGDOrthoZH?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&LAYERS=ortho&SRS=EPSG:4326" wms.xml -of WMS # Generate MBTiles file (-projwin takes coordinates as <ulx uly lrx lry> in # the given SRS, so depending on where you got the BBOX from, you might # have to switch positions of some of the values): gdal_translate -of MBTILES wms.xml zurich-mainstation.mbtiles -projwin 8.5334 47.3807 8.5426 47.3763 -projwin_srs EPSG:4326 -co TILE_FORMAT=JPEG -co QUALITY=100 -co TYPE=baselayer # Build overview images for lower zoom levels: gdaladdo zurich-mainstation.mbtiles 2 4 8 16 32 64 128 256 512 1024
From GeoTIFF to MBTiles using GDAL and mbutil
- Credits: Simon Poole.
- Tiling a large Swiss imagery with gdal (gdal2tiles_parallel version):
- The original imagery has 4 bands (RGB and NIR) and 16bit values
Build the virtual image using the RGB bands:
gdalbuildvrt -a_srs EPSG:21781 -allow_projection_difference -b 1 -b 2 -b 3 -input_file_list tifs16.list 2016rgb.vrt
Translate from 16 bit to 8 bit values (note this is fairly slow):
gdal_translate -scale -of VRT -ot Byte 2016rgb.vrt 2016rgb8.vrt
https://github.com/GitHubRGI/geopackage-python has the parallel gdal2tiles version:
gdal2tiles_parallel.py -s EPSG:21781 -z 8-19 -p mercator -f PNG --processes 4 2016rgb8.vrt
... wait a long time (on a 6 core xeon machine using --processes 6 ~47h down to zoom 20 which however doesn't make sense for 25cm/pixel imagery) ...
Generate mbtiles file:
- set SQLITE_TMPDIR to a location that has at least enough space for a full copy of the mbtiles file (vacuum needs this)
- https://github.com/mapbox/mbutil
mb-util --image_format=png --scheme=tms 2016rgb8/ /server/agis_2016.mbtiles
Takes roughly 20 hours (but slow disks).