3

Is it possible to specify the extent when creating an empty raster in GDAL?

I looked everywhere, but can't seem to find anything. The portion of my script where I write the raster would look something like this:

tifpath = "path/to/output/tiff.tif"
drvtif = gdal.GetDriverByName("GTiff")
rast = drvtif.Create(tifpath, Ncolumns, Nrows, Nbands, datatype)
rast.SetProjection(projection) # projection being a variable representing the desired projection
rast.GetRasterBand(1).WriteArray(array)

My array is an array of size Ncolumns x Nrows, filled with 0's (I want an empty raster). The number of pixels is predetermined, as is the cell size, and by multiplying npixels*cellsize, I've determined what my output extents need to be such that each pixel is a whole pixel (i.e. doesn't get cut off by the extent).

But, I can't find any options that would allow me to specify the desired extent.

It's also just occurred to me that I don't see any option to specify cell size.

  • So you want to specify the extent in projection coordinates? – BradHards Jan 22 '15 at 21:08
  • Yes. Projection is WGS84 (degrees) and I need the UL of my raster to begin at exactly (-180, 90). Using the predetermined cell size (0.0089952083) and number of pixels (2501 columns x 20011 rows), I know exactly where the LR coordinate should end up (-157.5029840417, -90.0031132913), and I want to specify to be sure that it does. Is this possible to do when setting the projection? Now that I think about it, where do I even specify cell size? Will update my question with that last bit. Thx – user20408 Jan 22 '15 at 21:31
  • PS: I need to create 15 more rasters with the same top/bottom extents, such that they are exactly side by side (no overlap or gap between them, and lined up exactly at top & bottom). So, raster1's right extent would become raster2's left extent, &so on. I've determined that ncol=2501 for the first 10 rasters, then ncol=2502 for the last 6, will get me as close to 180 degrees (these 16 rasters, when together, need to fill a global extent) as possible, with just a little bit hanging over (the final rightmost extent should be 180.0062265826). Not sure if this is relevant, but thought I'd share. – user20408 Jan 22 '15 at 21:42
  • I think you should rework the question to put those key information elements into the question. – BradHards Jan 22 '15 at 21:46
3

Yes, using the SetGeoTransform method. The Geographic Transform defines the origin of the raster in the upper left hand corner, as well as the cell size and the rotation in the x and y direction for the cells in this format:

geo_transform = (x top left, x cell size, x rotation, y top left, y rotation, negative y cell size)

Or in the example you've supplied:

geo_transform = (-180, 0.0089952083, 0., 90, 0., -0.0089952083)

This can set be applied to the dataset object;

rast.SetGeoTransform(geo_transform)

Note The top left will be the coordinates of the top left of the cell, not the cell centre.

  • wonderful, thanks! this is exactly what I was looking for. – user20408 Jan 22 '15 at 23:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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