Skip to content

Instantly share code, notes, and snippets.

@robinkraft
Last active December 16, 2015 00:39
Show Gist options
  • Save robinkraft/5349286 to your computer and use it in GitHub Desktop.
Save robinkraft/5349286 to your computer and use it in GitHub Desktop.
Create a random image the size of a MODIS tile using Numpy and GDAL. N.B. this script creates tile 28 8.
from osgeo import gdal, gdal_array, osr
import numpy
from osgeo.gdalconst import GDT_Byte
xsize, ysize = 4800, 4800
a = numpy.random.randint(0, 100, (xsize, ysize)).astype(numpy.int8)
xmin, xmax = 100, 110.
ymin, ymax = 0, 10.
driver = gdal.GetDriverByName('GTiff')
out = driver.Create('/tmp/dummy.tif', a.shape[1], a.shape[0], 1, GDT_Byte)
srs=osr.SpatialReference()
srs.ImportFromEPSG(4326)
out.SetGeoTransform([xmin
, (xmax - xmin)/a.shape[0]
, 0
, ymin
, 0
, (ymax - ymin)/a.shape[1]])
out.SetProjection(srs.ExportToWkt())
gdal_array.BandWriteArray(out.GetRasterBand(1), a)
out = None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment