def test_get_attributes(self): ds = zonalstats.DataSource(self.data) ds.set_attribute("test", self.values2) assert ds.get_attributes(["test"], filt=("index", 0)) == self.values2[0] assert ds.get_attributes(["test"], filt=("index", 1)) == self.values2[1]
def test_get_data_by_geom(self): ds = zonalstats.DataSource(self.data) lyr = ds.ds.GetLayer() lyr.ResetReading() lyr.SetSpatialFilter(None) lyr.SetAttributeFilter(None) for i, feature in enumerate(lyr): geom = feature.GetGeometryRef() assert np.allclose(ds.get_data_by_geom(geom), self.data[i])
def test_dump_raster(self): proj = osr.SpatialReference() proj.ImportFromEPSG(31466) filename = util.get_wradlib_data_file('shapefiles/agger/' 'agger_merge.shp') test = zonalstats.DataSource(filename, proj) test.dump_raster(tempfile.NamedTemporaryFile(mode='w+b').name, driver='netCDF', pixel_size=100.)
def test___init__(self): zdb = zonalstats.ZonalDataBase(self.src, self.trg, srs=self.proj) assert isinstance(zdb.src, zonalstats.DataSource) assert isinstance(zdb.trg, zonalstats.DataSource) assert isinstance(zdb.dst, zonalstats.DataSource) assert zdb._count_intersections == 2 zd = zonalstats.DataSource(self.src, name="src", srs=self.proj) zdb = zonalstats.ZonalDataBase(zd, self.trg, srs=self.proj) assert isinstance(zdb.src, zonalstats.DataSource) assert isinstance(zdb.trg, zonalstats.DataSource) assert isinstance(zdb.dst, zonalstats.DataSource) assert zdb._count_intersections == 2 zd1 = zonalstats.DataSource(self.src, name="src", srs=self.proj) zd2 = zonalstats.DataSource(self.trg, name="trg", srs=self.proj) zdb = zonalstats.ZonalDataBase(zd1, zd2, srs=self.proj) assert isinstance(zdb.src, zonalstats.DataSource) assert isinstance(zdb.trg, zonalstats.DataSource) assert isinstance(zdb.dst, zonalstats.DataSource) assert zdb._count_intersections == 2
def test_get_geom_properties(self): proj = osr.SpatialReference() proj.ImportFromEPSG(31466) filename = util.get_wradlib_data_file('shapefiles/agger/' 'agger_merge.shp') test = zonalstats.DataSource(filename, proj) np.testing.assert_array_equal([[76722499.98474795]], test.get_geom_properties(['Area'], filt=('FID', 1)))
def test___init__(self): zdb = zonalstats.ZonalDataBase(self.src, self.trg, srs=self.proj) self.assertIsInstance(zdb.src, zonalstats.DataSource) self.assertIsInstance(zdb.trg, zonalstats.DataSource) self.assertIsInstance(zdb.dst, zonalstats.DataSource) self.assertEqual(zdb._count_intersections, 2) zd = zonalstats.DataSource(self.src, name='src', srs=self.proj) zdb = zonalstats.ZonalDataBase(zd, self.trg, srs=self.proj) self.assertIsInstance(zdb.src, zonalstats.DataSource) self.assertIsInstance(zdb.trg, zonalstats.DataSource) self.assertIsInstance(zdb.dst, zonalstats.DataSource) self.assertEqual(zdb._count_intersections, 2) zd1 = zonalstats.DataSource(self.src, name='src', srs=self.proj) zd2 = zonalstats.DataSource(self.trg, name='trg', srs=self.proj) zdb = zonalstats.ZonalDataBase(zd1, zd2, srs=self.proj) self.assertIsInstance(zdb.src, zonalstats.DataSource) self.assertIsInstance(zdb.trg, zonalstats.DataSource) self.assertIsInstance(zdb.dst, zonalstats.DataSource) self.assertEqual(zdb._count_intersections, 2)
def setUp(self): # create synthetic box self.box0 = np.array([[2600000., 5630000.], [2600000., 5640000.], [2610000., 5640000.], [2610000., 5630000.], [2600000., 5630000.]]) self.box1 = np.array([[2700000., 5630000.], [2700000., 5640000.], [2710000., 5640000.], [2710000., 5630000.], [2700000., 5630000.]]) self.data = np.array([self.box0, self.box1]) self.ds = zonalstats.DataSource(self.data) self.values1 = np.array([47.11, 47.11]) self.values2 = np.array([47.11, 15.08])
def test_dump_raster(self): proj = osr.SpatialReference() proj.ImportFromEPSG(31466) filename = util.get_wradlib_data_file("shapefiles/agger/" "agger_merge.shp") test = zonalstats.DataSource(filename, srs=proj) test.dump_raster( tempfile.NamedTemporaryFile(mode="w+b").name, driver="netCDF", pixel_size=100.0, ) test.dump_raster( tempfile.NamedTemporaryFile(mode="w+b").name, driver="netCDF", pixel_size=100.0, attr="FID", )
def test_get_data_by_att(self): ds = zonalstats.DataSource(self.data) assert np.allclose(ds.get_data_by_att("index", 0), self.box0) assert np.allclose(ds.get_data_by_att("index", 1), self.box1)
def test_set_attribute(self): ds = zonalstats.DataSource(self.data) ds.set_attribute("test", self.values1) assert np.allclose(ds.get_attributes(["test"]), self.values1) ds.set_attribute("test", self.values2) assert np.allclose(ds.get_attributes(["test"]), self.values2)
def test_get_data_by_att(self): ds = zonalstats.DataSource(self.data) np.testing.assert_almost_equal(ds.get_data_by_att("index", 0), self.data[0:1]) np.testing.assert_almost_equal(ds.get_data_by_att("index", 1), self.data[1:2])
def test_get_data_by_idx(self): ds = zonalstats.DataSource(self.data) np.testing.assert_almost_equal(ds.get_data_by_idx([0]), self.data[0:1]) np.testing.assert_almost_equal(ds.get_data_by_idx([1]), self.data[1:2]) np.testing.assert_almost_equal(ds.get_data_by_idx([0, 1]), self.data)
def test__get_data(self): ds = zonalstats.DataSource(self.data) np.testing.assert_almost_equal(ds._get_data(), self.data)
def test__check_src(self): filename = util.get_wradlib_data_file("shapefiles/agger/" "agger_merge.shp") assert len(zonalstats.DataSource(filename).data) == 13 with pytest.raises(RuntimeError): zonalstats.DataSource("test_zonalstats.py")
def test_get_data_by_idx(self): ds = zonalstats.DataSource(self.data) assert np.allclose(ds.get_data_by_idx([0]), self.box0) assert np.allclose(ds.get_data_by_idx([1]), self.box1) assert np.allclose(ds.get_data_by_idx([0, 1]), self.data)
def test__check_src(self): filename = util.get_wradlib_data_file('shapefiles/agger/' 'agger_merge.shp') self.assertEqual(len(zonalstats.DataSource(filename).data), 13) self.assertRaises(RuntimeError, lambda: zonalstats.DataSource('test_zonalstats.py'))
def test_dump_vector(self): ds = zonalstats.DataSource(self.data) ds.dump_vector(tempfile.NamedTemporaryFile(mode="w+b").name)
def test_dump_raster(self): proj = osr.SpatialReference() proj.ImportFromEPSG(31466) test = zonalstats.DataSource(os.path.dirname(__file__) + '/../../examples/data/agger/agger_merge.shp', proj) self.assertRaises(AttributeError, test.dump_raster(tempfile.NamedTemporaryFile(mode='w+b').name, 'netCDF', pixel_size=100.))
def test__check_src(self): self.assertEqual(len(zonalstats.DataSource(os.path.dirname(__file__) + '/../../examples/data/agger/agger_merge.shp').data), 13) self.assertRaises(IOError, lambda: zonalstats.DataSource('test_zonalstats.py'))
class TestDataSource: # create synthetic box box0 = np.array([ [2600000.0, 5630000.0], [2600000.0, 5640000.0], [2610000.0, 5640000.0], [2610000.0, 5630000.0], [2600000.0, 5630000.0], ]) box1 = np.array([ [2700000.0, 5630000.0], [2700000.0, 5640000.0], [2710000.0, 5640000.0], [2710000.0, 5630000.0], [2700000.0, 5630000.0], ]) data = np.array([box0, box1], dtype=object) ds = zonalstats.DataSource(data) values1 = np.array([47.11, 47.11]) values2 = np.array([47.11, 15.08]) @requires_data def test__check_src(self): filename = util.get_wradlib_data_file("shapefiles/agger/" "agger_merge.shp") assert len(zonalstats.DataSource(filename).data) == 13 with pytest.raises(RuntimeError): zonalstats.DataSource("test_zonalstats.py") def test_data(self): np.testing.assert_almost_equal(self.ds.data, self.data) def test__get_data(self): ds = zonalstats.DataSource(self.data) np.testing.assert_almost_equal(ds._get_data(), self.data) def test_get_data_by_idx(self): ds = zonalstats.DataSource(self.data) np.testing.assert_almost_equal(ds.get_data_by_idx([0]), self.data[0:1]) np.testing.assert_almost_equal(ds.get_data_by_idx([1]), self.data[1:2]) np.testing.assert_almost_equal(ds.get_data_by_idx([0, 1]), self.data) def test_get_data_by_att(self): ds = zonalstats.DataSource(self.data) np.testing.assert_almost_equal(ds.get_data_by_att("index", 0), self.data[0:1]) np.testing.assert_almost_equal(ds.get_data_by_att("index", 1), self.data[1:2]) def test_get_data_by_geom(self): ds = zonalstats.DataSource(self.data) lyr = ds.ds.GetLayer() lyr.ResetReading() lyr.SetSpatialFilter(None) lyr.SetAttributeFilter(None) for i, feature in enumerate(lyr): geom = feature.GetGeometryRef() np.testing.assert_almost_equal(ds.get_data_by_geom(geom), self.data[i:i + 1]) def test_set_attribute(self): ds = zonalstats.DataSource(self.data) ds.set_attribute("test", self.values1) assert np.allclose(ds.get_attributes(["test"]), self.values1) ds.set_attribute("test", self.values2) assert np.allclose(ds.get_attributes(["test"]), self.values2) def test_get_attributes(self): ds = zonalstats.DataSource(self.data) ds.set_attribute("test", self.values2) assert ds.get_attributes(["test"], filt=("index", 0)) == self.values2[0] assert ds.get_attributes(["test"], filt=("index", 1)) == self.values2[1] @requires_data def test_get_geom_properties(self): proj = osr.SpatialReference() proj.ImportFromEPSG(31466) filename = util.get_wradlib_data_file("shapefiles/agger/" "agger_merge.shp") test = zonalstats.DataSource(filename, proj) np.testing.assert_array_equal([[76722499.98474795]], test.get_geom_properties(["Area"], filt=("FID", 1))) def test_dump_vector(self): ds = zonalstats.DataSource(self.data) ds.dump_vector(tempfile.NamedTemporaryFile(mode="w+b").name) @requires_data def test_dump_raster(self): proj = osr.SpatialReference() proj.ImportFromEPSG(31466) filename = util.get_wradlib_data_file("shapefiles/agger/" "agger_merge.shp") test = zonalstats.DataSource(filename, srs=proj) test.dump_raster( tempfile.NamedTemporaryFile(mode="w+b").name, driver="netCDF", pixel_size=100.0, ) test.dump_raster( tempfile.NamedTemporaryFile(mode="w+b").name, driver="netCDF", pixel_size=100.0, attr="FID", )
def test__get_data(self): ds = zonalstats.DataSource(self.data) assert np.allclose(ds._get_data(), self.data)