def test_get_land_geometry_extent_pass(self): """get_land_geometry with selected countries.""" lat = np.array([28.203216, 28.555994, 28.860875]) lon = np.array([-16.567489, -18.554130, -9.532476]) res = get_land_geometry(extent=(np.min(lon), np.max(lon), np.min(lat), np.max(lat)), resolution=10) self.assertIsInstance(res, shapely.geometry.multipolygon.MultiPolygon) self.assertAlmostEqual(res.bounds[0], -18.002186653) self.assertAlmostEqual(res.bounds[1], lat[0]) self.assertAlmostEqual(res.bounds[2], np.max(lon)) self.assertAlmostEqual(res.bounds[3], np.max(lat))
def test_get_land_geometry_country_pass(self): """get_land_geometry with selected countries.""" iso_countries = ['DEU', 'VNM'] res = get_land_geometry(iso_countries, 110) self.assertIsInstance(res, shapely.geometry.multipolygon.MultiPolygon) for res, ref in zip(res.bounds, (5.85248986800, 8.56557851800, 109.47242272200, 55.065334377000)): self.assertAlmostEqual(res, ref) iso_countries = ['ESP'] res = get_land_geometry(iso_countries, 110) self.assertIsInstance(res, shapely.geometry.multipolygon.MultiPolygon) for res, ref in zip(res.bounds, (-18.16722571499986, 27.642238674000, 4.337087436000, 43.793443101)): self.assertAlmostEqual(res, ref) iso_countries = ['FRA'] res = get_land_geometry(iso_countries, 110) self.assertIsInstance(res, shapely.geometry.multipolygon.MultiPolygon) for res, ref in zip(res.bounds, (-61.79784094999991, -21.37078215899993, 55.854502800000034, 51.08754088371883)): self.assertAlmostEqual(res, ref)
def test_get_land_geometry_all_pass(self): """get_land_geometry with all earth.""" res = get_land_geometry(resolution=110) self.assertIsInstance(res, shapely.geometry.multipolygon.MultiPolygon) self.assertEqual(res.area, 21496.99098799273)
def set_from_nc(self, dph_path=None, frc_path=None, origin=False, centroids=None, countries=None, reg=None, shape=None, ISINatIDGrid=False, years=None): """Wrapper to fill hazard from nc_flood file Parameters: dph_path (string): Flood file to read (depth) frc_path (string): Flood file to read (fraction) origin (bool): Historical or probabilistic event centroids (Centroids): centroids to extract countries (list of countries ISO3) selection of countries (reg must be None!) reg (list of regions): can be set with region code if whole areas are considered (if not None, countries and centroids are ignored) ISINatIDGrid (Bool): Indicates whether ISIMIP_NatIDGrid is used years (int list): years that are considered raises: NameError """ if years is None: years = [2000] if dph_path is None: LOGGER.error('No flood-depth-path set') raise NameError if frc_path is None: LOGGER.error('No flood-fraction-path set') raise NameError if not Path(dph_path).exists(): LOGGER.error('Invalid flood-file path %s', dph_path) raise NameError if not Path(frc_path).exists(): LOGGER.error('Invalid flood-file path %s', frc_path) raise NameError with xr.open_dataset(dph_path) as flood_dph: time = flood_dph.time.data event_index = self._select_event(time, years) bands = event_index + 1 if countries or reg: # centroids as points if ISINatIDGrid: dest_centroids = RiverFlood._select_exact_area(countries, reg)[0] meta_centroids = copy.copy(dest_centroids) meta_centroids.set_lat_lon_to_meta() self.set_raster(files_intensity=[dph_path], files_fraction=[frc_path], band=bands.tolist(), transform=meta_centroids.meta['transform'], width=meta_centroids.meta['width'], height=meta_centroids.meta['height'], resampling=Resampling.nearest) x_i = ((dest_centroids.lon - self.centroids.meta['transform'][2]) / self.centroids.meta['transform'][0]).astype(int) y_i = ((dest_centroids.lat - self.centroids.meta['transform'][5]) / self.centroids.meta['transform'][4]).astype(int) fraction = self.fraction[:, y_i * self.centroids.meta['width'] + x_i] intensity = self.intensity[:, y_i * self.centroids.meta['width'] + x_i] self.centroids = dest_centroids self.intensity = sp.sparse.csr_matrix(intensity) self.fraction = sp.sparse.csr_matrix(fraction) else: if reg: iso_codes = region2isos(reg) # envelope containing counties cntry_geom = get_land_geometry(iso_codes) self.set_raster(files_intensity=[dph_path], files_fraction=[frc_path], band=bands.tolist(), geometry=cntry_geom) # self.centroids.set_meta_to_lat_lon() else: cntry_geom = get_land_geometry(countries) self.set_raster(files_intensity=[dph_path], files_fraction=[frc_path], band=bands.tolist(), geometry=cntry_geom) # self.centroids.set_meta_to_lat_lon() elif shape: shapes = gpd.read_file(shape) rand_geom = shapes.geometry[0] self.set_raster(files_intensity=[dph_path], files_fraction=[frc_path], band=bands.tolist(), geometry=rand_geom) return elif not centroids: # centroids as raster self.set_raster(files_intensity=[dph_path], files_fraction=[frc_path], band=bands.tolist()) # self.centroids.set_meta_to_lat_lon() else: # use given centroids # if centroids.meta or grid_is_regular(centroids)[0]: #TODO: implement case when meta or regulargrid is defined # centroids.meta or grid_is_regular(centroidsxarray)[0]: # centroids>flood --> error # reprojection, resampling.average (centroids< flood) # (transform) # reprojection change resampling""" # else: if centroids.meta: centroids.set_meta_to_lat_lon() metafrc, fraction = read_raster(frc_path, band=bands.tolist()) metaint, intensity = read_raster(dph_path, band=bands.tolist()) x_i = ((centroids.lon - metafrc['transform'][2]) / metafrc['transform'][0]).astype(int) y_i = ((centroids.lat - metafrc['transform'][5]) / metafrc['transform'][4]).astype(int) fraction = fraction[:, y_i * metafrc['width'] + x_i] intensity = intensity[:, y_i * metaint['width'] + x_i] self.centroids = centroids self.intensity = sp.sparse.csr_matrix(intensity) self.fraction = sp.sparse.csr_matrix(fraction) self.units = 'm' self.tag.file_name = str(dph_path) + ';' + str(frc_path) self.event_id = np.arange(self.intensity.shape[0]) self.event_name = list(map(str, years)) if origin: self.orig = np.ones(self.size, bool) else: self.orig = np.zeros(self.size, bool) self.frequency = np.ones(self.size) / self.size with xr.open_dataset(dph_path) as flood_dph: self.date = np.array([dt.datetime(flood_dph.time[i].dt.year, flood_dph.time[i].dt.month, flood_dph.time[i].dt.day).toordinal() for i in event_index])