Example #1
0
def doRGBI(RGBI_path, RGB_outpath, CIR_outpath):
    outWorkspace = RGB_outpath

    try:
        makePath(RGB_outpath, "temp")
        arcpy.env.workspace = RGBI_path
        RGBIrasters = arcpy.ListRasters("", "TIF")
        rastCount = len(RGBIrasters)
        arcpy.AddMessage("Reconfiguring " + str(rastCount) + " rasters.")
        current = 1
        for RGBIraster in RGBIrasters:
            arcpy.env.workspace = RGBI_path
            # Get Bands for RGBI
            redin = RGBIraster + "\Band_1"
            red = os.path.join(RGB_outpath, "temp",
                               RGBIraster[:-4] + "_red.tif")
            arcpy.CopyRaster_management(redin, red)
            greenin = RGBIraster + "\Band_2"
            green = os.path.join(RGB_outpath, "temp",
                                 RGBIraster[:-4] + "_green.tif")
            arcpy.CopyRaster_management(greenin, green)
            bluein = RGBIraster + "\Band_3"
            blue = os.path.join(RGB_outpath, "temp",
                                RGBIraster[:-4] + "_blue.tif")
            arcpy.CopyRaster_management(bluein, blue)
            IRin = RGBIraster + "\Band_4"
            IR = os.path.join(RGB_outpath, "temp", RGBIraster[:-4] + "_ir.tif")
            arcpy.CopyRaster_management(IRin, IR)

            outRaster = RGBIraster[:-4] + ".tif"
            start = time.clock()
            arcpy.AddMessage("Reconfiguring file " + str(current) + " of " +
                             str(rastCount) + ": " + RGBIraster)

            arcpy.CompositeBands_management(
                '"' + red + ";" + green + ";" + blue + '"',
                os.path.join(RGB_outpath,
                             os.path.basename(RGBIraster[:-4] + ".tif")))
            arcpy.AddMessage("Successfully reconfigured: " + RGBIraster +
                             " to " + RGB_outpath)
            arcpy.CompositeBands_management(
                '"' + IR + ";" + red + ";" + green + '"',
                os.path.join(CIR_outpath,
                             os.path.basename(RGBIraster[:-4] + ".tif")))
            elapsed = (time.clock() - start)
            arcpy.AddMessage("Execution time: " + str(elapsed / 60)[:4] +
                             " minutes.")
            current = current + 1
            print arcpy.GetMessage(0)
    except Exception as e:
        arcpy.AddMessage("Reconfiguration attempt failed.")
        arcpy.AddError(e.message)
Example #2
0
    def create_multiband_images(self):
        arcpy.env.pyramid = "PYRAMIDS"
        arcpy.env.rasterStatistics = "STATISTICS"

        arcpy.env.workspace = self.output_TIFF_path
        rasList = arcpy.ListRasters("*I1*")
        if not os.path.exists(self.composite_path):
            os.mkdir(self.composite_path)
        for ras in rasList:
            composite_filename = join(
                self.composite_path, ras[:22] + self.instrument + "_" +
                str(self.numBands) + "Bands.tif")
            if not os.path.exists(composite_filename):
                rasters = ras[:
                              22] + 'I1.rev.' + self.projection + '.tif;' + ras[:
                                                                                22] + 'I2.rev.' + self.projection + '.tif;' + ras[:
                                                                                                                                  22] + 'I3.rev.' + self.projection + '.tif;' + ras[:
                                                                                                                                                                                    22] + 'I4.rev.' + self.projection + '.tif'
                print('Creating Composite Band Raster from: ' + rasters)
                arcpy.CompositeBands_management(rasters, composite_filename)
                for n in range(self.numBands):
                    print('Deleting ' + ras[:22] + 'I' + str(n + 1) + '.rev.' +
                          self.projection + '.tif')
                    arcpy.Delete_management(ras[:22] + 'I' + str(n + 1) +
                                            '.rev.' + self.projection + '.tif')
Example #3
0
def create_composite_raster(landsat_folder, bands_string, output_file):
    # Set the workspace
    arcpy.env.workspace = landsat_folder

    # Get the list of raster files in folder
    rasters = arcpy.ListRasters('*.TIF')
    if len(rasters) == 0:
        return

    # Get the list of file endings that match the user bands list
    bands_string = re.sub(r'[\n\t\s]*', '', bands_string)
    check_bands = ['B{0}.TIF'.format(i) for i in bands_string.split(',')]

    # Filter the raster files that match the user bands list
    rasters = [a for a in rasters if a[-6:] in check_bands]

    # Composite bands and save resulting raster to disk
    output_raster_name = os.path.join(
        landsat_folder, output_file or (rasters[1][0:-7] + '_composite.tif'))
    composite_raster = arcpy.CompositeBands_management(rasters,
                                                       output_raster_name)
    arcpy.AddMessage('Successfully created ' + output_raster_name + '.')

    # Add resulting raster to display
    arcpy.AddMessage('Adding layer to map document')
    band_layer = arcpy.mapping.Layer(output_raster_name)
    arcpy.mapping.AddLayer(dataFrame, band_layer, "TOP")

    return composite_raster
Example #4
0
def addNDVI(inputTif, workDir, outName):

    arcpy.CheckOutExtension('spatial')

    print "\nCalculating NDVI and stacking to band 5"
    red = arcpy.sa.Raster(os.path.join(inputTif, "Band_1"))
    NIR = arcpy.sa.Raster(os.path.join(inputTif, "Band_4"))

    numerator = arcpy.sa.Float(NIR - red)
    denominator = arcpy.sa.Float(NIR + red)
    NDVI = arcpy.sa.Divide(numerator, denominator)
    NDVI_times = arcpy.sa.Times(NDVI, 100)
    NDVI_add = arcpy.sa.Plus(NDVI_times, 100)
    NDVI_int = arcpy.sa.Int(NDVI_add)

    vtab = arcpy.CreateObject("ValueTable")
    vtab.addRow(os.path.join(inputTif, "Band_1"))
    vtab.addRow(os.path.join(inputTif, "Band_2"))
    vtab.addRow(os.path.join(inputTif, "Band_3"))
    vtab.addRow(os.path.join(inputTif, "Band_4"))
    vtab.addRow(NDVI_int)
    arcpy.CompositeBands_management(
        vtab, os.path.join(workDir, outName + '_RGBNIR_NDVI.tif'))

    arcpy.CheckInExtension('spatial')

    return os.path.join(workDir, outName + '_RGBNIR_NDVI.tif')
