Example #1
0
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)
Example #2
0
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)
Example #3
0
def test_Extent_exportWKT():
    # setup
    ex1 = Extent(1, 2, 3, 4, srs=srs.EPSG4326)
    s = ex1.exportWKT("|||")
    s2 = 'POLYGON ((1 2 0,3 2 0,3 4 0,1 4 0,1 2 0))|||GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]'
    assert s == s2

    ex1 = Extent(1, 2, 3, 4, srs=srs.EPSG3857)
    s = ex1.exportWKT("|||")
    s2 = 'POLYGON ((1 2 0,3 2 0,3 4 0,1 4 0,1 2 0))|||PROJCS["WGS 84 / Pseudo-Mercator",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]],PROJECTION["Mercator_1SP"],PARAMETER["central_meridian",0],PARAMETER["scale_factor",1],PARAMETER["false_easting",0],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["X",EAST],AXIS["Y",NORTH],EXTENSION["PROJ4","+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs"],AUTHORITY["EPSG","3857"]]'
    assert s == s2
Example #4
0
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
Example #5
0
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)
Example #6
0
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)
Example #7
0
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
Example #8
0
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
Example #9
0
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()
Example #10
0
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)
Example #11
0
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)
Example #12
0
def test_RegionMask_pixelRes():
    # test succeed
    rm1 = RegionMask.fromMask(Extent(0, 0, 100, 100, srs=EPSG3035), MASK_DATA)
    ps = rm1.pixelRes
    if not ps == 1: error("pixelRes")

    # test fail
    rm2 = RegionMask.fromMask(Extent(0, 0, 100, 200, srs=EPSG3035), MASK_DATA)
    try:
        ps = rm2.pixelRes
        error("pixelRes - fail test")
    except GeoKitRegionMaskError as e:
        if not str(
                e
        ) == 'pixelRes only accessable when pixelWidth equals pixelHeight':
            error("pixelRes - fail test")
    else:
        error("pixelRes - fail test")
    print("RegionMask_pixelRes passed")
Example #13
0
def test_RegionMask_fromMask():
    ext = Extent(0, 0, 100, 100, srs=EPSG3035)
    rm = RegionMask.fromMask(mask=MASK_DATA,
                             extent=ext,
                             attributes={"hats": 5})

    if not rm.mask.sum() == MASK_DATA.sum(): error("fromSource - mask")
    if not rm.extent == ext: error("fromSource - extent")
    if not rm.srs.IsSame(ext.srs): error("fromSource - srs")
    print("RegionMask_fromMask passed")
Example #14
0
def test_RegionMask___init__():
    ext = Extent(0, 0, 100, 100, srs=EPSG3035)
    # test succeed
    rm = RegionMask(ext, 1, mask=MASK_DATA)
    rm = RegionMask(ext, (1, 1), mask=MASK_DATA)
    rm = RegionMask(ext, 2, geom=GEOM)

    # no mask or geometry given
    try:
        rm = RegionMask(ext, 1)
        assert False
    except error.GeoKitRegionMaskError as e:
        assert str(e) == 'Either mask or geom should be defined'
    else:
        assert False

    # bad pixel given
    try:
        rm = RegionMask(ext, (1, 3), geom=GEOM)
        assert False
    except error.GeoKitRegionMaskError as e:
        assert str(e) == 'The given extent does not fit the given pixelRes'
    else:
        assert False

    # bad mask given
    try:
        rm = RegionMask(ext, (1, 2), mask=MASK_DATA)
        assert False
    except error.GeoKitRegionMaskError as e:
        assert str(
            e) == 'Extent and pixels sizes do not correspond to mask shape'
    else:
        assert False

    # bad geom given
    try:
        rm = RegionMask(ext, (1, 2), geom="bananas")
        assert False
    except error.GeoKitRegionMaskError as e:
        assert str(e) == 'geom is not an ogr.Geometry object'
    else:
        assert False

    # bad geom given 2
    badGeom = GEOM.Clone()
    badGeom.AssignSpatialReference(None)

    try:
        rm = RegionMask(ext, (1, 2), geom=badGeom)
        assert False
    except error.GeoKitRegionMaskError as e:
        assert str(e) == 'geom does not have an srs'
    else:
        assert False
