Ejemplo n.º 1
0
def test_resolution_and_rotation_accessors(fps):

    c, a, b, f, d, e = fps.A.gt
    assert eq(
        (a, b, c, d, e, f),
        fps.A.aff6,
        fps.A.aff23.flatten(),
        fps.A.aff33.flatten()[:6],
        fps.A.affine[0:6],
    )

    def _of_all(fn):
        return [fn(fp) for fp in fps.values()]

    assert eqall(
        _of_all(lambda fp: fp.gt[1]) + _of_all(lambda fp: fp.scale[0]) +
        _of_all(lambda fp: fp.pxsize[0]) + _of_all(lambda fp: fp.pxsize[1]) +
        _of_all(lambda fp: fp.pxsizex) + _of_all(lambda fp: fp.pxsizey) +
        _of_all(lambda fp: fp.pxvec[0]) + _of_all(lambda fp: -fp.gt[5]) +
        _of_all(lambda fp: -fp.scale[1]) + _of_all(lambda fp: -fp.pxvec[1]))
    assert eqall(_of_all(lambda fp: fp.angle) + [0])
    assert eqall(_of_all(lambda fp: fp.gt[2]))
    assert eqall(_of_all(lambda fp: fp.gt[4]))
    for fp in fps.values():
        assert eq(fp.gt[0], fp.tlx)
        assert eq(fp.gt[3], fp.tly)
Ejemplo n.º 2
0
def test_size_accessors_spatial(fps):
    assert eq(fps.A.size, fps.B.size, fps.C.size, fps.D.size, fps.E.size,
              fps.F.size, fps.G.size, fps.H.size, fps.I.size,)
    assert eq(fps.AE.size, fps.BF.size, fps.DH.size, fps.EI.size)
    assert eq(fps.AH.size, fps.BI.size)
    assert eq(fps.AG.h, fps.BH.h, fps.CI.h, fps.CI.height,)
    assert eq(fps.AB.w, fps.AE.w, fps.AH.w, fps.HI.w, fps.HI.width, )
Ejemplo n.º 3
0
def test_vector_accessors_spatial(fps):
    assert eq(
        fps.AC.lrvec,
        fps.AF.lrvec,
        fps.AI.lrvec,
        fps.DF.lrvec,
        fps.DI.lrvec,
        fps.GI.lrvec,
    )
    assert eq(
        fps.AG.tbvec,
        fps.AH.tbvec,
        fps.AI.tbvec,
        fps.BH.tbvec,
        fps.BI.tbvec,
        fps.CI.tbvec,
    )
    assert eq(
        fps.AE.diagvec,
        fps.BF.diagvec,
        fps.DH.diagvec,
        fps.EI.diagvec,
    )
    assert eqall([fp.pxvec for fp in fps.values()] +
                 [fp.diagvec / fp.rsize for fp in fps.values()], )
Ejemplo n.º 4
0
def test_coordinates_accessors_raster_others(fps):
    assert eq(
        fps.AC.rt,
        fps.AF.rt,
        fps.AI.rt,
        fps.DF.rt,
        fps.DI.rt,
        fps.GI.rt,
    )
    assert eq(
        fps.AG.rl,
        fps.AH.rl,
        fps.AI.rl,
        fps.BH.rl,
        fps.BI.rl,
        fps.CI.rl,
    )
    assert eq(
        fps.AE.rr,
        fps.BF.rr,
        fps.DH.rr,
        fps.EI.rr,
    )
    assert eq(
        fps.AE.rb,
        fps.BF.rb,
        fps.DH.rb,
        fps.EI.rb,
    )
    assert eq(
        fps.AE.rc,
        fps.BF.rc,
        fps.DH.rc,
        fps.EI.rc,
    )
Ejemplo n.º 5
0
def test_size_accessors_raster(fps):
    assert eq(fps.A.rtl, [0, 0], [fps.A.rtlx, fps.A.rtly])
    assert eq(fps.A.rtl, [0, 0])
    assert eq(
        fps.A.rsize,
        fps.B.rsize,
        fps.C.rsize,
        fps.D.rsize,
        fps.E.rsize,
        fps.F.rsize,
        fps.G.rsize,
        fps.H.rsize,
        fps.I.rsize,
    )
    assert eq(
        fps.AE.rsize,
        fps.BF.rsize,
        fps.DH.rsize,
        fps.EI.rsize,
    )
    assert eq(
        fps.AH.rsize,
        fps.BI.rsize,
    )
    assert eq(
        fps.AG.rh,
        fps.BH.rh,
        fps.CI.rh,
        fps.CI.rheight,
    )
    assert eq(
        fps.AB.rw,
        fps.AE.rw,
        fps.AH.rw,
        fps.HI.rw,
        fps.HI.rwidth,
    )

    assert eq(
        fps.A.shape,
        fps.B.shape,
        fps.C.shape,
        fps.D.shape,
        fps.E.shape,
        fps.F.shape,
        fps.G.shape,
        fps.H.shape,
        fps.I.shape,
    )
    assert eq(
        fps.AE.shape,
        fps.BF.shape,
        fps.DH.shape,
        fps.EI.shape,
    )
    assert eq(
        fps.AH.shape,
        fps.BI.shape,
    )
