Geographic Information Systems Asked by Theo F on November 28, 2020
Using GDAL, how can I simply create an empty single band raster? I can’t seem to find out how. I know in PostGIS you can create a new table with a ‘raster’ column type, then insert a band into it. However I’m looking for a slick, GDAL solution (or another efficient non-PostGIS solution).
What I’m looking to create is a raster (GeoTIFF) with these settings:
format: -of GTiff
type: -ot UInt16 (16 bit unsigned integer)
cell resolution: -tr 5 5 (5x5m)
nodata: -a_nodata 255 (the values I intend to burn into this raster range from 1 to 10)
SRID: 27700
extent: -te 0 0 680000 1240000 (rectangular extent of Great Britain)
resulting raster would contain almost 34 billion cells (at 5x5m cell size)!
In Python, this would be:
import gdal
import osr
driver = gdal.GetDriverByName('GTiff')
spatref = osr.SpatialReference()
spatref.ImportFromEPSG(27700)
wkt = spatref.ExportToWkt()
outfn = '/path/to/out.tif'
nbands = 1
nodata = 255
xres = 5
yres = -5
xmin = 0
xmax = 680000
ymin = 0
ymax = 1240000
dtype = gdal.GDT_Int16
xsize = abs(int((xmax - xmin) / xres))
ysize = abs(int((ymax - ymin) / yres))
ds = driver.Create(outfn, xsize, ysize, nbands, dtype, options=['COMPRESS=LZW', 'TILED=YES'])
ds.SetProjection(wkt)
ds.SetGeoTransform([xmin, xres, 0, ymax, 0, yres])
ds.GetRasterBand(1).Fill(0)
ds.GetRasterBand(1).SetNoDataValue(nodata)
ds.FlushCache()
ds = None
Correct answer by Johan on November 28, 2020
Since version 3.2, gdal offers gdal_create
command for this purpose. Just simply use the following:
gdal_create -of GTiff -ot UInt16 -a_nodata 255 -burn 0 -outsize 680000 1240000 -a_srs "EPSG:27700" -co COMPRESS=LZW large_file.tif
I assumed by the extent, you meant the width and height of the image. For the cell resolution of 5m or whatever else, just set up your georeferenced bounds of the output file based on the image size and the pixel resolution. Add this option to the command line: -a_ullr <ulx> <uly> <lrx> <lry>
I also added the compression option of -co COMPRESS=LZW
to be able to save disk space for this huge empty file! However, it did not work! It seems to be a bug for such a big image size. The compression works for smaller image size.
Also note the -burn
value has been set to 0
. You can change it to whatever value you like. It takes a while for the image to be created; my write speed was 0.4 MB/s so it took around 2 hours and 6 minutes and the file size was ~2.5 GB. Be patient!
Answered by shaheen g on November 28, 2020
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP