def test_spherical_to_centroids(self): r = np.array([10000., 10100.]) az = np.array([45., 90.]) sitecoords = (9., 48., 0.) sph = georef.get_default_projection() centroids = georef.spherical_to_centroids(r, az, 0, sitecoords, proj=sph) arr = np.asarray([[[9.09439583, 48.06323717, 6.], [9.09534571, 48.06387232, 6.]], [[9.1333325, 47.99992262, 6.], [9.13467253, 47.99992106, 6.]]]) np.testing.assert_array_almost_equal(centroids, arr, decimal=3) centroids, pr = georef.spherical_to_centroids(r, az, 0, sitecoords) arr = np.asarray([[[7.0357090e+03, 7.0357090e+03, 6.], [7.1064194e+03, 7.1064194e+03, 6.]], [[9.9499951e+03, 0., 6.], [1.0049995e+04, 0., 6.]]]) np.testing.assert_array_almost_equal(centroids, arr, decimal=3)
def test_spherical_to_centroids(self): r = np.array([10000., 10100.]) az = np.array([45., 90.]) sitecoords = (9., 48., 0.) sph = georef.get_default_projection() centroids = georef.spherical_to_centroids(r, az, 0, sitecoords, proj=sph) arr = np.asarray([[[9.09439583, 48.06323717, 6.], [9.09534571, 48.06387232, 6.]], [[9.1333325, 47.99992262, 6.], [9.13467253, 47.99992106, 6.]]]) np.testing.assert_array_almost_equal(centroids, arr, decimal=3) centroids, pr = georef.spherical_to_centroids(r, az, 0, sitecoords) arr = np.asarray([[[7.0357090e+03, 7.0357090e+03, 6.], [7.1064194e+03, 7.1064194e+03, 6.]], [[9.9499951e+03, 0., 6.], [1.0049995e+04, 0., 6.]]]) np.testing.assert_array_almost_equal(centroids, arr, decimal=3)
def test_spherical_to_centroids(self): r = np.array([10000., 10100.]) az = np.array([45., 90.]) sitecoords = (9., 48., 0.) sph = georef.get_default_projection() centroids = georef.spherical_to_centroids(r, az, 0, sitecoords, proj=sph) arr = np.asarray([[[9.09439583, 48.06323717, 6.], [9.09534571, 48.06387232, 6.]], [[9.1333325, 47.99992262, 6.], [9.13467253, 47.99992106, 6.]]]) self.assertTrue(np.allclose(centroids, arr))
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])
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]))
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])