def crop(shapefile_full, raster_full, raster2_full=None, raster3_full=None, prefix="new_", new_projcode=None, shapefile_tmpDir="tmp2"): shapefile = gcswift.get_raw_file_name(shapefile_full) raster = gcswift.get_raw_file_name(raster_full) layername = shapefile.split(".")[0] if not new_projcode: new_projcode = get_projection(raster_full) new_shapefile = reproject_shapefile(shapefile_full, shapefile, new_projcode, shapefile_tmpDir) command = "ogrinfo %s %s" % (new_shapefile,layername) # print "Getting shape info", command p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) # p.wait() out, err = p.communicate() #print out assert "Geometry: Polygon" in out, "Problem reading the shapefile" lines = out.split("\n") line = [x for x in lines if x.startswith("Extent: ")][0] bb = re.findall(r"[0-9.]*[0-9]+", line) bb = [bb[0], bb[3], bb[2], bb[1]] bb_str = " ".join(bb) dir_name = gcswift.get_dir_name(raster_full) full_prefix = dir_name + "/" + prefix command_prefix = "gdal_translate -projwin " + bb_str + " -of GTiff " command = command_prefix + raster_full + " " + full_prefix + raster # print "Crop command", command p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) if raster2_full: raster2 = gcswift.get_raw_file_name(raster2_full) command2 = command_prefix + raster2_full + " "+ full_prefix+raster2 # print "Crop command", command2 q = subprocess.Popen(command2, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) if raster3_full: raster3 = gcswift.get_raw_file_name(raster3_full) command3 = command_prefix + raster3_full + " "+full_prefix+raster3 # print "Crop command", command3 r = subprocess.Popen(command3, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) p.wait() assert p.returncode == 0, "Failed with %s"%(p.communicate()[0]) if raster2_full: q.wait() assert q.returncode == 0 if raster3_full: r.wait() assert r.returncode == 0 out, err = p.communicate()
def combine_bands(allfiles, file_manager, gid="new"): target = [] dirName = gcswift.get_dir_name(allfiles[0]) assert dirName == file_manager.tmp_file_dir, "band directory " + dirName + " ought to be equal to " + file_manager.tmp_file_dir if gid != "new" and os.path.exists(dirName + "/" + gid+"_b03.tif"): prefix = dirName + "/" + gid return [prefix + "_b03.tif", prefix + "_b04.tif", prefix + "_b07.tif"] assert dirName == file_manager.tmp_file_dir, "band directory " + dirName + " ought to be equal to " + file_manager.tmp_file_dir for full_name in allfiles: fname = gcswift.get_raw_file_name(full_name) info = grab_pathrow_band_time(fname) if info[3] != '7dt' and info[3] != '5dt': log("Skipping non-landsat 5/7 image" + fname) else: target.append((fname, info)) cwd = os.getcwd() os.chdir(dirName) # all operations now are local to dirName temps = {'b03':tempfile.mkdtemp(prefix='landsatb03', dir=file_manager.tmp_file_dir), 'b04':tempfile.mkdtemp(prefix='landsatb04', dir=file_manager.tmp_file_dir), 'b07':tempfile.mkdtemp(prefix='landsatb07', dir=file_manager.tmp_file_dir)} for f in target: shutil.copy(f[0], temps[f[1][1]]+"/"+f[0]) done = [] for d in temps.items(): new = _compose_tiff(gid+d[0], d[1]) done.append(new.split('/')[-1]) shutil.copy(new,".") shutil.rmtree(d[1]) # return done os.chdir(cwd) # Return the absolute paths result = [] for item in done: result.append(dirName + "/" + item) return result
def combineIntoPng(self): """ """ def getMostRecentSet(fnames): """ """ dates = [] for f in fnames: # we are always encounter 'dt' before the date fdate = f.split('t')[1].split('.')[0] if fdate not in dates: dates.append(int(fdate)) maxdate = str(max(dates)) newfiles = [] for f in fnames: if maxdate in f: newfiles.append(f) log(" in getMostRecentSet, newfiles are " + " ".join(newfiles)) return newfiles def decompressTiffs(files): """ """ import zlib tiffs = [] cwd = os.getcwd() log("In decompressTiffs, cwd is " + cwd) log("Decompressing files") try: for f in files: # tname = f.rstrip('.gz') tname = self.file_manager.get_tiff_name(f) tiffs.append(tname) # print "In decompress_tiffs: file is " + f + " tname is " + tname if not os.path.exists(tname): # inf = gzip.GzipFile(os.path.join(cwd, f), 'rb') inf = gzip.GzipFile(f, 'rb') dcmpdata = inf.read() inf.close() outf = open(tname, "w") # try catches???? outf.write(dcmpdata) outf.close() except IOError as e: # might be corrupt - delete now so not cached for f in files: print "Corrupt files, unlinking:", f os.unlink(f) raise AssertionError(e) except zlib.error as e: # might be corrupt - delete now so not cached for f in files: print "Corrupt files, unlinking:", f os.unlink(f) raise AssertionError(e) log(" At the end of decompressTiffs, files are: " + " ".join(files) + ". Tiffs are:" + " ".join(tiffs)) log("Decompressing files done") return tiffs assert(self.havefiles) assert(len(self.files)) # get raster 3 band images for each prefix fnames = decompressTiffs(self.files) #print fnames if len(fnames) > 3: log("Combining images") fnames = combine.combine_bands(fnames, self.file_manager, str(self.gid)) log("Getting shapefile for", str(self.gid)) shpname = trim.getShapefile(self.file_manager.tmp_file_dir, self.gid) log("Shapefile is " + shpname) #try: trim.crop(shpname, fnames[0], fnames[1], fnames[2], prefix="trim_", shapefile_tmpDir = self.file_manager.shapefile_tmpDir) dir_name = gcswift.get_dir_name(fnames[0]) fnames = [dir_name + "/trim_"+gcswift.get_raw_file_name(name) for name in fnames] log("New fnames are: " + " ".join(fnames)) #except AssertionError as e: # print e namesAndBands = [(fname, getBand(fname)) for fname in fnames] namesAndBands = sorted(namesAndBands, key=itemgetter(1), reverse=True) fnames = [fname for (fname, band) in namesAndBands] pre = self.file_manager.tmp_file_dir + "/" + str(self.gid) allbandsTIF = pre +"_allbands.tif" allbandsPNG = pre +"_allbands.png" mergeCmd = '/usr/local/bin/gdal_merge.py -n -9999 -a_nodata -9999 -separate -o ' mergeCmd += allbandsTIF for filename in fnames: mergeCmd += ' ' + filename log("creating... " + allbandsTIF + " with "+ mergeCmd) p = Popen(mergeCmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True) p.wait() output = p.stdout.read() assert p.returncode == 0, "Creation of merged bands Failed" log(output) # # Now convert to PNG # translateCmd = "/usr/local/bin/gdal_translate -of png -ot Byte -scale %s %s" % (allbandsTIF, allbandsPNG) log("creating... " + allbandsPNG + " with " + translateCmd) p = Popen(translateCmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True) p.wait() output = p.stdout.read() assert p.returncode == 0, "Creation of png Failed" log(output) # we give it 1) the name of the png created from grass # and 2) the name the greenspace calc will create # and 3) the directory where they will be stored pngimg = pngImg(allbandsPNG, str(self.gid) + ".png", self.file_manager.tmp_file_dir) pngimg.bbox = self.bbox self.img = pngimg