Ejemplo n.º 6
0
def test_numpy_like_functions(fps, fps1px):

    assert eq(
        fps.A.meshgrid_raster,
        fps.B.meshgrid_raster,
        fps.C.meshgrid_raster,
        np.meshgrid(range(fps.A.rw), range(fps.A.rh)),
    )
    assert eq(
        fps.A.meshgrid_spatial,
        fps.C.meshgrid_spatial + fps.A.tl[:, None, None] -
        fps.C.tl[:, None, None],
        fps.I.meshgrid_spatial + fps.A.tl[:, None, None] -
        fps.I.tl[:, None, None],
    )

    assert eq(
        fps.A.meshgrid_raster,
        fps.A.meshgrid_raster_in(fps.AI),
        fps.A.meshgrid_raster_in(fps.A),
        fps.I.meshgrid_raster_in(fps.A) +
        ((fps.A.tl - fps.I.tl) / fps.A.scale)[:, None, None],
    )
    assert fps.A.meshgrid_raster_in(fps.A, dtype='uint8')[0].dtype == np.uint8
    assert fps.A.meshgrid_raster_in(fps.A, dtype='float64',
                                    op=42)[0].dtype == np.float64

    fps = fps1px

    dense = fps.AI.move(fps.A.tl, fps.A.tr)
    mesh = np.dstack(fps.EI.meshgrid_raster_in(fps.AI))
    mesh_dense = np.dstack(fps.EI.meshgrid_raster_in(dense))
    assert ((mesh_dense / mesh) == 3).all()

    op = lambda arr: (arr * 0 + 42)

    assert (np.asarray(fps.AI.meshgrid_raster_in(fps.AI, op=op)) == 42).all()

    mg_ai = np.asarray(fps.AI.meshgrid_spatial)
    for fp in fps.values():
        mg = np.asarray(fp.meshgrid_spatial)
        slices = (slice(0, 2), ) + fp.slice_in(fps.AI)
        assert (mg == mg_ai[slices]).all()

    assert fps.A.meshgrid_spatial[0][fps.I.slice_in(fps.A,
                                                    clip=True)].size == 0
Ejemplo n.º 7
0
def test_morpho(fps):

    def create(rsizex, rsizey):
        return buzz.Footprint(gt=fps.AI.gt, rsize=(rsizex, rsizey))
    assert eq(create(3, 3).erode(1).rarea, 1 * 1)
    assert eq(create(4, 4).erode(1).rarea, 2 * 2)
    assert eq(create(5, 5).erode(1).rarea, 3 * 3)
    assert eq(create(5, 5).erode(2).rarea, 1 * 1)

    assert eq(create(3, 4).erode(1).rarea, 1 * 2)
    assert eq(create(4, 3).erode(1).rarea, 1 * 2)

    assert eq(create(2, 2).dilate(1).rarea, 4 * 4)
    assert eq(create(2, 3).dilate(1).rarea, 4 * 5)

    assert eq(create(1, 1).dilate(1).dilate(2).erode(3).rarea, 1 * 1)
Ejemplo n.º 8
0
def test_coordinates_accessors_spatial_corners(fps):
    buzz.Footprint(gt=fps.A.gt, rsize=fps.A.rsize)
    assert eq(
        fps.E.tl,
        fps.B.bl,
        fps.A.br,
        fps.D.tr,
    )
    assert eq(
        fps.E.bl,
        fps.D.br,
        fps.G.tr,
        fps.H.tl,
    )
    assert eq(
        fps.E.br,
        fps.H.tr,
        fps.I.tl,
        fps.F.bl,
    )
    assert eq(
        fps.E.tr,
        fps.F.tl,
        fps.C.bl,
        fps.B.br,
    )
    for letter in LETTERS:
        those_tl = [v.tl for k, v in fps.items() if k.startswith(letter)]
        assert eq(*those_tl)
        those_br = [v.br for k, v in fps.items() if k.endswith(letter)]
        assert eq(*those_br)