Example #5
0
def create_response_raster(classifier, rasters, output, messages):
    messages.AddMessage("Creating response raster...")
    scratch_files = []

    try:
        messages.AddMessage("Debug: Checkpoint 1")
        scratch_multi_rasters = arcpy.CreateScratchName("temp", workspace=arcpy.env.scratchWorkspace)
        scratch_files.append(scratch_multi_rasters)
        arcpy.CompositeBands_management(rasters, scratch_multi_rasters)

        raster = arcpy.Raster(scratch_multi_rasters)

        lower_left_corner = arcpy.Point(raster.extent.XMin, raster.extent.YMin)
        x_cell_size = raster.meanCellWidth
        y_cell_size = raster.meanCellHeight
        messages.AddMessage("Debug: Checkpoint 2")
        try:
            raster_array = arcpy.RasterToNumPyArray(scratch_multi_rasters, nodata_to_value=np.NaN)
            messages.AddMessage("Debug: Checkpoint 3")
        except ValueError:
            messages.AddMessage("Integer type raster, changed to float")
            raster_array = 1.0 * arcpy.RasterToNumPyArray(scratch_multi_rasters, nodata_to_value=sys.maxint)
            raster_array[raster_array == sys.maxint] = np.NaN
            messages.AddMessage("Debug: Checkpoint 4")

        n_regr = raster_array.shape[0]
        n_rows = raster_array.shape[1]
        n_cols = raster_array.shape[2]

        raster_array2 = np.empty([n_rows, n_cols, n_regr])
        for raster_index in xrange(n_regr):
            raster_array2[:, :, raster_index] = raster_array[raster_index, :, :]

        raster_array2 = np.reshape(raster_array2, [n_rows * n_cols, n_regr])

        finite_mask = np.all(np.isfinite(raster_array2), axis=1)
        nan_mask = np.logical_not(finite_mask)
        messages.AddMessage("Debug: Checkpoint 5")
        responses = classifier.predict_proba(raster_array2[finite_mask])[:, classifier.classes_ == 1]

        response_vector = np.empty(n_rows * n_cols)
        response_vector[finite_mask] = responses
        response_vector[nan_mask] = -9
        response_array = np.reshape(response_vector, [n_rows, n_cols])
        messages.AddMessage("Debug: Checkpoint 6")
        response_raster = arcpy.NumPyArrayToRaster(response_array, lower_left_corner=lower_left_corner,
                                                   x_cell_size=x_cell_size, y_cell_size=y_cell_size, value_to_nodata=-9)
        response_raster.save(output)
        messages.AddMessage("Raster file created in " + output)
        arcpy.DefineProjection_management(output, arcpy.Describe(scratch_multi_rasters).spatialReference)

    finally:
        messages.AddMessage("Debug: Checkpoint 7")
        for s_f in scratch_files:
            arcpy.Delete_management(s_f)

        return
Example #6
0
def StackRaster(lstRastIn, strPathOut):
    """ arcpy.Composite bands and rename bands to match input rasters. """
    arcpy.CompositeBands_management(';'.join(lstRastIn), strPathOut)

    for i, rast in enumerate(lstRastIn):
        strBNin = 'Band_' + str(i + 1)
        strBNout = os.path.splitext(os.path.basename(rast))[0]
        arcpy.Rename_management(strPathOut + os.sep + strBNin,
                                strPathOut + os.sep + strBNout)
Example #7
0
def pansharpen(folder):
    # init
    arcpy.env.workspace = folder

    nir_band, r_band, g_band, b_band, p_band = None, None, None, None, None

    # find relevant file
    for eachfile in os.listdir(folder):
        if eachfile.endswith('B5.TIF'):
            nir_band = eachfile
        elif eachfile.endswith('B4.TIF'):
            r_band = eachfile
        elif eachfile.endswith('B3.TIF'):
            g_band = eachfile
        elif eachfile.endswith('B2.TIF'):
            b_band = eachfile
        elif eachfile.endswith('B8.TIF'):
            p_band = eachfile
        else:
            pass

    # make sure all bands are available
    if nir_band and r_band and g_band and b_band and p_band:
        temp_composite = r'in_memory\temp_composite'  # in_memory space

        # composite bands first
        try:
            arcpy.CompositeBands_management(
                ';'.join([r_band, g_band, b_band, nir_band]), temp_composite)
        except Exception as e:
            print(e)
            return None

        # pansharpen
        try:
            # construct new text
            out_tif = os.path.splitext(nir_band)[0].split(
                '_')[0] + '_' + 'PAN432' + '.tif'

            # create out put
            arcpy.CreatePansharpenedRasterDataset_management(
                temp_composite, "1", "2", "3", "4", out_tif, p_band,
                "Gram-Schmidt", "0.42", "0.51", "0.07", "0", "Landsat 8")

        except Exception as e:
            print(e)
            return None

        # clean up
        arcpy.Delete_management(temp_composite)

        return out_tif

    else:
        print('Error: some bands are missing, exit')
        return None
    def mergeImages(self):  # This is where images are combined using ArcPy
        global fileListNorms
        global fileListRGB
        global wrkspace

        arcpy.env.workspace = wrkspace

        if len(fileListNorms) == len(fileListRGB):
            for RGBitem, NormItem in zip(fileListRGB, fileListNorms):
                fnamex = RGBitem.split('/')[-1].split('.')[0] + '_stacked.tif'
                arcpy.CompositeBands_management([RGBitem, NormItem], fnamex)
