Changes between Version 15 and Version 16 of FAQRaster


Ignore:
Timestamp:
Oct 5, 2009, 3:34:55 PM (15 years ago)
Author:
leaffan
Comment:

included a second alternative for blank raster creation

Legend:

Unmodified
Added
Removed
Modified
  • FAQRaster

    v15 v16  
    2828One approach is to use gdal_translate to clip out and flatten an existing raster file that covers your area of interest. See this [http://lists.osgeo.org/pipermail/gdal-dev/2008-February/016061.html example of syntax].
    2929
    30 Another approach is to use one of the language bindings to GDAL to create a blank raster from scratch. The tricky part is understanding the !GeoTransform syntax.
    31 This python snippet shows how to read in a shapefile and output a tiff that matches the shapefile extents
     30Another approach is to use one of the language bindings to GDAL to create a blank raster from scratch. The tricky part is understanding the !GeoTransform syntax. This python snippet shows how to read in a shapefile and output a geo-tiff that matches the shapefile extents. It deals with two conceivable alternatives: a) the calculation of the output pixel resolution of the geo-tiff by using the desired dataset dimensions and b) the calculation of the output dimensions based on desired pixel resolutions:
    3231
    3332{{{
     
    4645shp = 'test.shp'
    4746tiff = 'test.tif'
     47# Alternative A: Set raster dataset dimensions
    4848tiff_width = 7850
    4949tiff_height = 3500
     50# Alternative B: Set pixel resolution
     51x_res = 0.5
     52y_res = 0.75
    5053
    5154# Import vector shapefile
     
    5558src_extent = src_lyr.GetExtent()
    5659
     60# Alternative A: Calculate pixel resolution from desired dataset dimensions
     61x_res = (src_extent[1] - src_extent[0]) / tiff_width
     62y_res = (src_extent[3] - src_extent[2]) / tiff_height
     63# Alternative B: Calculate raster dataset dimensions from desired pixel resolution
     64tiff_width = int(math.ceil(abs(src_extent[1] - src_extent[0]) / x_res))
     65tiff_height = int(math.ceil(abs(src_extent[3] - src_extent[2]) / y_res))
     66
     67# Create raster GeoTransform based on upper left corner and pixel dimensions
     68raster_transform = [src_extent[0], x_res, 0.0, src_extent[3], 0.0, -y_res]
     69dst_ds.SetGeoTransform(raster_transform)
     70
    5771# Create new raster layer with 4 bands
    5872raster = gdal.GetDriverByName('GTiff')
    59 dst_ds = raster.Create( tiff, tiff_width, tiff_height, 4, gdal.GDT_Byte)
    60 
    61 # Create raster GeoTransform based on upper left corner and pixel resolution
    62 xres = (src_extent[1] - src_extent[0]) / tiff_width
    63 yres = (src_extent[3] - src_extent[2]) / tiff_height
    64 raster_transform = [src_extent[0], xres, 0.0, src_extent[3], 0.0, -yres]
    65 dst_ds.SetGeoTransform( raster_transform )
     73dst_ds = raster.Create(tiff, tiff_width, tiff_height, 4, gdal.GDT_Byte)
    6674
    6775# Get projection of shapefile and assigned to raster
    6876srs = osr.SpatialReference()
    6977srs.ImportFromWkt(src_lyr.GetSpatialRef().__str__())
    70 dst_ds.SetProjection( srs.ExportToWkt() )
     78dst_ds.SetProjection(srs.ExportToWkt())
    7179
    7280# Create blank raster with fully opaque alpha band