def add_sea(exposures, sea_res, scheduler=None): """Add sea to geometry's surroundings with given resolution. region_id set to -1 and other variables to 0. Parameters ---------- exposures : Exposures the Exposures object without sea surroundings. sea_res : tuple (float,float) (sea_coast_km, sea_res_km), where first parameter is distance from coast to fill with water and second parameter is resolution between sea points scheduler : str, optional used for dask map_partitions. “threads”, “synchronous” or “processes” Returns: Exposures """ LOGGER.info( "Adding sea at %s km resolution and %s km distance from coast.", str(sea_res[1]), str(sea_res[0])) sea_res = (sea_res[0] / ONE_LAT_KM, sea_res[1] / ONE_LAT_KM) min_lat = max(-90, float(exposures.gdf.latitude.min()) - sea_res[0]) max_lat = min(90, float(exposures.gdf.latitude.max()) + sea_res[0]) min_lon = max(-180, float(exposures.gdf.longitude.min()) - sea_res[0]) max_lon = min(180, float(exposures.gdf.longitude.max()) + sea_res[0]) lat_arr = np.arange(min_lat, max_lat + sea_res[1], sea_res[1]) lon_arr = np.arange(min_lon, max_lon + sea_res[1], sea_res[1]) lon_mgrid, lat_mgrid = np.meshgrid(lon_arr, lat_arr) lon_mgrid, lat_mgrid = lon_mgrid.ravel(), lat_mgrid.ravel() on_land = ~u_coord.coord_on_land(lat_mgrid, lon_mgrid) sea_exp_gdf = GeoDataFrame() sea_exp_gdf['latitude'] = lat_mgrid[on_land] sea_exp_gdf['longitude'] = lon_mgrid[on_land] sea_exp_gdf['region_id'] = np.zeros(sea_exp_gdf.latitude.size, int) - 1 if 'geometry' in exposures.gdf.columns: u_coord.set_df_geometry_points(sea_exp_gdf, crs=exposures.crs, scheduler=scheduler) for var_name in exposures.gdf.columns: if var_name not in ('latitude', 'longitude', 'region_id', 'geometry'): sea_exp_gdf[var_name] = np.zeros(sea_exp_gdf.latitude.size, exposures.gdf[var_name].dtype) return Exposures(pd.concat([exposures.gdf, sea_exp_gdf], ignore_index=True, sort=False), crs=exposures.crs, ref_year=exposures.ref_year, value_unit=exposures.value_unit, meta=exposures.meta, tag=exposures.tag)
def set_geometry_points(self, scheduler=None): """Set geometry attribute of GeoDataFrame with Points from latitude and longitude attributes. Parameters: scheduler (str): used for dask map_partitions. “threads”, “synchronous” or “processes” """ u_coord.set_df_geometry_points(self.gdf, scheduler)
def test_set_df_geometry_points_pass(self): """ Test set_df_geometry_points """ df_val = gpd.GeoDataFrame(crs={'init':'epsg:2202'}) df_val['latitude'] = np.ones(10)*40.0 df_val['longitude'] = np.ones(10)*0.50 set_df_geometry_points(df_val) self.assertTrue(np.allclose(df_val.geometry[:].x.values, np.ones(10)*0.5)) self.assertTrue(np.allclose(df_val.geometry[:].y.values, np.ones(10)*40.))