示例#1
0
def run(oldpath, newpath):
    """
    Runs the 16th basic test suite. Tests:

    creating a raster with interpolation using the 'new' non-spatial mode
    """
    inputSPD = os.path.join(oldpath, INPUT_SPD)
    outputDEM = os.path.join(newpath, OUTPUT_DEM)

    data = spatial.readLidarPoints(inputSPD, 
            classification=lidarprocessor.CLASSIFICATION_GROUND)

    (xMin, yMax, ncols, nrows) = spatial.getGridInfoFromData(data['X'], data['Y'],
                BINSIZE)

    pxlCoords = spatial.getBlockCoordArrays(xMin, yMax, ncols, nrows, BINSIZE)

    dem = interpolation.interpGrid(data['X'], data['Y'], data['Z'], pxlCoords, 
                    method='pynn') 

    iw = spatial.ImageWriter(outputDEM, tlx=xMin, tly=yMax, binSize=BINSIZE)
    iw.setLayer(dem)
    iw.close()

    utils.compareImageFiles(os.path.join(oldpath, OUTPUT_DEM), outputDEM)
示例#2
0
def interpGroundReturns(data):
    # if given a list of fields, returns a structured array with all of them
    ptVals = data.input.getPoints(colNames=['X', 'Y', 'Z', 'CLASSIFICATION'])
    # create mask for ground
    mask = ptVals['CLASSIFICATION'] == lidarprocessor.CLASSIFICATION_GROUND

    # get the coords for this block
    pxlCoords = data.info.getBlockCoordArrays()

    if ptVals.shape[0] > 0:
        # there is data for this block
        xVals = ptVals['X'][mask]
        yVals = ptVals['Y'][mask]
        zVals = ptVals['Z'][mask]
        # 'pynn' needs the pynnterp module installed
        out = interpolation.interpGrid(xVals,
                                       yVals,
                                       zVals,
                                       pxlCoords,
                                       method='pynn')

        # mask out where interpolation failed
        invalid = numpy.isnan(out)
        out[invalid] = 0
    else:
        # no data - set to zero
        out = numpy.empty(pxlCoords[0].shape, dtype=numpy.float64)
        out.fill(0)

    out = numpy.expand_dims(out, axis=0)
    data.imageOut.setData(out)
示例#3
0
def doInterp(minfilteredimage, outinterpimage, controlPtArray):

    inImage = gdal.Open(minfilteredimage)
    nCols = inImage.RasterXSize
    nRows = inImage.RasterYSize
    geotransform = inImage.GetGeoTransform()
    resolution = geotransform[1]
    xmin = numpy.floor(geotransform[0])
    xmax = numpy.ceil((xmin + (nCols * resolution)) + resolution)
    ymax = numpy.ceil(geotransform[3])
    ymin = numpy.floor((ymax - (nRows * resolution)) - resolution)

    print(xmin, xmax, ymin, ymax, resolution)
    print(geotransform)

    proj = inImage.GetProjection()
    nulVal = inImage.GetRasterBand(1).GetNoDataValue()

    z = ((inImage.GetRasterBand(1)).ReadAsArray())
    x = ((inImage.GetRasterBand(2)).ReadAsArray())
    y = ((inImage.GetRasterBand(3)).ReadAsArray())

    filteredOutMask = (z == nulVal)

    mz = numpy.ma.array(z, mask=filteredOutMask).compressed()
    mx = numpy.ma.array(x, mask=filteredOutMask).compressed()
    my = numpy.ma.array(y, mask=filteredOutMask).compressed()

    # Generate a regular grid to interpolate the data.
    xi = numpy.linspace(xmin, xmax - resolution, nCols)
    yi = numpy.linspace(ymin, ymax - resolution, nRows)
    xi, yi = numpy.meshgrid(xi, yi)

    #        ## Interpolate the values of z for all points in the rectangular grid
    #        ## Method 1 - Interpolate by matplotlib delaunay triangularizatio and nearest neigh. PLEASE NOTE! THIS FAILS QUITE OFTEN (http://matplotlib.org/api/mlab_api.html#matplotlib.mlab.griddata) But there might be a solution - install mpl_toolkits.natgrid (http://matplotlib.org/mpl_toolkits/)
    #        #zi = ml.griddata(mx,my,mz,xi,yi,interp='nn') #interpolation is 'nn' by default (natural neighbour based on delaunay triangulation) but 'linear' is faster (see http://matplotlib.1069221.n5.nabble.com/speeding-up-griddata-td20906.html)
    #surfaceImg = numpy.flipud(zi)

    #use the pylidar natural neighbour interpolation routine

    surfaceImg = interpolation.interpGrid(mx, my, mz, (xi, yi), 'pynn')
    surfaceImg = numpy.flipud(surfaceImg)
    #elif cmdargs.interp == 'mann':
    #zi = ml.griddata(mx,my,mz,xi,yi,interp='nn') #interpolation is 'nn' by default (natural neighbour based on delaunay triangulation) but 'linear' is faster (see http://matplotlib.1069221.n5.nabble.com/speeding-up-griddata-td20906.html)
    #surfaceImg = numpy.flipud(zi)

    saveImage(outinterpimage, surfaceImg, xmin, ymax, resolution, proj, nulVal)

    xyAtControl = controlPtArray[:, :2]
    zAtControl = controlPtArray[:, 2]
    interpzAtControl = pynninterp.NaturalNeighbourPts(mx.astype(numpy.float64),
                                                      my.astype(numpy.float64),
                                                      mz.astype(numpy.float64),
                                                      xyAtControl)
    zDiff = zAtControl - interpzAtControl
    outf = open('controlCompare_noMed_20151125.csv', 'w')
    for i in range(len(zAtControl)):
        outf.write("{},{},{}\n".format(interpzAtControl[i], zAtControl[i],
                                       zDiff[i]))
    outf.close()