# #C. Macon - October 04, 2017, Modify VDatum MultiCPU for GDAL translate LZW Compression # # import os, time, glob, Queue, threading, math, multiprocessing ############################# Variables to edit ################################ gdalPath = 'C:\\GDAL' # Change path to the GDAL install folder nProc = multiprocessing.cpu_count() - 12 # Number of cpu's to use for multi-threading. Use 12 (or other #) less than the machine has ################################################################################ start = time.time() # Input path and files tifPath = os.getcwd() print(' Compressing TIF files in %s')%(os.getcwd()) tiff = glob.glob('*.tif') # Output path if not os.path.isdir("GDAL_lzw"): os.makedirs("GDAL_lzw") outTifDir = '\\'.join( [tifPath, "GDAL_lzw"] ) # Organize batch jobs for multi-cpu processing nTif = len( tiff ) nBatches = nTif tifPerBatch = nTif/nBatches if tifPerBatch < 1: tifPerBatch = 1 lzwCmd = [] for i in range( 0, nTif, tifPerBatch ): tiffIdx1 = i tiffIdx2 = i+tifPerBatch if tiffIdx2 > nTif: # last batch could have fewer files tiffIdx2 = nTif tifFiles = tiff[tiffIdx1:tiffIdx2] inTifFiles = ';'.join(tifPath+'\\'+str(f) for f in tifFiles) outTifFiles = ';'.join(outTifDir+'\\'+str(f) for f in tifFiles) lzwCmdStr = gdalPath + '\\gdal_translate.exe -co "COMPRESS=LZW" ' + inTifFiles + ' ' + outTifFiles lzwCmd.append(lzwCmdStr) os.chdir(gdalPath) queue = Queue.Queue() class ThreadJobs(threading.Thread): def __init__(self, queue): threading.Thread.__init__(self) self.queue = queue def run(self): while True: # get job from queue myJob = self.queue.get() inCmd = myJob os.system(inCmd) # signal queue job is done self.queue.task_done() def main(): # spawn a pool of processes, and let the queue manage them for i in range(nProc): t = ThreadJobs(queue) t.setDaemon(True) t.start() # populate queue with jobs for cmd in lzwCmd: queue.put(cmd) # wait on the queue until everything has been processed queue.join() return 'Done' main() print '\n\nProcessing completed in: %s' % ((time.time() - start)/60),'minutes' del start