def stack_bands(path, meta, data_type):
    '''Stack Landsat bands 1 - 11 within a directory

        @param output: path of output reflectance.
        @ptype output: c{str}
    '''

    print "\nStacking bands to create a composite raster"
    ap.AddMessage("\nStacking bands to create a composite raster")
    msg = str(datetime.now()
              ) + '\t' + "Stacking bands to create a composite raster" + "\n"
    redis.rpush(config.MESSAGES_KEY, msg)
    redis.publish(config.CHANNEL_NAME, msg)

    ap.env.workspace = path

    ap.CheckOutExtension("Spatial")
    rasters = ap.ListRasters()

    if (data_type == "LANDSAT_8"):
        print "LANDSAT_8"
        msg = str(datetime.now()) + '\t' + "LANDSAT_8" + "\n"
        redis.rpush(config.MESSAGES_KEY, msg)
        redis.publish(config.CHANNEL_NAME, msg)
        rgb_rasters = [
            rgb for rgb in rasters
            if band_nmbr(rgb) >= 2 and band_nmbr(rgb) <= 5
        ]

    else:
        rgb_rasters = [
            rgb for rgb in rasters
            if band_nmbr(rgb) >= 1 and band_nmbr(rgb) <= 4
        ]

    print "\nRGB Bands:"
    print " " + str(rgb_rasters)

    out_stack = str(
        meta['L1_METADATA_FILE']['LANDSAT_SCENE_ID']) + 'STACK_RGB.img'

    ap.AddMessage("Stacking R, G, B, and NIR bands")
    msg = str(datetime.now()) + '\t' + "Stacking R, G, B, and NIR bands" + "\n"
    redis.rpush(config.MESSAGES_KEY, msg)
    redis.publish(config.CHANNEL_NAME, msg)

    ap.CompositeBands_management(rgb_rasters, out_stack)

    print "\nComposite Stack Complete!"
    ap.AddMessage("\nComposite Stack Complete")
    msg = str(datetime.now()) + '\t' + "Composite Stack Complete" + "\n"
    redis.rpush(config.MESSAGES_KEY, msg)
    redis.publish(config.CHANNEL_NAME, msg)
Example #10
0
def trunkToFourLayerTif(inputTif, workDir, outName):

    print "\nTruncating alpha band"
    vtab = arcpy.CreateObject("ValueTable")
    vtab.addRow(os.path.join(inputTif, "Band_1"))
    vtab.addRow(os.path.join(inputTif, "Band_2"))
    vtab.addRow(os.path.join(inputTif, "Band_3"))
    vtab.addRow(os.path.join(inputTif, "Band_4"))
    arcpy.CompositeBands_management(vtab, os.path.join(workDir,
                                                       'fourComp.tif'))
    arcpy.Delete_management(inputTif)
    arcpy.Rename_management(os.path.join(workDir, 'fourComp.tif'), outName)
Example #11
0
def TaveDaily(rasterObj, rasterObj2, output, DateDecodeObj, DateDecodeObj2):
    inputList = []

    listRas = rasterObj.filteredDateList
    listRas.sort()
    for i in range(len(listRas)):
        raster1 = DateDecodeObj.ws + '\\' + listRas[i]
        raster2 = DateDecodeObj2.ws + '\\' + listRas[i]

        outraster = (Raster(raster1) + Raster(raster2)) / 2
        inputList.append(outraster)

    arcpy.CompositeBands_management(inputList, output)
 def CreateMultibandImages(self):
     arcpy.env.workspace = self.output_TIFF_path
     rasList = arcpy.ListRasters("*I1*")
     instrument = "IRAC"
     numBands = 4
     os.mkdir(self.output_TIFF_path + "\\" + "COMPOSITES" + "\\")
     for ras in rasList:
         composite_filename = self.output_TIFF_path + "\\" + "COMPOSITES" + "\\" + ras[:22] + instrument + "_" + str(
             numBands) + "Bands.tif"
         if not os.path.exists(composite_filename):
             rasters = ras[:
                           22] + 'I1.rev.GALACTIC.tif;' + ras[:
                                                              22] + 'I2.rev.GALACTIC.tif;' + ras[:
                                                                                                 22] + 'I3.rev.GALACTIC.tif;' + ras[:
                                                                                                                                    22] + 'I4.rev.GALACTIC.tif'
             print(rasters)
             arcpy.CompositeBands_management(rasters, composite_filename)
Example #13
0
   def ConvertFITStoTIFF(self):
       for galaxy in self.galaxy_list:
           composite_filename = self.path_to_files + '\\' + "COMPOSITES" + "\\" + galaxy + "_" + self.instrument + "_" + str(self.numBands) + "Bands.tif"
           if not os.path.exists(composite_filename):    
               for num in range(1,self.numBands+1):
                   filename            = galaxy + "_v7.phot." + str(num)+ ".fits"
                   output_filename     = filename[:-4] + 'rev.tif'
                   geo_output_filename = filename[:-4] + 'rev.WCS.tif'
                   outputfile_and_path = self.output_tiff_path + output_filename
                   geo_file_and_path   = self.output_tiff_path + geo_output_filename
                   file_and_path       = self.input_fits_path + filename
                   print(outputfile_and_path)
                   print(geo_file_and_path)
                   hdulist = fits.open(file_and_path)
                   #print(hdulist.info())
                   w = wcs.WCS(hdulist[0].header)
                   image = hdulist[0].data
                   
                   # Some pixel coordinates of interest.
                   pixcrd = numpy.array([[0, 0], [0, image.shape[0]],[image.shape[1], 0], [image.shape[1], image.shape[0]]], numpy.float_)
                   pixcrd_string = '0 0;' + '0 ' + str(image.shape[0]) + ';' +  str(image.shape[1]) + ' 0;' + str(image.shape[1]) + ' ' + str(image.shape[0])

                   # Convert pixel coordinates to world coordinates
                   # The second argument is "origin" -- in this case we're declaring we
                   # have 1-based (Fortran-like) coordinates.
                   world = w.wcs_pix2world(pixcrd, 1)
                   coordinate_string = str(-1*world[0,0]) + ' ' + str(world[0,1]) + ';' + str(-1*world[1,0]) + ' ' + str(world[1,1]) + ';' + str(-1*world[2,0]) + ' ' + str(world[2,1]) + ';' + str(-1*world[3,0]) + ' ' + str(world[3,1]) 
                   # Convert the same coordinates back to pixel coordinates.
                   pixcrd2 = w.wcs_world2pix(world, 1)
                   
                   # These should be the same as the original pixel coordinates, modulo
                   # some floating-point error.
                   assert numpy.max(numpy.abs(pixcrd - pixcrd2)) < 1e-6
                   flipim= numpy.flipud(image)
                   double_image = numpy.float64(flipim)
                   myRaster = arcpy.NumPyArrayToRaster(double_image)
                   myRaster.save(outputfile_and_path)
                   source_pnt = pixcrd_string
                   target_pnt = coordinate_string
                   arcpy.Warp_management(outputfile_and_path, source_pnt, target_pnt, geo_file_and_path, "POLYORDER1","BILINEAR")
                   arcpy.Delete_management(outputfile_and_path)
           arcpy.env.workspace = output_path
           rasters = arcpy.ListRasters(galaxy + "*", "TIF")
           print(rasters)
           arcpy.CompositeBands_management(rasters,composite_filename)
       print("Done Converting FITS to TIFF.")
