Created
August 16, 2011 08:59
-
-
Save philipn/1148693 to your computer and use it in GitHub Desktop.
GeoTIFFs -> One Big GeoTIFF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Likely not useful to anyone else, but just putting it out there. | |
This script will take a directory of GeoTIFFs and merge them together without issues. | |
This script simply decompresses the files, runs nearblack to remove pseudo-black borders caused by compression, and then uses gdalwarp to stitch the files together. | |
The script is designed to use the minimal amount of disk space possible -- it cleans up each file after decompression and continually merges with a master image. | |
""" | |
import os | |
import shutil | |
from subprocess import Popen, PIPE, STDOUT | |
GEOTIFF_DIR = 'all_2009_tiffs' | |
def skip_file(filename): | |
num = int(filename.split('_')[2]) | |
if num < 11781: | |
return True | |
return False | |
def uncompress(file_path): | |
cmd = '/usr/bin/gdal_translate %s %s.uncompressed.tif' % (file_path, file_path) | |
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True) | |
output = p.stdout.read() | |
print output | |
def remove_nearblack(file_path): | |
cmd = '/usr/bin/nearblack -near 20 %s' % file_path | |
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True) | |
output = p.stdout.read() | |
print output | |
def stitch_with_master(file_path): | |
if not os.path.exists(master_path): | |
shutil.copyfile(file_path, master_path) | |
return | |
cmd = '/usr/bin/gdalwarp -multi -dstalpha -srcnodata 0 -wo "SKIP_NOSOURCE" --config "GDAL_CACHEMAX=500" -wm=5000 %s %s temp.tif' % (file_path, master_path) | |
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True) | |
output = p.stdout.read() | |
print output | |
os.remove(master_path) | |
shutil.copyfile('temp.tif', master_path) | |
master_path = 'master.tiff' | |
for filename in sorted(os.listdir(GEOTIFF_DIR)): | |
if skip_file(filename): | |
continue | |
file_path = os.path.join(GEOTIFF_DIR, filename) | |
print "Uncompressing...%s" % file_path | |
uncompress(file_path) | |
print "Uncompressed %s!" % file_path | |
print "Removing near black...%s" % file_path | |
remove_nearblack("%s.uncompressed.tif" % file_path) | |
print "Removed near black %s!" % file_path | |
print "Stitching with master...%s" % file_path | |
stitch_with_master("%s.uncompressed.tif" % file_path) | |
print "Stitched with master %s!" % file_path | |
os.remove("%s.uncompressed.tif" % file_path) | |
print "Removed file %s.uncompressed.tif" % file_path |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On windows I had to set close_fds=False and I had to add this to the end:
Otherwise it worked perfect.
Thanks!