Changes between Version 15 and Version 16 of FAQRaster
- Timestamp:
- Oct 5, 2009, 3:34:55 PM (15 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
FAQRaster
v15 v16 28 28 One 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]. 29 29 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 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. 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: 32 31 33 32 {{{ … … 46 45 shp = 'test.shp' 47 46 tiff = 'test.tif' 47 # Alternative A: Set raster dataset dimensions 48 48 tiff_width = 7850 49 49 tiff_height = 3500 50 # Alternative B: Set pixel resolution 51 x_res = 0.5 52 y_res = 0.75 50 53 51 54 # Import vector shapefile … … 55 58 src_extent = src_lyr.GetExtent() 56 59 60 # Alternative A: Calculate pixel resolution from desired dataset dimensions 61 x_res = (src_extent[1] - src_extent[0]) / tiff_width 62 y_res = (src_extent[3] - src_extent[2]) / tiff_height 63 # Alternative B: Calculate raster dataset dimensions from desired pixel resolution 64 tiff_width = int(math.ceil(abs(src_extent[1] - src_extent[0]) / x_res)) 65 tiff_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 68 raster_transform = [src_extent[0], x_res, 0.0, src_extent[3], 0.0, -y_res] 69 dst_ds.SetGeoTransform(raster_transform) 70 57 71 # Create new raster layer with 4 bands 58 72 raster = 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 ) 73 dst_ds = raster.Create(tiff, tiff_width, tiff_height, 4, gdal.GDT_Byte) 66 74 67 75 # Get projection of shapefile and assigned to raster 68 76 srs = osr.SpatialReference() 69 77 srs.ImportFromWkt(src_lyr.GetSpatialRef().__str__()) 70 dst_ds.SetProjection( srs.ExportToWkt())78 dst_ds.SetProjection(srs.ExportToWkt()) 71 79 72 80 # Create blank raster with fully opaque alpha band