Example #14
0
    def raster_to_grid(input_table):
        input_grid_name_list = []

        # Loop through the list of inputs
        for i in range(0, input_table.rowCount):
            input_grid_name_list.append(input_table.getRow(i))

        #rasterlist= map(lambda x:Con(IsNull(x),-999,x),input_grid_name_list)
        rasterlist = map(lambda x: x.replace("'", ""), input_grid_name_list)

        compgrid = arcpy.env.workspace + "\\" + "tempcomp"

        #COMBINE NODES INTO A SINGLE MULTIBAND IMAGE
        arcpy.CompositeBands_management(rasterlist, compgrid)
        arcpy.AddMessage("\nMultiband Image located in : " + compgrid)

        return compgrid
Example #15
0
def stack(rootdir):
    """
    Grabs desired layers to stack, creates a .txt file to document which layers
    are represented by correspoinding band list.

    Assumes directory structure of rootdir\refID\sensor\output...
    """
    refIDs = []
    for subdir in os.listdir (rootdir):
        refIDs.append(rootdir + "\\" + subdir)
    for refID in refIDs:
        sensors =  ["\\Mavic", "\\Sequoia"]
        for sensor in sensors:
            ## Creating an open string to serve as the string of files to use
            ## for the first argument in the CompositeBands_management tool
            layers = ""
            ## Creating a .txt file that lists bands for reference
            with open(refID + sensor + "\\Output" + sensor + "_lyrs.txt", "w+") as lyrfile:
                for root, dirs, files in os.walk(refID + sensor + "\\Output"):
                    for dir in dirs:
                        ## Directory exclusion
                        if "tiles"in dirs:
                            dirs.remove("tiles")
                        if "reflectance" in dirs:
                            dirs.remove("reflectance")
                        if "dtm" in dirs:
                            dirs.remove("dtm")
                        if sensor == "\\Sequoia" and "2_mosaic" in dirs:
                            dirs.remove("2_mosaic")
                    for file in files:
                        ## 'and not file.endswith("view.tif")' added 20180504 due to a dsm_preview.tif in new p4d version
                        if file.endswith(".tif") and not file.endswith("view.tif"):
                            ## Writing band to the file containing list of layers
                            lyrfile.write(file + "\n")
                            ## Writing band to the string of layers
                            layers += (os.path.join(root, file + "; "))
            #lyrfile.close()
            arcpy.CompositeBands_management(str(layers), str(refID + sensor + "\\Output\\Stack.tif"))
            print (refID + sensor + " Stacked")
def makeCIR(inWS, outWS):
	try:
		makePath(outWS,"temp")
		arcpy.env.workspace = inWS
		rasters = arcpy.ListRasters("","")
		rastCount = len(rasters)
		arcpy.AddMessage("Converting " + str(rastCount) + " images to CIR.")
		i = 1
		for raster in rasters:
			arcpy.AddMessage("Reconfiguring file " + str(i) + " of " + str(rastCount) + ": " + raster)
			start = time.clock()
			redIn = raster + "\Band_1"
			red = os.path.join(outWS, "temp", raster[:-4] + "_red.tif")
			arcpy.CopyRaster_management(redIn,red)
			greenIn = raster + "\Band_2"
			green = os.path.join(outWS, "temp", raster[:-4] + "_green.tif")
			arcpy.CopyRaster_management(greenIn,green)
			irIn = raster + "\Band_3"
			ir = os.path.join(outWS, "temp", raster[:-4] + "_ir.tif")
			arcpy.CopyRaster_management(irIn,ir)
			
			outRaster = raster[:-4] + ".tif"
			arcpy.CompositeBands_management('"' + ir + ";" + red + ";" + green + '"', os.path.join(outWS, os.path.basename(raster[:-4]+".tif")))
			
			os.remove(os.path.join(outWS,raster[:-4] + ".tfw"))
			os.remove(os.path.join(outWS,raster[:-4] + ".tif.aux.xml"))
			os.remove(os.path.join(outWS,raster[:-4] + ".tif.xml"))
			
			arcpy.AddMessage("Successfully reconfigured: " + raster + " to " + outWS)
			elapsed = (time.clock() - start)
			arcpy.AddMessage("Execution time: " + str(elapsed / 60)[:4] + " minutes.")
			i += 1
			print arcpy.GetMessage(0)
	
	except Exception as e:
		arcpy.AddMessage("Reconfiguration attempt failed.")
		arcpy.AddError(e.message)
