# This script takes *.BIL files that are still packed in a *.zip file
# extracts them and converts the output to GeoTIFF files
# The actual example below handles SRTM files as downloaded from EarthExplorer.usgs.gov
# Some information on how to do this came from this post:
# http://gis.stackexchange.com/questions/42584/how-to-call-gdal-translate-from-python-code
import os, gdal, zipfile
from gdalconst import *
# Function converts one BIL file to a GeoTiff file
def BILtoTIF(inBilPath,outTifPath):
inBil = gdal.Open(inBilPath) # InBIL
driver = gdal.GetDriverByName('Gtiff') # Output Driver
outTif = driver.CreateCopy( outTifPath, inBil, 0 )
# Properly close the datasets to flush to disk
inBil = None
outTif = None
# Actual example on how to convert an entire folder
inFol = ".../BIL_SRTM_Original/" # Folder containing the zipped BIL files
outFol = ".../Single_TIFF_SRTM/" # Folder to save converted *.tif files to
scratchFol = ".../Scratch/Convert/" # extract files here first before converting, files in here will be deleted again
for zipFile in os.listdir(inFol): # iterate through zip files in folder
if zipFile[-3:].lower() == "zip":
zipName = inFol+zipFile
zipped = zipfile.ZipFile(zipName, mode='r')
zipped.extractall(scratchFol, pwd=None) # extract all contents in zip to scratch
# convert BIL files in scratch
for j in os.listdir(scratchFol):
if j[-3:].lower() == "bil":
BIL = scratchFol + j
out = outFol+zipFile[:8]+".tif"
BILtoTIF(BIL, out)
# remove all file contents in scratch
for old_file in os.listdir(scratchFol):
file_path = os.path.join(scratchFol, old_file)
os.remove(file_path)
print(zipFile, " done")