Ejemplo n.º 9
0
def test_coordinates_accessors_spatial_others(fps):
    assert eq(
        fps.B.b,
        fps.E.t,
        fps.EH.t,
        fps.AC.b,
        fps.DF.t,
        fps.DI.t,
    )
    assert eq(
        fps.D.r,
        fps.E.l,
        fps.EF.l,
        fps.AG.r,
        fps.BH.l,
        fps.BI.l,
    )
    assert eq(
        fps.H.t,
        fps.E.b,
        fps.BE.b,
        fps.GI.t,
        fps.DF.b,
        fps.AF.b,
    )
    assert eq(
        fps.F.l,
        fps.E.r,
        fps.DE.r,
        fps.CI.l,
        fps.BH.r,
        fps.AH.r,
    )
    assert eq(
        fps.E.c,
        fps.AI.c,
        fps.DF.c,
        fps.BH.c,
    )
Ejemplo n.º 10
0
def test_coordinates_accessors_raster_corners(fps):
    assert eq(
        fps.AC.rtr,
        fps.AF.rtr,
        fps.AI.rtr,
        fps.DF.rtr,
        fps.DI.rtr,
        fps.GI.rtr,
    )
    assert eq(
        fps.AG.rbl,
        fps.AH.rbl,
        fps.AI.rbl,
        fps.BH.rbl,
        fps.BI.rbl,
        fps.CI.rbl,
    )
    assert eq(
        fps.AE.rbr,
        fps.BF.rbr,
        fps.DH.rbr,
        fps.EI.rbr,
    )
Ejemplo n.º 11
0
def test_non_trivial_accessors(fps):
    assert eq(fps.AI.semimajoraxis, fps.AH.semimajoraxis, fps.AG.semimajoraxis, )
    assert eq(fps.BH.semiminoraxis, fps.BE.semiminoraxis, fps.B.semiminoraxis, )

    assert eq(
        fps.A.length + fps.B.length + fps.D.length + fps.E.length,
        fps.AE.length + fps.A.length * 2
    )

    assert eq(fps.AI.rsemimajoraxis, fps.AH.rsemimajoraxis, fps.AG.rsemimajoraxis, )
    assert eq(fps.BH.rsemiminoraxis, fps.BE.rsemiminoraxis, fps.B.rsemiminoraxis, )

    assert eq(fps.AI.rarea, np.prod(fps.AI.rsize), np.prod(fps.AI.size / fps.AI.pxsize))

    assert eq(fps.AI.rlength,
              # sum of sides minus 4
              fps.AI.rsizex * 2 + fps.AI.rsizey * 2 - 4,
              # sum of smaller rlength plus delta
              fps.AC.rlength + fps.AD.rsizey * 2,)
    fp = buzz.Footprint(gt=fps.AI.gt, rsize=(2, 10))
    assert eq(fp.rsemiminoraxis, 1)
    assert eq(fp.rlength, fp.rsemimajoraxis * 4)

    fp = buzz.Footprint(gt=fps.AI.gt, rsize=(1, 10))
    assert eq(fp.rsemiminoraxis, 1, tol=1)
    assert eq(fp.rlength, fp.rsemimajoraxis * 2)

    fp = buzz.Footprint(gt=fps.AI.gt, rsize=(1, 1))
    assert eq(fp.rsemiminoraxis, 1, fp.rsemimajoraxis, tol=1)
    assert eq(fp.rlength, 1)
