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 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]))
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])
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])