Example #17
0
def stack_bands(path, meta):
    '''Stack Landsat bands 1 - 11 within a directory

        @param output: path of output reflectance.
        @ptype output: c{str}
    '''

    print "\nStacking bands to create a composite raster"
    ap.AddMessage("\nStacking bands to create a composite raster")

    ap.env.workspace = path
    rasters = ap.ListRasters()
    rgb_rasters = [
        rgb for rgb in rasters if band_nmbr(rgb) >= 2 and band_nmbr(rgb) <= 5
    ]
    out_stack = str(
        meta['L1_METADATA_FILE']['LANDSAT_SCENE_ID']) + 'STACK_RGB.img'
    print "\nRGB Bands:"
    print " " + str(rgb_rasters)
    ap.AddMessage("Stacking R, G, B, and NIR bands")

    ap.CompositeBands_management(rgb_rasters, out_stack)
    print "\nComposite Stack Complete!"
    ap.AddMessage("Composite Stack Complete")
def landsatComposite(resultsWorkspace):
    print("\n\nCommencing Composite Operation...".upper())
    reflectanceFolder = os.path.join(resultsWorkspace, "SurfaceReflectance")
    CompositeFolder = os.path.join(resultsWorkspace, "Composites")
    os.makedirs(CompositeFolder, exist_ok=True)
    print("Composite Save  Directory Created\n\n")
    for path, dirs, files in os.walk(reflectanceFolder):
        for directory in dirs:
            compWorkspace = os.path.join(path, directory)
            print(compWorkspace)
            try:
                arcpy.env.workspace = compWorkspace
                arcpy.env.overwriteOutput = True
                compFile = ((os.path.split(compWorkspace))[-1].split("_"))
                compFileName = "Scene" + compFile[2] + "_" + compFile[3] + "_composite.tif"
                print(compFileName)
                outFileSave = os.path.join(CompositeFolder, compFileName)
                RefCorrBands = arcpy.ListRasters(raster_type="TIF")
                print("Running Band Composite...")
                arcpy.CompositeBands_management(in_rasters=RefCorrBands, out_raster=outFileSave)
                print("Composite Completed\n")
            except IOError:
                print("Error Accessing File")
    print("\n\nAll Operations Complete".upper())
Example #19
0
                    print "Converting band 3 of imagery"

                    #Calculate reflectance for band 3
                    outRasB3 = (math.pi * (Raster(rasBand3) * (10**-3)) *
                                (esDis * esDis)) / (
                                    EsunB3 * (math.sin(solElev *
                                                       (math.pi / 180))))

                    #Save the converted band 3 raster
                    #outRasB3.save("K:\\general\\cmzarzar\\CIR_UAS_Imagery\\correctedImagery\\singleBandRasters\\"+tifName+"_B3.tif")
                    outRasB3.save(tempDir + tifName + "_B3.tif")

                    #Combine the rasters back into a multiband raster
                    #arcpy.CompositeBands_management("K:\\general\\cmzarzar\\CIR_UAS_Imagery\\correctedImagery\\singleBandRasters\\"+tifName+"_B1.tif;K:\\general\\cmzarzar\\CIR_UAS_Imagery\\correctedImagery\\singleBandRasters\\"+tifName+"_B2.tif;K:\\general\\cmzarzar\\CIR_UAS_Imagery\\correctedImagery\\singleBandRasters\\"+tifName+"_B3.tif","K:\\general\\cmzarzar\\CIR_UAS_Imagery\\correctedImagery\\"+tifName+".tif")
                    arcpy.CompositeBands_management(
                        "" + tempDir + tifName + "_B1.tif;" + tempDir +
                        tifName + "_B2.tif;" + tempDir + tifName + "_B3.tif",
                        "" + outDir + tifName + ".tif")
                    print "Correction complete for image %s" % inRas

                except:
                    if "ERROR 000725" in arcpy.GetMessages(2):
                        print "%s exists and overwrite turned off." % (
                            os.path.basename(fname))
                    else:
                        arcpy.GetMessages(1)
                        arcpy.GetMessages(2)

print "Program complete"
#END
Example #20
0
training_folder = os.path.join(support_folder, 'training')
os.mkdir(training_folder)
normalized_training_data = {}
for key, filepath in training_data.items():
    ras = arcpy.Raster(filepath)
    norm_raster = (ras - ras.minimum) / (ras.maximum - ras.minimum) * 100
    norm_name = r'norm' + key
    norm_path = os.path.join(training_folder, norm_name + r'.tif')
    norm_raster.save(norm_path)
    normalized_training_data[norm_name] = norm_path

# stick it in a multiband raster for training
composite_input = ';'.join(normalized_training_data.values())
composite_output = os.path.join(training_folder, 'trainingset.tif')
arcpy.CompositeBands_management(composite_input, composite_output)

arcpy.AddMessage("Training model")
progress += 1

svm = os.path.join(training_folder, 'svm.ecd')
arcpy.gp.TrainSupportVectorMachineClassifier(composite_output, training_file,
                                             svm)

arcpy.AddMessage("Classifying")
progress += 1

classified = arcpy.sa.ClassifyRaster(composite_output, svm)

arcpy.AddMessage("Cleaning classifications")
progress += 1
Example #21
0
        Landsat_name = "_".join(fname.split('_')[:-3])
                
        # Create oupput composite folder
        Composite_Band_folder = Output_folder + '\CompositeBand'
        if not os.path.exists(Composite_Band_folder):
            os.makedirs(Composite_Band_folder)
        
        # Create oupput composite raster name
        out_comp = Composite_Band_folder + '\\' + Landsat_name + '_composite.tif'
        # Select the bands that need to be composited
        
        if name.endswith(('B1.TIF', 'B2.TIF', 'B3.TIF', 'B4.TIF', 'B5.TIF', 'B6.TIF', 'B7.TIF')):
            Composite_list.append(name)

    print Landsat_name + " is being processed."
    arcpy.CompositeBands_management(Composite_list, out_comp)
print("")


### Clip the composite landsat image to the Lake Lanier Watershed ###
# Create oupput Clip LLWatershed folder
Clip_LLWatershed_folder = Output_folder + '\LLWatershedClip'
if not os.path.exists(Clip_LLWatershed_folder):
    os.makedirs(Clip_LLWatershed_folder)
    
for comp_name in glob.glob(Composite_Band_folder + '\*.tif'):
    # Seperate the file path and the filename
    cpath, cname_wtif = os.path.split(comp_name)

    # Get the .tif out of the file name 
    Composite_name = cname_wtif.split('.')[0]
