def test__get_idx_weights(self): zdp = zonalstats.ZonalDataPoly(self.src, self.trg, srs=self.proj) assert zdp._get_idx_weights() == ( [np.array([0]), np.array([1])], [np.array([25000000.0]), np.array([25000000.0])], )
def setUp(self): # GK3-Projection self.proj = osr.SpatialReference() self.proj.ImportFromEPSG(31466) # create synthetic box self.box0 = np.array([[2600000., 5630000.], [2600000., 5640000.], [2610000., 5640000.], [2610000., 5630000.], [2600000., 5630000.]]) self.box1 = np.array([[2610000., 5630000.], [2610000., 5640000.], [2620000., 5640000.], [2620000., 5630000.], [2610000., 5630000.]]) self.box3 = np.array([[2595000., 5625000.], [2595000., 5635000.], [2605000., 5635000.], [2605000., 5625000.], [2595000., 5625000.]]) self.box4 = np.array([[2615000., 5635000.], [2615000., 5645000.], [2625000., 5645000.], [2625000., 5635000.], [2615000., 5635000.]]) self.box5 = np.array([[2600000., 5635000.], [2605000., 5635000.], [2605000., 5630000.], [2600000., 5630000.], [2600000., 5635000.]]) self.box6 = np.array([[2615000., 5635000.], [2615000., 5640000.], [2620000., 5640000.], [2620000., 5635000.], [2615000., 5635000.]]) self.box7 = np.array([[2715000., 5635000.], [2715000., 5640000.], [2720000., 5640000.], [2720000., 5635000.], [2715000., 5635000.]]) self.src = np.array([self.box0, self.box1]) self.trg = np.array([self.box3, self.box4]) self.dst = np.array([[self.box5], [self.box6]]) self.zdb = zonalstats.ZonalDataBase(self.src, self.trg, srs=self.proj) self.zdp = zonalstats.ZonalDataPoly(self.src, self.trg, srs=self.proj)
def setUp(self): global skip # setup test grid and catchment lon = 7.071664 lat = 50.730521 r = np.array(range(50, 100 * 1000 + 50, 100)) a = np.array(range(0, 360, 1)) rays = a.shape[0] bins = r.shape[0] # setup OSR objects self.proj_gk = osr.SpatialReference() self.proj_gk.ImportFromEPSG(31466) self.proj_ll = osr.SpatialReference() self.proj_ll.ImportFromEPSG(4326) # create polar grid polygon vertices in lat,lon radar_ll = georef.spherical_to_polyvert(r, a, 0, (lon, lat), proj=self.proj_ll)[..., 0:2] # create polar grid centroids in lat,lon coords = georef.spherical_to_centroids(r, a, 0, (lon, lat), proj=self.proj_ll) radar_llc = coords[..., 0:2] # project ll grids to GK2 self.radar_gk = georef.reproject(radar_ll, projection_source=self.proj_ll, projection_target=self.proj_gk) self.radar_gkc = georef.reproject(radar_llc, projection_source=self.proj_ll, projection_target=self.proj_gk) # reshape self.radar_gk.shape = (rays, bins, 5, 2) self.radar_gkc.shape = (rays, bins, 2) self.box0 = np.array([[2600000., 5630000.], [2600000., 5630100.], [2600100., 5630100.], [2600100., 5630000.], [2600000., 5630000.]]) self.box1 = np.array([[2600100., 5630000.], [2600100., 5630100.], [2600200., 5630100.], [2600200., 5630000.], [2600100., 5630000.]]) self.data = np.array([self.box0, self.box1]) # create catchment bounding box buffer = 5000. bbox = zonalstats.get_bbox(self.data[..., 0], self.data[..., 1]) bbox = dict(left=bbox['left'] - buffer, right=bbox['right'] + buffer, bottom=bbox['bottom'] - buffer, top=bbox['top'] + buffer) mask, shape = zonalstats.mask_from_bbox(self.radar_gkc[..., 0], self.radar_gkc[..., 1], bbox, polar=True) self.radar_gkc = self.radar_gkc[mask, :] self.radar_gk = self.radar_gk[mask] self.zdpoly = zonalstats.ZonalDataPoly(self.radar_gk, self.data, srs=self.proj_gk) # self.zdpoly.dump_vector('test_zdpoly') self.zdpoint = zonalstats.ZonalDataPoint(self.radar_gkc, self.data, srs=self.proj_gk) # self.zdpoint.dump_vector('test_zdpoint') isec_poly0 = np.array([ np.array([[2600000., 5630000.], [2600000., 5630100.], [2600009.61157242, 5630100.], [2600041.77844048, 5630000.], [2600000., 5630000.]]), np.array([[2600009.61157242, 5630100.], [2600100., 5630100.], [2600100., 5630000.], [2600041.77844048, 5630000.], [2600009.61157242, 5630100.]]), np.array([[2600091.80406488, 5630100.], [2600100., 5630100.], [2600100., 5630074.58501104], [2600091.80406488, 5630100.]]) ]) isec_poly1 = np.array([ np.array([[2600100., 5630000.], [2600100., 5630100.], [2600114.66582085, 5630100.], [2600146.83254704, 5630000.], [2600100., 5630000.]]), np.array([[2600114.66582085, 5630100.], [2600200., 5630100.], [2600200., 5630000.], [2600146.83254704, 5630000.], [2600114.66582085, 5630100.]]), np.array([[2600197.20644071, 5630100.], [2600200., 5630100.], [2600200., 5630091.33737992], [2600197.20644071, 5630100.]]) ]) isec_point0 = np.array([[2600077.2899581, 5630056.0874306]]) isec_point1 = np.array([[2600172.498418, 5630086.7127034]]) self.isec_poly = np.array([isec_poly0, isec_poly1]) self.isec_point = np.array([isec_point0, isec_point1])
def test__get_idx_weights(self): zdp = zonalstats.ZonalDataPoly(self.src, self.trg, srs=self.proj) self.assertEqual(zdp._get_idx_weights(), ([np.array([0]), np.array([1]) ], [np.array([25000000.]), np.array([25000000.])]))
def setUp(self): global skip # setup test grid and catchment lon = 7.071664 lat = 50.730521 r = np.array(range(50, 100 * 1000 + 50, 100)) a = np.array(range(0, 360, 1)) rays = a.shape[0] bins = r.shape[0] # create polar grid polygon vertices in lat,lon radar_ll = georef.polar2polyvert(r, a, (lon, lat)) # create polar grid centroids in lat,lon rlon, rlat = georef.polar2centroids(r, a, (lon, lat)) radar_llc = np.dstack((rlon, rlat)) # setup OSR objects self.proj_gk = osr.SpatialReference() self.proj_gk.ImportFromEPSG(31466) self.proj_ll = osr.SpatialReference() self.proj_ll.ImportFromEPSG(4326) # project ll grids to GK2 self.radar_gk = georef.reproject(radar_ll, projection_source=self.proj_ll, projection_target=self.proj_gk) self.radar_gkc = georef.reproject(radar_llc, projection_source=self.proj_ll, projection_target=self.proj_gk) # reshape self.radar_gk.shape = (rays, bins, 5, 2) self.radar_gkc.shape = (rays, bins, 2) self.box0 = np.array([[2600000., 5630000.], [2600000., 5630100.], [2600100., 5630100.], [2600100., 5630000.], [2600000., 5630000.]]) self.box1 = np.array([[2600100., 5630000.], [2600100., 5630100.], [2600200., 5630100.], [2600200., 5630000.], [2600100., 5630000.]]) self.data = np.array([self.box0, self.box1]) # create catchment bounding box buffer = 5000. bbox = zonalstats.get_bbox(self.data[..., 0], self.data[..., 1]) bbox = dict(left=bbox['left'] - buffer, right=bbox['right'] + buffer, bottom=bbox['bottom'] - buffer, top=bbox['top'] + buffer) mask, shape = zonalstats.mask_from_bbox(self.radar_gkc[..., 0], self.radar_gkc[..., 1], bbox, polar=True) self.radar_gkc = self.radar_gkc[mask, :] self.radar_gk = self.radar_gk[mask] self.zdpoly = zonalstats.ZonalDataPoly(self.radar_gk, self.data, srs=self.proj_gk) # self.zdpoly.dump_vector('test_zdpoly') self.zdpoint = zonalstats.ZonalDataPoint(self.radar_gkc, self.data, srs=self.proj_gk) # self.zdpoint.dump_vector('test_zdpoint') isec_poly0 = np.array([ np.array([[2600000., 5630000.], [2600000., 5630057.83273596], [2600018.65014816, 5630000.], [2600000., 5630000.]]), np.array([[2600000., 5630057.83273596], [2600000., 5630100.], [2600091.80406488, 5630100.], [2600100., 5630074.58501104], [2600100., 5630000.], [2600018.65014816, 5630000.], [2600000., 5630057.83273596]]), np.array([[2600091.80406488, 5630100.], [2600100., 5630100.], [2600100., 5630074.58501104], [2600091.80406488, 5630100.]]) ]) isec_poly1 = np.array([ np.array([[2600100., 5630000.], [2600100., 5630074.58501104], [2600124.05249566, 5630000.], [2600100., 5630000.]]), np.array([[2600100., 5630074.58501104], [2600100., 5630100.], [2600197.20644071, 5630100.], [2600200., 5630091.33737992], [2600200., 5630000.], [2600124.05249566, 5630000.], [2600100., 5630074.58501104]]), np.array([[2600197.20644071, 5630100.], [2600200., 5630100.], [2600200., 5630091.33737992], [2600197.20644071, 5630100.]]) ]) isec_point0 = np.array([[2600062.31245173, 5630031.20266055]]) isec_point1 = np.array([[2600157.8352244, 5630061.85098382]]) self.isec_poly = np.array([isec_poly0, isec_poly1]) self.isec_point = np.array([isec_point0, isec_point1])
class TestZonalData: global skip # setup test grid and catchment lon = 7.071664 lat = 50.730521 r = np.array(range(50, 100 * 1000 + 50, 100)) a = np.array(range(0, 360, 1)) rays = a.shape[0] bins = r.shape[0] # setup OSR objects proj_gk = osr.SpatialReference() proj_gk.ImportFromEPSG(31466) proj_ll = osr.SpatialReference() proj_ll.ImportFromEPSG(4326) # create polar grid polygon vertices in lat,lon radar_ll = georef.spherical_to_polyvert(r, a, 0, (lon, lat), proj=proj_ll)[..., 0:2] # create polar grid centroids in lat,lon coords = georef.spherical_to_centroids(r, a, 0, (lon, lat), proj=proj_ll) radar_llc = coords[..., 0:2] # project ll grids to GK2 radar_gk = georef.reproject(radar_ll, projection_source=proj_ll, projection_target=proj_gk) radar_gkc = georef.reproject(radar_llc, projection_source=proj_ll, projection_target=proj_gk) # reshape radar_gk.shape = (rays, bins, 5, 2) radar_gkc.shape = (rays, bins, 2) box0 = np.array([ [2600000.0, 5630000.0], [2600000.0, 5630100.0], [2600100.0, 5630100.0], [2600100.0, 5630000.0], [2600000.0, 5630000.0], ]) box1 = np.array([ [2600100.0, 5630000.0], [2600100.0, 5630100.0], [2600200.0, 5630100.0], [2600200.0, 5630000.0], [2600100.0, 5630000.0], ]) data = np.array([box0, box1]) # create catchment bounding box buffer = 5000.0 bbox = zonalstats.get_bbox(data[..., 0], data[..., 1]) bbox = dict( left=bbox["left"] - buffer, right=bbox["right"] + buffer, bottom=bbox["bottom"] - buffer, top=bbox["top"] + buffer, ) mask, shape = zonalstats.mask_from_bbox(radar_gkc[..., 0], radar_gkc[..., 1], bbox, polar=True) radar_gkc = radar_gkc[mask, :] radar_gk = radar_gk[mask] zdpoly = zonalstats.ZonalDataPoly(radar_gk, data, srs=proj_gk) # zdpoly.dump_vector('test_zdpoly') zdpoint = zonalstats.ZonalDataPoint(radar_gkc, data, srs=proj_gk) # zdpoint.dump_vector('test_zdpoint') isec_poly0 = np.array( [ np.array([ [2600000.0, 5630000.0], [2600000.0, 5630100.0], [2600009.61157242, 5630100.0], [2600041.77844048, 5630000.0], [2600000.0, 5630000.0], ]), np.array([ [2600009.61157242, 5630100.0], [2600100.0, 5630100.0], [2600100.0, 5630000.0], [2600041.77844048, 5630000.0], [2600009.61157242, 5630100.0], ]), np.array([ [2600091.80406488, 5630100.0], [2600100.0, 5630100.0], [2600100.0, 5630074.58501104], [2600091.80406488, 5630100.0], ]), ], dtype=object, ) isec_poly1 = np.array( [ np.array([ [2600100.0, 5630000.0], [2600100.0, 5630100.0], [2600114.66582085, 5630100.0], [2600146.83254704, 5630000.0], [2600100.0, 5630000.0], ]), np.array([ [2600114.66582085, 5630100.0], [2600200.0, 5630100.0], [2600200.0, 5630000.0], [2600146.83254704, 5630000.0], [2600114.66582085, 5630100.0], ]), np.array([ [2600197.20644071, 5630100.0], [2600200.0, 5630100.0], [2600200.0, 5630091.33737992], [2600197.20644071, 5630100.0], ]), ], dtype=object, ) isec_point0 = np.array([[2600077.2899581, 5630056.0874306]]) isec_point1 = np.array([[2600172.498418, 5630086.7127034]]) isec_poly = np.array([isec_poly0, isec_poly1]) isec_point = np.array([isec_point0, isec_point1]) def test_srs(self): assert self.zdpoly.srs == self.proj_gk assert self.zdpoint.srs == self.proj_gk def test_isecs(self): # need to iterate over nested array for correct testing for i, ival in enumerate(self.zdpoly.isecs): for k, kval in enumerate(ival): np.testing.assert_allclose(kval.astype(float), self.isec_poly[i, k], rtol=1e-6) np.testing.assert_allclose(self.zdpoint.isecs.astype(float), self.isec_point, rtol=1e-6) def test_get_isec(self): for i in [0, 1]: for k, arr in enumerate(self.zdpoly.get_isec(i)): np.testing.assert_allclose(arr.astype(float), self.isec_poly[i, k], rtol=1e-6) np.testing.assert_allclose(self.zdpoint.get_isec(i).astype(float), self.isec_point[i], rtol=1e-6) def test_get_source_index(self): np.testing.assert_array_equal(self.zdpoly.get_source_index(0), np.array([2254, 2255])) np.testing.assert_array_equal(self.zdpoly.get_source_index(1), np.array([2255, 2256])) np.testing.assert_array_equal(self.zdpoint.get_source_index(0), np.array([2255])) np.testing.assert_array_equal(self.zdpoint.get_source_index(1), np.array([2256]))
class TestZonalStatsBase: # GK3-Projection proj = osr.SpatialReference() proj.ImportFromEPSG(31466) # 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([ [2610000.0, 5630000.0], [2610000.0, 5640000.0], [2620000.0, 5640000.0], [2620000.0, 5630000.0], [2610000.0, 5630000.0], ]) box3 = np.array([ [2595000.0, 5625000.0], [2595000.0, 5635000.0], [2605000.0, 5635000.0], [2605000.0, 5625000.0], [2595000.0, 5625000.0], ]) box4 = np.array([ [2615000.0, 5635000.0], [2615000.0, 5645000.0], [2625000.0, 5645000.0], [2625000.0, 5635000.0], [2615000.0, 5635000.0], ]) box5 = np.array([ [2600000.0, 5635000.0], [2605000.0, 5635000.0], [2605000.0, 5630000.0], [2600000.0, 5630000.0], [2600000.0, 5635000.0], ]) box6 = np.array([ [2615000.0, 5635000.0], [2615000.0, 5640000.0], [2620000.0, 5640000.0], [2620000.0, 5635000.0], [2615000.0, 5635000.0], ]) box7 = np.array([ [2715000.0, 5635000.0], [2715000.0, 5640000.0], [2720000.0, 5640000.0], [2720000.0, 5635000.0], [2715000.0, 5635000.0], ]) src = np.array([box0, box1]) trg = np.array([box3, box4]) dst = np.array([[box5], [box6]]) zdb = zonalstats.ZonalDataBase(src, trg, srs=proj) zdp = zonalstats.ZonalDataPoly(src, trg, srs=proj) def test__init__(self): with pytest.raises(NotImplementedError): zonalstats.ZonalStatsBase(self.zdb) zonalstats.ZonalStatsBase(self.zdp) with pytest.raises(TypeError): zonalstats.ZonalStatsBase("test") with pytest.raises(TypeError): zonalstats.ZonalStatsBase() with pytest.raises(TypeError): zonalstats.ZonalStatsBase(ix=np.arange(10), w=np.arange(11)) def test_w(self): zdp = zonalstats.ZonalStatsBase(self.zdp) np.testing.assert_equal(zdp.w, np.array([[25000000.0], [25000000.0]])) np.testing.assert_equal(zdp.ix, np.array([[0], [1]])) def test__check_vals(self): zdp = zonalstats.ZonalStatsBase(self.zdp) with pytest.raises(AssertionError): zdp._check_vals(np.arange(3)) def test_mean(self): zdp = zonalstats.ZonalStatsBase(self.zdp) np.testing.assert_equal(zdp.mean(np.arange(10, 21, 10)), np.array([10, 20])) def test_var(self): zdp = zonalstats.ZonalStatsBase(self.zdp) np.testing.assert_equal(zdp.var(np.arange(10, 21, 10)), np.array([0, 0]))