Ejemplo 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
Ejemplo n.º 2
0
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