def test_RegionMask_fromGeom(): # fromGeom with wkt rm1 = RegionMask.fromGeom(geom.convertWKT( POLY, srs='latlon'), pixelRes=1000) assert (rm1.extent.xXyY == (4329000.0, 4771000.0, 835000.0, 1682000.0)) assert (rm1.extent.srs.IsSame(EPSG3035)) assert rm1.mask.sum() == 79274 # fromGeom with geometry dxy = 0.05 rm2 = RegionMask.fromGeom(GEOM, pixelRes=dxy, srs=EPSG4326, padExtent=0.2) assert rm2.extent == Extent(9.90, 30.30, 14.80, 38.30) assert rm2.extent.srs.IsSame(EPSG4326) g = GEOM.Clone() g.TransformTo(EPSG4326) assert np.isclose(rm2.mask.sum() * dxy * dxy, g.Area(), rtol=1e-3) # fromGeom with geometry and extent dxy = 0.05 definedExtent = Extent(9.50, 30.25, 14.80, 38.35) rm3 = RegionMask.fromGeom( GEOM, pixelRes=dxy, srs=EPSG4326, extent=definedExtent) assert rm3.extent == definedExtent assert rm3.extent.srs.IsSame(EPSG4326) g = GEOM.Clone() g.TransformTo(EPSG4326) assert np.isclose(rm3.mask.sum() * dxy * dxy, g.Area(), rtol=1e-3)
def test_Extent_pad(): # setup ex1 = Extent(3, -4, -5, 10, srs=EPSG4326) # do test ex_pad = ex1.pad(1) # check assert np.isclose(ex_pad.xyXY, (-6, -5, 4, 11)).all()
def test_Extent_contains(): # setup ex1 = Extent(3, -4, -5, 10, srs=EPSG4326) # Test for contains assert (ex1.contains(Extent(-5, -4, 3, 12)) == False) assert (ex1.contains(Extent(-5, -3.3333, 2.0002, 8)) == True) assert (ex1.contains(Extent(-2.0, -3.5, 1.0, 8.5), 0.5) == True) assert (ex1.contains(Extent(-2.0, -3.25, 1.0, 8.25), 0.5) == False)
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_computePixelSize(): ex = Extent(0, 1, 2, 3, srs=EPSG4326) dx, dy = ex.computePixelSize(5) assert np.isclose(dx, 0.4) assert np.isclose(dy, 0.4) dx, dy = ex.computePixelSize(5, 10) assert np.isclose(dx, 0.4) assert np.isclose(dy, 0.2)
def test_Extent_containsLoc(): ex1 = Extent(3, 4, 5, 10, srs=EPSG4326) assert ex1.containsLoc((4, 8)) assert not ex1.containsLoc((7, 0)) s = ex1.containsLoc([(4, 8), (7, 0), (7, 1), (3, 4)]) assert s[0] == True assert s[1] == False assert s[2] == False assert s[3] == True
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___add__(): # setup ex1 = Extent.load((1, 2, 3, 4), srs=EPSG4326) ex2 = Extent.load((1, 1, 3, 3), srs=EPSG4326) # Simple add ex = ex1 + ex2 assert ex.xMin == 1 assert ex.xMax == 3 assert ex.yMin == 1 assert ex.yMax == 4
def test_RegionMask_fromVector(): # fromVector with a padded extent and defined srs rm0 = RegionMask.fromVector( AACHEN_SHAPE_PATH, pixelRes=0.001, srs=EPSG4326, padExtent=0.1) assert rm0.mask.sum() == 90296 # fromVector - ID select rm1 = RegionMask.fromVector(MULTI_FTR_SHAPE_PATH, where=1) assert (rm1.extent == Extent( 4069100, 2867000, 4109400, 2954000, srs=EPSG3035)) assert (rm1.attributes["name"] == "dog") ds = ogr.Open(MULTI_FTR_SHAPE_PATH) lyr = ds.GetLayer() ftr = lyr.GetFeature(1) g = ftr.GetGeometryRef().Clone() g.TransformTo(EPSG3035) # check if total areas are close to one another assert np.isclose(rm1.mask.sum() * 100 * 100, g.Area(), rtol=1e-3) # fromVector - 'where' select rm2 = RegionMask.fromVector( MULTI_FTR_SHAPE_PATH, srs=EPSG4326, pixelRes=0.01, where="name='monkey'") assert (rm2.extent == Extent(6.83, 49.52, 7.53, 49.94)) assert (rm2.attributes["id"] == 3) ftr = lyr.GetFeature(3) g = ftr.GetGeometryRef().Clone() g.TransformTo(EPSG4326) assert np.isclose(rm2.mask.sum(), 1948.0) # fromVector - 'where' select fail no features try: rm3 = RegionMask.fromVector( MULTI_FTR_SHAPE_PATH, srs=EPSG4326, pixelRes=0.01, where="name='monkeyy'") assert False except error.GeoKitRegionMaskError as e: assert str(e) == 'Zero features found' else: assert False # fromVector - 'where' finds many features try: rm4 = RegionMask.fromVector( MULTI_FTR_SHAPE_PATH, srs=EPSG4326, pixelRes=0.01, where=r"name like 'mo%'") assert False except error.GeoKitRegionMaskError as e: assert 'Multiple fetures found' in str(e) else: assert False
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_fromTileAt(): # fun func tile = Extent.fromTileAt(6.083, 50.775, zoom=8, srs=EPSG4326) assert np.isclose(tile.xMin, 626172.135712) assert np.isclose(tile.xMax, 782715.169640) assert np.isclose(tile.yMin, 6574807.424978) assert np.isclose(tile.yMax, 6731350.458906) tile = Extent.fromTileAt(x=4101103, y=2978620, zoom=8, srs=EPSG3035) assert np.isclose(tile.xMin, 626172.135712164) assert np.isclose(tile.xMax, 782715.169640205) assert np.isclose(tile.yMin, 6418264.3910496775) assert np.isclose(tile.yMax, 6574807.4249777235)
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_shift(): ex = Extent.load((1, 2, 3, 4), srs=4326) ex1 = ex.shift(-1, 2) np.isclose(ex1.xMin, 0) np.isclose(ex1.xMax, 2) np.isclose(ex1.yMin, 4) np.isclose(ex1.yMax, 6)
def test_RegionMask_pixelRes(): # test succeed rm1 = RegionMask.fromMask(Extent(0, 0, 100, 100, srs=EPSG3035), MASK_DATA) ps = rm1.pixelRes assert ps == 1 # test fail rm2 = RegionMask.fromMask(Extent(0, 0, 100, 200, srs=EPSG3035), MASK_DATA) try: ps = rm2.pixelRes assert False except error.GeoKitRegionMaskError as e: assert str( e) == 'pixelRes only accessable when pixelWidth equals pixelHeight' else: assert False
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_castTo(): # setup ex3 = Extent.fromGeom( GEOM ) ## Projecting ex_cast = ex3.castTo(EPSG3035) assert np.isclose(ex_cast.fit(1).xyXY, (4329833, 835682, 4770009, 1681039)).all()
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_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_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_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_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_RegionMask_fromMask(): ext = Extent(0, 0, 100, 100, srs=EPSG3035) rm = RegionMask.fromMask( mask=MASK_DATA, extent=ext, attributes={"hats": 5}) assert rm.mask.sum() == MASK_DATA.sum() # mask assert rm.extent == ext # extent assert rm.srs.IsSame(ext.srs) # srs
def test_Extent_fromTile(): # fun func ext = Extent.fromTile(xi=1062, yi=693, zoom=11) assert np.isclose(ext.xMin, 743579.411158) assert np.isclose(ext.xMax, 763147.290399) assert np.isclose(ext.yMin, 6457400.149532) assert np.isclose(ext.yMax, 6476968.028773)
def test_Extent_findWithin(): bigExt = Extent.fromRaster(CLC_RASTER_PATH) smallExt = Extent(4050200, 3076800, 4055400, 3080300, srs=EPSG3035) # do regular w1 = bigExt.findWithin(smallExt, res=100) assert w1.xStart == 381 # xStart assert w1.yStart == 307 # yStart assert w1.xWin == 52 # xWin assert w1.yWin == 35 # yWin # do flipped (only yStart should change) w2 = bigExt.findWithin(smallExt, res=100, yAtTop=False) assert w2.xStart == 381 # flipped - xStart assert w2.yStart == 450 # flipped - yStart assert w2.xWin == 52 # flipped - xWin assert w2.yWin == 35 # flipped - yWin
def test_Extent_fromWKT(): wkt = 'POLYGON ((4031300 2684000 0,4672600 2684000 0,4672600 3551300 0,4031300 3551300 0,4031300 2684000 0))|PROJCS["ETRS89 / LAEA Europe",GEOGCS["ETRS89",DATUM["European_Terrestrial_Reference_System_1989",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY["EPSG","6258"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4258"]],PROJECTION["Lambert_Azimuthal_Equal_Area"],PARAMETER["latitude_of_center",52],PARAMETER["longitude_of_center",10],PARAMETER["false_easting",4321000],PARAMETER["false_northing",3210000],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","3035"]]' ex1 = Extent.fromWKT(wkt) assert np.isclose(ex1.xMin, 4031300.0) assert np.isclose(ex1.xMax, 4672600.0) assert np.isclose(ex1.yMin, 2684000.0) assert np.isclose(ex1.yMax, 3551300.0) assert ex1.srs.IsSame(srs.EPSG3035)
def test_Extent_fromLocationSet(): ls = LocationSet(pointsInAachen3035) ex1 = Extent.fromLocationSet(ls) assert np.isclose(ex1.xMin, 4039553.19006) assert np.isclose(ex1.yMin, 3052769.53854) assert np.isclose(ex1.xMax, 4065568.41553) assert np.isclose(ex1.yMax, 3087947.74366)
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_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_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_load(): # Explicit ex1 = Extent.load((1, 2, 3, 4), srs=4326) assert np.isclose(ex1.xMin, 1) assert np.isclose(ex1.xMax, 3) assert np.isclose(ex1.yMin, 2) assert np.isclose(ex1.yMax, 4) assert ex1.srs.IsSame(EPSG4326) # Geometry ex2 = Extent.load(GEOM) assert np.isclose(ex2.xMin, 10.10000) assert np.isclose(ex2.xMax, 14.60000) assert np.isclose(ex2.yMin, 30.50000) assert np.isclose(ex2.yMax, 38.10000) assert ex2.srs.IsSame(EPSG4326) # vector ex3 = Extent.load(AACHEN_POINTS) assert np.isclose(ex3.xMin, 6.03745) assert np.isclose(ex3.xMax, 6.33091) assert np.isclose(ex3.yMin, 50.5367) assert np.isclose(ex3.yMax, 50.9227) assert ex3.srs.IsSame(EPSG4326) # Raster ex4 = Extent.load(CLC_RASTER_PATH) assert np.isclose(ex4.xMin, 4012100) assert np.isclose(ex4.yMin, 3031800) assert np.isclose(ex4.xMax, 4094600) assert np.isclose(ex4.yMax, 3111000) assert ex4.srs.IsSame(EPSG3035) # Location set ls = LocationSet(pointsInAachen3035, srs=3035) ex5 = Extent.load(ls) assert np.isclose(ex5.xMin, 6.02141) assert np.isclose(ex5.yMin, 50.51939) assert np.isclose(ex5.xMax, 6.371634) assert np.isclose(ex5.yMax, 50.846025) assert ex5.srs.IsSame(EPSG4326)