def make_osmexposure(highValueArea, mode="default", country=None, save_path=None, check_plot=1, **kwargs): """ Generate climada-compatiple entity by assigning values to midpoints of individual house shapes from OSM query, according to surface area and country. Parameters: highValueArea (str): absolute path for gdf of building features queried from get_features_OSM() mode (str): "LitPop" or "default": Default assigns a value of 5400 Chf to each m2 of building, LitPop assigns total LitPop value for the region proportionally to houses (by base area of house) Country (str): ISO3 code or name of country in which entity is located. Only if mode = LitPop kwargs (dict): arguments for LitPop set_country method Returns: exp_building (Exposure): (CLIMADA-compatible) with allocated asset values. Saved as exposure_buildings_mode_lat_lon.h5 Example: buildings_47_8 = \ make_osmexposure(save_path + '/OSM_features_47_8.shp', mode="default", save_path = save_path, check_plot=1) """ if save_path is None: save_path = Path.cwd() elif isinstance(save_path, str): save_path = Path(save_path) High_Value_Area_gdf = _get_midpoints(highValueArea) High_Value_Area_gdf = _assign_values_exposure(High_Value_Area_gdf, mode, country, **kwargs) # put back into CLIMADA-compatible entity format and save as hdf5 file: exp_buildings = Exposures(High_Value_Area_gdf) exp_buildings.set_lat_lon() exp_buildings.check() exp_buildings.write_hdf5( save_path.joinpath('exposure_buildings_' + mode + '_' + str(int(min(High_Value_Area_gdf.bounds.miny))) + '_' + str(int(min(High_Value_Area_gdf.bounds.minx))) + '.h5')) # plotting if check_plot == 1: # normal hexagons exp_buildings.plot_hexbin(pop_name=True) # select the OSM background image from the available ctx.sources # - returns connection error, left out for now: #fig, ax = exp_buildings.plot_basemap(buffer=30000, url=ctx.sources.OSM_C, cmap='brg') return exp_buildings
def get_osmstencil_litpop(bbox, country, mode, highValueArea=None, \ save_path=os.getcwd(), check_plot=1, **kwargs): """ Generate climada-compatible exposure by downloading LitPop exposure for a bounding box, corrected for centroids which lie inside a certain high-value multipolygon area from previous OSM query. Parameters: bbox (array): List of coordinates in format [South, West, North, East] Country (str): ISO3 code or name of country in which bbox is located highValueArea (str): path of gdf of high-value area from previous step. If empty, searches for cwd/High_Value_Area_lat_lon.shp mode (str): mode of re-assigning low-value points to high-value points. "nearest", "even", or "proportional" kwargs (dict): arguments for LitPop set_country method Returns: exp_sub_high_exp (Exposure): (CLIMADA-compatible) with re-allocated asset values with name exposure_high_lat_lon Example: exposure_high_47_8 = get_osmstencil_litpop([47.16, 8.0, 47.3, 8.0712],\ 'CHE',"proportional", highValueArea = \ save_path + '/High_Value_Area_47_8.shp' ,\ save_path = save_path) """ if highValueArea == None: try: High_Value_Area_gdf = \ geopandas.read_file(os.getcwd() + '/High_Value_Area_'+ str(int(bbox[0]))+'_'+ str(int(bbox[1]))+".shp") except: print('No file found of form %s. Please add or specify path.' \ %(os.getcwd() + 'High_Value_Area_'+str(int(bbox[0]))+'_'+\ str(int(bbox[1]))+".shp")) else: High_Value_Area_gdf = geopandas.read_file(highValueArea) exp_sub = _get_litpop_bbox(country, High_Value_Area_gdf, **kwargs) exp_sub_high = _split_exposure_highlow(exp_sub, mode, High_Value_Area_gdf) ###### how to "spread" centroids with value to e.g. hexagons? ########### # put exp_sub_high back into CLIMADA-compatible exposure format and save as hdf5 file: exp_sub_high_exp = Exposures(exp_sub_high) exp_sub_high_exp.set_lat_lon() exp_sub_high_exp.check() exp_sub_high_exp.write_hdf5(save_path + '/exposure_high_'+str(int(bbox[0]))+\ '_'+str(int(bbox[1]))+'.h5') # plotting if check_plot == 1: # normal hexagons exp_sub_high_exp.plot_hexbin(pop_name=True) # select the OSM background image from the available ctx.sources - doesnt work atm #fig, ax = exp_sub_high_exp.plot_basemap(buffer=30000, url=ctx.sources.OSM_C, cmap='brg') return exp_sub_high_exp
def load_exp_agr(force_new_hdf5_generation, name_hdf5_file, input_folder, haz_real): """ Parameters ---------- Load generate Exposure of agriculture if forced or if hdf5 file not present. Otherwise load hdf5 file. Parameters ---------- force_new_hdf5_generation : dict of bool contains bool wether new Exposure should be forcefully generated. name_hdf5_file : str name of hdf5 file from wich Exposure is loaded. input_folder : str Path to input folder containing hdf5 file. haz_real : climada.hazard.base.Hazard CLIMADA hazard. Returns ------- exp_infr : climada.entity.exposures.base.Exposures CLIMADA Exposure of Exposure. """ file1 = Path(input_folder + "/" + name_hdf5_file["exp_agr"]) file2 = Path(input_folder + "/" + "exp_agr_no_centr.hdf5") if not file2.exists() and not file1.exists(): print("Please use import_agrar_exposure to create the hdf5 file!" + " and move it to the input folder") sys.exit() elif force_new_hdf5_generation["exp_agr"]: #be carefull, this step will take ages when you do both at once if not file2.exists(): print("Please use import_agrar_exposure to create the hdf5 file!" + " and move it to the input folder") sys.exit() exp_agr = Exposures() exp_agr.read_hdf5(input_folder + "/exp_agr_no_centr.hdf5") exp_agr.check() exp_agr.assign_centroids(haz_real, method = "NN", distance = "haversine", threshold = 2) exp_agr.check() exp_agr.write_hdf5(input_folder + "/exp_agr.hdf5") else: #Agrar Exposure exp_agr = Exposures() exp_agr.read_hdf5(input_folder + "/exp_agr.hdf5") exp_agr.check() return exp_agr
exposure_tmp = Exposures() if os.path.exists(os.path.join(RES_DIR, '%s_%ias.tiff' %(fi[0:-4]+fadd, res_target))): print('\n' + '\x1b[1;03;30;30m' + 'TIFF exists already, skipping: %s_%ias.tiff' %(fi[0:-4], res_target) + '\x1b[0m') continue else: print('\n' + '\x1b[1;03;30;30m' + 'Loading: %s ...' %(fi) + '\x1b[0m') exposure_tmp = exposure_tmp.from_csv(os.path.join(ENTITY_DIR, fi), index_col=None) if np.isnan(exposure_tmp.value.max()): continue exposure_tmp = Exposures(exposure_tmp) exposure_tmp.set_geometry_points() # set geometry attribute (shapely Points) from GeoDataFrame from latitude and longitude exposure_tmp.check() # puts metadata that has not been assigned if write_to_hdf5: exposure_tmp.write_hdf5(os.path.join(ENTITY_DIR_HDF5, '%s.hdf5' %(fi[0:-4]+fadd))) if write_to_tiff: if fix_zeros: exposure_tmp.value[exposure_tmp.value<1] = 1 # exposure_tmp.plot_raster(res=RES_ARCSEC/3600, save_tiff=\ # os.path.join(RES_DIR, '%s_%ias.tiff' %(fi[0:-4], RES_ARCSEC))) exposure_tmp.plot_raster(res=RES_ARCSEC/3600, raster_res=res_target/3600, save_tiff=\ os.path.join(RES_DIR, '%s_%ias.tiff' %(fi[0:-4]+fadd, res_target))) """COMBINE AND PLOT EXPOSURE AT TARGET RESOLUTION:""" print('\n' + '\x1b[1;03;30;30m' + 'COMBINE AND PLOT EXPOSURE AT TARGET RESOLUTION' + '\x1b[0m') for res_target in res_targets: exposure_data = Exposures() for idx, fi in enumerate(files): exposure_tmp = Exposures() if try_read_from_tiff and os.path.exists(os.path.join(RES_DIR, '%s_%ias.tiff' %(fi[0:-4]+fadd, res_target))):
avg_value_ackerbau = value_ackerbau / exp_hail_agr[exp_hail_agr["region_id"] == 221].shape[0] a = exp_hail_agr[exp_hail_agr["region_id"] == 201].assign(value=avg_value_obst) b = exp_hail_agr[exp_hail_agr["region_id"] == 202].assign( value=avg_value_rebbau) c = exp_hail_agr[exp_hail_agr["region_id"] == 221].assign( value=avg_value_ackerbau) exp_hail_agr = pd.concat([a, b, c]).sort_index() exp_hail_agr = Exposures(exp_hail_agr) exp_hail_agr.loc[exp_hail_agr["region_id"] == 201, "if_"] = int(3) exp_hail_agr.loc[exp_hail_agr["region_id"] == 202, "if_"] = int(2) exp_hail_agr.loc[exp_hail_agr["region_id"] == 221, "if_"] = int(4) exp_hail_agr = exp_hail_agr.rename(columns={'if_': 'if_HL'}) exp_hail_agr.check() exp_hail_agr.head() exp_hail_agr.value_unit = "CHF" exp_hail_agr.write_hdf5(input_folder + "/exp_agr_no_centr.hdf5") test = exp_hail_agr[exp_hail_agr["value"] > 0] plt.scatter(test[test["region_id"].isin([221, 222, 223])]["longitude"], test[test["region_id"].isin([221, 222, 223])]["latitude"]) plt.scatter(test[test["region_id"] == 201]["longitude"], test[test["region_id"] == 201]["latitude"]) plt.scatter(test[test["region_id"] == 202]["longitude"], test[test["region_id"] == 202]["latitude"]) plt.legend(labels=["Acker - Futterbau", "Obstbau", "Rebbau"])