def test_mode4(fps, shp1_path, tif1_path, shp2_path, tif2_path, shp3_path,
               tif3_path, random_path_shp, random_path_tif, env):
    ds = buzz.DataSource(sr_work=SR1['wkt'], sr_forced=SR2['wkt'])
    ds.open_raster('tif1', tif1_path)
    ds.open_vector('shp1', shp1_path)
    ds.open_raster('tif2', tif2_path)
    ds.open_vector('shp2', shp2_path)
    ds.open_raster('tif3', tif3_path)
    ds.open_vector('shp3', shp3_path)

    # Test file creation without spatial reference
    with buzz.Env(allow_complex_footprint=True):
        with ds.acreate_vector(random_path_shp, 'polygon', [],
                               sr=None).close as r:
            assert r.wkt_stored == None
            assert sreq(r.wkt_virtual, SR2['wkt'])
        with ds.acreate_raster(random_path_tif,
                               fps.AI,
                               'int32',
                               1, {},
                               sr=None).close as v:
            assert v.wkt_stored == None
            assert sreq(v.wkt_virtual, SR2['wkt'])

    # Test SR equality
    assert sreq(
        ds.wkt,
        ds.proj4,
        ds.tif1.wkt_stored,
        ds.tif1.proj4_stored,
        ds.shp1.wkt_stored,
        ds.shp1.proj4_stored,
    )
    assert sreq(
        ds.tif1.wkt_virtual,
        ds.tif1.proj4_virtual,
        ds.shp1.wkt_virtual,
        ds.shp1.proj4_virtual,
        ds.tif2.wkt_virtual,
        ds.tif2.proj4_virtual,
        ds.shp2.wkt_virtual,
        ds.shp2.proj4_virtual,
        ds.tif2.wkt_stored,
        ds.tif2.proj4_stored,
        ds.shp2.wkt_stored,
        ds.shp2.proj4_stored,
        ds.tif3.wkt_virtual,
        ds.tif3.proj4_virtual,
        ds.shp3.wkt_virtual,
        ds.shp3.proj4_virtual,
    )
    assert not sreq(ds.tif1.wkt_stored, ds.tif2.wkt_stored)
    assert eq(None == ds.tif3.wkt_stored == ds.tif3.proj4_stored ==
              ds.shp3.wkt_stored == ds.shp3.proj4_stored)

    # Test foorprints equality
    assert fpeq(
        fps.AI,
        # tif/shp 1
        ds.tif1.fp_origin,
        buzz.Footprint.of_extent(ds.shp1.extent_stored, fps.AI.scale),
        buzz.Footprint.of_extent(ds.shp1.bounds_stored[[0, 2, 1, 3]],
                                 fps.AI.scale),

        # tif/shp 2
        ds.tif2.fp_origin,
        buzz.Footprint.of_extent(ds.shp2.extent_stored, fps.AI.scale),
        buzz.Footprint.of_extent(ds.shp2.bounds_stored[[0, 2, 1, 3]],
                                 fps.AI.scale),

        # tif/shp 3
        ds.tif3.fp_origin,
        buzz.Footprint.of_extent(ds.shp3.extent_stored, fps.AI.scale),
        buzz.Footprint.of_extent(ds.shp3.bounds_stored[[0, 2, 1, 3]],
                                 fps.AI.scale),
    )
    assert fpeq(
        # tif/shp 1
        ds.tif1.fp,
        buzz.Footprint.of_extent(ds.shp1.extent, fps.AI.scale),
        buzz.Footprint.of_extent(ds.shp1.bounds[[0, 2, 1, 3]], fps.AI.scale),

        # tif/shp 2
        ds.tif2.fp,
        buzz.Footprint.of_extent(ds.shp2.extent, fps.AI.scale),
        buzz.Footprint.of_extent(ds.shp2.bounds[[0, 2, 1, 3]], fps.AI.scale),

        # tif/shp 3
        ds.tif3.fp,
        buzz.Footprint.of_extent(ds.shp3.extent, fps.AI.scale),
        buzz.Footprint.of_extent(ds.shp3.bounds[[0, 2, 1, 3]], fps.AI.scale),
    )
    assert ds.tif1.fp != ds.tif1.fp_stored

    # Test what's written in all 6 files
    tif1 = ds.tif1.get_data()
    tif2 = ds.tif2.get_data()
    tif3 = ds.tif3.get_data()
    assert np.all(tif1 == tif2)
    assert np.all(tif1 == tif3)

    def f(x, y, z=None):
        return np.around(x, 6), np.around(y, 6)

    for i, letter in enumerate(string.ascii_uppercase[:9]):
        # tif/shp 1
        shp1 = ds.shp1.get_data(i, None)
        shp1 = shapely.ops.transform(f, shp1)

        raster1_polys = ds.tif1.fp.find_polygons(tif1 == ord(letter))
        assert len(raster1_polys) == 1
        raster1_poly = raster1_polys[0]
        raster1_poly = shapely.ops.transform(f, shp1)

        assert (shp1 ^ raster1_poly).is_empty

        # tif/shp 2
        shp2 = ds.shp2.get_data(i, None)
        shp2 = shapely.ops.transform(f, shp2)

        raster2_polys = ds.tif2.fp.find_polygons(tif2 == ord(letter))
        assert len(raster2_polys) == 1
        raster2_poly = raster2_polys[0]
        raster2_poly = shapely.ops.transform(f, shp2)

        assert (shp2 ^ raster2_poly).is_empty

        # tif/shp 3
        shp3 = ds.shp3.get_data(i, None)
        shp3 = shapely.ops.transform(f, shp3)

        raster3_polys = ds.tif3.fp.find_polygons(tif3 == ord(letter))
        assert len(raster3_polys) == 1
        raster3_poly = raster3_polys[0]
        raster3_poly = shapely.ops.transform(f, shp3)

        assert (shp3 ^ raster3_poly).is_empty