# Task 1 - Using Step_2_data.zip, select the first layer, and execute the tool "Composite Bands" on Winter_2013.
# you will need to composite all the band images (ending in Bn.tif (where n = band number), do not add the BQA.tif
# file. See Landsat 8 band reference - https://landsat.usgs.gov/what-are-best-spectral-bands-use-my-study

# Once the tool has successfully completed, go into the Geoprocessing "Results" window, right click on the
# Completed tool, select "Copy as Python Snippet" and paste the tool output below:

import arcpy

arcpy.CompositeBands_management(
    in_rasters=
    "D:/Python-Class/Class7/Winter_2013/LC08_L1TP_012031_20131212_20170307_01_T1_B1.tif;"
    "D:/Python-Class/Class7/Winter_2013/LC08_L1TP_012031_20131212_20170307_01_T1_B10.tif;"
    "D:/Python-Class/Class7/Winter_2013/LC08_L1TP_012031_20131212_20170307_01_T1_B11.tif;"
    "D:/Python-Class/Class7/Winter_2013/LC08_L1TP_012031_20131212_20170307_01_T1_B2.tif;"
    "D:/Python-Class/Class7/Winter_2013/LC08_L1TP_012031_20131212_20170307_01_T1_B3.tif;"
    "D:/Python-Class/Class7/Winter_2013/LC08_L1TP_012031_20131212_20170307_01_T1_B4.tif;"
    "D:/Python-Class/Class7/Winter_2013/LC08_L1TP_012031_20131212_20170307_01_T1_B5.tif;"
    "D:/Python-Class/Class7/Winter_2013/LC08_L1TP_012031_20131212_20170307_01_T1_B6.tif;"
    "D:/Python-Class/Class7/Winter_2013/LC08_L1TP_012031_20131212_20170307_01_T1_B7.tif;"
    "D:/Python-Class/Class7/Winter_2013/LC08_L1TP_012031_20131212_20170307_01_T1_B8.tif;"
    "D:/Python-Class/Class7/Winter_2013/LC08_L1TP_012031_20131212_20170307_01_T1_B9.tif",
    out_raster="D:/Python-Class/Class7/winter13")

# Task 2 -  Now it is relatively easy to copy this and change the file name in order to run it on the Winter_2014 data
# but we are not going to do that, instead, we can going to use Python to do this for us from a directory name. Below,
# complete the code that I have provided.

arcpy.env.workspace = "D:/Python-Class/Class7/Winter_2014/"
listRasters = arcpy.ListRasters("*", "TIF")
print listRasters
                print conta
                outfile = infile
                outfile = outfile.replace('U:\\imagens2\\RapidEye\\NORTE\\FUSO_20S\\', 'W:\\IMAGENS\\RapidEye\\Marta\\')
                outfile = os.path.dirname(os.path.dirname(outfile))+"\\"+os.path.basename(infile)
                outfile = outfile.replace('.tif\n', 'rgb.tif')
                print outfile
#Verifica se pasta da Ginf tem imagem processada
                if os.path.isfile(outfile):
                    print 'saida existe'
                else:
                    print 'Sem arquivo, criando imagem'               
#Extrai apenas as camadas da imagem referentes a bandas RGB
                    arcpy.env.workspace = 'D:\\Biondo\\gcad\\rapideye\\dados\\'
                    arcpy.MakeRasterLayer_management(infile, "layer4.tif", "#", "", "4")
                    arcpy.MakeRasterLayer_management(infile, "layer5.tif", "#", "", "5")
                    arcpy.MakeRasterLayer_management(infile, "layer2.tif", "#", "", "2")
                    print 'camadas extraidas'
#Compoe nova imagem e salva na estrutura criada com nome do arquivo original e um "rgb" no final
                    arcpy.CompositeBands_management("layer4.tif;layer5.tif;layer2.tif",outfile)
                    print 'arquivo criado'

sys.exit()

    





#-------------------------------------------------------------------------------
TWI_raster_masked = arcpy.sa.ExtractByMask(TWI_raster, mask)
TWI_out_str = "TWI"
TWI_raster_masked.save(arcpy.env.scratchWorkspace + "\\" + TWI_out_str)
master_list += [TWI_out_str]
print(TWI_out_str + ' has been created and saved')
"""____________CREATE MASTER RASTER____________"""

# Create the name of the master file which includes the resolution of the rasters
master_name_str = 'Master_' + str(new_res_value_float).replace(
    '.', '_') + 'm_dry_cover'

print(master_list)

# Add the scratch workspace string to the master list
master_list_scratch = []
for i in master_list:
    master_list_scratch += [arcpy.env.scratchWorkspace + "\\" + i]

# Combine all vegetation and topographic indices and the SMC validation dataset into 1 master raster file
arcpy.CompositeBands_management(master_list_scratch, master_name_str)
"""____________RENAME BANDS IN MASTER RASTER____________"""

n = 0
for name in master_list:
    n += 1
    arcpy.Rename_management(master_name_str + r"\Band_" + str(n),
                            master_name_str + r"\\" + name)

print("All bands in the master raster have been renamed")
print("Closing script....")
Example #25
0
#can combine all the 7 bands for the Landsat Image. You could emit band 6 though since that is a thermal band. 
import arcpy

#If does not run, change .TIF to .tif

# RS Composite Bands - bringing them in and combining them. 
source = r"D:\Fall Semester Senior Year\GIS Programming (GEOG 392)\Lab07"
band1 = arcpy.sa.Raster(source + "\LT05_L1TP_026039_20110819_20160831_01_T1_B1.TIF")
band2 = arcpy.sa.Raster(source + "\LT05_L1TP_026039_20110819_20160831_01_T1_B2.TIF")
band3 = arcpy.sa.Raster(source + "\LT05_L1TP_026039_20110819_20160831_01_T1_B3.TIF")
band4 = arcpy.sa.Raster(source + "\LT05_L1TP_026039_20110819_20160831_01_T1_B4.TIF")
band5 = arcpy.sa.Raster(source + "\LT05_L1TP_026039_20110819_20160831_01_T1_B5.TIF")
band7 = arcpy.sa.Raster(source + "\LT05_L1TP_026039_20110819_20160831_01_T1_B7.TIF")
composite = arcpy.CompositeBands_management([band1, band2, band3, band4, band5, band7], source + "\combined.TIF")

