def import_file(self, in_file, out_file): """ Import Image and auxiliary information Detailed description Imports the image and all auxiliary information from a source file to a newly created PCIDSK file Parameters: in_file -- Input file out_file -- Output file Return value: Error statement if error occurs. Limits and constraints: """ try: logging.info(' Executing: OrthoMosaic.import_file') dbiw = [] poption = "NEAR" dblayout = "BAND" fimport(in_file, out_file, dbiw, poption, dblayout) logging.info(' Successfully completed OrthoMosaic.import_file: fimport') except PCIException, e: EGS_utility.EGSUtility().error('import_file(): {:s}'.format(e))
def readtopix(indir): infiles = os.listdir(indir) # List input folders for i in range(len(infiles)): # Add path for 10m S2 Bands infiles[i] = "input/" + infiles[ i] + "/MTD_MSIL1C.xml?r=%3ABand+Resolution%3A10M" pix = "pix/tile" + str(i) + ".pix" # Name converted pix output pixfiles.append(pix) # Add to list of pix files fili = infiles[i] print "\tStarting pix conversion file %s" % str(i) fimport(fili, pix) # Run import function print "\tPix conversion completed for %s:" % infiles[i] print "\tOutput to %s" % pix
def readtopix(inputdir): infiles = os.listdir(inputdir) # List input folders for i in range(len(infiles)): # Add paths for S2 band sets fili_10 = "input/" + infiles[ i] + "/MTD_MSIL1C.xml?r=%3ABand+Resolution%3A10M" fili_20 = "input/" + infiles[ i] + "/MTD_MSIL1C.xml?r=%3ABand+Resolution%3A20M" fili_60 = "input/" + infiles[ i] + "/MTD_MSIL1C.xml?r=%3ABand+Resolution%3A60M" name_fields = infiles[i].split("_") # Set up fields for filenames mission = name_fields[0] date = (name_fields[2])[:8] if name_fields[ 1] == "OPER": # Long-format naming scheme from 2015-2016 date = (name_fields[5])[:8] findpath = os.path.join(indir, infiles[i]) find_xml = os.listdir(findpath) for j in range(len(find_xml)): if find_xml[j][-3:] == "xml" and find_xml[j] != "INSPIRE.xml": xml_name = find_xml[j] fili_10 = "input/" + infiles[ i] + "/" + xml_name + "?r=%3ABand+Resolution%3A10M" fili_20 = "input/" + infiles[ i] + "/" + xml_name + "?r=%3ABand+Resolution%3A20M" fili_60 = "input/" + infiles[ i] + "/" + xml_name + "?r=%3ABand+Resolution%3A60M" pix10full = "pix/" + mission + "_" + date + "_10m_full.pix" # Set up paths for functions pix20full = "pix/" + mission + "_" + date + "_20m_full.pix" pix60full = "pix/" + mission + "_" + date + "_60m_full.pix" pix10 = "pix/" + mission + "_" + date + "_10m_unmerged.pix" pix20 = "pix/" + mission + "_" + date + "_20m_unmerged.pix" pix_merged = "pix/" + mission + "_" + date + "_10m_merged.pix" pix60 = "pix/" + mission + "_" + date + "_60m_atmospheric.pix" start_time = time.time() print "Starting pix conversion file %s_%s." % (mission, date) fimport(fili_10, pix10full) # Import R,G,B,NIR bands fimport(fili_20, pix20full) # Import RE,NIR,SWIR bands fimport(fili_60, pix60full) # Import Coastal, Vapour, Cirrus clip( fili=pix10full, # Clip 10m bands dbic=[1, 2, 3, 4], dbsl=[], sltype="", filo=pix10, ftype="PIX", foptions="", clipmeth="LAYERVEC", clipfil=clipvec, cliplay=[2]) clip( fili=pix20full, # Clip 20m bands dbic=[1, 2, 3, 4, 5, 6], dbsl=[], sltype="", filo=pix20, ftype="PIX", foptions="", clipmeth="LAYERVEC", clipfil=clipvec, cliplay=[2]) clip( fili=pix60full, # Clip 60m bands dbic=[1, 2, 3], dbsl=[], sltype="", filo=pix60, ftype="PIX", foptions="", clipmeth="LAYERVEC", clipfil=clipvec, cliplay=[2]) os.remove(pix10full) # Delete un-clipped images os.remove(pix20full) os.remove(pix60full) # The order of data in merge list file matters: # 10m bands first results in resampling of 20m resolution # to 10m resolution. Avoids data loss due to resampling of 10m to 20. mergefile_name = mission + "_" + date + "_merge.txt" mergefile_path = os.path.join(mergedir, mergefile_name) path10 = os.path.join(workingdir, pix10) path20 = os.path.join(workingdir, pix20) mergefile = open(mergefile_path, "w") mergefile.write('"' + path10 + '"' + "\n") mergefile.write('"' + path20 + '"') mergefile.close() datamerge( mfile= mergefile_path, # Merge 10m bands and 20m bands into one pix file dbic=[], filo=pix_merged, ftype="PIX", foptions="", extent="UNION", nodatval=[], resample="NEAR") completion_time = time.time() - start_time print "Pix conversion completed for image %s_%s in %i seconds." % ( mission, date, completion_time) print "Wrote files to:\n\t%s\n\t%s\n\t%s\n\t%s\n" % (pix_merged, pix10, pix20, pix60)
def merge_filter_export(self, in_file, in_file2, out_file, out_file2, out_file3, hole_size): """ Imports, merges, filters and exports flooded products. Import flood products into PCIPIX format, merge products, filters to remove holes / areas. Outputs resulting products as compressed geotif and shapefile. Merges image value polygons smaller than a user-specified threshold with the largest neighboring polygon Parameters: in_file -- Input file1 in_file2 -- Input file2 out_file -- Working PCIPIX file out_file2 -- Resuling output geotif hole_size -- threshold for filter (hectares) Return value: Error statement if error occurs. """ #import required PCI Geomatica modules import pci from pci.sieve import * from pci.fimport import * from pci.fexport import * from pci.thmrovr import * from pci.mosaic import * from pci.thmrmer import * from pci.model import * from pci.nspio import Report, enableDefaultReport from pci.exceptions import PCIException from pci.ras2poly import * import arcpy from osgeo import gdal, ogr, osr from gdalconst import GA_Update try: util = EGS_utility.EGSUtility() #Import first image (flooded vegetation) into PCIPIX file logging.info(' Executing: MergeProcess.merge_filter_export') fili = in_file filo = out_file dbiw = [] poption = "" dblayout = "" #Defaults to "BAND" Report.clear() fimport(fili, filo, dbiw, poption, dblayout) enableDefaultReport('term') logging.info(' Successfully completed MergeProcess.merge_filter_export: fimport') #Import second image (open water) into PCIPIX file new_chans = EGS_utility.EGSUtility().add_8_channel(out_file) fili = in_file2 dbic = [1] # use channel 1 dbvs = [] # default, no cutline defined dblut = [] # default, no lookup table filo = out_file dboc = [int(new_chans[0])] # overwrite channel 1 blend = [] # default, no blending backval = [] # default, 0 mosaic( fili, dbic, dbvs, dblut, filo, dboc, blend, backval ) logging.info(' Successfully completed MergeProcess.merge_filter_export: mosaic') # Convert values using MODEL so 255 = 0 file = out_file source_model = """if (%2 = 255) then %2 = 0 endif""" model( file, source=source_model) #Merge two image together, possible use thmrovr for one step instead of two merge_chan = util.add_8_channel(out_file) fili = out_file # input file filo = out_file # output file will be created dbic = [1,int(new_chans[0])] # input thematic raster channels dbib = [] # no bitmap mask dboc = [int(merge_chan[0])] omethod = "UNION" # default, "UNION" thmrovr( fili, filo, dbic, dbib, dboc, omethod ) logging.info(' Successfully completed MergeProcess.merge_filter_export: thmrovr') # Combine values using MODEL so zero = not flooded, 255 = flooded file = out_file source_model = """if (%3 >= 1) then %3 = 255 endif""" model( file, source=source_model) #Determine filter size as pixel number filtered_chan = util.add_8_channel(out_file) pixelwidth = util.gdal_geotransform(out_file)[1] pixelheight = util.gdal_geotransform(out_file)[5] logging.info(' pixelwidth: ' + str(pixelwidth)) logging.info(' pixelheight: ' + str(pixelheight)) num_pixel = int((hole_size * 10000)/abs(pixelwidth * pixelheight)) # hole_size is in hectares logging.info(' Number of pixels for threshold hole size: ' + str(num_pixel)) #Filter in order to remove holes file = out_file dbic = [int(merge_chan[0])] dboc = [int(filtered_chan[0])] sthresh = [num_pixel] # polygon size threshold in keepvalu = [] # no keep value connect = [] # default, 4-connection sieve( file, dbic, dboc, sthresh, keepvalu, connect ) logging.info(' Successfully completed MergeProcess.merge_filter_export: sieve') #EGS_utility.EGSUtility().raster_his(out_file,int(filtered_chan[0])) #Export resulting product as a geotif fili = out_file filo = out_file2 dbiw = [] dbic = [int(filtered_chan[0])] dbib = [] dbvs = [] dblut = [] dbpct = [] ftype = "tif" foptions = "LZW" fexport( fili, filo, dbiw, dbic, dbib, dbvs, dblut, dbpct, ftype, foptions ) logging.info(' Successfully completed MergeProcess.merge_filter_export: fexport') fili = out_file # input raster file dbic = [int(filtered_chan[0])] # using channel 6 from irvine.pix filo = out_file # output file to be created smoothv = "" # default, YES vectors are smoothed dbsd = "" # defaults to "Created from Raster" ftype = "" # default, PIX foptions = "" # output format options ras2poly(fili, dbic, filo, smoothv, dbsd, ftype, foptions) logging.info(' Successfully completed MergeProcess.merge_filter_export: ras2poly') # Export resulting product as a shapefile # PCI creates shapefile with projection 'UTM 18 S E008 ', compared to 'UTM 18 T D122' # These projections are the same projection, as E008 is the ellipsoid used by NAD 1983. # but opted for arcpy export fili = out_file filo = out_file3 dbiw = [] dbic = [] dbib = [] dbvs = [3] dblut = [] dbpct = [] ftype = "shp" foptions = "" #fexport( fili, filo, dbiw, dbic, dbib, dbvs, dblut, dbpct, ftype, foptions ) arcpy.RasterToPolygon_conversion(out_file2, out_file3, "NO_SIMPLIFY", "") logging.info(' Successfully completed MergeProcess.merge_filter_export: arcpy.RasterToPolygon_conversion') except PCIException, e: EGS_utility.EGSUtility().error('merge_filter_export(): {:s}'.format(e))
folderz(report_dir) folderz(final_dir) print(' ...Part 1 completed') #------------------------------------------ # Part2: Clip Imagery with SHP file #------------------------------------------ print("... Part 2: Clipping Scene") #make a list for shp file of clipping area input_shp = (makelist(rootdir =working_dir, suffix = '.shp')) #import shp file as pix with one vector layer for shp in input_shp: try: fili = shp filo = working_dir + "/" + "clip_pix" + "/" +"clip.pix" fimport(fili, filo) except PCIException as e: print (e) except Exception as e: print (e) # clip scene using pix file with the vector layer input_meta = (makelist(rootdir = working_dir, suffix = 'MTL.txt')) for image in input_meta: try: fili ='-'.join([image, 'MS']) dbic = [1,2,3,4,5,6] dbsl = [] sltype = "" filo = clip_scene + '/' + "scene_clipped.pix" ftype = "PIX"