2

When I run the following code in the python console in QGIS I get a syntax error:

import osgeo.gdal
gdal_rasterize [-b 1] [-burn 255] [-a Pixelkost] [-te 170000.2 168000.27 177999.8 177999.8] <Adp322.shp> <work.tif>

b command for burning it in band number 1 burn command for the color red a command for using the attribute pixelkost te command for x,y min and x,y max the already loaded Adp322.shp file (I also tried it with the whole system path towards the file but this didn't work either) work.tif is the name the new file should have. (Should this maybe be an already existing file? but how should I produce this?)

AndreJ
  • 76,698
  • 5
  • 86
  • 162
Peter Vanvoorden
  • 133
  • 1
  • 3
  • 11

1 Answers1

3

gdal_rasterize is not a python module, but an exe file. You can not call it that easy inside the python console.

See this blogpost on how to wrap the operating system around it:

http://geoinformaticstutorial.blogspot.de/2012/11/convert-shapefile-to-raster-with-gdal.html

import os
os.system('gdal_rasterize -a ICE_TYPE -where \"ICE_TYPE=\'Open Water\'\" -burn 2 -l ' + shapefileshortname +' -tr 1000 -1000 ' +  shapefile + ' ' + outraster
AndreJ
  • 76,698
  • 5
  • 86
  • 162
  • But I am trying to do this straight in the command line from the console in QGIS...

    Your document is telling me to do this: Using gdal_rasterize is described here and from command line I would call for one of the polygons: gdal_rasterize -a ICE_TYPE -where "ICE_TYPE='Open Water'" -burn 2 -l ice20120608_3575 -tr 1000 -1000 C:...\ice20120608_3575.shp C:...\ice20120608_3575.tif

    What I am already doing.

    – Peter Vanvoorden May 10 '14 at 13:55
  • Command line is the DOS box called OSGEO4W Shell on Windows, or Terminal on Linux. That is not the python console. – AndreJ May 10 '14 at 14:08
  • or Mac OS X ...(Terminal) – gene May 10 '14 at 14:16
  • Still not working. When I run the following code in python console after 'import os', I just get the value '32512' as a result...

    os.system('gdal_rasterize -b 1 -a Pixelkost -burn 255 -te 170000.2 168000.27 177999.8 177999.8 /Users/PeterVanvoorden/Desktop/GRBgis_322/Shapefile/Adp322.shp /Users/PeterVanvoorden/Desktop/Test.tif')

    – Peter Vanvoorden May 10 '14 at 15:52
  • when I type the command as in the link, in the terminal: mbp-van-peter:~ PeterVanvoorden$ gdal_rasterize -b 1 -a Pixelkost -burn 255 -te 170000.2 168000.27 177999.8 177999.8 /Users/PeterVanvoorden/Desktop/GRBgis_322/Shapefile/Adp322.shp /Users/PeterVanvoorden/Desktop/test.tif ....... I get: -bash: gdal_rasterize: command not found ........ maybe there is something wrong with my gdal installation? – Peter Vanvoorden May 10 '14 at 16:40
  • If gdalinfo --version does not work, yes. What repository did you use, on which OS? I use ubuntugis unstable (not yet for Ubuntu 14.04) – AndreJ May 10 '14 at 17:18
  • I am working with Mac OSX Version 10.9.2 I have no idea which repository, where can I find it? – Peter Vanvoorden May 10 '14 at 18:01
  • http://www.kyngchaos.com/software/frameworks#gdal_complete. But I can not help you further with Mac issues. – AndreJ May 10 '14 at 18:40