shapefile to raster conversion using Python and GDAL library

Asked

Viewed 149 times

0

I’m doing a file conversion shapefile for crawl, using a Python script and the GDAL library.

For this I use two functions:

  1. The function gdal.rasterizeOptions (put the output options of my die in raster).

  2. Function gdal.Rasterize (function that will Rasterize my shapefile effectively).

However, my code does not run. Does anyone know if I am passing the wrong function parameters?

from osgeo 
import gdal
from osgeo import ogr


dado_saida = 'C:\Lilian\_fabio\criando-aplicacao-conversao\python\pre\saida.tif'

dado_entrada = 'C:\Lilian\_fabio\criando-aplicacao-conversao\python\pre\grid_pontos_1.shp'

data = ogr.Open(dado_entrada)

rasterizeOptions=gdal.RasterizeOptions(options=[], format='Gtiff',creationOptions = None, noData=None,initValues = None, outputBounds = None, outputSRS = None,width = None, height = None, xRes= 1, yRes= 1, targetAlignedPixels = False,bands = None, inverse = False, allTouched=True,burnValues = None, attribute='Z', useZ = False, layers = None,SQLStatement='select Z, * from saida',SQLDialect = None, where = None, callback=None, callback_data=None)

gdal.Rasterize(dado_entrada, dado_saida, options=rasterizeOptions)
  • important detail: avoid using \ to separate directories, even in windows - why several combinations of \ one more character can generate a different character. For example \n internally becomes a single character with code 16 decimal.

  • (in Python you can normally use the / to separate directories, even if in windows)

1 answer

0


Lilian,

I modified your code together with a friend of mine and we managed to make it Compile and generate a .tif. Check if this is what you want and if the output is consistent with the expected.

import ogr, gdal

dado_entrada = '/home/caiquefortunato/GDAL/cavas-juntas/saida.tif'

dado_saida = '/home/caiquefortunato/GDAL/cavas-juntas/grid_pontos_1.shp'

rasterizeOptions = gdal.RasterizeOptions(options=[], format='Gtiff',creationOptions = None, noData=None,initValues = None, outputBounds = None, outputSRS = None,width = None, height = None, xRes= 1, yRes= 1, targetAlignedPixels = False,bands = None, inverse = False, allTouched=True,burnValues = None, attribute='field_4', useZ = False, layers = None,SQLStatement='select field_4, * from grid_pontos_1',SQLDialect = None, where = None, callback=None, callback_data=None)

gdal.Rasterize(dado_entrada, dado_saida, options=rasterizeOptions)

The error was that in the Rasterize function first comes the output and then the input. To save time I only renamed the variables.

I hope I’ve helped. Have a merry Christmas with lots of lights.

  • You are the best people that Santa Claus will present with many many many many gifts these year hehe <3 <3

Browser other questions tagged

You are not signed in. Login or sign up in order to post.