 In this tutorial you will learn GDAL. GDAL is a translated library for raster and vector geospatial data formats. After this tutorial you will be able to retrieve information from GIS data, reproject GIS data, change raster properties, convert raster formats, convert vector formats, apply spatial queries on vectors, and convert comma separated value files. In the end you will also learn how to perform batch conversion. If you have QGIS installed you can run GDAL commands from the OSGO 4W shell. Let's first go to the folder where we have stored the tutorial data. It contains a shapefile with municipalities, a folder with land use data from a model, a CSV file with points of interest, a road network shapefile, and a digital elevation model from SRTM as a geotiff. Let's first explore the SRTM geotiff. So we use GDAL info and then we give the name of the TIFF file. And here we can see the information. It says that it's a geotiff. The size is 6001 by 6001, so it means rows and columns. The coordinate system is a geographic coordinate system. We can see it here, EPSG4326, always look at the last EPSG code that is mentioned. And the units here are in degree, so the coordinates are in latitude, longitude, pixel size, which is also in degrees, and the boundary coordinates also in degrees, and a no data value. Now let's in the same way explore the vector data. So we go to OGR info. Let's also see how to use OGR info. So go here to vector programs, OGR info. And I see that we can use the argument AL to list all features of all layers, so it gives us all the information that is there. If you want to summary, you can also use minus SO. Let's try those. If I do AL, and then we apply it to the rows shape file, the row network vector file, I get here all the data. That's of course too much to read. So I stop it with Ctrl C, and I use the more command to look at it carefully, page to page. And there I see how many features it has, had a lot of features. Space, I go to the next page. I also see that this one is EPSG4226, so geographic coordinate system that is too longitude and contains a lot of attributes. If I would have used the summary mode, so here you see that that is SO, it will only show very little information that is not very useful. Let's check the layer with the municipalities, and also use their AL. Also a lot of data in it, so use more. And there I see 418 features. The extent looks not like latitude longitude, and I can see here from the projection that it's the Dutch projection, Amersfoort, and I can see that it's also new, which is EPSG28992 with units in meters. And then there's all kinds of census data per municipality in this file. I get out of here with Ctrl C. So the idea is to re-project the data sets to the Dutch projection, so we can do further analysis with that. And if we want to convert the DEM, the raster file, we use GDALWARP, the raster programs, GDALWARP is for image re-projection and warping, and that's what we're going to do. We use GDALWARP, and I use the target projection with TSRS, and then EPSG, the target projection is the Dutch one, that's EPSG28992, Amersfoort.exe, and then give the input file and the output file name, let's call it dmrd.exe.tif, and run it, and here we see that it converts the file to the projection. And we can check the result in QGIS, so let's see. Here's the file, and I drag it to the map canvas, it asks me which conversion I want to use, I use the default one, and there it is, in the correct projection, I can check it with the quick map services plugin, and I can use OpenStreakMap, and we see that it matches the background, so that looks okay. Let's do the same for the vector data, that is done with OGR to OGR, convert simple features data between file formats, and here you see all the arguments, so OGR to OGR, and the target, TSRS is the same, EPSG28992, but here you need to give the output first and then the input, so it will be here, RodesReprojected.shp for shapefile, so it will recognize that and the input RodesShp, and when I do enter, it will do the reprojection, it's a big file, so it takes a while, you see that it gives a warning, that has to do with some characters in the attribute table, like accents that are giving some issues, but it doesn't affect the data, just the text in the attributes. You can solve that if you use languages with a lot of accents by using another encoding instead of UTF-8. Okay, there it is, we can also open it in QGIS, and there we see the Rodes dataset appearing, and indeed it's a very big file, and when I hover my mouse over it, it will say which projection it has, now I would like to convert also raster formats, and for conversions I use one of the raster programs, which is GDAL Translate, to convert raster data between different formats. So I'm going to convert the DEM from the GeoTiff format to the SAGA format, SAGA is a GIS, which is also having a GDAL supported format, and so I'm going to use GDAL Translate, it's with an underscore, and I specify the output format that I want with OF, and that is SAGA, you can find these formats also here on the side under raster drivers, and you need to use these short names, and here you find SAGA, and you can find a description about the format and how to use it. So our input is the DEM in the correct projection, the one we just made, and I'm just going to save it to dmrd.s. That is the extension of the SAGA format, and here it's done, you can again check it in QGIS, should be exactly the same as the other file, up there it is, it goes on top, so it covers our roads now, it looks okay, S.file in the Dutch projection. For some formats you need some more arguments, and the rest of the course we will also use PC Raster, PC Raster needs the specification of the data type, if it's Boolean, nominal, scalar, etc. So let's also convert the same data to PC Raster format to see the difference. So we use GDAL translate, and the output format is PC Raster, so let's see here's PC Raster, and we see here these different formats, and it uses a value scale, and it has a certain data type that we need to define. So our dm is floating point, so we use minus ot float32, with float64 that is for a lot of decimals, but here float32 would be okay, and we need to use the options for this format with MO, and because it's scalar in PC Raster we use VS scalar in quotes, and then the input file, we just use the TIFF, you can also use the S dot, and then we write it to dmrd.map, which is the PC Raster format, and there we go, and also PC Raster is a GDAL format which you can read in QGIS, and we need to choose the Raster one and simply drag it, we need to indicate the transformation, and there it is. So this is PC Raster, PC Raster doesn't have a projection, therefore we see the brackets here, we can set the projection in this way, there is, and we can also convert vector formats, therefore we use OGR to OGR, and that's what we're going to do here, and we're going to create a Google KML file that can be opened in Google Earth. So to specify the format we use minus F, and then we have KML as the format, if you want to see there the formats we go to Vector Drivers, and there you find KML, Keyhole Markup Language. So that's what we use, and then remember we first put the output file, let's call it gen.kml, and then the input file, that's the shape file, I press Enter, give some warnings, but it still works, and now I can open that file in Google Earth, simply double clicking it, and there it is, all the municipalities of the Netherlands in the KML format. Besides conversions we can also do spatial queries of vector data, and we want to create a map of one municipality, the municipality of Delft, so we're going to do the following analysis with GDAL, we're going to select the community of Delft from the community map and save it into a new shape file, and then we're going to intersect the community boundaries of Delft with the road map of the Netherlands, so we only have the roads in Delft. So we can use a spatial query to select the feature from the vector map, and we need to know which attribute is there for the names of the municipalities, so I'm going to use OGR info on the municipality map, and there's this feature GMNAM, which is the name of the municipality, so I can execute also OGR to OGR for doing the spatial queries. The format that I want to use as an output SV shape file, and then I'm going to do the query with WHERE, GMNAM equals, and then I want the municipality with the name Delft. Output will be Delft.shape, and the input is the municipality layer, and then I execute it, and there it is, and then let's open it in QGIS, here Delft, and it's over there. So that works, let's check, it has the correct projection, so that's all correct, we have the boundary, now the next step is that we want to get the roads out of it, and to do that we also use OGR to OGR, and let's check the result, there we have the roads in Delft, the next step is that we want to convert the comma separated files that we have here, so there's a CSV file, and we want to turn that also into a shape file, but also re-projected, so let's see what is in this file, and we see it has latitude, longitude coordinates and different objects, IDs, and it's separated with a comma. So we're going to make a file, a little XML file that is needed to tell GDAL what kind of information is in this file, and I call this locations.vrt, I have to be very precise, these three spaces, LGR, VRT, layer, name equals locations, that's the name of the layer, and then the layer has a source file, locations.csv, that's where the source data is, we specify the geometry type, which is a point file, and we use their WKB point, and it has a projection, we recognize the latitude-longitude coordinates, so we define the layer SRS, and that's EPSG4326, which is for latitude-longitude, and then we need to indicate where the coordinates are stored in the comma-separated values file, and there we use geometry field, encoding equals point from columns, so this indicates that the points are in the column coordinates, where the x-coordinates come from the long column, and the y-coordinate comes from the lot column, then we close this, and we close the layer, and we close the data source, and that's it, with Ctrl Z we save the file, right there, and then we can convert it with OGR to OGR, and we want to convert this to the dot projection, and let's convert the CSV to another CSV where we can read the coordinates, and let's call it locations-reprojected.csv, and as an input we give our VRT file, that's the XML file that we created, a virtual file, and we want that the coordinates are added to the CSV file that we create, so geometry-sxy. There we go, that's pretty fast, and then it should be somewhere here, locations-reprojected.csv, so let's have a look at it, and here we see that it has the x and y values added, it looks like there's a little bug because the coordinates look swapped, and there we have the latitude and longitude coordinates. We can also make a shapefile, so we do OGR, it's basically the same command, but then the output format should be s3-shapefile, and then let's call this locations.shp, and we don't need that, there we go, and then we can open it in QGIS, so I can drag the file to the canvas, the shapefile, and there I see these points of interest, I can also convert it to the Google KML format, as we did before, and then I can open it in Google Earth and visualize it. These are some points of interest in Delft, like also the building of IHG Delft Institute for Water Education and the church, different churches. The last thing that we're going to do is a batch conversion, so here we have a whole set of Idrisi files, these are a roster output from a model in Idrisi format, and I would like to convert all of them to GeoTiff, there will be a lot of work to do that one by one, so we're going to use the g.translate command, but then in batch, so I'm going to make another file, a batch file, I call it rst2tiff.dat, so it's a batch file, and there I'm going to make a loop for each file, that's what this means, in, and then the pattern that we are looking for, rst, so for each file which complies to the extension rst, do, then I want to see the file name, and if I use nf, in this way I can show the part that is before the extension, before the dot, so it will show that on the screen, and I want to do gdall.translate, input format GeoTiff, then as an input, the file name of the input, the Idrisi file, and as an output, I want the part before the dot, and then I add .tiff to it, and that makes the command, and then a bracket to close this loop, and then Ctrl Z to save the file, and then I can run it, rst2tiff, give some errors, but it worked, and we have here all the tiff files, the errors have to do with the projection, so we can visualize this also in QGIS, and here it is.