예제 #1
0
def calculate_vulnerability(iso3, country_folder, country_bounds, pop_folder, pop_files):
    ''' The hospitalization rates listed in the vul_def dictionary (at the top of this script) are
        combined to create a vulnerability layer
        
        INPUTS
            iso3 [string] - iso3 code for country of interest
            country_folder [string] - path to output folder 
            country_bounds [geopandas dataframe] - boundary within which to extract
            pop_files [list of file paths] - list of global demographic rasters which are clipped out with country_bounds
        RETURNS
            NA - creates a file called WP2020_vulnerability_map.tif in country_folder            
    '''
    
    # clip out the temporary vulnerability metrics
    out_vulnerability = os.path.join(country_folder, "WP2020_vulnerability_map.tif")
    if not os.path.exists(out_vulnerability):
        wp_files = []
        if not os.path.exists(country_folder):
            os.makedirs(country_folder)
        for pFile in pop_files:
            curR = rasterio.open((os.path.join(pop_folder, pFile)))
            out_file = os.path.join(country_folder, pFile)
            if not os.path.exists(out_file):
                rMisc.clipRaster(curR, country_bounds, out_file)
            wp_files.append(out_file)
        #Calculate vulnerability
        wp_file_objects  = [vulmap.wp_demographics(os.path.join(country_folder, x)) for x in wp_files]
        vul = vulmap.wp_vulnerability(wp_file_objects, vul_def)
        vul.calculate_vulnerability()
        vul.combine_results(out_vulnerability, '')
        for f in wp_files:
            os.remove(f)
예제 #2
0
def extract_data(inG, inG1, inG2, inL, inR):
    country_folder = os.path.join(output_folder, iso3)
    adm0_file = os.path.join(country_folder, "adm0.shp")
    adm1_file = os.path.join(country_folder, "adm1.shp")
    adm2_file = os.path.join(country_folder, "adm2.shp")
    lc_file = os.path.join(country_folder, "LC.tif")

    if not os.path.exists(country_folder):
        os.makedirs(country_folder)
    country_bounds = inG.loc[inG['ISO3'] == iso3].to_crs({'init': 'epsg:4326'})
    if not os.path.exists(adm0_file):
        country_bounds.to_file(adm0_file)
    if not os.path.exists(adm1_file):
        try:
            country_adm1 = inG1.loc[inG1['ISO3'] == iso3].to_crs(
                {'init': 'epsg:4326'})
            country_adm1.to_file(adm1_file)
        except:
            misc.tPrint("%s Could not extract ADMIN 1" % iso3)
    if not os.path.exists(adm2_file):
        try:
            country_adm2 = inG2.loc[inG2['ISO3'] == iso3].to_crs(
                {'init': 'epsg:4326'})
            country_adm2.to_file(adm2_file)
        except:
            misc.tPrint("%s Could not extract ADMIN 2" % iso3)
    if not os.path.exists(lc_file):
        rMisc.clipRaster(inL, gpd.read_file(adm0_file), lc_file)

    calculate_vulnerability(iso3, country_folder, country_bounds, pop_folder,
                            pop_files)
    misc.tPrint("***%s Calculated Vulnerability" % iso3)
    try:
        create_urban_data(iso3,
                          country_folder,
                          country_bounds,
                          inR,
                          calc_urban=False)
        misc.tPrint("***%s Calculated Urban Extents" % iso3)
    except:
        misc.tPrint("%s errored on HD clusters" % iso3)
        try:
            create_urban_data(iso3,
                              country_folder,
                              country_bounds,
                              inR,
                              calc_urban=True,
                              calc_hd_urban=False)
        except:
            misc.tPrint("%s errored on all clusters" % iso3)
예제 #3
0
def create_urban_data(iso3,
                      country_folder,
                      country_bounds,
                      inR,
                      calc_urban=True,
                      calc_hd_urban=True,
                      verbose=False,
                      urb_dens=300,
                      urb_pop=5000,
                      hd_urb_dens=1500,
                      hd_urb_pop=50000):
    ''' Extract urban areas from gridded population data following the EC density methodology
    
    INPUTS:
        iso3 [string] - iso3 code for country of interest
        country_folder [string] - path to output folder 
        country_bounds [geopandas dataframe] - boundary within which to extract
        inR [rasterio object] - population datasets from which country specific pop layer is extracted
        [optional] calc_urban [boolean] - whether to calculate urban (lower density) extents
        [optional] calc_hd_urban [boolean] - whether to calculate hd urban (higher density) extents
        [optional] urb_dens/hd_urb_dens [int] - population density threshold for calculating urban areas IN THE PER PIXEL UNITS OF inR
        [optional] urb_pop/hd_urb_pop [int] - total population threshold for calculating urban areas
        
    RETURNS:
        NA - the function calculates a number of files in the folder country_folder:
            -- WP_2020_1km.tif: population dataaset clipped from inR
            -- urban_areas.shp: vectorization of urban areas calculation
            -- urban_areas_hd.shp: vectorization of high density urban areas calculation
            -- urban_fishnets/URBAN_XX.shp: a 1 km fishnet is created over the five highest population areas in urban_areas.shp 
            -- hd_urban_fishnets/HD_URBAN_XX.shp: a 1 km fishnet is created over the five highest population areas in hd_urban_areas.shp 
    '''

    country_pop = os.path.join(country_folder, "WP_2020_1km.tif")
    urban_pop = os.path.join(country_folder, "WP_2020_1km_urban_pop.tif")
    hd_urban_pop = os.path.join(country_folder, "WP_2020_1km_hd_urban_pop.tif")
    urb_bounds = os.path.join(country_folder, "urban_areas.shp")
    hd_urb_bounds = os.path.join(country_folder, "urban_areas_hd.shp")
    urban_fishnets = os.path.join(country_folder, "urban_fishnets")
    hd_urban_fishnets = os.path.join(country_folder, "hd_urban_fishnets")

    # Clip pop raster
    if not os.path.exists(country_pop):
        rMisc.clipRaster(inR, country_bounds, country_pop)

    if not os.path.exists(urban_pop) and calc_urban:
        urbanR = urban.urbanGriddedPop(country_pop)
        urban_vec = urbanR.calculateUrban(densVal=urb_dens,
                                          totalPopThresh=urb_pop,
                                          smooth=True,
                                          queen=False,
                                          raster_pop=urban_pop)
        urban_vec.to_file(urb_bounds)

    if not os.path.exists(hd_urban_pop) and calc_hd_urban:
        urbanR = urban.urbanGriddedPop(country_pop)
        urban_vec = urbanR.calculateUrban(densVal=hd_urb_dens,
                                          totalPopThresh=hd_urb_pop,
                                          smooth=True,
                                          queen=True,
                                          raster_pop=urban_pop)
        urban_vec.to_file(hd_urb_bounds)
    #Generate Fishnets
    if not os.path.exists(urban_fishnets):
        os.makedirs(urban_fishnets)

    if not os.path.exists(hd_urban_fishnets):
        os.makedirs(hd_urban_fishnets)

    if calc_urban:
        create_fishnet(urb_bounds, urban_fishnets, "URBAN")
    if calc_hd_urban:
        create_fishnet(hd_urb_bounds, hd_urban_fishnets, "HD_URBAN")