def test_Extent_mutateRaster(): ex = Extent.fromVector(AACHEN_SHAPE_PATH).castTo(4326).fit(0.001) # test a simple clip r = ex.mutateRaster(CLC_RASTER_PATH, output=result("extent_mutateRaster1.tif")) mat = raster.extractMatrix(r) assert np.isclose(mat.mean(), 17.14654805) # test a clip and warp r = ex.mutateRaster(CLC_RASTER_PATH, pixelHeight=0.001, pixelWidth=0.001, matchContext=True, output=result("extent_mutateRaster2.tif"), resampleAlg='near') mat2 = raster.extractMatrix(r) assert np.isclose(mat2.mean(), 17.14768769) # simple processor @util.KernelProcessor(1, edgeValue=-9999) def max_3x3(mat): goodValues = mat[mat != -9999].flatten() return goodValues.max() r = ex.mutateRaster(CLC_RASTER_PATH, processor=max_3x3, output=result("extent_mutateRaster3.tif")) mat3 = raster.extractMatrix(r) assert np.isclose(mat3.mean(), 19.27040301)
def test_Extent_warp(): ex = Extent.fromVector(AACHEN_SHAPE_PATH).castTo(3035).fit(200) # Change resolution to disk d = ex.warp(CLC_RASTER_PATH, pixelHeight=200, pixelWidth=200, output=result("extent_warp1.tif")) v1 = raster.extractMatrix(d) assert np.isclose(v1.mean(), 17.18144279) # change resolution to memory d = ex.warp(CLC_RASTER_PATH, pixelHeight=200, pixelWidth=200) v2 = raster.extractMatrix(d) assert np.isclose(v1, v2).all() # Do a cutline from disk d = ex.warp(CLC_RASTER_PATH, pixelHeight=100, pixelWidth=100, cutline=AACHEN_SHAPE_PATH, output=result("extent_warp3.tif"), noData=99) v3 = raster.extractMatrix(d) assert np.isclose(v3.mean(), 66.02815723) assert np.isclose(v3[0, 0], 99)
def test_Extent_rasterize(): ex = Extent.fromVector(AACHEN_SHAPE_PATH).castTo(3035).fit(250) # Simple vectorization to file r = ex.rasterize(source=AACHEN_ZONES, pixelWidth=250, pixelHeight=250, output=result("extent_rasterized1.tif")) mat1 = raster.extractMatrix(r) assert np.isclose(mat1.mean(), 0.22881653) # Simple vectorization to mem r = ex.rasterize( source=AACHEN_ZONES, pixelWidth=250, pixelHeight=250, ) mat2 = raster.extractMatrix(r) assert np.isclose(mat2, mat1).all() # Write attribute values to disc r = ex.rasterize(source=AACHEN_ZONES, value="YEAR", pixelWidth=250, pixelHeight=250, output=result("extent_rasterized3.tif"), noData=-1) mat = raster.extractMatrix(r, autocorrect=True) assert np.isclose(np.isnan(mat).sum(), 22025) assert np.isclose(np.nanmean(mat), 1996.36771232)
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
def test_rasterize(): # Simple vectorization to file r = vector.rasterize(source=AACHEN_ZONES, pixelWidth=250, pixelHeight=250, output=result("rasterized1.tif")) mat1 = raster.extractMatrix(r) assert np.isclose(mat1.mean(), 0.13910192) # Simple vectorization to mem r = vector.rasterize( source=AACHEN_ZONES, pixelWidth=250, pixelHeight=250, ) mat2 = raster.extractMatrix(r) assert np.isclose(mat2, mat1).all() # Change srs to disc r = vector.rasterize(source=AACHEN_ZONES, srs=4326, pixelWidth=0.01, pixelHeight=0.01, output=result("rasterized2.tif")) mat = raster.extractMatrix(r) assert np.isclose(mat.mean(), 0.12660478) # Write attribute values to disc r = vector.rasterize(source=AACHEN_ZONES, value="YEAR", pixelWidth=250, pixelHeight=250, output=result("rasterized3.tif"), noData=-1) mat = raster.extractMatrix(r, autocorrect=True) assert np.isclose(np.isnan(mat).sum(), 48831) assert np.isclose(np.nanmean(mat), 1995.84283904) # Write attribute values to mem, and use where clause r = vector.rasterize(source=AACHEN_ZONES, value="YEAR", pixelWidth=250, pixelHeight=250, noData=-1, where="YEAR>2000") mat = raster.extractMatrix(r, autocorrect=True) assert np.isclose(np.isnan(mat).sum(), 53706) assert np.isclose(np.nanmean(mat), 2004.96384743)
def test_Extent_mosiacTiles(): ext = Extent.fromVector(_test_data_['aachenShapefile.shp']) ras = ext.tileMosaic( join(_test_data_['prior_tiles'], "osm_roads_minor.{z}.{x}.{y}.tif"), 9, ) rasmat = raster.extractMatrix(ras) assert np.isclose(np.nanmean(rasmat), 568.8451589061345) assert np.isclose(np.nanstd(rasmat), 672.636988117134)
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!
def test_gradient(): # create a sloping surface dataset x, y = np.meshgrid(np.abs(np.arange(-100, 100)), np.abs(np.arange(-150, 150))) arr = np.ones((300, 200)) + 0.01 * y + x * 0.03 slopingDS = raster.createRaster(bounds=(0, 0, 200, 300), pixelWidth=1.0, pixelHeight=1.0, data=arr, srs=None) # do tests total = raster.gradient(slopingDS, mode="total", asMatrix=True) assert np.isclose(total.mean(), 0.0312809506031) # total - mean assert np.isclose(total[10, 10], 0.0316227766017) # total - nw quartile assert np.isclose(total[200, 10], 0.0316227766017) # total - sw quartile assert np.isclose(total[10, 150], 0.0316227766017) # total - ne quartile assert np.isclose(total[200, 150], 0.0316227766017) # total - se quartile ns = raster.gradient(slopingDS, mode="north-south", asMatrix=True) assert np.isclose(ns.mean(), -3.33333333333e-05) # north-south - mean assert np.isclose(ns[10, 10], -0.01) # north-south - nw quartile assert np.isclose(ns[200, 10], 0.01) # north-south - sw quartile assert np.isclose(ns[10, 150], -0.01) # north-south - ne quartile assert np.isclose(ns[200, 150], 0.01) # north-south - se quartile ew = raster.gradient(slopingDS, mode="east-west", asMatrix=True) assert np.isclose(ew.mean(), 0.00015) # east-west - mean assert np.isclose(ew[10, 10], 0.03) # east-west - nw quartile assert np.isclose(ew[200, 10], 0.03) # east-west - sw quartile assert np.isclose(ew[10, 150], -0.03) # east-west - ne quartile assert np.isclose(ew[200, 150], -0.03) # east-west - se quartile aspect = raster.gradient(slopingDS, mode="dir", asMatrix=True) assert np.isclose(aspect.mean(), 0.0101786336761) # aspect - mean assert np.isclose(180 * aspect[10, 10] / np.pi, -18.4349488229) # aspect - nw quartile assert np.isclose(180 * aspect[200, 10] / np.pi, 18.4349488229) # aspect - sw quartile assert np.isclose(180 * aspect[10, 150] / np.pi, -161.565051177) # aspect - ne quartile assert np.isclose(180 * aspect[200, 150] / np.pi, 161.565051177) # aspect - se quartile # calculate elevation slope output = result("slope_calculation.tif") slopeDS = raster.gradient(ELEVATION_PATH, factor='latlonToM', output=output, overwrite=True) slopeMat = raster.extractMatrix(output) assert np.isclose(slopeMat.mean(), 0.0663805622803) # elevation slope
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()
def test_Extent_createRaster(): ex = Extent.fromRaster(CLC_RASTER_PATH) # Test failure on bad resolution # 200m does not fit to the CLC raster by design try: r = ex.createRaster(pixelHeight=200, pixelWidth=200, fill=2) assert False except error.GeoKitExtentError: assert True else: assert False # Test successful r = ex.createRaster(pixelHeight=100, pixelWidth=100, fill=2) assert np.isclose(raster.extractMatrix(r).mean(), 2)
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
def test_warp(): # Change resolution to disk d = raster.warp(CLC_RASTER_PATH, pixelHeight=200, pixelWidth=200, output=result("warp1.tif")) v1 = raster.extractMatrix(d) assert np.isclose(v1.mean(), 16.3141463057) # change resolution to memory d = raster.warp(CLC_RASTER_PATH, pixelHeight=200, pixelWidth=200) v2 = raster.extractMatrix(d) assert np.isclose(v1, v2).all() # Do a cutline from disk d = raster.warp(CLC_RASTER_PATH, cutline=AACHEN_SHAPE_PATH, output=result("warp3.tif"), noData=99) v3 = raster.extractMatrix(d) assert np.isclose(v3.mean(), 89.9568135904) assert np.isclose(v3[0, 0], 99) # Do a cutline from memory d = raster.warp(CLC_RASTER_PATH, cutline=geom.box(*AACHEN_SHAPE_EXTENT_3035, srs=EPSG3035), noData=99) v4 = raster.extractMatrix(d) assert np.isclose(v4[0, 0], 99) assert np.isclose(v4.mean(), 76.72702479) # Do a flipped-source check d = raster.warp(CLC_FLIPCHECK_PATH, cutline=geom.box(*AACHEN_SHAPE_EXTENT_3035, srs=EPSG3035), noData=99) v5 = raster.extractMatrix(d) assert np.isclose(v4, v5).all() d = raster.warp(CLC_FLIPCHECK_PATH, pixelHeight=200, pixelWidth=200, output=result("warp6.tif")) v6 = raster.extractMatrix(d) assert np.isclose(v1, v6).all()