[gdal-dev] ERROR 4: `AOI.tif' not recognised as a supported file format.

Even Rouault even.rouault at mines-paris.org
Mon Mar 30 13:02:25 EDT 2009


Oz,

See the hereafter a correction to insert in your code that will hopefully fix 
your issue.

Le Monday 30 March 2009 17:25:13 Oz Nahum, vous avez écrit :
> Hi,
> I have encountered a weired problem with gdal_rasterize.
>
> when I run the script from here
> http://trac.osgeo.org/gdal/wiki/FAQRaster#HowcanIcreateablankrasterbasedona
>vectorfilesextentsforusewithgdal_rasterize
>
> When I run:
> #!/usr/bin/env python
>
> from osgeo import gdal
> from osgeo import osr
> from osgeo import ogr
> import numpy
> #from numpy import ones
> import stat, sys, os, string, commands
>
> shp = 'AOI'
> tiff = 'AOI.tif'
> px = 1
> tiff_width = 80
> tiff_height = 80
>
> # Import vector shapefile
> vector = ogr.GetDriverByName('ESRI Shapefile')
> src_ds = vector.Open(shp +'.shp')
> src_lyr = src_ds.GetLayerByIndex(index=0)
> src_extent = src_lyr.GetExtent()
>
> # Create new raster layer with 4 bands
> raster = gdal.GetDriverByName('GTiff')
> dst_ds = raster.Create( tiff, tiff_width, tiff_height, 1, gdal.GDT_Byte)
>
> # Create raster GeoTransform based on upper left corner and pixel
> resolution raster_transform = [src_extent[0], px, 0.0, src_extent[3], 0.0,
> -px] dst_ds.SetGeoTransform( raster_transform )
>
> # Get projection of shapefile and assigned to raster
> srs = osr.SpatialReference()
> srs.ImportFromWkt(src_lyr.GetSpatialRef().__str__())
> dst_ds.SetProjection( srs.ExportToWkt() )
>
> # Create blank raster with fully opaque alpha band
> zeros = numpy.zeros( (tiff_height, tiff_width), numpy.uint8 )
> dst_ds.GetRasterBand(1).WriteArray( zeros )

# Properly close the dataset before calling gdal_rasterize, so that the TIF
# file is properly written.
dst_ds = None

>
> commandString =  'gdal_rasterize -burn 255 -l AOI AOI.shp AOI.tif'
> commandOutput = commands.getoutput(commandString)
> print commandOutput
> #end of code
>
> I get the error this error:
>
> augmented to the gdal_rasterize command I see this error. When I run it via
> a shell script, I don't see the error:
> ERROR 4: `AOI.tif' not recognised as a supported file format.
>
>
> #!/bin/sh
> python create_empty_raster.py
> gdal_rasterize -burn 255 -l AOI AOI.shp AOI.tif
>
> Runs ok.

--> This runs ok as at the end of the create_empty_raster.py script, the GDAL
dataset is automatically closed.

>
> So I am confused, why I can't call gdal_rasterize from python ?
> Thanks, for any help
>
> Oz Nahum
> Universtät Tuebingen
> Applied Environmental Sciences




More information about the gdal-dev mailing list