def test_mode1(fps, shp1_path, tif1_path, shp2_path, tif2_path, shp3_path,
               tif3_path):
    ds = buzz.DataSource()
    ds.open_raster('tif1', tif1_path)
    ds.open_vector('shp1', shp1_path)
    ds.open_raster('tif2', tif2_path)
    ds.open_vector('shp2', shp2_path)
    ds.open_raster('tif3', tif3_path)
    ds.open_vector('shp3', shp3_path)

    # Test SR equality
    assert sreq(
        ds.tif1.wkt_virtual,
        ds.tif1.wkt_stored,
        ds.tif1.proj4_virtual,
        ds.tif1.proj4_stored,
        ds.shp1.wkt_virtual,
        ds.shp1.wkt_stored,
        ds.shp1.proj4_virtual,
        ds.shp1.proj4_stored,
    )
    assert sreq(
        ds.tif2.wkt_virtual,
        ds.tif2.wkt_stored,
        ds.tif2.proj4_virtual,
        ds.tif2.proj4_stored,
        ds.shp2.wkt_virtual,
        ds.shp2.wkt_stored,
        ds.shp2.proj4_virtual,
        ds.shp2.proj4_stored,
    )
    assert not sreq(ds.tif1.wkt_stored, ds.tif2.wkt_stored)
    assert eq(
        None == ds.wkt == ds.proj4 == ds.tif3.wkt_virtual == ds.tif3.wkt_stored
        == ds.tif3.proj4_virtual == ds.tif3.proj4_stored == ds.shp3.wkt_virtual
        == ds.shp3.wkt_stored == ds.shp3.proj4_virtual == ds.shp3.proj4_stored)

    # Test footprints equality
    assert fpeq(
        fps.AI,
        # tif/shp 1
        ds.tif1.fp,
        ds.tif1.fp_origin,
        buzz.Footprint.of_extent(ds.shp1.extent, fps.AI.scale),
        buzz.Footprint.of_extent(ds.shp1.extent_stored, fps.AI.scale),
        buzz.Footprint.of_extent(ds.shp1.bounds[[0, 2, 1, 3]], fps.AI.scale),
        buzz.Footprint.of_extent(ds.shp1.bounds_stored[[0, 2, 1, 3]],
                                 fps.AI.scale),
        # tif/shp 2
        ds.tif2.fp,
        ds.tif2.fp_origin,
        buzz.Footprint.of_extent(ds.shp2.extent, fps.AI.scale),
        buzz.Footprint.of_extent(ds.shp2.extent_stored, fps.AI.scale),
        buzz.Footprint.of_extent(ds.shp2.bounds[[0, 2, 1, 3]], fps.AI.scale),
        buzz.Footprint.of_extent(ds.shp2.bounds_stored[[0, 2, 1, 3]],
                                 fps.AI.scale),
        # tif/shp 3
        ds.tif3.fp,
        ds.tif3.fp_origin,
        buzz.Footprint.of_extent(ds.shp3.extent, fps.AI.scale),
        buzz.Footprint.of_extent(ds.shp3.extent_stored, fps.AI.scale),
        buzz.Footprint.of_extent(ds.shp3.bounds[[0, 2, 1, 3]], fps.AI.scale),
        buzz.Footprint.of_extent(ds.shp3.bounds_stored[[0, 2, 1, 3]],
                                 fps.AI.scale),
    )

    # Test what's written in all 6 files
    tif1 = ds.tif1.get_data()
    tif2 = ds.tif2.get_data()
    tif3 = ds.tif3.get_data()
    assert np.all(tif1 == tif2)
    assert np.all(tif1 == tif3)
    for i, letter in enumerate(string.ascii_uppercase[:9]):
        # tif/shp 1
        shp1 = ds.shp1.get_data(i, None)
        raster1_polys = ds.tif1.fp.find_polygons(tif1 == ord(letter))
        assert len(raster1_polys) == 1
        assert (shp1 ^ raster1_polys[0]).is_empty

        # tif/shp 2
        shp2 = ds.shp2.get_data(i, None)
        raster2_polys = ds.tif2.fp.find_polygons(tif2 == ord(letter))
        assert len(raster2_polys) == 1
        assert (shp2 ^ raster2_polys[0]).is_empty

        # tif/shp 3
        shp3 = ds.shp3.get_data(i, None)
        raster3_polys = ds.tif3.fp.find_polygons(tif3 == ord(letter))
        assert len(raster3_polys) == 1
        assert (shp3 ^ raster3_polys[0]).is_empty