Convert shapefile to raster using Python and GDAL library

0

I am converting a shapefile file to raster , using a Python script and the GDAL library.

For this I use two functions:

  • However, my code does not spin. Does anyone know if I'm passing the parameters of the wrong function?

    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)
    
        
    asked by anonymous 22.12.2017 / 14:45

    1 answer

    0

    Lilian,

    I modified your code with a friend of mine and we were able to compile it and generate a .tif. Check if it 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 first Rasterize function comes the output and then the input. To save time, I just renamed the variables.

    I hope I have helped. Have a merry christmas with lots of lights.

        
    22.12.2017 / 15:11