Esempio n. 1
0
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
Esempio n. 3
0
         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
         exposure_data = exposure_data.append(exposure_tmp)
         if fix_zeros:
             exposure_tmp.value[exposure_tmp.value<plot_minimum] = plot_minimum
     else:
         print('\n' + '\x1b[1;03;30;30m' + 'ERROR Loading: %s_%ias.tiff' %(fi[0:-4]+fadd, res_target) + '\x1b[0m')
     
 print('\n' + '\x1b[1;03;30;30m' + 'Checking combined data...' + '\x1b[0m')
 exposure_data.check()
 if write_to_tiff:
     print('\n' + '\x1b[1;03;30;30m' + 'Writing combined data to TIFF...' + '\x1b[0m')
     exposure_data.plot_raster(res=res_target/3600, raster_res=res_target/3600, save_tiff=\
         os.path.join(RES_DIR, '%s_000_%ias.tiff' %(files[0][0:-8]+fadd, res_target)))
 ax_exp = exposure_data.plot_hexbin(pop_name=False, cmap='plasma', norm=LogNorm(vmin=plot_minimum, vmax=0.1*exposure_data.value.max()))
 if save_plots:
     plt.savefig(os.path.join(RES_DIR, 'LitPop_pc_%iarcsec_%i_%s_world_map.png' % (res_target, REF_YEAR, fadd)), \
                   dpi=300, facecolor='w', edgecolor='w', \
                   orientation='portrait', papertype=None, format='png', \
                   transparent=False, bbox_inches=None, pad_inches=0.1, \
                   frameon=None, metadata=None)
     plt.savefig(os.path.join(RES_DIR, 'LitPop_pc_%iarcsec_%i_%s_world_map.pdf' % (res_target, REF_YEAR, fadd)), \
                   dpi=300, facecolor='w', edgecolor='w', \
                   orientation='portrait', papertype=None, format='pdf', \
                   transparent=False, bbox_inches=None, pad_inches=0.1, \
                       frameon=None, metadata=None)
 
 
 ax_exp_scatter = exposure_data.plot_scatter(pop_name=False, cmap='plasma', s=.01, shapes=False, \
                                             norm=LogNorm(vmin=plot_minimum, vmax=np.max([0.1*exposure_data.value.max(), 10**9])))