def get_incorrect_pixels(inputpath, referencepath, vitimeseriespath, startdoy, interval, nodata=None):

    #Open images
    inputimage = openImage(inputpath)
    referenceimage = openImage(referencepath)
    timeseries = openImage(vitimeseriespath)

    #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(numpy.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))
Example #2
0
def plot_points(multidateraster, pointfile, startdoy, doyinterval):
    """

    """
    import os
    from utils import unique_name
    from plotting import PixelPlot
    from core import pixel as pixelObject
    from vectorFunctions import get_px_coords_from_shapefile
    from imageFunctions import openImage

    outpath = unique_name(os.path.dirname(multidateraster), "plots", ext=".pdf", usetime=True)

    coords = get_px_coords_from_shapefile(multidateraster, pointfile)

    plot = PixelPlot(os.path.dirname(outpath), os.path.basename(outpath))
    raster = openImage(multidateraster)

    for coord in coords:
        pixel = pixelObject(coord[0], coord[1])
        pixel.get_pixel_values(raster, startdoy, doyinterval)
        plot.add_pixel(pixel, closefigure=True)

    plot.close_plot()
    raster = None
Example #3
0
def get_px_coords_from_shapefile(raster, shapefile):
    """
    Takes geographic coordinates from a shapefile and finds the corresponding pixel coordinates on a raster.

    rst = "/Users/phoetrymaster/Documents/School/Geography/Thesis/Data/MODIS_KANSAS_2007-2012/reprojected/clips/KansasEVI_2012_clip1.tif"
    #rst = "/Users/phoetrymaster/Documents/School/Geography/Thesis/Data/polygonclip_20130929223024_325071991/resampled/newclips/2012clip1.tif"
    shp = "/Users/phoetrymaster/Documents/School/Geography/Thesis/Data/MODIS_KANSAS_2007-2012/SampleAreas/samplepoints2012_clip1_new.shp"

    print get_px_coords_from_shapefile(rst, shp)
    """
    #TODO docstrings

    from imageFunctions import openImage

    # open, close image file and get properties
    raster = openImage(raster)
    imageproperties = gdalProperties(raster)

    rasterwkt = raster.GetProjectionRef()

    oSRSop = osr.SpatialReference()
    oSRSop.ImportFromWkt(rasterwkt)
    raster = None

    shppoints = load_points(shapefile, oSRSop)

    # get pixel coords from point coords
    pxcoords = get_px_coords_from_geographic_coords(imageproperties, shppoints)

    return pxcoords
Example #4
0
def fit_refs_to_image(imagetoprocess, outputdirectory, signaturecollection, startDOY,
                               doyinterval, bestguess, threshold=None, ndvalue=-3000, fitmethod=None, subset=None,
                               meantype=None, workers=4, timebounds=None, xbounds=None, ybounds=None):
    """
    imagepath = "/Users/phoetrymaster/Documents/School/Geography/Thesis/Data/ARC_Testing/ClipTesting/ENVI_1/test_clip_envi_3.dat"
    outdir = "/Users/phoetrymaster/Documents/School/Geography/Thesis/Data/OutImages/"

    newfoldername = "Testing"

    drivercode1 = 'ENVI'
    #ndvalue = -3000

    startDOY = 1
    interval = 16
    threshold = 500
    bestguess = 0
    fitmthd = 'SLSQP'
    mean = geometric  # Acceptable values are geometric (geometric mean) and arithmetic (arithmetic mean). It is an optional argument for the classifier.


    refs = {
        'soy': {1: 174.5, 97: 1252.25, 65: 1139.5, 209: 7659.0, 273: 4606.75, 337: 1371.75, 17: 1055.5, 33: 1098.0,
                49: 1355.25,
                129: 1784.75, 257: 6418.0, 321: 1644.5, 305: 1472.75, 193: 5119.75, 289: 1878.75, 177: 3439.5, 241: 7565.75,
                81: 1205.5, 225: 7729.75, 145: 1736.25, 161: 1708.25, 353: 1358.25, 113: 1340.0},
        'corn': {1: 392.25, 97: 1433.25, 65: 1258.5, 209: 6530.0, 273: 1982.5, 337: 1658.5, 17: 1179.25, 33: 1196.75,
                 49: 1441.25, 129: 1885.25, 257: 2490.25, 321: 1665.75, 305: 1439.0, 193: 6728.25, 289: 1634.5,
                 177: 6356.75,
                 241: 4827.25, 81: 1355.75, 225: 5547.5, 145: 2196.5, 161: 3143.25, 353: 1704.75, 113: 1716.5},
        'wheat': {1: 719.75, 97: 6594.75, 65: 1935.25, 209: 2013.5, 273: 1493.5, 337: 1498.25, 17: 1816.5, 33: 1815.0,
                  49: 1985.25, 129: 6758.0, 257: 1685.75, 321: 1582.5, 305: 1163.25, 193: 2186.25, 289: 1264.5, 177: 2222.5,
                  241: 2301.0, 81: 4070.5, 225: 1858.0, 145: 6228.5, 161: 3296.5, 353: 1372.5, 113: 7035.25}
    }

    sys.exit(fit_refs_to_image(imagepath, outdir, newfoldername, refs, startDOY, interval, threshold, bestguess, fitmthd, meantype=mean))
    """
    #TODO docstrings

    start = dt.now()
    print(start)
    try:
        print("\nProcessing {0}...".format(imagetoprocess))
        print("Outputting files to {0}\n".format(outputdirectory))

        #Open multi-date image to analyze
        image = openImage(imagetoprocess)
        imageproperties = gdalProperties(image)

        print("Input image dimensions are {0} columns by {1} rows and contains {2} bands.".format(imageproperties.cols,
                                                                                                  imageproperties.rows,
                                                                                                  imageproperties.bands))

        array = read_image_into_array(image)  # Read all bands into a 3d array representing the image stack (x, y, time orientation)
        image = ""

        if timebounds:
            timebounds = (bestguess + timebounds[0], bestguess + timebounds[1])
        else:
            timebounds = (bestguess - 10, bestguess + 10)

        if not xbounds:
            xbounds = (0.6, 1.4)

        if not ybounds:
            ybounds = (0.6, 1.4)

        bounds = (xbounds, ybounds, timebounds)
        print(bounds)

        if subset:
            subset = get_px_coords_from_shapefile(imagetoprocess, subset)

        processes = []
        for signum, signature in enumerate(signaturecollection.signatures, start=1):
            p = multiprocessing.Process(target=process_reference,
                                        args=(outputdirectory, signature, array, imageproperties, startDOY, doyinterval,
                                              bestguess, ndvalue),
                                        kwargs={"subset": subset, "fitmthd": fitmethod, "meantype": meantype,
                                                "thresh": threshold, "bounds": bounds})

            #TODO: Problem with joining/starting processes--original thread closes before others are completed -- believe this is now fixed.

            p.start()
            processes.append(p)

            if len(processes) == workers:
                for p in processes:
                    p.join()
                    processes.remove(p)

        for p in processes:
                    p.join()

    except Exception as e:
        import traceback
        exc_type, exc_value, exc_traceback = sys.exc_info()
        print(e)
        traceback.print_exception(exc_type, exc_value, exc_traceback, limit=2, file=sys.stdout)

    finally:
        print(dt.now() - start)
