2

I tried to make raster file with GTiff format by using python GDAL, this is my sample gdal logic:

driver = gdal.GetDriverByName('GTiff')
outRaster = driver.Create(raster_classified, col, row, 1, gdal.GDT_Int16)
outRaster.SetGeoTransform((origin_x, cell_size, 0, origin_y - (cell_size * row), 0, cell_size))
outband = outRaster.GetRasterBand(1)
outband.WriteArray(outras_array[::-1])
outRasterSRS = osr.SpatialReference()
outRasterSRS.ImportFromEPSG(4326)
outRaster.SetProjection(outRasterSRS.ExportToWkt())
outband.FlushCache()

But after the raster file was created.. it's return with VRT format.. why? This is the raster properties :

Driver
GDAL provider
VRT
Virtual Raster
Dataset Description
Band 1
STATISTICS_MAXIMUM=9
STATISTICS_MEAN=0.99751150063449
STATISTICS_MINIMUM=0
STATISTICS_STDDEV=2.3322020706147
Dimensions
X: 6500 Y: 4500 Bands: 1
Origin
110.5,-4.5
Pixel Size
0.001,-0.001
No Data Value
*NoDataValue not set* 
Data Type
Int16 - Sixteen bit signed integer 
Pyramid overviews
Layer Spatial Reference System
+proj=longlat +datum=WGS84 +no_defs
Layer Extent (layer original source projection)
110.5000000000000000,-9.0000000000000000 : 117.0000000000000000,-4.5000000000000000
Band
Band 1
Band No
1
No Stats
No stats collected yet 

how to make geotiff format with python GDAL?

3
  • Could you try this example pcjericks.github.io/py-gdalogr-cookbook/…? It does create a tiff for me with OSGeo4W, though the result appears totally black and not with the text GDAL as in the example.
    – user30184
    Commented Jun 13, 2017 at 9:29
  • The script you provided should make a GeoTIFF. Are you certain raster_classified ends in '.tif'? Does raster_classified already exist? If so, delete it and try it again. Commented Jun 13, 2017 at 12:32
  • I found out that this extrange issue was due to the absence of the minus sign in the last parameter of 'SetGeoTransform' method (-cell_size). Please, see my answer.
    – xunilk
    Commented Jun 13, 2017 at 15:17

1 Answer 1

1

I fixed some minor issues and assumed some value parameters to test your code. I found out that this extrange issue was due to the absence of the minus sign in the last parameter of 'SetGeoTransform' method (-cell_size).

from osgeo import gdal, osr
import numpy as np

raster_classified = 'c:/Users/Usuario/pyqgis_data/raster_classified.tif'

col = 20 
row = 20

cell_size = 0.003
origin_x = 10000
origin_y = 10000

driver = gdal.GetDriverByName('GTiff')

outRaster = driver.Create(raster_classified, col, row, 1, gdal.GDT_Int16)

outband = outRaster.GetRasterBand(1)

outRaster.SetGeoTransform((origin_x, 
                           cell_size, 
                           0, 
                           origin_y  - (cell_size * row), 
                           0, 
                           -cell_size))

values = [ [] for i in range(row) ]

for i in range(row):
    for j in range(col):
            values[i].append(1)

outras_array = np.array(values)

outband.WriteArray(outras_array)
outRasterSRS = osr.SpatialReference()
outRasterSRS.ImportFromEPSG(4326)
outRaster.SetProjection(outRasterSRS.ExportToWkt())
outRaster = None

After running it at the Python Console of QGIS, I got a 20x20 *.tif raster (all pixels equal 1); as it can be seen at next image:

enter image description here

It worked.

1
  • it's work on me too, but the problem is that the format of raster output is still "VRT" eventhough the extention is "tif", try to right click on raster layer then properties then meta data then see the Properties info.. Commented Jun 14, 2017 at 5:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.