Changes between Version 10 and Version 11 of FAQRaster


Ignore:
Timestamp:
Jul 31, 2008, 12:07:53 PM (16 years ago)
Author:
springmeyer
Comment:

Add python example for creating a blank GTiff with numpy from shapefile extents

Legend:

Unmodified
Added
Removed
Modified
  • FAQRaster

    v10 v11  
    2020
    2121''TBD''
     22
     23== How can I create a blank raster based on a vector files extents for use with gdal_rasterize? ==
     24
     25Currently the gdal_rasterize utility cannot create a blank raster based a vector datasource although this feature has been requested (ticket 1599).
     26Until that feature is add you'll need to find a way to generate a raster that matches the extent of your vector layer that you plan to use to burn features into the raster.
     27
     28One way to do this 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].
     29
     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.
     31This python snippet shows how to read in a shapefile and output a tiff that matches the shapefile extents
     32 * Note: you'll need to modify the `px` variable based on your desired resolution and raster dimensions
     33
     34{{{
     35#!/usr/bin/env python
     36
     37from osgeo import gdal
     38from osgeo import osr
     39from osgeo import ogr
     40import numpy
     41
     42shp = 'test.shp'
     43tiff = 'test.tif'
     44px = .001
     45tiff_width = 7850
     46tiff_height = 3500
     47
     48# Import vector shapefile
     49vector = ogr.GetDriverByName('ESRI Shapefile')
     50src_ds = vector.Open(shp)
     51src_lyr = src_ds.GetLayerByIndex(index=0)
     52src_extent = src_lyr.GetExtent()
     53
     54# Create new raster layer with 4 bands
     55raster = gdal.GetDriverByName('GTiff')
     56dst_ds = raster.Create( tiff, tiff_width, tiff_height, 4, gdal.GDT_Byte)
     57
     58# Create raster GeoTransform based on upper left corner and pixel resolution
     59raster_transform = [src_extent[0], px, 0.0, src_extent[3], 0.0, -px]
     60dst_ds.SetGeoTransform( raster_transform )
     61
     62# Get projection of shapefile and assigned to raster
     63srs = osr.SpatialReference()
     64srs.ImportFromWkt(src_lyr.GetSpatialRef().__str__())
     65dst_ds.SetProjection( srs.ExportToWkt() )
     66
     67# Create blank raster with fully opaque alpha band
     68zeros = numpy.zeros( (tiff_height, tiff_width), numpy.uint8 )
     69dst_ds.GetRasterBand(1).WriteArray( zeros )
     70dst_ds.GetRasterBand(2).WriteArray( zeros )
     71dst_ds.GetRasterBand(3).WriteArray( zeros )
     72opaque = numpy.ones((tiff_height,tiff_width), numpy.uint8 )*255
     73dst_ds.GetRasterBand(4).WriteArray( opaque )
     74
     75}}}
     76
    2277
    2378== Can I use gdal_rasterize to generate non-solid polygons? ==