# Hillshade - creating the Hillshade
source = r"D:\Fall Semester Senior Year\GIS Programming (GEOG 392)\Lab07"
azimuth = 315
altitude = 45
shadows = "NO_SHADOWS"
z_factor = 1
arcpy.ddd.HillShade(source + r"\n30_w097_1arc_v3.TIF", source + r"\Hillshade.TIF", azimuth, altitude, shadows, z_factor)

# Slope - creating the Slope
source = r"D:\Fall Semester Senior Year\GIS Programming (GEOG 392)\Lab07"
output_measurement = "DEGREE"
z_factor = 1
method = "PLANAR"
z_unit = "METER"
arcpy.ddd.Slope(source + r"\n30_w097_1arc_v3.TIF", source + r"\Slope.TIF", output_measurement, z_factor) 
Example #26
0
del wd

#Activate Spatial Analyst extension
arcpy.CheckOutExtension("Spatial")

#Allowing outputs to be overwritten
arcpy.env.overwriteOutput = True

#Upload only layers of interest using Arcpy
for i in range(0, len(dates)):
    arcpy.MakeNetCDFRasterLayer_md("erdMBsstdmday_LonPM180_b102_bcb5_c347.nc",
                                   "sst", "longitude", "latitude", indT[i], "",
                                   dates[i])

#Create a composite raster with all bands previously identified
SST = arcpy.CompositeBands_management(indT, "SST_compMOI.TIF")

#Names of shapefiles containing limits for Machalilla and GSF Marine Reserves
AOIs = [r"LimitesRMCM.shp", r"LimitesRMGSF.shp"]
#Names for output files in the same order as shapefiles
outname = ["RMCM", "RMGSF"]


