gdal_t1
In [2]:
cd /Users/abharathi/Documents/gis_data/gdal-tools/
In [3]:
ls
GDAL and OGR started as two different programs, hence have a different usage pattern or design.
Why use GDAL
- faster
- smaller, fits headless, remote execution
- FOSS
- great for server-side execution
- great for compression
Speed
- GDAL is fast. If we use Python to kick off GDAL as a CLI - it is still fast (
os.system
) - If we use Python bindings, they are much slower than using raw GDAL
Tools with GDAL¶ ¶
We get 2 sets of tools - raster tools that come with GDAL and vector tools that come with OGR.
3 Main tools
-
gdalinfo
- quick metadata -
gdal_translate
- convert formats, apply compression -
gdalwarp
- resampling and reprojecting
Other tools
-
gdaladdo
- pyramid tiles for images for quicker GUI loading -
gdaltindex
- tile index will give overviews of tiles -
gdalbuiltvrt
- for creating virtual rasters -
gdaldem
,gdalcounter
- elevation -
gdal_rasterize
,gdal_poligonize
- for conversion to and from raster <—> vector -
gdal_grid
- interpolations -
nearblack
- work with edge artifacts in rasters
Python bindings
-
gdal_merge.py
- merge rasters -
gdal_calc.py
- raster algebra -
gdal_pansharpen.py
- for optical images -
gdal_retile.py
- tiling / chipping for deep learning
OGR
-
ogrinfo
- lists source and metadata -
ogr2ogr
- format conversion -
ogrmerge.py
- merge vector layers
In [5]:
!which gdalinfo
In [13]:
!gdalinfo --version
In [11]:
ls /Users/abharathi/micromamba/envs/opengeo/bin/ogr*
In [15]:
# formats supported by gdal
!gdalinfo --formats
In [17]:
cd srtm/
In [19]:
ls -lh
In [20]:
!gdalinfo N28E086.hgt
In [25]:
# Can chain command options
!gdalinfo -hist N28E086.hgt
In [27]:
ls *.hgt > srtm_filelist.txt
In [28]:
cat srtm_filelist.txt
In [29]:
!gdalbuildvrt -input_file_list srtm_filelist.txt merged.vrt
In [30]:
cat merged.vrt
In [31]:
!gdalinfo -stats merged.vrt
In [33]:
# Get maximum of the merged rasters
%time
!gdalinfo -stats -json merged.vrt | jq ".bands[0].maximum"
In [34]:
!gdal_translate --formats
In [35]:
%time
# gdal will guess the output format
!gdal_translate merged.vrt merged.tif
In [36]:
ls -lh merged*
In [37]:
%time
# gdal will guess the output format
!gdal_translate merged.vrt merged.zarr
In [38]:
!gdalinfo merged.tif
Use creation options (-co
) to specify compression when creating the resulting raser. Doc: https://gdal.org/drivers/raster/gtiff.html#creation-options
TILDED=YES
- stores tiled TIFF where neighborhood effect is used for compression
PREDICTOR=YES
- stores diff in values
In [43]:
%time
!gdal_translate merged.vrt merged.tif -co COMPRESS=DEFLATE -co TILED=YES -co PREDICTOR=2
In [44]:
ls -lh *.tif
In [45]:
!gdalinfo merged.tif
In [46]:
%time
!gdal_translate merged.vrt merged.tif -co COMPRESS=DEFLATE \
-co TILED=YES -co PREDICTOR=2 -a_nodata -9999
In [57]:
!gdalinfo -json merged.tif | jq ".bands[0].noDataValue"
In [59]:
%time
!gdal_translate -of COG merged.vrt merged_cog.tif \
-co COMPRESS=DEFLATE -co PREDICTOR=2 -co NUM_THREADS=ALL_CPUS \
-a_nodata -9999
In [60]:
!ls -lh *.tif
In [65]:
!gdalinfo merged_cog.tif -json | jq ".metadata.IMAGE_STRUCTURE"
Using GDALINFO on COG files stored on the cloud¶ ¶
I am using Open aerial map site for this: https://map.openaerialmap.org/#/-117.81875610351562,33.76544869849223,9/latest/643b06ca8cae390005a1456f?_k=9krcby
In [68]:
!gdalinfo https://oin-hotosm.s3.amazonaws.com/643b06548cae390005a1456c/0/643b06548cae390005a1456d.tif
In [70]:
# We specify scale since the vert units are in meters whereas the horz units are in degrees
%time
!gdaldem hillshade merged.vrt hillshade.tif -s 111120
In [71]:
ls -lh hill*
In [74]:
# first create a file to store the classes
1000,101,146,82
1500,190,202,130
2000,241,225,145
2500,244,200,126
3000,197,147,117
4000,204,169,170
5000,251,238,253
6000,255,255,255
Out[74]:
In [77]:
cat colormap.txt
In [78]:
%time
!gdaldem color-relief merged.vrt colormap.txt colorized_dem.tif
In [82]:
# Convert to PNG, but a downsampled file that is 10% x 10%
%time
!gdal_translate -of PNG colorized_dem.tif colorized.png -outsize 10% 10%
In [83]:
ls -lh colorized*
In [87]:
0.00009259259259 * 111320
Out[87]:
In [ ]: