Exemplo n.º 1
0
def pan_sharpen(path, meta, data_type):
    '''Use the pan chromatic layer to pan-sharen the Blue, Green, Red, & NIR stack

        @param   stack: Input composite image stack
        @ptype   stack: c{string}
        @param   meta:  Metadat built from MTL.txt
        @ptype   meta:  Dictionary of landsat scene metadata
        @return  Output composite image with pan-sharpening
    '''
    ap.env.workspace = path
    ap.CheckOutExtension("Spatial")
    out_stack = str(
        meta['L1_METADATA_FILE']['LANDSAT_SCENE_ID']) + 'STACK_PANSHARP.img'

    rasters = ap.ListRasters()
    rgb = [img for img in rasters if 'STACK_RGB.img' in img]
    pan = [img for img in rasters if 'B8.img' in img]

    print rgb
    print pan
    print ""
    print "Begining Pan Sharpen"

    if (data_type == "Landsat8"):
        print "Landsat8"
        ap.CreatePansharpenedRasterDataset_management(rgb[0], "4", "3", "2",
                                                      "5", out_stack, pan[0],
                                                      "Brovey")
    else:
        ap.CreatePansharpenedRasterDataset_management(rgb[0], "3", "2", "1",
                                                      "4", out_stack, pan[0],
                                                      "Brovey")

    print ""
    print "Pan Sharpen Complete"
Exemplo n.º 2
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
Exemplo n.º 3
0
arcpy.env.workspace = cur_dir + '/data/'
arcpy.env.overwriteOutput = True
arcpy.env.scratchWorkspace = SW = cur_dir + '/temp/'

for row in arcpy.da.SearchCursor(aoi_poly, ['Name', 'SHAPE@']):
    if row[0] == "Dillon":
        arcpy.env.extent = row[1].extent

ls_MS_C = arcpy.CopyRaster_management(in_raster=ls_MS, out_rasterdataset="#")

ls_pan_C = arcpy.CopyRaster_management(in_raster=ls_pan, out_rasterdataset="#")

ls_MS_C_P = arcpy.CreatePansharpenedRasterDataset_management(
    in_raster=ls_MS_C,
    red_channel=3,
    green_channel=2,
    blue_channel=1,
    infrared_channel=4,
    in_panchromatic_image=ls_pan_C,
)
print(ls_MS_C_P)

sigs = arcpy.sa.CreateSignatures(
    in_raster_bands=ls_MS_C_P,
    in_sample_data=sigs_shp,
    sample_field='Classvalue',
    out_signature_file="#"
)

# classed_rast = arcpy.ia.MLClassify(arcpy.sa.Raster(ls_MS_C_P), sigs)
# result = classed_rast.save('CLASSED.tif')
#
          ('tri', 'ms_tri.tif', 'pan_tri.tif', 5, 3, 2, 7, 0.3937, 0.03117,
           0.4153, 0.1598),
          ('was', 'ms_was.tif', 'pan_was.tif', 3, 2, 1, 4, 0.3601, 0.09414,
           0.3964, 0.1494)]

# Compute Weights (only required for Esri)

##for s in scenes:
##    arcpy.ComputePansharpenWeights_management('{}'.format(s[1]),'{}'.format(s[2]),str('{}'.format(s[3]) + " " + '{}'.format(s[4]) + " " + '{}'.format(s[5]) + " " + '{}'.format(s[6])))

# Create pansharpened rasters using nested for loop
for s in scenes:
    for a in algorithms:
        arcpy.CreatePansharpenedRasterDataset_management(
            '{}'.format(s[1]),  # in_raster
            '{}'.format(s[3]),  # red_channel
            '{}'.format(s[4]),  # green_channel
            '{}'.format(s[5]),  # blue_channel
            '{}'.format(s[6]),  # infrared_channel
            arcpy.env.workspace + '\Processed\\' + '{}'.format(a[0]) + '_' +
            '{}'.format(s[0]) + '.tif',  # out_raster_dataset
            '{}'.format(s[2]),  # in_panchromatic_image
            '{}'.format(a[0]),  # pansharpening_type
            '{}'.format(s[7]),  # red_weight
            '{}'.format(s[8]),  # green_weight
            '{}'.format(s[9]),  # blue_weight
            '{}'.format(s[10]),  # infrared_weight
            '{}'.format(a[1]))  # sensor

    # Calculate NDVI, in separate script, <ndvi.py>
# Compute Weights (only required for Esri)

##for s in scenes:
##    arcpy.ComputePansharpenWeights_management('{}'.format(s[1]),'{}'.format(s[2]),"4 3 2 5")

# Create pansharpened rasters using nested for loop
for s in scenes:
    for a in algorithms:
        arcpy.CreatePansharpenedRasterDataset_management(
            '{}'.format(s[1]),  # in_raster
            4,
            3,
            2,
            5,  # r, g, b, nir channels
            arcpy.env.workspace + '\Processed\\' + '{}'.format(a[0]) + '_' +
            '{}'.format(s[0]) + '.tif'  # out_raster_dataset
            ,
            '{}'.format(s[2])  # in_panchromatic_image
            ,
            '{}'.format(a[0])  # pansharpening_type
            ,
            '{}'.format(s[3])  # red_weight
            ,
            '{}'.format(s[4])  # green_weight
            ,
            '{}'.format(s[5])  # blue_weight
            ,
            '{}'.format(s[6])  # infrared_weight
            ,
            '{}'.format(a[1]))  # sensor