I used the following code for converting my layer into raster format but I get a tif file that is completely black or white...
from osgeo import gdal, ogr
# Define pixel_size and NoData value of new raster
pixel_size = 1
NoData_value = 100
# Filename of input OGR file
# vector_fn = '/Users/PeterVanvoorden/Desktop/GRBgis_322/Shapefile/Adp322.shp'
vector_fn = '/Users/PeterVanvoorden/Desktop/GRBgis_322/New_Shapefiles/Sbne40179.shp'
# Filename of the raster Tiff that will be created
raster_fn = '/Users/PeterVanvoorden/Desktop/test.tif'
# Open the data source and read in the extent
source_ds = ogr.Open(vector_fn)
source_layer = source_ds.GetLayer()
x_min, x_max, y_min, y_max = source_layer.GetExtent()
print x_min, x_max, y_min, y_max
# Create the destination data source
x_res = int((x_max - x_min) / pixel_size)
y_res = int((y_max - y_min) / pixel_size)
target_ds = gdal.GetDriverByName('GTiff').Create(raster_fn, x_res, y_res, 1, gdal.GDT_Byte)
target_ds.SetGeoTransform((x_min, pixel_size, 0, y_max, 0, -pixel_size))
band = target_ds.GetRasterBand(1)
band.SetNoDataValue(NoData_value)
# Rasterize
gdal.RasterizeLayer(target_ds, [1], source_layer, burn_values=[0])
STARTING QUESTION:
I am trying to rasterize vector data using the GDAL_rasterize function. (Started here: syntax error when using gdal in python console QGIS) But with all methods they told me were possible, I always get something I don't need.
Below the 3 methods I tried...
Directly in python IDLE:
>>> import os
>>> os.system('gdal_rasterize -tr 7999.8 9999.53 -te 170000.2 168000.27 177999.8 177999.8 -a Pixelkost /Users/PeterVanvoorden/Desktop/GRBgis_322/Shapefile/Adp322.shp /Users/PeterVanvoorden/Desktop/Test.tif')
Then I get the value: 32512 as result but it should output a tif file on my desktop...
When I do the same in a python file and run it through IDLE:
import os
shapefile = '/Users/PeterVanvoorden/Desktop/GRBgis_322/Shapefile/Adp322.shp'
(shapefilefilepath, shapefilename) = os.path.split(shapefile)
(shapefileshortname, extension) = os.path.splitext(shapefilename)
outrastername = shapefileshortname + '.tif'
outraster = '/Users/PeterVanvoorden/Desktop/' + outrastername
os.system('gdal_rasterize -tr 7999.8 9999.53 -te 170000.2 168000.27 177999.8 177999.8 -a Pixelkost' + shapefile + ' ' + outraster)
In this case nothing happens at all.
Running it in terminal (OSX):
mbp-van-peter:~ PeterVanvoorden$ gdal_rasterize -tr 7999.8 9999.53 -te 170000.2 168000.27 177999.8 177999.8 -a Pixelkost /Users/PeterVanvoorden/Desktop/GRBgis_322/Shapefile/Adp322.shp /Users/PeterVanvoorden/Desktop/Test.tif
Than I get: -bash: gdal_rasterize: command not found
I assume, looking at the last method, that there is a problem with my gdal package. I reinstalled it after having a few problems and installed GDAL 1.10 using my terminal just with a easy install like this: sudo easy_install /Users/PeterVanvoorden/Downloads/GDAL-1.10.0