Geospatial Data Abstraction Library
GDAL is a translator library for raster and vector geospatial data formats that is released under an X/MIT style Open Source license by the Open Source Geospatial Foundation. This library is very important when we are working with rasters and vectors. It supports 155 raster and 95 vector formats. This library is available in C++, C#, Python, Java and Ruby for Linux, Mac and Windows platform. For more information you can refer to GDALWiki.
DOWNLOAD & SETUP
You can download the executable GDAL packages (stable releases) from GISinternals. Choose compiler (Microsoft Visual C) & architecture (32bit or 64bit) depending upon your PC and download the compiled binaries zip file (not the Compiled libraries and headers).
Or in short download the compiled binaries of GDAL from the below links
Now Follow the below mentioned Setup procedure:
Every raster file is associated with georeferencing information which is used by GIS applications. This georeferencing information may be in the form of PAM (Persistent Auxiliary metadata) .aux.xml sidecar file, INTERNAL, TABFILE (.tab) or WORLDFILE. By default GDAL uses one of these forms in the priority order as mentioned above. This priority order can be changed (for GDAL v2.2+) with the GDAL_GEOREF_SOURCES configuration option (or GEOREF_SOURCES open option) whose value is a comma-separated list of the following keywords : PAM,INTERNAL,TABFILE,WORLDFILE,NONE. First mentioned sources are the most priority over the next ones. A non mentioned source will be ignored.
For example set Environmental variable GDAL_GEOREF_SOURCES and value as WORLDFILE,PAM,INTERNAL,TABFILE,NONE for giving first priority to Worldfile.
Now you test the GDAL tools. Open Command prompt in your PC and type gdalinfo --formats. This command will list out the type of raster formats it supports. Alternatively ogrinfo --formats will display vector formats.
If before showing the raster/vector formats, the command prompt window shows some error with .dll file like ogr_OCI.dll or/and gdal_GEOR.dll then you can delete these files from C:\gdal\bin\gdal\plugins folder.
Now you have setup GDAL in your PC and ready to go. Below we will see some important gdal tools which can help us in dealing with rasters and vectors.
Raster formats supported by GDAL
Vector formats supported by GDAL
The are many GDAL tools you will find in C:\gdal\bin\gdal\apps folder. Some of which are explained below.
1. gdalinfo : Suppose you have got a raster file in your system say D:\raster\bluemarbel.tif and you want to read the geospatial information in its header file, then run the following command in the command prompt of your PC and press enter.
You will see the following information. This information is stored in raster data header and GDAL helps us to display it in human readable format. Look the displayed information carefully.
Driver: GTiff/GeoTIFF Files: D:\raster\bluemarbel.tif Size is 21600, 21600 Coordinate System is: GEOGCS["WGS 84", DATUM["WGS_1984", SPHEROID["WGS 84",6378137,298.257223563, AUTHORITY["EPSG","7030"]], AUTHORITY["EPSG","6326"]], PRIMEM["Greenwich",0], UNIT["degree",0.0174532925199433], AUTHORITY["EPSG","4326"]] Origin = (-180.000000000000000,90.000000000000000) Pixel Size = (0.002083333333333,-0.002083333333333) Metadata: AREA_OR_POINT=Area Image Structure Metadata: INTERLEAVE=PIXEL Corner Coordinates: Upper Left (-180.0000000, 90.0000000) (180d 0' 0.00"W, 90d 0' 0.00"N) Lower Left (-180.0000000, 45.0000000) (180d 0' 0.00"W, 45d 0' 0.00"N) Upper Right (-135.0000000, 90.0000000) (135d 0' 0.00"W, 90d 0' 0.00"N) Lower Right (-135.0000000, 45.0000000) (135d 0' 0.00"W, 45d 0' 0.00"N) Center (-157.5000000, 67.5000000) (157d30' 0.00"W, 67d30' 0.00"N) Band 1 Block=512x512 Type=Byte, ColorInterp=Red Overviews: 10800x10800, 5400x5400, 2700x2700, 1350x1350, 675x675, 338x338, 169x169, 85x85, 43x43 Band 2 Block=512x512 Type=Byte, ColorInterp=Green Overviews: 10800x10800, 5400x5400, 2700x2700, 1350x1350, 675x675, 338x338, 169x169, 85x85, 43x43 Band 3 Block=512x512 Type=Byte, ColorInterp=Blue Overviews: 10800x10800, 5400x5400, 2700x2700, 1350x1350, 675x675, 338x338, 169x169, 85x85, 43x43
Driver: GDAL has got various drivers for reading the header information of various raster formats. For this raster (tif format) it used GTiff/GeoTIFF driver.
Files: It displays the files GDAL operated to extract the geospatial information. If there were external pyramid layers (bluemarbel.aux) along with this raster file, it would have displayed it also.
Size: It represents the width and height of the raster data in terms of pixels. This raster file has 21600 pixels along its width and 21600 pixels along its height.
Coordinate system: The Coordinate system of the raster data is represented in WKT (Well-Known Text) format.In this format the datum is mentioned as WGS 84 and projection as EPSG:4326 which is equivalent to WGS84 in LatLong projection. You can even see the radius of earth value (6378137), flattening value (298.257223563) and unit (degree). Refer for more information on various EPSG (European Petroleum Survey Group) projections.
Origin: Origin represents the Coordinate of the centre of the Upper-left pixel of the raster data.
Pixel Size: This represents how much units (degrees in this case) each pixel covers. In case the projection system were in UTM, then pixel size would have been represented in meters.
Corner Coordinates: This information is displayed in five parts as Upper Left, Lower Left, Upper Right, Lower Right and Center. You can observe that each cordinates have been written twice. First one is in the projection system which the data has and the next in Degree Minute Second.
Bands & Overviews: Band will display different band information like block size (512 pixel x 512 pixel), datatype (Byte i.e 8bit) and color of the band like red, blue, green or gray. Overview displays the number of pyramid layers (internal or external) the raster data has. In this case it has got 9 pyramid layers.
2. gdallocationinfo: This utility provide a mechanism to query information about a pixel given it's location in one of a variety of coordinate systems. This utility is very usefulwhen we want to find out the height of a perticular location in terms of coordinates. You can Download all DEM files of you area of interest.
For example if you have a dem file at location D:\GDem\ASTGTM2_N27E086_dem.tif and you want to find the height at latitude 27.45 & longitude 86.29, run the following command in commad prompt
gdallocationinfo -geoloc -valonly D:\GDem\ASTGTM2_N27E086_dem.tif 86.29 27.45
The output will be 1765.
This perticular DEM will give you height value only in the latitude range 27-28 and longitude range 86-87. What if you have downloaded all the DEM files and you want to query a single file instead of different DEM file for every different location? You need a mosaic of all DEM. But this will result in a very BIGTIFF data. For such problems, GDAL has provided a tool called gdalbuildvrt which is explained below.
3. gdalbuildvrt: This utility builds a virtual raster data in vrt (xml) format which has the reference of one or more raster files. Suppose you want to create a single vrt file Gdem.vrt for all DEM files downloaded, you can run the following command
gdalbuildvrt D:/GDem/GDem.vrt D:/GDEMv2/TIF/*.tif
Now you can run the gdallocationinfo command on a single raster file called GDem.vrt for all coordinates.
If you want to create a vrt file raster data having different coordinate systems, then add -a_srs EPSG:4326 after gdalbuildvrt above.
4. gdal_translate: This utility is used to convert one raster data to another format in compressed (optional) way. We will not be using this command but you can convert a raster format (8bit) to JPG format with 25% compression in EPSG:4326 coordinate system as follows
gdal_translate -a_srs EPSG:4326 -outsize 25% 25% -of JPEG D:/Raster/A.tif D:/Raster/A.jpg
5. gdaladdo: This tool will be used to create the overview (pyramid) layers of raster files so as to enable it render fast when opened in geospatial viewer or through WMS (Web Map Service). You can create pyramid layers internally or externally. But remember that if internal pyramid layers are created, they cannot be cleanly removed and will not allow further pyramids to be created externally. Hence go for external pyramid creation preferably in .aux format. For example the following command creates external pyramid layers in .aux format in 6 layers (2 4 8 16 32 64).
gdaladdo -r NEAREST --config USE_RRD YES D:/Raster/A.tif 2 4 8 16 32 64
If the given raster data was of 21600x21600 size then the pyramid layers will have 10800x10800, 5400x5400, 2700x2700, 1350x1350, 675x675, 338x338.
6. gdalwarp: This creates a new raster data with new coordinate system. We will not be using this tool. If you want to create a raster data in EPSG:4326 coordinate system irrespective of its original coordinate system, then run the following command
gdalwarp -t_srs EPSG:4326 D:/Raster/A.tif D:/Raster/A_reproj4326.tif
7. gdalsrsinfo: This tool displays the Spatial Reference System (SRS) of the raster in WKT (Well Known Text) format and PROJ.4 format.
The following will be the output for this raster data:
PROJ.4 : '+proj=longlat +datum=WGS84 +no_defs ' OGC WKT : GEOGCS["WGS 84", DATUM["WGS_1984", SPHEROID["WGS 84",6378137,298.257223563, AUTHORITY["EPSG","7030"]], AUTHORITY["EPSG","6326"]], PRIMEM["Greenwich",0], UNIT["degree",0.0174532925199433], AUTHORITY["EPSG","4326"]]
8. gdaltindex: GDALtindex is used to create a tile index shape file having the location details of all the raster files along with the boundary geometry. This utility is very useful when rendering a large number of raster data through WMS server like MS4W (Map Server 4 Windows).
For example you want to display all Landsat satellite data on a web page through WMS service, then use the tile index shape file instead of using the raster files directly. Create a tile index shape file (rastertindex.shp) using the following command
gdaltindex D:/Raster/rastertindex.shp D:/Raster/*.tif
If you have a collection of raster files in a folder but all have different Coordinate system, then you can save the coordinate system in tile index shape file as a column in EPSG format which is very useful when rendering raster through WMS service. Do it suing the following command
gdaltindex -t_srs EPSG:4326 -src_srs_name epsg_no D:/Raster/rastertindex.shp D:/Raster/*.tif
where -t_srs is the image boundary calculated in EPSG:4326 and epsg_no is the column of tile index shape file which saves the coordinate system of each raster data in epsg format.
9. ogr2ogr: This is an important tool which is used to convert vector data from one format to another.
For example, if we want to create KML file new vector.kml from shape.shp shapefile we can run the following command
ogr2ogr -f "KML" D:\vector\newvector.kml D:\vector\shape.shp
We can also create shapefile from geospatial database table having geometry column. Suppose we have got a table oStable in Oracle spatial, then we can create shapefile from this table as follows
ogr2ogr -f "Shapefile" D:\vector\newvector.shp OCI:dbusername/dbpassword@orcl -sql "Select * from OStable"
where dbusername is the database username, dbpassword is database password and orcl is the SID. You can even run a conditional query in -sql option and save the result in shapefile.
Sometimes it is required to merge two or more shapefiles into one, this can be accomplished with ogr2ogr tool using the following command for each shapefile to be merged.
ogr2ogr -f "ESRI Shapefile" D:\Merged.shp -update -append C:\FirstSHP.shp
ogr2ogr -f "ESRI Shapefile" D:\Merged.shp -update -append C:\SecondSHP.shp
This will mearge FirstSHP.shp and SecondSHP.shp shapefiles into Merged.shp.
10. ogrtindex: This has got the similar function as gdaltindex provided it takes vector files only.
11. ogrinfo: It displays the geometry information of a vector file. To see the geometry infomation like its coordinate system, contents and geometry details run the following command
ogrinfo -al D:\vector\newvector.kml
For this query find the output yourself. You can even run the conditional query to filter the content of a vector file.