3

I have a vector shapefile of a geographic area that contains several different object codes that I need to rasterize all as one image.

enter image description here

I am working with QGIS, but the Raster>conversion tool seems to be unable to do this (see below). In the tool, it seems as though you cannot select more than one value, not to mention the tool - even when it works - appears to ignore whatever value you have set and rasterizes all values at once, in the same image. That would be ok, but I have a large volume of output to complete, and I don't want to separate every needed raster mask into a separate layer, as this would be extremely inefficient and time consuming

enter image description here

So, I tried running the GDAL command it generates, and even the GDAL command, when entered in the python console, errors out. I have never worked with GDAL before and I am way out of my depth, so I am looking for pointers.

Below is the command I started with (and that QGIS ran successfully when I attempted to rasterize using the form), and no variation of this seems to work in the console, and any edit I perform also returns a syntax error.

gdal_rasterize -l bornholm_final_OSM -a code -ts 100492.0 100492.0 -te 14.510849428030344 54.922004004836744 15.299793055831714 55.37191520877061 -ot Float32 -of GTiff "E:/My Documents/OSM Stuffbornholm_final_OSM.shp" "E:/My Documents/OSM Stuff/point-5m test_qgis2.tif"

I have looked at the GDAL documentation about "gdal_rasterize", but I am truly stumped at this point. I just need the raster to do multiple classes in one image.

Using @BenW'S answer I am now able to create a raster using GDAL, but I now need to ensure that it retains the colors.

enter image description here

2 Answers 2

3

I notice you have entered a field to use for the burn in value AND a fixed value to burn. I think you should only enter one or the other. If you enter a fixed value, all your vector features will be burnt to the resulting raster with that one value. If your vector layer has a numeric attribute field with certain values for each feature then you select that field to used for burn in value. Your vector features will then be rasterized with the values from the specified attribute field being burnt to the resulting raster.

If you want to run processing algorithms from the Python console you have to do something like:

import processing
processing.run(name_of_algorithm, parameters)

EDIT:

GDAL is a library of command line utilities, so the command as you have typed it above should be run from the OSGEO4W shell (I am assuming you are on Windows here) not from inside QGIS. The quickest way to launch it is to type "osgeo4w shell" into the search function. At the command prompt you first need to change directory to the location of your shapefile. To do this type cd followed by a space then the full path to your file. Hit enter then type your command e.g. gdal_rasterize etc and hit enter again to run the command

QGIS also provides access to GDAL utilities through the processing toolbox with a gui for entering parameters as shown in in your original post. This should also work fine for your purpose. It is not entirely clear to me what result you are looking for. It seems from your comment that you want to rasterize all features in your vector that have the value 7502 in an attribute field "code" and burn only those features with that value to the resulting raster. You should select those features in QGIS first which you can do with the option: select features by expression and use your expression "code" = 7502. Make sure the Selected features only box is checked and enter EITHER the "code" field to use for the burn in value OR 7502 as the fixed value (not both).

EDIT 2:

From your original question I assumed that your vector layer has an attribute field "code" with a number of unique numerical values; I guess that field is what has been used to style the vector, and that you want to produce a raster where all the vector features are burnt with the corresponding raster pixels having the values from the "code" field. If that's the case then I'm not sure why you included the 'where' expression. You should include all features and just specify the attribute field "code" to use for the burn in value. You can then use those values to apply a similar colour style to your raster.

10
  • Hi Ben, thanks for your response. First of all, I am trying to run this in QGIS and seems to not accept any command I give it. I feel like I am following the syntax exactly off the documentation, but it just won't do it. I specified the algorithm, specified the attribute name, added the where clause, given it extents, specified the layer, and the output. What else do I need to do? gdal_rasterize -a code -where 'code="7502"' -ts 4096.0 4096.0 -te 14.510849428030344 54.922004004836744 15.299793055831714 55.37191520877061 -l bornholm_final_OSM.shp testqgis2.tif
    – wyatt8919
    Commented Sep 26, 2018 at 3:07
  • Hi Wyatt, GDAL is a library of command line utilities, so the command as you have typed it above should be run from the OSGEO4W shell (I am assuming you are on Windows here) not from inside QGIS. The quickest way to launch it is to type "osgeo4w" into the search function. At the command prompt you first need to change directory to the location of your shapefile. To do this type cd followed by a space then the full path to your file. Hit enter then type your command e.g. gdal_rasterize etc and hit enter again to run the command.
    – Ben W
    Commented Sep 26, 2018 at 4:07
  • The QGIS processing toolbox implementation of the GDAL utility should also work fine for you if you adjust your parameter settings in the GUI. I have edited my answer with a bit more info.
    – Ben W
    Commented Sep 26, 2018 at 4:29
  • Ben - thanks for your clarification. You were indeed right, running in command line works (why, then, does QGIS have a console??). Anyway...I now need to generate a raster that retains color. I have read that this is possible, but I am still struggling with the syntax. I found this link that explains it is possible, but I am struggling with the dev's explanation. How do I burn each into a separate band? EDIT: I updated the post to show where I am now.
    – wyatt8919
    Commented Sep 26, 2018 at 4:32
  • 1
    Well the colours of your vector layer are just a style setting, but If you burnt your raster using values from an attribute field in your vector then you should have those corresponding values burnt into your raster. I assume it's the same field you used to style your original vector layer. When styling your raster, add as many classes as you have values, then double click on the value of each class and enter the value that corresponds to your vector layer. Do the same for the colour (you can get the rgb values from the colour selector dialog in the vector layer symbology).
    – Ben W
    Commented Sep 26, 2018 at 6:43
1

Here is a Python GDAL implementation for burning one value per feature (specified as a feature field in an ESRI shapefile).

The possible scenarios in terms of vector data formats and use-cases is very large. This code shows a working example for burning a raster with a different value per feature determined as a feature field. Hopefully it is useful for the community and can be easily adapted to other cases:

from osgeo import gdal
from osgeo import ogr
from osgeo import gdalconst

#This raster is the model for our output (CRS, extent)
ndsm = 'geotif_output_extent_proj.tif'
#This shapefile contains the features we want to burn
shp = 'shapefile_features_fields.shp'

data = gdal.Open(ndsm, gdalconst.GA_ReadOnly)

geo_transform = data.GetGeoTransform()
x_min = geo_transform[0]
y_max = geo_transform[3]
x_max = x_min + geo_transform[1] * data.RasterXSize
y_min = y_max + geo_transform[5] * data.RasterYSize
x_res = data.RasterXSize
y_res = data.RasterYSize


vec = ogr.Open(shp)
lyr = vec.GetLayer(0)
pixel_width = geo_transform[1]

output = 'output.tif'
target_ds = gdal.GetDriverByName('GTiff').Create(output, x_res, y_res, 1, gdal.GDT_UInt32)
target_ds.SetGeoTransform((x_min, pixel_width, 0, y_min, 0, pixel_width))
band = target_ds.GetRasterBand(1)
NoData_value = 0
band.SetNoDataValue(NoData_value)
band.FlushCache()


driver = ogr.GetDriverByName('ESRI Shapefile')
for feat in lyr:
    burn_value = int(feat.GetField("FIELD_NAME"))
    datasource = driver.CreateDataSource('temp.shp')
    layer = datasource.CreateLayer('temp', lyr.GetSpatialRef(), geom_type=ogr.wkbPolygon)
    layer.CreateFeature(feat)
    gdal.RasterizeLayer(target_ds, [1], layer, burn_values=[burn_value], options=["ALL_TOUCHED=TRUE"])
    datasource.Destroy()

#This makes raster to write to disk
target_ds = None

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.