# GDAL & OGR {%hackmd BJrTq20hE %} [TOC] ## Misc [Turn off the setting of QGIS to produce XML when closing Raster layer - Geographic Information Systems Stack Exchange](https://gis.stackexchange.com/questions/136366/turn-off-the-setting-of-qgis-to-produce-xml-when-closing-raster-layer) ## Docs - [Official python doc](https://gdal.org/python/) - just a list containing all python class , method , attrubute and functions with sparse comment. - [GDAL python gotchas](https://trac.osgeo.org/gdal/wiki/PythonGotchas) - May be more useful ## Raster - [Cropping Geotiff with gdalwarp](https://gis.stackexchange.com/questions/79919/cropping-geotiff-with-gdalwarp) - [Clipping raster with vector layer using GDAL](https://gis.stackexchange.com/questions/16657/clipping-raster-with-vector-layer-using-gdal) ```bash gdalwarp -of GTiff -cutline cutline.shp -cl layer_name.shp -crop_to_cutline src_raster.tiff dst_raster.tiff ``` - [Clipping raster with vector boundaries using QGIS?](https://gis.stackexchange.com/questions/10117/clipping-raster-with-vector-boundaries-using-qgis) ```bash gdalwarp -cutline extent.shp -crop_to_cutline -of GTiff -dstnodata 255 inraster.tif inraster_cropped.tif -co COMPRESS=LZW -co TILED=YES --config GDAL_CACHEMAX 2048 -multi ``` - The last two parameters allows you to boost the process using: - A multicore implementation. - Setting the cache available to the function. Change resolution ```bash= res=40 datum=TWD97 gdal_translate -of "GTiff" -tr ${res} ${res} ./970822_sub5lay.tif ./${res}m/970822_sub5lay_${res}m_${datum}.tif ``` Export raster to JPEG ```bash= dirname=converted_tif find ./ -name "*.tif" -exec gdal_translate -of JPEG -scale -co worldfile=yes {} {}.jpg mkdir $dirname \; find ./ -name "*.jpeg" -exec mv {} $dirname/ \; ``` ### Cloud optimized GTiff For gdal >= 3.1.x: ```bash= gdal_translate src.tif dst.tif -of COG -co COMPRESS=ZSTD -co QUALITY=100 ``` ```bash dirname=COG_tif mkdir $dirname find ./ -maxdepth 1 -not -path "${dirname}" -name "*.tif" \ -exec gdal_translate \ -of COG \ -co COMPRESS=ZSTD \ -co QUALITY=100 \ -co BIGTIFF=YES \ {} "${dirname}"/{} \; ``` Batch coversion: IMG => HFA ```bash= find ./ -name "*.img" -exec gdal_translate -of "HFA" -ot Byte {} {}.byte.img \; ``` ### Rasterize ```bash= gdal_rasterize -burn 255 -of lan -ot Byte -init 0 -tr 5 5 ./ridge_study_5.shp ./40m_TSIv2.lan ``` ### Get raster value ```python from osgeo import gdal ds = gdal.Open('something.tif') band = ds.GetRasterBand(1) nodata_value = band.GetNoDataValue() ``` ## Vector ### Vector conversion shp polygon to gpkg ```bash= src=input.shp dst=output.gpkg ogr2ogr -s_SRS EPSG:3824 -t_srs EPSG:4326 -of gpkg -nlt MULTIPOLYGON ${dst} ${src} ``` to csv - Quickly convert with bash command ```bash= dirname=converted_csv find ./ -name "*.shp" -exec ogr2ogr -f "CSV" {}.csv {} -lco GEOMETRY=AS_XY \; mkdir $dirname find ./ -name "*.csv" -exec mv {} $dirname/ \; ``` csv to ? ```bash= ogr2ogr \ -s_srs EPSG:4326 \ -t_srs EPSG:3857 \ -oo X_POSSIBLE_NAMES=Lon* \ -oo Y_POSSIBLE_NAMES=Lat* \ -f "ESRI Shapefile" \ test.shp test.csv ``` [qgis - Bulk csv to shapefile (using ogr2ogr) - Geographic Information Systems Stack Exchange](https://gis.stackexchange.com/questions/276590/bulk-csv-to-shapefile-using-ogr2ogr) ### remove all empty geometry/geometries ```bash dst=clean.gpkg src=raw.gpkg ogr2ogr \ -dialect sqlite \ -sql \ "select * from WT_label where ST_isEmpty(WT_label.geom) is not true as species" $dst $src ``` ## Bash tricks ### List all raster file's size (XSize,YSize) ```bash= for f in $(find . -name '*.tif'); do gdalinfo ${f} | grep 'Size is' ; done ```