コード例 #1
0
 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]
コード例 #2
0
ファイル: test_zonalstats.py プロジェクト: egouden/wradlib
 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])
コード例 #3
0
 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.)
コード例 #4
0
 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
コード例 #5
0
 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)))
コード例 #6
0
 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)
コード例 #7
0
    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])
コード例 #8
0
 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",
     )
コード例 #9
0
ファイル: test_zonalstats.py プロジェクト: egouden/wradlib
 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)
コード例 #10
0
 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)
コード例 #11
0
 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])
コード例 #12
0
 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)
コード例 #13
0
 def test__get_data(self):
     ds = zonalstats.DataSource(self.data)
     np.testing.assert_almost_equal(ds._get_data(), self.data)
コード例 #14
0
 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")
コード例 #15
0
ファイル: test_zonalstats.py プロジェクト: egouden/wradlib
 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)
コード例 #16
0
 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'))
コード例 #17
0
 def test_dump_vector(self):
     ds = zonalstats.DataSource(self.data)
     ds.dump_vector(tempfile.NamedTemporaryFile(mode="w+b").name)
コード例 #18
0
 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.))
コード例 #19
0
 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'))
コード例 #20
0
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",
        )
コード例 #21
0
ファイル: test_zonalstats.py プロジェクト: egouden/wradlib
 def test__get_data(self):
     ds = zonalstats.DataSource(self.data)
     assert np.allclose(ds._get_data(), self.data)