def test_numpy(meta_numpy): meta = meta_numpy arrptr = meta_numpy['array'].__array_interface__['data'][0] fp = meta['fp'] ds = DataSource() arr = np.add(*fp.meshgrid_raster) arr = np.repeat(arr[..., np.newaxis], meta['array'].shape[-1], -1) with ds.awrap_numpy_raster(**meta).close as r: r.set_data(arr, band=-1) assert r.wkt_stored == meta['sr'] assert r.wkt_virtual == meta['sr'] for k, v in meta['band_schema'].items(): assert r.band_schema[k] == tuple(v) assert r.dtype == meta['array'].dtype assert r.fp_stored == fp if 'nodata' in meta['band_schema']: assert r.nodata == meta['band_schema']['nodata'][0] else: assert r.nodata == None assert len(r) == meta['array'].shape[-1] assert r.fp == fp assert r.mode == 'w' assert np.all(r.get_data(band=[-1]) == arr) r.array assert (meta_numpy['array'].__array_interface__['data'][0] == r.array.__array_interface__['data'][0])
def test_mem(meta_mem): meta = meta_mem fp = meta['fp'] ds = DataSource() arr = np.add(*fp.meshgrid_raster) arr = np.repeat(arr[..., np.newaxis], meta['band_count'], -1) with ds.acreate_raster(**meta).close as r: r.set_data(arr, band=-1) assert r.wkt_stored == meta['sr'] assert r.wkt_virtual == meta['sr'] for k, v in meta['band_schema'].items(): assert r.band_schema[k] == v assert r.dtype == np.dtype(meta['dtype']) assert r.fp_stored == fp if 'nodata' in meta['band_schema']: assert r.nodata == meta['band_schema']['nodata'][0] else: assert r.nodata == None assert len(r) == meta['band_count'] assert r.fp == fp assert r.mode == 'w' assert np.all(r.get_data(band=[-1]) == arr)
def test_file(meta_file, path): meta = meta_file fp = meta['fp'] ds = DataSource() arr = np.add(*fp.meshgrid_raster) arr = np.repeat(arr[..., np.newaxis], meta['band_count'], -1) with ds.acreate_raster(path, **meta).close as r: r.set_data(arr, band=-1) if DRIVER_STORES_SRS[meta['driver']]: assert r.wkt_stored == meta['sr'] assert r.wkt_virtual == meta['sr'] for k, v in meta['band_schema'].items(): assert r.band_schema[k] == v assert r.dtype == np.dtype(meta['dtype']) assert r.fp_stored == fp if 'nodata' in meta['band_schema']: assert r.nodata == meta['band_schema']['nodata'][0] else: assert r.nodata == None assert len(r) == meta['band_count'] assert r.fp == fp assert r.mode == 'w' assert r.driver == meta['driver'] assert r.open_options == meta['options'] assert r.path == path assert np.all(r.get_data(band=[-1]) == arr) assert os.path.isfile(path) with ds.aopen_raster(path, driver=meta['driver']).close as r: if DRIVER_STORES_SRS[meta['driver']]: assert r.wkt_stored == meta['sr'] assert r.wkt_virtual == meta['sr'] for k, v in meta['band_schema'].items(): assert r.band_schema[k] == v assert r.dtype == np.dtype(meta['dtype']) assert r.fp_stored == fp if 'nodata' in meta['band_schema']: assert r.nodata == meta['band_schema']['nodata'][0] else: assert r.nodata == None assert len(r) == meta['band_count'] assert r.fp == fp assert r.mode == 'r' assert r.driver == meta['driver'] assert r.path == path assert np.all(r.get_data(band=[-1]) == arr) with pytest.raises(RuntimeError): r.delete() assert os.path.isfile(path) with ds.aopen_raster(path, driver=meta['driver'], mode='w').delete as r: assert r.mode == 'w' assert not os.path.isfile(path)
def test_mem(driver_mem, gtype, geoms, ftypes, fields): driver = driver_mem ds = DataSource(allow_none_geometry=1) with ds.acreate_vector('', gtype, ftypes, driver=driver, options=[], sr=SR1['wkt']).close as v: for geom in geoms: v.insert_data(geom, fields) # TESTS 0 assert srs.wkt_same(v.wkt_stored, SR1['wkt']) assert srs.wkt_same(v.wkt_virtual, SR1['wkt']) for ftype, ftype_stored in zip(ftypes, v.fields): for key, value in ftype.items(): assert ftype_stored[key] == value assert v.mode == 'w' assert v.type.lower() == gtype.lower() # TESTS 1 datas = list(v.iter_data()) assert len(datas) == len(geoms) assert len(v) == len(geoms) # assert v.layer in {0, ''} if not sg.GeometryCollection(geoms).is_empty: assert eq( v.bounds, v.bounds_stored, v.extent[[0, 2, 1, 3]], v.extent_stored[[0, 2, 1, 3]], sg.GeometryCollection(geoms).bounds ) for geom, data in zip(geoms, datas): if not isinstance(data, tuple): data = (data,) read_geom, *read_fields = data if read_geom is None or read_geom.is_empty: assert geom.is_empty else: assert (geom ^ read_geom).is_empty, ( geom.wkt, read_geom.wkt, )
def ds(): """Fixture for the datasource creation""" path = '{}/{}.tif'.format(tempfile.gettempdir(), uuid.uuid4()) tools.make_tif2(path=path, nodata=TIF_NODATA, nodata_border_size=TIF_NODATA_BORDER_SIZE, reso=TIF_FP.scale, rsize=TIF_FP.rsize, tl=TIF_FP.tl) ds = DataSource() ds.open_raster('dsm', path) path2 = '{}/{}.tif'.format(tempfile.gettempdir(), uuid.uuid4()) ds.create_raster( 'out', path2, TIF_FP, 'float32', 1, {'nodata': TIF_NODATA}, sr=ds.dsm.wkt_origin ) yield ds ds.dsm.close() os.remove(path) ds.out.close() os.remove(path2)
def ds(): return DataSource(allow_interpolation=0)
def test_file(path, driver_file, gtype, geoms, ftypes, fields): driver = driver_file ds = DataSource(allow_none_geometry=1) with ds.acreate_vector(path, gtype, ftypes, driver=driver, options=[], sr=SR1['wkt']).close as v: for geom in geoms: v.insert_data(geom, fields) # TESTS 0 if DRIVER_STORES_SRS[driver]: assert srs.wkt_same(v.wkt_stored, SR1['wkt']) assert srs.wkt_same(v.wkt_virtual, SR1['wkt']) for ftype, ftype_stored in zip(ftypes, v.fields): for key, value in ftype.items(): assert ftype_stored[key] == value assert v.mode == 'w' if DRIVER_STORES_GTYPE[driver]: assert v.type.lower() == gtype.lower() assert os.path.isfile(path) with ds.aopen_vector(path, driver=driver, options=[]).close as v: # TESTS 0 if DRIVER_STORES_SRS[driver]: assert srs.wkt_same(v.wkt_stored, SR1['wkt']) assert srs.wkt_same(v.wkt_virtual, SR1['wkt']) for ftype, ftype_stored in zip(ftypes, v.fields): for key, value in ftype.items(): assert ftype_stored[key] == value assert v.mode == 'r' # if DRIVER_STORES_GTYPE[driver]: # assert v.type.lower() == gtype.lower() # TESTS 1 datas = list(v.iter_data()) assert len(datas) == len(geoms) assert len(v) == len(geoms) # assert v.layer in {0, ''} if not sg.GeometryCollection(geoms).is_empty: assert eq( v.bounds, v.bounds_stored, v.extent[[0, 2, 1, 3]], v.extent_stored[[0, 2, 1, 3]], sg.GeometryCollection(geoms).bounds ) for geom, data in zip(geoms, datas): if not isinstance(data, tuple): data = (data,) read_geom, *read_fields = data if read_geom is None or read_geom.is_empty: assert geom.is_empty else: assert (geom ^ read_geom).is_empty, ( geom.wkt, read_geom.wkt, ) with pytest.raises(RuntimeError): v.delete() with ds.aopen_vector(path, driver=driver, options=[], mode='w').delete as v: assert v.mode == 'w' assert not os.path.isfile(path)