示例#1
0
def test_mutateRaster():
    # Setup
    def isOdd(mat):
        return np.mod(mat, 2)

    source = gdal.Open(CLC_RASTER_PATH)
    sourceInfo = raster.rasterInfo(source)

    # Process Raster with no processor or extent
    # , overwrite=True, output=result("algorithms_mutateRaster_1.tif"))
    res1 = raster.mutateRaster(source, processor=None)

    info1 = raster.rasterInfo(res1)
    assert info1.srs.IsSame(sourceInfo.srs)  # srs
    assert info1.bounds == sourceInfo.bounds  # bounds

    # mutateRaster with a simple processor
    output2 = result("algorithms_mutateRaster_2.tif")
    raster.mutateRaster(source,
                        processor=isOdd,
                        overwrite=True,
                        output=output2)
    res2 = gdal.Open(output2)

    info2 = raster.rasterInfo(res2)
    assert info2.srs.IsSame(sourceInfo.srs)  # srs
    assert np.isclose(info2.xMin, sourceInfo.xMin)  # bounds
    assert np.isclose(info2.xMax, sourceInfo.xMax)  # bounds
    assert np.isclose(info2.yMin, sourceInfo.yMin)  # bounds
    assert np.isclose(info2.yMax, sourceInfo.yMax)  # bounds

    band2 = res2.GetRasterBand(1)
    arr2 = band2.ReadAsArray()

    assert (arr2.sum() == 156515)  # data

    # Process Raster with a simple processor (flip check)
    output2f = output = result("algorithms_mutateRaster_2f.tif")
    raster.mutateRaster(CLC_FLIPCHECK_PATH,
                        processor=isOdd,
                        overwrite=True,
                        output=output2f)
    res2f = gdal.Open(output2f)

    info2f = raster.rasterInfo(res2f)
    assert info2f.srs.IsSame(sourceInfo.srs)  # srs
    assert np.isclose(info2f.xMin, sourceInfo.xMin)  # bounds
    assert np.isclose(info2f.xMax, sourceInfo.xMax)  # bounds
    assert np.isclose(info2f.yMin, sourceInfo.yMin)  # bounds
    assert np.isclose(info2f.yMax, sourceInfo.yMax)  # bounds

    arr2f = raster.extractMatrix(res2f)

    assert (arr2f.sum() == 156515)  # data

    # Check flipped data
    assert (arr2f == arr2).all()  # flipping error!
示例#2
0
def test_indexToCoord():
    rasterSource = gdal.Open(CLC_RASTER_PATH)

    # Test single index
    xy = raster.indexToCoord(xi=10, yi=5, source=rasterSource)
    assert np.isclose(xy, np.array([[4013150.0, 3110450.0]])).all()

    # Test multiple indexes
    xy = raster.indexToCoord(xi=np.array([10, 11, 22, 5]),
                             yi=np.array([5, 5, 3, 5]),
                             source=rasterSource)
    assert np.isclose(
        xy,
        np.array([[4013150., 3110450.], [4013250., 3110450.],
                  [4014350., 3110650.], [4012650., 3110450.]])).all()

    # Test multiple indexes, with a flipped source
    rasterSource = gdal.Open(CLC_FLIPCHECK_PATH)
    YS = raster.rasterInfo(CLC_FLIPCHECK_PATH).yWinSize - 1

    xy_flipped = raster.indexToCoord(
        xi=np.array([10, 11, 22, 5]),
        yi=np.array([YS - 5, YS - 5, YS - 3, YS - 5]),
        source=rasterSource,
    )
    assert np.isclose(xy_flipped, xy).all()
示例#3
0
def test_extractMatrix():
    # source, bounds=None, boundsSRS='latlon', maskBand=False, autocorrect=False
    ri = raster.rasterInfo(CLC_RASTER_PATH)

    # Do a full read
    mat1 = raster.extractMatrix(CLC_RASTER_PATH)
    assert np.isclose(10650913, mat1.sum())  # full read values
    assert np.isclose(7.93459728918, mat1.std())  # full read values

    # Read within a boundary
    mat2 = raster.extractMatrix(CLC_RASTER_PATH,
                                bounds=(4015000.0, 3032000.0, 4020000.0,
                                        3040000.0),
                                boundsSRS=3035)
    assert np.isclose(mat1[710:790, 29:79], mat2).all()  # extract bounds

    # Read with conversion
    mat3, bounds = raster.extractMatrix(CLC_RASTER_PATH,
                                        bounds=(6, 50.5, 6.5, 50.75),
                                        boundsSRS=4326,
                                        returnBounds=True)
    assert bounds == (4037300.0, 3049100.0, 4074100.0, 3078600.0)
    assert np.isclose(mat3.sum(), 2280501)
    assert np.isclose(mat3.std(), 7.40666185608)

    # Test flipped raster
    mat4, bounds = raster.extractMatrix(CLC_FLIPCHECK_PATH,
                                        bounds=(6, 50.5, 6.5, 50.75),
                                        boundsSRS=4326,
                                        returnBounds=True)
    assert np.isclose(mat4, mat3).all()  # flipped raster
