示例#1
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
def get_reference_curves(image, refstoget, startdoy, imageinterval, outdir="", filepostfix=""):
    """

    """

    #TODO docstring

    if not outdir:
        outdir = os.path.dirname(image)

    for shapefile in refstoget:
        cropname = os.path.splitext(os.path.basename(shapefile))[0]
        locs = get_px_coords_from_shapefile(image, shapefile)
        refvals = get_crop_pixel_values(image, locs)
        comment = "Generated from {0} by get_crop_pixel_values version 0.1.".format(image)
        write_refs_to_txt(cropname, refvals, startdoy, imageinterval, outdir, comment=comment, postfix=filepostfix)
        write_mean_ref_to_txt(cropname, refvals, startdoy, imageinterval, outdir, comment=comment,
                              postfix=filepostfix)
示例#3
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)