Rohit Handique.
Rohit Handique.

Reputation: 53

Converting Vector (.shp) to Raster (.tiff) using Python GDAL library

I'm trying to convert a vector shapefile to a raster tiff file. I adapted the source code from Python Geospatial Analysis Cookbook by Michael Diener. The code works for line and polygon shapefiles but shows only a black screen for point shapefiles.

def main(shapefile):

#making the shapefile as an object.
input_shp = ogr.Open(shapefile)

#getting layer information of shapefile.
shp_layer = input_shp.GetLayer()

#pixel_size determines the size of the new raster.
#pixel_size is proportional to size of shapefile.
pixel_size = 0.1

#get extent values to set size of output raster.
x_min, x_max, y_min, y_max = shp_layer.GetExtent()

#calculate size/resolution of the raster.
x_res = int((x_max - x_min) / pixel_size)
y_res = int((y_max - y_min) / pixel_size)

#get GeoTiff driver by 
image_type = 'GTiff'
driver = gdal.GetDriverByName(image_type)

#passing the filename, x and y direction resolution, no. of bands, new raster.
new_raster = driver.Create(output_raster, x_res, y_res, 1, gdal.GDT_Byte)

#transforms between pixel raster space to projection coordinate space.
new_raster.SetGeoTransform((x_min, pixel_size, 0, y_min, 0, pixel_size))

#get required raster band.
band = new_raster.GetRasterBand(1)

#assign no data value to empty cells.
no_data_value = -9999
band.SetNoDataValue(no_data_value)
band.FlushCache()

#main conversion method
gdal.RasterizeLayer(new_raster, [1], shp_layer, burn_values=[255])

#adding a spatial reference
new_rasterSRS = osr.SpatialReference()
new_rasterSRS.ImportFromEPSG(2975)
new_raster.SetProjection(new_rasterSRS.ExportToWkt())
return gdal.Open(output_raster)

So, my problem is:

What do I change in the code so that it works for point shapefiles too? and how do I get my final output raster file to be of the same color as the input vector file?

The things that do work in the code is: 1. getting input and returning the output properly. 2. getting the proper size/resolution and creating a raster of the required resolution. 3. proper shapes in the output for polygon and line shapefiles.

Upvotes: 4

Views: 12234

Answers (1)

Rutger Kassies
Rutger Kassies

Reputation: 64443

If you want to keep the code you have, you can add the ALL_TOUCHED option and set it to TRUE.

Change the rasterization line to:

gdal.RasterizeLayer(new_raster, [1], shp_layer, 
                    burn_values=[255], 
                    options=['ALL_TOUCHED=FALSE'])

However, you might also consider using the bindings to the command line utilities, since that would save you a lot of code. For example:

input_shp = ogr.Open(shape_file)
shp_layer = input_shp.GetLayer()

pixel_size = 0.1
xmin, xmax, ymin, ymax = shp_layer.GetExtent()

ds = gdal.Rasterize(output_raster, shape_file, xRes=pixel_size, yRes=pixel_size, 
                    burnValues=255, outputBounds=[xmin, ymin, xmax, ymax], 
                    outputType=gdal.GDT_Byte)
ds = None

Upvotes: 4

Related Questions