示例#4
0
def test_createRaster():
    ######################
    # run and check funcs

    # mem creation
    inputBounds = (10.0, 30.0, 15.0, 40.0)
    inputPixelHeight = 0.02
    inputPixelWidth = 0.01
    inputSRS = 'latlon'
    inputDataType = 'Float32'
    inputNoData = -9999
    inputFillValue = 12.34

    memRas = raster.createRaster(bounds=inputBounds,
                                 pixelHeight=inputPixelHeight,
                                 pixelWidth=inputPixelWidth,
                                 srs=inputSRS,
                                 dtype=inputDataType,
                                 noData=inputNoData,
                                 fillValue=inputFillValue)

    assert not memRas is None  # creating raster in memory

    mri = raster.rasterInfo(memRas)
    assert mri.bounds == inputBounds  # bounds
    assert mri.dx == inputPixelWidth  # pixel width
    assert mri.dy == inputPixelHeight  # pixel height
    assert mri.noData == inputNoData  # no data
    assert mri.srs.IsSame(EPSG4326)  # srs

    # Disk creation
    data = (np.ones((1000, 500)) * np.arange(500)).astype("float32")
    outputFileName = result("util_raster1.tif")

    raster.createRaster(bounds=(10, 30, 15, 40),
                        output=outputFileName,
                        pixelHeight=0.01,
                        pixelWidth=0.01,
                        compress=True,
                        srs=EPSG4326,
                        noDataValue=100,
                        data=data,
                        overwrite=True,
                        meta=dict(bob="bob", TIM="TIMMY"))

    ds = gdal.Open(outputFileName)
    bd = ds.GetRasterBand(1)
    srs = osr.SpatialReference()
    srs.ImportFromWkt(ds.GetProjection())
    assert srs.IsSame(EPSG4326)  # disk raster, srs mismatch

    arr = bd.ReadAsArray()
    assert not (arr.sum() != data.sum())  # disk rsater, data mismatch")

    meta = ds.GetMetadata_Dict()
    assert meta["bob"] == "bob"  # dist raster, data mismatch
    assert meta["TIM"] == "TIMMY"  # dist raster, data mismatch
示例#5
0
def test_contours():
    geoms = raster.contours(AACHEN_ELIGIBILITY_RASTER, contourEdges=[0.5])

    ri = raster.rasterInfo(AACHEN_ELIGIBILITY_RASTER)

    assert geoms.shape[0] == 114  # geom count
    assert np.isclose(geoms.geom[59].Area(), 0.022376976699986426)
    assert np.isclose(geoms.ID[59], 1)
    assert geoms.geom[59].GetSpatialReference().IsSame(ri.srs)
示例#6
0
def test_rasterInfo():
    info = raster.rasterInfo(CLC_RASTER_PATH)

    assert (info.xMin, info.yMin, info.xMax,
            info.yMax) == (4012100.0, 3031800.0, 4094600.0, 3111000.0
                           )  # min/max values
    assert (info.dx == 100 and info.dy == 100)  # dx/dy
    assert (info.bounds == (4012100.0, 3031800.0, 4094600.0, 3111000.0)
            )  # bounds
    assert (info.dtype == gdal.GDT_Byte)  # datatype
    assert (info.srs.IsSame(EPSG3035))  # srs
    assert (info.noData == 0)  # noData
    assert (info.flipY == True)  # flipY
示例#7
0
def test_Extent_clipRaster():
    ex = Extent.fromVector(AACHEN_SHAPE_PATH)

    # test a simple clip
    r = ex.clipRaster(AACHEN_URBAN_LC)
    mat = raster.extractMatrix(r)

    assert np.isclose(mat.mean(), 1.583447145588637)
    assert np.isclose(mat.std(), 0.5784475661496283)

    ri = raster.rasterInfo(r)
    assert ri.dx == 100
    assert ri.dy == 100
    assert ri.xMin == 4038300.0
    assert ri.yMin == 3048700.0
    assert ri.xMax == 4067000.0
    assert ri.yMax == 3101000.0
