Changes between Version 14 and Version 15 of FAQRaster
- Timestamp:
- Oct 5, 2009, 11:16:01 AM (15 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
FAQRaster
v14 v15 30 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 31 This 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 dimensions33 32 34 33 {{{ 35 34 #!/usr/bin/env python 36 35 37 from osgeo import gdal 38 from osgeo import osr 39 from osgeo import ogr 40 import numpy 41 36 try: 37 from osgeo import gdal 38 from osgeo import osr 39 from osgeo import ogr 40 except: 41 import gdal 42 import osr 43 import ogr 44 45 # To be adjusted to your needs 42 46 shp = 'test.shp' 43 47 tiff = 'test.tif' 44 px = .00145 48 tiff_width = 7850 46 49 tiff_height = 3500 … … 49 52 vector = ogr.GetDriverByName('ESRI Shapefile') 50 53 src_ds = vector.Open(shp) 51 src_lyr = src_ds.GetLayer ByIndex(index=0)54 src_lyr = src_ds.GetLayer(0) 52 55 src_extent = src_lyr.GetExtent() 53 56 … … 57 60 58 61 # Create raster GeoTransform based on upper left corner and pixel resolution 59 raster_transform = [src_extent[0], px, 0.0, src_extent[3], 0.0, -px] 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] 60 65 dst_ds.SetGeoTransform( raster_transform ) 61 66 … … 66 71 67 72 # Create blank raster with fully opaque alpha band 68 zeros = numpy.zeros( (tiff_height, tiff_width), numpy.uint8 ) 69 dst_ds.GetRasterBand(1).WriteArray( zeros ) 70 dst_ds.GetRasterBand(2).WriteArray( zeros ) 71 dst_ds.GetRasterBand(3).WriteArray( zeros ) 72 opaque = numpy.ones((tiff_height,tiff_width), numpy.uint8 )*255 73 dst_ds.GetRasterBand(4).WriteArray( opaque ) 74 73 dst_ds.GetRasterBand(1).Fill(0) 74 dst_ds.GetRasterBand(2).Fill(0) 75 dst_ds.GetRasterBand(3).Fill(0) 76 dst_ds.GetRasterBand(4).Fill(255) 75 77 }}} 76 78