def orthorectify(self, in_file, out_file, DEM, pixelSpacing_x,pixelSpacing_y, proj): """ Method for orthorectification of radarsat2 image Orthorectify RADARSAT-2 imagery contained in a PCIPIX file. Outputs the Orthorectified results into a newly created PCIPIX file Parameters: in_file -- Input file out_file -- Output file DEM -- DEM file pixelSpacing_x -- x pixel spacing pixelSpacing_x -- y pixel spacing proj -- data projection Return value: Error statement if error occurs. Limits and constraints: """ try: logging.info(' Executing: OrthoMosaic.orthorectify') mfile = in_file # Input image file name dbic = [1,2] # input HH + incidence angle channels mmseg = [3] # use the last math segment dbiw = [] # Use all image srcbgd = "NONE" filo = out_file # Uses the default file name ftype = "PIX" # Use the PCIDSK format foptions = "BAND" outbgd = [0] ulx = '' uly = '' lrx = '' lry = '' edgeclip = [0] tipostrn = "" mapunits = proj bxpxsz = str(pixelSpacing_x) bypxsz = str(pixelSpacing_y) filedem = DEM dbec = [1] backelev = [] elevref = "" elevunit = "METER" elfactor = [] # Specifies the offset only proc = "" sampling = [4] # Ortho correction is computed for every pixel resample = "BILIN" Report.clear() ortho2(mfile, dbic, mmseg, dbiw, srcbgd, filo, ftype, foptions, outbgd, ulx, uly, lrx, lry, edgeclip, tipostrn, mapunits, bxpxsz, bypxsz, filedem, dbec, backelev, elevref, elevunit, elfactor, proc, sampling, resample) enableDefaultReport('term') logging.info(' Successfully completed OrthoMosaic.orthorectify: ortho2') except PCIException, e: EGS_utility.EGSUtility().error('orthorectify(): {:s}'.format(e))
def classification(data): # Add channel pcimod(file=inputfile, pciop='add', pcival=[0, 0, 1, 0, 0, 0]) # Define input parameters classesNumber = [8] # define number of classes iterations = [10] # define number iterations moveThresh = [0.01] # define move threshhold print "Running unsupervized k-means classification..." print "Creating " + str(classesNumber) + " classes, applying " + str( iterations) + " iterations at a move-threshhold of " + str( moveThresh) # Run algorithm and create classification report try: Report.clear() enableDefaultReport(report) kclus(file=inputfile, dbic=inputChans, dboc=[chansCount + 1], numclus=classesNumber, maxiter=iterations, movethrs=moveThresh) finally: enableDefaultReport('term') # this will close the report file flag1 = time.time() print "Classification complete! Time elapsed: " + str( flag1 - start) + " seconds" print ""
def raster_his(self, in_file, band, mask, rep_file): """ Logs histogram. Parameters: in_file -- input file band -- band rep_file -- report file Return value: Logs histogram for input raster image, error statement otherwise. """ from pci.api import datasource as ds from pci.pcimod import pcimod from pci.exceptions import PCIException from pci.his import his from pci.nspio import Report, enableDefaultReport from osgeo import gdal, ogr, osr ## from gdalconst import * import numpy try: logging.info('') logging.info(' Executing: EGSUtility.raster_his') logging.info(' Compare product: ' + in_file) logging.info(' Band: ' + str(band)) if os.path.exists(rep_file): os.remove(rep_file) file = in_file dbic = [band] # 8-bit unsigned gmod = 'ON' # show graphic mode cmod = 'OFF' # no cumulative mode pcmo = 'OFF' # no percentage mode nsam = [] # no number of samples trim = [] # no trimming hisw = [] # no specific histogram window mask = [mask] # process entire image imstat_list = [] imstat = imstat_list Report.clear() enableDefaultReport(rep_file) his(file, dbic, gmod, cmod, pcmo, nsam, trim, hisw, mask, imstat) enableDefaultReport('term') # this will close the report file # open results file, output results to log file #with open(rep_file) as infile: # for line in infile: # logging.info(line) # infile.close() #logging.info(imstat) return imstat except PCIException, e: self.error('raster_his(): {:s}'.format(e))
def vegcover2bit(self, in_file, out_file, veg_channel=4, size_pixel=10): """ IIA Veg Cover Shapefile to ortho file Parameters: in_file -- Input file out_file -- Output file veg_channel -- Vegetation land cover (vector) size_pixel -- Pixel size Return value: Newly create vegetation land cover bitmap segment # when successful, Error statement otherwise. Limits and constraints: """ try: logging.info(' Executing: VegFloodProcess.vegcover2bit') util = EGS_utility.EGSUtility() bitmap_ids_before = util.bitmap_list( out_file ) # Run bitmap_list to get a list of IDs from before poly2bit fili = in_file dbvs = [veg_channel] # polygon layer filo = out_file dbsd = "" # use default, "Bitmap polygons gridded from vector" pixres = [size_pixel, size_pixel] # make 10 meter square pixels ftype = "PIX" # output format type foptions = "" # output options Report.clear() poly2bit(fili, dbvs, filo, dbsd, pixres, ftype, foptions) logging.info( ' Successfully completed VegFloodProcess.vegcover2bit: poly2bit' ) enableDefaultReport('term') bitmap_ids_after = util.bitmap_list( out_file ) # Run bitmap_list to get a list of IDs from after poly2bit bit_seg = list(set(bitmap_ids_after) - set(bitmap_ids_before) ) # Get the difference between the two lists return bit_seg[0] except PCIException, e: EGS_utility.EGSUtility().error('vegcover2bit(): {:s}'.format(e))
def make_pca(merged_input, pca_out, identifier): start_time = time.time() print "Starting Principal Component Analysis for file %s" % identifier pca_rep = os.path.join(pcadir, "PCA_" + identifier + "_report.txt") fexport(fili=merged_input, filo=pca_out, dbic=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], ftype="PIX") try: Report.clear() # Clear report file enableDefaultReport(pca_rep) # Change output folder location pcimod(file=pca_out, pciop="ADD", pcival=[0, 0, 3]) # Add 3 16 bit unsigned channels pca(file=pca_out, dbic=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], # Use first ten bands eign=[1, 2, 3], # Output first three eigenchannels dboc=[11, 12, 13], # Output to 3 new channels rtype="LONG") # Output extended report format except PCIException, e: print e
def threshold_sar(self, in_file, band=4, veg_thr=-3.5, openwater_thres=-12.5): """ Threshold high and low Apply low threshold for open water areas for Edge preservation filter results Apply high threshold for flooded tree areas for Edge preservation filter results The input is a PCIPIX file containing a filtered and scaled imagery. Bitmaps are added to PCIPIX representing open water and flood vegeatation areas Note: PCI's developers are going to try to add a 'last segment created' (lasc) parameter for this function in the next release, thereby enabling cleaner and more reliable automation. Parameters: in_file -- Input file band -- Input band veg_thr -- Minimum threshold for vegetation flooded area openwater_thres -- Maximum threshold for open water flooded areas Return value: Newly create vegetation and open water bitmap segment # when successful, Error statement otherwise. Limits and constraints: """ try: logging.info(' Executing: VegFloodProcess.threshold_sar') # Find min and max of band imstat = EGS_utility.EGSUtility().gdal_stat(in_file, band) max_pixel = imstat[1] min_pixel = imstat[0] + 1 # Create and name newly created segment, 'thr' does not rename segments dataset = ds.open_dataset(in_file, ds.eAM_WRITE) veg_seg = dataset.create_bitmap( ) # Get the number of the newly created bitmap segment file = in_file dbsl = [veg_seg] dbsn = "vegflood" dbsd = "Veg Flood seg" mas(file, dbsl, dbsn, dbsd) logging.info( ' Successfully completed VegFloodProcess.threshold_sar: mas' ) # Threshold for flood vegetation file = in_file dbic = [band] #dbob = [] # create new bitmap dbob = [veg_seg] tval = [veg_thr, max_pixel] # threshold range (min,max) comp = 'OFF' # threshold values from 9 to 11 dbsn = 'vegflood' # output segment name dbsd = 'Veg Flood seg' # output segment description Report.clear() thr(file, dbic, dbob, tval, comp, dbsn, dbsd) enableDefaultReport('term') logging.info( ' Successfully completed VegFloodProcess.threshold_sar: thr' ) # Create and name newly created segment, 'thr' does not rename segments water_seg = dataset.create_bitmap( ) # Get the number of the newly created bitmap segment file = in_file dbsl = [water_seg] dbsn = "openwate" dbsd = "Open Water seg" mas(file, dbsl, dbsn, dbsd) logging.info( ' Successfully completed VegFloodProcess.threshold_sar: mas' ) # Threshold for open water file = in_file dbic = [band] #dbob = [] # create new bitmap dbob = [water_seg] tval = [min_pixel, openwater_thres] # threshold range (min,max) comp = 'OFF' # threshold values dbsn = 'openwate' # output segment name dbsd = 'Open Water seg' # output segment description Report.clear() thr(file, dbic, dbob, tval, comp, dbsn, dbsd) enableDefaultReport('term') logging.info( ' Successfully completed VegFloodProcess.threshold_sar: thr' ) return [veg_seg, water_seg] except PCIException, e: EGS_utility.EGSUtility().error('threshold_sar(): {:s}'.format(e))
def classification(self, file, alg): if (alg == 'kmeans'): print("Your selection is K-means Classification") else: print("Your selection is Iso-Cluster Classification") report = os.getcwd() + "\\Data\\Result\\Report.txt" if os.path.isfile(report): print("Deleting old Report") os.remove(report) with datasource.open_dataset(file) as dataset: cc = dataset.chan_count print("Available Channels: ") print(cc) if (alg == 'kmeans'): file = file dbic = list(range(1, cc + 1, 1)) # input channels dboc = [cc + 1] # output channel print(dboc) mask = [] # process entire image numclus = [5] # requested number of clusters seedfile = '' # automatically generate seeds maxiter = [20] # no more than 20 iterations movethrs = [0.01] print("The file is: " + root.filename) ## enableDefaultReport(prev_report) pcimod(file=file, pciop='add', pcival=[1, 0, 0, 0, 0, 0]) Report.clear() enableDefaultReport(report) print("Applying K-Means Classification") kcluster = kclus(file, dbic, dboc, mask, numclus, seedfile, maxiter, movethrs) print("K-Means Classification is successful") else: file = file # input file dbic = list(range(1, cc + 1, 1)) # input channels dboc = [cc + 1] # output channel print(dboc) mask = [] # process entire image numclus = [5] # requested number of clusters maxclus = [7] # at most 20 clusters minclus = [5] # at least 5 clusters seedfile = '' # automatically generate seeds maxiter = [5] # no more than 20 iterations movethrs = [0.01] siggen = "NO" # no signature generation samprm = [5] stdv = [10.0] lump = [1.0] maxpair = [5] # no more than 5 cluster center pairs # clumped in one iteration backval = [] # no background value nsam = [] # default number of samples print("The file is: " + root.filename) ## enableDefaultReport(prev_report) pcimod(file=file, pciop='add', pcival=[1, 0, 0, 0, 0, 0]) Report.clear() enableDefaultReport(report) print("Applying Iso-cluster Classification") isoclus( file, dbic, dboc, mask, numclus, maxclus, minclus, seedfile, maxiter,\ movethrs, siggen, samprm, stdv, lump, maxpair, backval, nsam ) print("Iso-Cluster Classification is successful") pcimod(file=file, pciop='add', pcival=[1]) ## root.status.set("Running fmo") print("Applying MOD filter") kfmo = fmo(file=file, dbic=[cc + 1], dboc=[cc + 2]) print("MOD filter is successfully applied") pcimod(file=file, pciop='add', pcival=[1]) ## root.status.set("Running sieve") print("Applying SIEVE filter") ksieve = sieve(file=file, dbic=[cc + 1], dboc=[cc + 3], sthresh=[32]) print("SIEVE filter is successfully applied") ## Split and insert directory name split_file = file.split("/") split_file.insert(-1, "Result") join_result = "\\".join(split_file) ## Split by . dot_result = join_result.split(".") ## print(dot_result) ## print("dot result") ## join .shp at last shp_result = dot_result[0] + ".shp" current_file_path, ext = os.path.splitext(str(file)) current_file = current_file_path.split("/")[-1] ## print(current_file) ## print("current file") ## Delete previously generated shapefiles prev_files = glob.glob(os.getcwd() + "\\Data\\Result\\" + "*") ## print(prev_files) ## print("previous files") for prev_file in prev_files: prev_file_name, format = os.path.splitext( str(os.path.basename(prev_file))) ## print(prev_file_name) ## print(" prev file names") if (current_file in prev_file_name): print("DELETING: " + str(prev_file)) os.remove(prev_file) print("Successfully DELETED: " + str(prev_file)) ## if(os.path.exists("../Data/Result/"+dot_result[0])): ## print("inside if ......") ## os.remove(shp_result) ## print("shp is deleted...") print("Exporting to shapefile") ras2poly(fili=file, dbic=[cc + 3], filo=shp_result, smoothv="Yes", ftype="SHP", foptions="") print("Shapefile is successfully created")
from pci.mcd import mcd from pci.fme import fme from pci.sieve import sieve from pci.thr import thr from pci.api import gobs from pci.fexport import fexport from pci.pctwrit import pctwrit from pci.pctread import pctread from pci.nspio import Report # set up logging logger = logging.getLogger() logger.info('Icemapr loaded') # turn off pci output Report.clear() def icemapr(infile, infilec, inmask, inmasks, inpct, outfile, logfile=None, logdetails=2): """ IceMAP-R algorithm for river ice using SAR images with HH polarization Parameters: INFILE : Name of the file for the ortorectified HH radar image
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))