示例#8
0
def test_RegionMask_createRaster():

    rm = RegionMask.fromGeom(point(6.20, 50.75).Buffer(0.05),
                             srs=EPSG4326,
                             pixelRes=0.001)

    ## Create a raster like the mask
    ds = rm.createRaster()

    dsInfo = rasterInfo(ds)
    if not abs(dsInfo.xMin - 6.15) < 0.001: error("createRaster 1 - extent")
    if not abs(dsInfo.xMax - 6.25) < 0.001: error("createRaster 1 - extent")
    if not abs(dsInfo.yMin - 50.70) < 0.001: error("createRaster 1 - extent")
    if not abs(dsInfo.yMax - 50.80) < 0.001: error("createRaster 1 - extent")
    if not dsInfo.srs.IsSame(EPSG4326): error("createRaster 1 - srs")
    if not dsInfo.dtype == gdal.GDT_Byte: error("createRaster 1 - dtype")

    # Fill a raster with mask data
    out2 = result("rasterMast_createRaster_2.tif")
    rm.createRaster(output=out2, data=rm.mask, overwrite=True)

    ds = gdal.Open(out2)
    band = ds.GetRasterBand(1)
    if (band.ReadAsArray() - rm.mask).any():
        error("createRaster 2 - data mismatch")

    ############ The function is not meant for scaling down
    # # test Scaling down
    # scaledData = scaleMatrix(rm.mask,-4)
    # ds = rm.createRaster(resolutionDiv=1/4, data=scaledData, overwrite=True)

    # band = ds.GetRasterBand(1)
    # if (band.ReadAsArray()-scaledData).any(): error("createRaster 3 - data mismatch")

    # test Scaling up
    scaledData = scaleMatrix(rm.mask, 2)

    ds = rm.createRaster(resolutionDiv=2, data=scaledData, overwrite=True)
    band = ds.GetRasterBand(1)
    if (band.ReadAsArray() - scaledData).any():
        error("createRaster 4 - data mismatch")

    print("RegionMask_createRaster passed")
示例#9
0
def test_createRasterLike():
    source = gdal.Open(CLC_RASTER_PATH)
    sourceInfo = raster.rasterInfo(source)

    data = raster.extractMatrix(source)

    # From raster, no output
    newRaster = raster.createRasterLike(source, data=data * 2)
    newdata = raster.extractMatrix(newRaster)
    assert np.isclose(data, newdata / 2).all()

    # From raster, with output
    raster.createRasterLike(source,
                            data=data * 3,
                            output=result("createRasterLike_A.tif"))
    newdata = raster.extractMatrix(result("createRasterLike_A.tif"))
    assert np.isclose(data, newdata / 3).all()

    # From rasterInfo, no output
    newRaster = raster.createRasterLike(sourceInfo, data=data * 4)
    newdata = raster.extractMatrix(newRaster)
    assert np.isclose(data, newdata / 4).all()
示例#10
0
def test_RegionMask_createRaster():
    rm = RegionMask.fromGeom(geom.point(6.20, 50.75).Buffer(0.05),
                             srs=EPSG4326,
                             pixelRes=0.001)

    # Create a raster like the mask
    ds = rm.createRaster()

    dsInfo = raster.rasterInfo(ds)
    assert np.isclose(dsInfo.xMin, 6.15)
    assert np.isclose(dsInfo.xMax, 6.25)
    assert np.isclose(dsInfo.yMin, 50.70)
    assert np.isclose(dsInfo.yMax, 50.80)
    assert dsInfo.srs.IsSame(EPSG4326)
    assert dsInfo.dtype == gdal.GDT_Byte

    # Fill a raster with mask data
    out2 = result("rasterMast_createRaster_2.tif")
    rm.createRaster(output=out2, data=rm.mask, overwrite=True)

    ds = gdal.Open(out2)
    band = ds.GetRasterBand(1)
    assert np.isclose(band.ReadAsArray(), rm.mask).all()

    # The function is not meant for scaling down
    # # test Scaling down
    # scaledData = scaleMatrix(rm.mask,-4)
    # ds = rm.createRaster(resolutionDiv=1/4, data=scaledData, overwrite=True)

    # band = ds.GetRasterBand(1)
    # if (band.ReadAsArray()-scaledData).any(): error("createRaster 3 - data mismatch")

    # test Scaling up
    scaledData = util.scaleMatrix(rm.mask, 2)

    ds = rm.createRaster(resolutionDiv=2, data=scaledData, overwrite=True)
    band = ds.GetRasterBand(1)
    assert np.isclose(band.ReadAsArray(), scaledData).all()