def test_Extent___eq__(): # setup ex1 = Extent(3, -4, -5, 10, srs=EPSG4326) ex2 = Extent.fromVector(source=MULTI_FTR_SHAPE_PATH) ex3 = Extent.fromVector(source=MULTI_FTR_SHAPE_PATH) # Equality assert (ex2 != ex1) assert (ex2 == ex2) assert (ex2 == ex3)
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_Extent_mutateVector(): ex = Extent.fromVector(AACHEN_SHAPE_PATH).castTo(4326) # Test simple clipping vi = ex.mutateVector(AACHEN_ZONES, matchContext=False, output=result("extent_mutateVector1.shp")) info = vector.vectorInfo(vi) assert np.isclose(info.count, 101) assert info.srs.IsSame(EPSG3035) # clip and convert vi = ex.mutateVector( AACHEN_ZONES, matchContext=True, ) info = vector.vectorInfo(vi) assert np.isclose(info.count, 101) assert info.srs.IsSame(EPSG4326) # Simple operation def docenter(x): return {'geom': x.geom.Centroid()} vi = ex.mutateVector(AACHEN_ZONES, matchContext=True, keepAttributes=False, processor=docenter, output=result("extent_mutateVector3.shp")) info = vector.vectorInfo(vi) assert np.isclose(len(info.attributes), 1) assert np.isclose(info.count, 101) assert info.srs.IsSame(EPSG4326)
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_inSourceExtent(): # setup ex2 = Extent.fromVector( source=MULTI_FTR_SHAPE_PATH ) ## Test if in source assert ( ex2.inSourceExtent(LUX_SHAPE_PATH) == True) assert ( ex2.inSourceExtent(AACHEN_SHAPE_PATH) == False)
def test_Extent_fromVector(): ex1 = Extent.fromVector(AACHEN_POINTS) assert np.isclose(ex1.xMin, 6.03745) assert np.isclose(ex1.xMax, 6.33091) assert np.isclose(ex1.yMin, 50.5367) assert np.isclose(ex1.yMax, 50.9227)
def test_Extent_tileBox(): ext = Extent.fromVector(_test_data_['aachenShapefile.shp']) ext_box = ext.tileBox(12) assert np.isclose(ext_box.xMin, 655523.954574) assert np.isclose(ext_box.xMax, 724011.531917) assert np.isclose(ext_box.yMin, 6525887.726875) assert np.isclose(ext_box.yMax, 6613943.183460) assert ext_box.srs.IsSame(srs.EPSG3857)
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_Extent_contoursFromRaster(): ext = Extent.fromVector(AACHEN_SHAPE_PATH) geoms = ext.contoursFromRaster(AACHEN_URBAN_LC, contourEdges=[1, 2, 3], transformGeoms=True) assert geoms.iloc[0].geom.GetSpatialReference().IsSame(ext.srs) assert len(geoms) == 95 assert np.isclose(geoms.iloc[61].geom.Area(), 0.08834775465377398) assert geoms.iloc[61].ID == 1
def test_Extent_fit(): # setup ex1 = Extent(3, -4, -5, 10, srs=EPSG4326) ex2 = Extent.fromVector(source=MULTI_FTR_SHAPE_PATH) # Fiting ex_fit1 = ex1.fit(2, float) assert np.isclose(ex_fit1.xyXY, (-6.0, -4.0, 4.0, 10.0)).all() ex_fit2 = ex2.fit(0.01) assert np.isclose(ex_fit2.xyXY, (6.21, 48.86, 7.79, 49.94)).all()
def test_Extent_filterSources(): sources = source("*.shp") ex = Extent.fromVector(source=AACHEN_SHAPE_PATH) goodSources = list(ex.filterSources(sources)) # Inclusion assert AACHEN_ZONES in goodSources assert AACHEN_POINTS in goodSources # Exclusion assert not BOXES in goodSources assert not LUX_SHAPE_PATH in goodSources assert not LUX_LINES_PATH in goodSources
def test_Extent_inSourceExtent(): # setup ex2 = Extent.fromVector(source=MULTI_FTR_SHAPE_PATH) # Test if in source assert (ex2.inSourceExtent(LUX_SHAPE_PATH) == True) assert (ex2.inSourceExtent(AACHEN_SHAPE_PATH) == False) # Overlapping, but not within eachother ext1 = Extent(0, 0, 3, 3, srs=4326) ext2 = Extent(-1, 1, 4, 2, srs=4326) vec = vector.createVector(ext2.box) assert (ext1.inSourceExtent(vec) == True)
def test_Extent___init__(): # basic ex1 = Extent(3, -4, -5, 10, srs=EPSG4326) assert (ex1.xyXY == (-5, -4, 3, 10)) ex1b = Extent((3, -4, -5, 10), srs=EPSG4326) assert (ex1b.xyXY == (-5, -4, 3, 10)) # from source ex2 = Extent.fromVector( source=MULTI_FTR_SHAPE_PATH ) assert (ex2.xyXY == (6.212409755390094, 48.864894076418935, 7.782393932571588, 49.932593106005655)) # from source ex3 = Extent.fromRaster( source=AACHEN_ELIGIBILITY_RASTER ) assert (ex3.xyXY == (5.974, 50.494, 6.42, 50.951)) assert ex3.srs.IsSame(EPSG4326)
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_Extent_subTiles(): ext = Extent.fromVector(_test_data_['aachenShapefile.shp']) tiles = [Extent.fromTile(t.xi, t.yi, t.zoom) for t in ext.subTiles(9)] assert len(tiles) == 4 assert np.isclose(tiles[0].xMin, 626172.135712) assert np.isclose(tiles[0].xMax, 704443.652676) assert np.isclose(tiles[0].yMin, 6574807.424978) assert np.isclose(tiles[0].yMax, 6653078.941942) assert tiles[0].srs.IsSame(srs.EPSG3857) assert np.isclose(tiles[1].xMin, 626172.135712) assert np.isclose(tiles[1].xMax, 704443.652676) assert np.isclose(tiles[1].yMin, 6496535.908014) assert np.isclose(tiles[1].yMax, 6574807.424978) assert tiles[1].srs.IsSame(srs.EPSG3857) assert np.isclose(tiles[2].xMin, 704443.652676) assert np.isclose(tiles[2].xMax, 782715.169640) assert np.isclose(tiles[2].yMin, 6574807.424978) assert np.isclose(tiles[2].yMax, 6653078.941942) assert tiles[2].srs.IsSame(srs.EPSG3857) assert np.isclose(tiles[3].xMin, 704443.652676) assert np.isclose(tiles[3].xMax, 782715.169640) assert np.isclose(tiles[3].yMin, 6496535.908014) assert np.isclose(tiles[3].yMax, 6574807.424978) assert tiles[3].srs.IsSame(srs.EPSG3857) tiles = list(ext.subTiles(10)) assert len(tiles) == 9 tiles = list(ext.subTiles(11)) assert len(tiles) == 20 tiles = list(ext.subTiles(12)) assert len(tiles) == 63
def test_Extent_extractFeatures(): ex = Extent.fromVector(AACHEN_SHAPE_PATH) # Test loading as a dataframe vi = ex.extractFeatures(AACHEN_ZONES, asPandas=True) assert vi.shape[0] == 101