[GIS] TIFF and TFW file validation

fmegeotiff-tiffpythonrastervalidation

This is a bit of a cry for help as I’m still quite new to this.

I am looking to create a thing that can validate some specific parameters for TIFF and TFW files, preferable using python, FME or something similar.

  • It is a TIFF file
  • TIFF file size – This should be equal to or greater than 46MB – Equal to or less than 47MB
  • TIFF dimensions – they should be 4000 * 4000 pixels in size
  • It has three colour bands
  • It has an associated, valid TFW file

I’ll roughly have to check 100 TIFF and 100 TFW files at a time.

The TIFF and TFW files have the corresponding 1KM grid reference as their names, for example I have TQ7926.tif and TQ7926.tfw.

What would be the best way to achieve this? I’m happy to answer any questions to assist with the answering of my question.

My python GDAL knowledge is pretty poor, so i've had ago in FME.
See my fmw here

I'd really appricate any feedback on it or suggestions to make it quicker/tidier/better. It needs to be used on a directory of folders containing TIFFs

Best Answer

Since GDAL provides a well-documented python API this is reasonably easy to do in python.

from osgeo import gdal
import sys
import os
# this allows GDAL to throw Python Exceptions
gdal.UseExceptions()

file='/data/os-data/rasters/streetview/tq/tq00ne.tif'
try:
    gtif  = gdal.Open( file )
except RuntimeError, e:
    print 'Unable to open '+file
    print e
    sys.exit(1)

#print gtif.GetMetadata()
statinfo = os.stat(file)
size=statinfo.st_size/1e6

print size,"Mb"

print "bands ",gtif.RasterCount

cols = gtif.RasterXSize
rows = gtif.RasterYSize
print cols," cols x ",rows," rows"
gtif = None

If you wrap this into a function and then loop through the .tif files in your directory this should work.

Related Question