コード例 #1
0
def build_multiband_image(rootDIR, outName, newfoldername, find, drivercode, ndvalue):

    outdir = create_output_dir(rootDIR, newfoldername)
    print "\nOutputting files to : {0}".format(outdir)

    print "\nFinding HDF files in directory/subfolders: {0}".format(rootDIR)
    hdfs = find_files(rootDIR, ".hdf")
    print "\tFound {0} files.".format(len(hdfs))

    print "\nGetting images to process of type {0}...".format(find)
    toprocess = []

    for hdf in hdfs:
        sds = get_hdf_subdatasets(hdf)
        for ds in sds:
            if find.upper() in ds[1].upper():
                toprocess.append(ds[0])
                print "\t\t{0}".format(ds[0])

    bands = len(toprocess)
    print "\tFound {0} images of type {1}.".format(bands, find)

    print "\nGetting output parameters..."
    rows, cols, datatype, geotransform, projection = open_image(toprocess[0])
    print "\tParameters: rows: {0}, cols: {1}, datatype: {2}, projection: {3}.".format(rows, cols, datatype, projection)

    outfile = os.path.join(outdir, outName) + ".tif"
    print "\nOutput file is: {0}".format(outfile)

    outds = create_output_raster(outfile, cols, rows, bands, datatype, drivername=drivercode)
    print "\tCreated output file."

    print"\nAdding bands to output file..."
    for i in range(0, bands):
        print "\tProcessing band {0} of {1}...".format(i + 1, bands)
        image = gdal.Open(toprocess[i])
        band = image.GetRasterBand(1)

        outband = outds.GetRasterBand(i + 1)

        print "\t\tReading band data to array..."
        data = band.ReadAsArray(0, 0, cols, rows)

        print "\t\tWriting band data to output band..."
        outband.WriteArray(data, 0, 0)
        outband.SetNoDataValue(ndvalue)
        outband.FlushCache()

        del data, outband
        image = ""

    print "\tFinished adding bands to output file."

    print "\nSetting transform and projection..."
    outds.SetGeoTransform(geotransform)
    outds.SetProjection(projection)

    outDS = ""

    print "\nProcess completed."
コード例 #2
0
def get_incorrect_px_curves(inputpath, referencepath, vitimeseriespath, startdoy, interval, outputpath=None, nodata=None):

    if not outputpath:
        root = os.path.dirname(inputpath)
        outputpath = create_output_dir(root, "plots")

    #Open images
    inputimage = build_multiband_image.open_image(inputpath, returnimage=True)
    referenceimage = build_multiband_image.open_image(referencepath, returnimage=True)
    timeseries = build_multiband_image.open_image(vitimeseriespath, returnimage=True)

    #Get band 1 of input and reference images and read as arrays
    inband = inputimage[5].GetRasterBand(1)
    inarray = inband.ReadAsArray(0, 0, inputimage[0], inputimage[1])
    inband = ""

    refband = referenceimage[5].GetRasterBand(1)
    refarray = refband.ReadAsArray(0, 0, referenceimage[0], referenceimage[1])
    refband = ""

    #Close in and ref images

    #Find unique values in the input image
    uniquevals = set(np.unique(inarray).tolist())
    uniquevals.remove(nodata)
    print uniquevals

    #Iterate through pixels in images, comparing arrays for each value in input image, making note of tested values and excluding them from comparison with 0 value in input image
    #Return pixel coordinates and the identified and true identities of the pixels from the input and reference images
    coordinatelist = {}
    for value in uniquevals:
        coordinatelist[value] = []

    numincorrect = 0

    for col in range(0, inputimage[0]):
        for row in range(0, inputimage[1]):

            if (inarray[row, col] != 0) and (inarray[row, col] != refarray[row, col]) and (inarray[row, col] != nodata):

                if refarray[row, col] in uniquevals:
                    coordinatelist[refarray[row, col]].append([col, row, inarray[row, col], refarray[row, col]])
                    numincorrect += 1
                else:
                    coordinatelist[0].append([col, row, inarray[row, col], refarray[row, col]])
                    numincorrect += 1

            elif (inarray[row,col] == 0) and (refarray[row, col] in uniquevals):
                coordinatelist[refarray[row, col]].append([col, row, inarray[row, col], refarray[row, col]])
                numincorrect += 1

    #For each returned pixel, extract and plot the curve from vitimeseries and export plot as image; be sure to label plot correctly
    bandcnt = timeseries[5].RasterCount
    arrays = {}
    for i in range(1, bandcnt + 1):
        band = timeseries[5].GetRasterBand(i)
        data = band.ReadAsArray(0, 0, timeseries[0], timeseries[1])
        arrays[i] = data

    print("Found {0} incorrect pixels to process...".format(numincorrect))

    plotvalues = []
    i = 0
    for group, pixels in coordinatelist.items():
        for pixel in pixels:
            pxvalues = {}
            for key, val in arrays.items():
                doy = interval * (key - 1) + startdoy

                if doy >= 366:
                    t = floor((doy - 366)/interval)
                    doy = interval * t + 366

                pxvalues[doy] = val[pixel[1], pixel[0]]
            color = color_picker(i)
            pixel.append(pxvalues)
            pixel.append(color)
            plotvalues.append(pixel)
        i += 1

    pdf = PdfPages(os.path.join(outputpath, "plots.pdf"))
    for pixel in plotvalues:
        figure = plot_pixel(pixel, outputpath, color=pixel[5])
        pdf.savefig()
        plt.close(figure)
    pdf.close()