Example #5
0
def classify_and_assess_accuracy(outputdir, cropimgpath, searchstringsvals, filevalist, nodata, thresholdsandlength,
                                 classifiedimagename=None, numberofprocesses=4):
    """
    """
    #TODO Docstring

    #from multiprocessing import Pool
    #pool = Pool(numberofprocesses)

    if classifiedimagename is None:
        today = dt.now()
        classifiedimagename = today.strftime("%Y-%m-%d_%H%M_") + os.path.splitext(os.path.basename(cropimgpath))[0]

    classificationimage = os.path.join(outputdir, classifiedimagename + ".tif")
    accuracyimage = os.path.join(outputdir, classifiedimagename + "_accuracy.tif")
    accuracyreport = os.path.join(outputdir, classifiedimagename + ".txt")

    #np.set_printoptions(threshold=np.nan)  # For debug: Makes numpy print whole contents of an array.
    #Crop image is constant for all iterations
    cropimg = openImage(cropimgpath)
    cropimgproperties = gdalProperties(cropimg)
    croparray = read_image_into_array(cropimg)
    cropimg = None

    arraylist = [(read_image_into_array(openImage(f[0])), f[1]) for f in filevalist]  # fit images with crop vals

    writestring = "The fit rasters and truth values used for this classification process are:\n"
    for f in filevalist:
        writestring = writestring + "\t{0}\t{1}\n".format(f[0], f[1])

    writestring = writestring + "\n"

    bestacc = 0
    bestthresh = None

    thresholdlist, lengthofthresholdlist = thresholdsandlength

    try:
        if lengthofthresholdlist == 1:
            writestring = writestring + "\n\n**Only using a single threshold value--not iterating.**\n\n"
            bestthresh = thresholdlist[0]
        else:

            #TODO: Refactor to allow use of multiprocessing.Pool.map -- need to reason about the output/logging
            for thresh in thresholdlist:
                start = dt.now()
                accuracy, classification, outstring = classify_with_threshold(croparray, arraylist, searchstringsvals,
                                                                              thresh, nodata, cropimgproperties.nodata)
                writestring = writestring + outstring

                if accuracy > bestacc:
                    bestacc = accuracy
                    bestthresh = thresh

                elapsed = dt.now() - start
                toprint = [thresh, "{}:{}".format(elapsed.seconds, str(elapsed.microseconds).zfill(6)), accuracy, bestacc, bestthresh]
                width = (6 * len(arraylist))
                sys.stdout.write("Thresh: {: <{width}}   Time: {}   Acc: {: <14}   Best: {: <14} at {}\r".format(*toprint, width=width))
                sys.stdout.flush()

    except Exception as e:
        import traceback
        exc_type, exc_value, exc_traceback = sys.exc_info()
        print e
        traceback.print_exception(exc_type, exc_value, exc_traceback, limit=2, file=sys.stdout)

    finally:
        accuracy, classificationarray, outstring = classify_with_threshold(croparray, arraylist, searchstringsvals,
                                                                           bestthresh, nodata, cropimgproperties.nodata)

        writestring = writestring + outstring

        accuracyarray = find_correct_incorrect_array(croparray, classificationarray, ndvalue=nodata,
                                                     truthndvalue=cropimgproperties.nodata)

        with open(accuracyreport, 'w') as text:
            text.write("Classification using fit images from {0}\n\n".format(os.path.dirname(filevalist[0][0])))
            text.write("{0}\nBest:\n{1} {2}".format(writestring, bestthresh, accuracy))

        print("\n{0}, {1}".format(bestthresh, accuracy))

        driver = gdal.GetDriverByName("ENVI")
        driver.Register()

        write_output_image(cropimgproperties, classificationimage, classificationarray, nodata)
        write_output_image(cropimgproperties, accuracyimage, accuracyarray, nodata)

        print("outputted")

        return 0