def ExtractVals(nc, aoi, out):
    i = 0
    for j in aoi:
        #Mask out raster with Marine Reserve limits and save it
        mask = arcpy.sa.ExtractByMask(nc, j)
        mask.save("SST_" + out[i] + ".TIF")
        #Create point features from masked out raster
        Pts = arcpy.RasterToPoint_conversion(mask, "SST_Pts" + out[i] + ".shp",
Example #27
0
    def construct_graph(self, graph_vis=False):
        """
        Convert matrices to weighted inverse digraph
        key = to_vertex
        values = list of tuples [(from_vertex, cost),...] with cost of getting from from_vertex to to_vertex
        """
        self.logger.info("Constructing graph...")
        for i, row in enumerate(self.h_mat):
            for j, val in enumerate(row):
                # check if val is nan
                if not np.isnan(val):
                    key = str(i) + ',' + str(j)
                    neighbors, octants = self.get_neighbors(i, j)

                    for neighbor_i, octant in zip(neighbors, octants):
                        neighbor_key = str(neighbor_i[0]) + ',' + str(
                            neighbor_i[1])
                        # check if neighbor index is within array
                        if (0 <= neighbor_i[0] < self.h_mat.shape[0]) and (
                                0 <= neighbor_i[1] < self.h_mat.shape[1]):
                            # check if neighbor is nan
                            if not np.isnan(self.h_mat[neighbor_i]):
                                # check if depth > threshold (at neighbor location)
                                if self.h_mat[neighbor_i] > self.h_thresh:
                                    # check velocity condition
                                    if self.analyze_v:
                                        mag_u_w = self.u_mat[
                                            i,
                                            j]  # magnitude of water velocity
                                        dir_u_w = self.va_mat[
                                            i, j]  # angle from north
                                    else:
                                        mag_u_w = 0
                                        dir_u_w = 0
                                    if self.check_velocity_condition(
                                            mag_u_w, dir_u_w, octant):
                                        cost = self.get_cost(key, neighbor_key)
                                        try:
                                            self.graph[key] = self.graph[
                                                key] + [neighbor_key]
                                        except KeyError:
                                            self.graph[key] = [neighbor_key]
                                        try:
                                            self.inv_graph[
                                                neighbor_key] = self.inv_graph[
                                                    neighbor_key] + [
                                                        (key, cost)
                                                    ]
                                        except KeyError:
                                            self.inv_graph[neighbor_key] = [
                                                (key, cost)
                                            ]

        if graph_vis:
            # outputs for graph visualization
            self.logger.info("Making rasters for graph visualization...")
            for key, neighbor_keys in self.graph.items():
                i1, j1 = list(map(int, key.split(",")))
                for n, neighbor_key in enumerate(neighbor_keys):
                    i2, j2 = list(map(int, neighbor_key.split(",")))
                    self.graph_mats[n, 1, i1,
                                    j1] = -(i2 - i1)  # increasing row =  down
                    self.graph_mats[n, 0, i1, j1] = j2 - j1
            q = os.path.basename(self.path2_h_ras).replace("h",
                                                           "").split("_")[0]
            graph_vis_dir = os.path.join(
                os.path.dirname(self.path2_target_ras), "graph_vis%s" % q)
            fGl.chk_dir(graph_vis_dir)
            for i, graph_mat in enumerate(self.graph_mats):
                ras_x = arcpy.NumPyArrayToRaster(graph_mat[0],
                                                 lower_left_corner=self.ref_pt,
                                                 x_cell_size=self.cell_size,
                                                 value_to_nodata=np.nan)
                ras_y = arcpy.NumPyArrayToRaster(graph_mat[1],
                                                 lower_left_corner=self.ref_pt,
                                                 x_cell_size=self.cell_size,
                                                 value_to_nodata=np.nan)
                ras = arcpy.CompositeBands_management(
                    [ras_x, ras_y],
                    os.path.join(graph_vis_dir, "graph_vis%i.tif" % (i + 1)))

            i_mat = np.zeros(self.h_mat.shape)
            j_mat = np.zeros(self.h_mat.shape)
            for i, row in enumerate(i_mat):
                for j in range(len(row)):
                    i_mat[i, j] = i
                    j_mat[i, j] = j
            ras_i = arcpy.NumPyArrayToRaster(i_mat,
                                             lower_left_corner=self.ref_pt,
                                             x_cell_size=self.cell_size,
                                             value_to_nodata=np.nan)
            ras_j = arcpy.NumPyArrayToRaster(j_mat,
                                             lower_left_corner=self.ref_pt,
                                             x_cell_size=self.cell_size,
                                             value_to_nodata=np.nan)
            ras_i.save(
                os.path.join(os.path.dirname(self.path2_target_ras),
                             "i_mat%s.tif" % q))
            ras_j.save(
                os.path.join(os.path.dirname(self.path2_target_ras),
                             "j_mat%s.tif" % q))

        self.logger.info("Merging target vertices...")
        # make copy so we can delete keys of original graph during iteration (not containing "end" key)
        graph_copy = {k: v for k, v in self.inv_graph.items()}
        self.inv_graph["end"] = []
        for v, neighbors in graph_copy.items():
            # update values
            self.inv_graph[v] = list(
                map(lambda x: ("end", x[1])
                    if x[0] in self.end else x, neighbors))
            # merge vertices
            if v in self.end:
                self.inv_graph["end"] += neighbors
                del self.inv_graph[v]
        del graph_copy
        # remove duplicate values
        for v, neighbors in self.inv_graph.items():
            num_vs = [neighbor[0] for neighbor in neighbors].count("end")
            if num_vs > 1:
                # keep one with least cost, remove other duplicates
                least_cost = min([x[1] for x in neighbors if x[0] == "end"])
                self.inv_graph[v] = [
                    x for x in self.inv_graph[v] if x[0] != "end"
                ] + [("end", least_cost)]
Example #28
0
    data = list(csv.reader(csvfile))

# set workspace for image output
env.workspace = "O:\\PRIV\\NERL_ORD_CYAN\\Sentinel2\\Images\\composited\\0day\\composited"

# perform composite for each image
for row in range(1,len(data)):
    print("Processing #" + str(row) + " at " + datetime.datetime.now().strftime("%Y-%m-%d %H:%M"))
    
    # assign get granule dir and set workspace there
    folder = data[row][1]
    granule_id = data[row][2]
    granule_dir = "D:\\s2\\raw\\" + folder + ".SAFE\\GRANULE\\" + granule_id + "\\IMG_DATA"
    #arcpy.env.workspace = granule_dir

    # assign band names; convert to string for input
    b2 = ''.join(glob.glob(granule_dir + "\\*B02.jp2"))
    b3 = ''.join(glob.glob(granule_dir + "\\*B03.jp2"))
    b4 = ''.join(glob.glob(granule_dir + "\\*B04.jp2"))
    
    bands_str = [b2, b3, b4]
    in_rasts = '; '.join(bands_str)

    # composite
    print("Compositing image #" + str(row))
    arcpy.CompositeBands_management(in_rasts, granule_id + "_" + str(row) + ".tif")

    print("Done with image #" + str(row))

print("All done!!!")
import arcpy
from datetime import datetime

start_timestamp = datetime.now()
arcpy.env.compression = "LZ77"
arcpy.env.outputCoordinateSystem = arcpy.SpatialReference(3057)
# path to folder that includes .SAFE folders
workspace = r"C:\temp"

RasterList = []

# look for folders and image bands and append to list of aquisitions
for dirpath, dirnames, filenames in arcpy.da.Walk(workspace,
                                                  datatype="RasterDataset"):
    for filename in filenames:
        if filename[-7:] == "B08.jp2":
            item = os.path.join(dirpath, filename)
            RasterList.append(item)

# for each item in the list
for raster in RasterList:
    band8 = raster
    band4 = raster[:-7] + "B04.jp2"
    band3 = raster[:-7] + "B03.jp2"
    BandCombination = band8 + ";" + band4 + ";" + band3
    OutputRasterName = raster[:-7] + "NIR_B843.tif"
    arcpy.CompositeBands_management(BandCombination, OutputRasterName)
    print "Done with " + OutputRasterName

print "done in: " + str(datetime.now() - start_timestamp)
Example #30
0
    double_image = numpy.float64(flipim)
    myRaster = arcpy.NumPyArrayToRaster(double_image)
    myRaster.save(temp_file_and_path)
    #Giving raster the sky coordinates
    source_pnt = pixcrd_string
    target_pnt = coordinate_string
    arcpy.Warp_management(temp_file_and_path, source_pnt, target_pnt,
                          output_file_and_path, "POLYORDER1", "BILINEAR")
    #Deleting the unreference image
    arcpy.Delete_management(temp_file_and_path)
    print(output_file_and_path)

#Composite bands
arcpy.env.workspace = output_image_path
rasters = arcpy.ListRasters(galaxy + "*", "TIF")
arcpy.CompositeBands_management(rasters, composite_path_and_name)

#Create the mosaic and add images to it
coordinate_sys = "PROJCS['WGS_1984_Web_Mercator_Auxiliary_Sphere',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Mercator_Auxiliary_Sphere'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',0.0],PARAMETER['Standard_Parallel_1',0.0],PARAMETER['Auxiliary_Sphere_Type',0.0],UNIT['Meter',1.0]];-20037700 -30241100 10000;-100000 10000;-100000 10000;0.001;0.001;0.001;IsHighPrecision"
imagery_spatial_ref = "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]];-400 -400 1000000000;-100000 10000;-100000 10000;8.98315284119522E-09;0.001;0.001;IsHighPrecision"
mosaic_gdb = r"C:\PROJECTS\R&D\ASTROARC\SINGS\Spitzer.gdb"
mosaic_name = "SINGS"
mosaic_dataset = os.path.join(mosaic_gdb, mosaic_name)
arcpy.CreateMosaicDataset_management(mosaic_gdb,
                                     mosaic_name,
                                     coordinate_sys,
                                     num_bands="",
                                     pixel_type="",
                                     product_definition="NONE",
                                     product_band_definitions="")
arcpy.AddRastersToMosaicDataset_management(