Example #15
0
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
Example #16
0
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()
Example #17
0
def test_RegionMask_fromGeom():
    # fromGeom with wkt
    rm1 = RegionMask.fromGeom(convertWKT(POLY, srs='latlon'), pixelRes=1000)
    if (rm1.extent.xXyY != (4329000.0, 4771000.0, 835000.0, 1682000.0)):
        error("fromGeom - extent bounds")
    if not (rm1.extent.srs.IsSame(EPSG3035)): error("fromGeom - extent srs")
    if (rm1.mask.sum() != 79274): error("fromGeom - mask")

    # fromGeom with geometry
    dxy = 0.05
    rm2 = RegionMask.fromGeom(GEOM, pixelRes=dxy, srs=EPSG4326, padExtent=0.2)

    if (not rm2.extent == Extent(9.90, 30.30, 14.80, 38.30)):
        error("fromGeom - extent bounds")
    if not (rm2.extent.srs.IsSame(EPSG4326)): error("fromGeom - extent srs")

    g = GEOM.Clone()
    g.TransformTo(EPSG4326)
    if not abs(rm2.mask.sum() * dxy * dxy - g.Area()) / g.Area(
    ) < 0.01:  # check if total areas are close to one another
        error("fromGeom - mask")

    # 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)

    if (not rm3.extent == definedExtent):
        error("fromGeom - extent bounds")
    if not (rm3.extent.srs.IsSame(EPSG4326)): error("fromGeom - extent srs")

    g = GEOM.Clone()
    g.TransformTo(EPSG4326)
    if not abs(rm3.mask.sum() * dxy * dxy - g.Area()) / g.Area(
    ) < 0.01:  # check if total areas are close to one another
        error("fromGeom - mask")

    print("RegionMask_fromGeom passed")
Example #18
0
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
Example #19
0
def test_Extent_extractMatrix():

    # setup
    ex = Extent(6.022, 50.477, 6.189, 50.575).castTo(EPSG3035).fit(100)

    # extract
    mat1 = ex.extractMatrix(CLC_RASTER_PATH)
    assert np.isclose(mat1.sum(), 392284)

    # extract
    mat2 = ex.extractMatrix(CLC_FLIPCHECK_PATH)
    assert np.isclose(mat2.sum(), 392284)

    # Make sure matricies are the same
    assert np.isclose(mat1, mat2).all()

    # test fail since the given extent does not fit in the grid of the raster
    try:
        p = ex.shift(dx=1).extractMatrix(CLC_RASTER_PATH)
        assert False
    except util.GeoKitError as e:
        assert True
    else:
        assert False
Example #20
0
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)
    if (rm0.mask.sum() != 90296): error("fromVector - mask")

    # fromVector - ID select
    rm1 = RegionMask.fromVector(MULTI_FTR_SHAPE_PATH, where=1)

    if not (rm1.extent == Extent(
            4069100, 2867000, 4109400, 2954000, srs=EPSG3035)):
        error("fromVector - extent bounds")
    if not (rm1.attributes["name"] == "dog"): error("fromVector - attributes")

    ds = ogr.Open(MULTI_FTR_SHAPE_PATH)
    lyr = ds.GetLayer()
    ftr = lyr.GetFeature(1)
    g = ftr.GetGeometryRef().Clone()
    g.TransformTo(EPSG3035)
    if not abs(rm1.mask.sum() * 100 * 100 - g.Area()) / g.Area(
    ) < 0.01:  # check if total areas are close to one another
        error("fromVector - mask area")

    # fromVector - 'where' select
    rm2 = RegionMask.fromVector(MULTI_FTR_SHAPE_PATH,
                                srs=EPSG4326,
                                pixelRes=0.01,
                                where="name='monkey'")

    if not (rm2.extent == Extent(6.83, 49.52, 7.53, 49.94)):
        error("fromVector - extent bounds")
    if not (rm2.attributes["id"] == 3): error("fromVector - attributes")

    ftr = lyr.GetFeature(3)
    g = ftr.GetGeometryRef().Clone()
    g.TransformTo(EPSG4326)
    if not isclose(rm2.mask.sum(), 1948.0):
        error("fromVector - mask area")

    # fromVector - 'where' select fail no features
    try:
        rm3 = RegionMask.fromVector(MULTI_FTR_SHAPE_PATH,
                                    srs=EPSG4326,
                                    pixelRes=0.01,
                                    where="name='monkeyy'")
        error("fromVector - fail no features")
    except GeoKitRegionMaskError as e:
        if not str(e) == 'Zero features found':
            error("fromVector - fail no features")
    else:
        error("fromVector - fail no features")

    # fromVector - 'where' finds many features
    try:
        rm4 = RegionMask.fromVector(MULTI_FTR_SHAPE_PATH,
                                    srs=EPSG4326,
                                    pixelRes=0.01,
                                    where=r"name like 'mo%'")
        error("fromVector - fail multi features")
    except GeoKitRegionMaskError as e:
        if not 'Multiple fetures found' in str(e):
            error("fromVector - fail multi features")
    else:
        error("fromVector - fail multi features")

    print("RegionMask_fromVector passed")
Example #21
0
def test_Extent_fitsResolution():
    ex = Extent(-6, -5, 4, 11)
    assert ex.fitsResolution(0.5)
    assert ex.fitsResolution(2)
    assert not ex.fitsResolution(3)