create your dummy geotiff

Useful for unit testing

Write a Numpy array to GeoTIFFf file.

from osgeo import gdal, osr
from osgeo import gdalnumeric
import numpy as np

# get the osr spatial reference
srs = osr.SpatialReference()
# we want meters
srs.ImportFromEPSG(3857)

# set pixel dimensions
nx = 32
ny = 32

# we create a geotiff with 1 band
dst_ds = gdal.GetDriverByName('GTiff').Create('/path/to/dummy.tif', nx, ny, 1, gdal.GDT_Float32)

# create a random grid of 32 by 32 pixels
arr = np.random.rand(32, 32)

# write the array to the geotiff
dst_ds.GetRasterBand(1).WriteArray(arr)
# optionally set some nodata
dst_ds.GetRasterBand(1).SetNoDataValue(-9999)
# set the projection
dst_ds.SetProjection(srs.ExportToWkt())
#/* top left x */
#/* w-e pixel resolution */
#/* 0 */
# /* top left y */
# /* 0 */
# /* n-s pixel resolution (negative value) */
dst_ds.SetGeoTransform([32, 1, 0, 32, 0, -1])
# FlushChache to write
dst_ds.FlushCache()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s