Esempio n. 1
0
 def calculate_vulnerability(self, 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            
     '''
     iso3 = self.iso3
     country_folder = self.out_folder
     country_bounds = self.country_bounds
     out_vulnerability = self.out_vulnerability
     # clip out the temporary vulnerability metrics
     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)
Esempio n. 2
0
    def process_dem(self, global_dem=''):
        ''' Download DEM from AWS, calculate slope
        '''
        # Download DEM

        if not os.path.exists(self.dem_file) and global_dem == '':
            tPrint("Downloading DEM")
            elevation.clip(bounds=self.inD.total_bounds, max_download_tiles=90000, output=self.dem_file, product='SRTM3')

        if not os.path.exists(self.dem_file) and not global_dem == '':
            tPrint("Downloading DEM")
            rMisc.clipRaster(rasterio.open(global_dem), self.inD, self.dem_file)
            
        # Calculate slope
        if not os.path.exists(self.slope_file) and os.path.exists(self.dem_file):
            tPrint("Calculating slope")
            in_dem = rasterio.open(self.dem_file)
            in_dem_data = in_dem.read()
            beau  = richdem.rdarray(in_dem_data[0,:,:], no_data=in_dem.meta['nodata'])
            slope = richdem.TerrainAttribute(beau, attrib='slope_riserun')
            meta = in_dem.meta.copy()
            meta.update(dtype = slope.dtype)
            with rasterio.open(self.slope_file, 'w', **meta) as outR:
                outR.write_band(1, slope)
Esempio n. 3
0
    def extract_layers(self, global_landcover, global_ghspop, global_ghspop1k, global_ghbuilt, global_ghsl, global_smod):
        ''' extract global layers for current country
        '''
        # Extract water from globcover
        if not os.path.exists(self.lc_file_h20):
            tPrint("Extracting water")
            if not os.path.exists(self.lc_file):
                rMisc.clipRaster(rasterio.open(global_landcover), self.inD, self.lc_file)
            in_lc = rasterio.open(self.lc_file)
            inL = in_lc.read()
            lcmeta = in_lc.meta.copy()
            tempL = (inL == 210).astype(lcmeta['dtype'])
            lcmeta.update(nodata=255)
            with rasterio.open(self.lc_file_h20, 'w', **lcmeta) as out:
                out.write(tempL)
            os.remove(self.lc_file)
            
        # Extract water from GHSL
        if not os.path.exists(self.ghsl_h20):
            tPrint("Extracting water from GHSL")
            inR = rasterio.open(global_ghsl)
            ul = inR.index(*self.inD.total_bounds[0:2])
            lr = inR.index(*self.inD.total_bounds[2:4])
            # read the subset of the data into a numpy array
            window = ((float(lr[0]), float(ul[0]+1)), (float(ul[1]), float(lr[1]+1)))
            data = inR.read(1, window=window, masked = False)
            data = data == 1
            b = self.inD.total_bounds
            new_transform = rasterio.transform.from_bounds(b[0], b[1], b[2], b[3], data.shape[1], data.shape[0])
            meta = inR.meta.copy()
            meta.update(driver='GTiff',width=data.shape[1], height=data.shape[0], transform=new_transform)
            data = data.astype(meta['dtype'])
            with rasterio.open(self.ghsl_h20, 'w', **meta) as outR:
                outR.write_band(1, data)
            '''
            bounds = box(*self.inD.total_bounds)
            if inG.crs != self.inD.crs:
                destCRS = pyproj.Proj(inG.crs)
                fromCRS = pyproj.Proj(self.inD.crs)
                projector = partial(pyproj.transform, fromCRS, destCRS)
                bounds = transform(projector, bounds)
            def getFeatures(gdf):
                #Function to parse features from GeoDataFrame in such a manner that rasterio wants them
                return [json.loads(gdf.to_json())['features'][0]['geometry']]
            tD = gpd.GeoDataFrame([[1]], geometry=[bounds])
            coords = getFeatures(tD)
            out_img, out_transform = mask(inG, shapes=coords, crop=True)
            out_meta = inG.meta.copy()
            out_meta.update({"driver": "GTiff",
                             "height": out_img.shape[1],
                             "width": out_img.shape[2],
                             "transform": out_transform})
            water_data = (out_img == 1).astype(out_meta['dtype'])
            with rasterio.open(self.ghsl_h20, 'w', **out_meta) as outR:
                outR.write(water_data)
            '''
            
        #Extract GHS-Pop
        if not os.path.exists(self.ghspop_file):
            tPrint("Extracting GHS-POP")
            rMisc.clipRaster(rasterio.open(global_ghspop), self.inD, self.ghspop_file)
            
        #Extract GHS-Pop-1k
        if not os.path.exists(self.ghspop1k_file):
            tPrint("Extracting GHS-POP")
            rMisc.clipRaster(rasterio.open(global_ghspop1k), self.inD, self.ghspop1k_file)

        #Extract GHS-Built
        if not os.path.exists(self.ghsbuilt_file):
            tPrint("Clipping GHS-Built")
            rMisc.clipRaster(rasterio.open(global_ghbuilt), self.inD, self.ghsbuilt_file)        
            
        #Extract GHS-SMOD
        if not os.path.exists(self.ghssmod_file):
            tPrint("Clipping GHS-SMOD")
            rMisc.clipRaster(rasterio.open(global_smod), self.inD, self.ghssmod_file)
            
        #Rasterize admin boundaries
        if not os.path.exists(self.admin_file):
            tPrint("Rasterizing admin boundaries")
            xx = rasterio.open(self.ghspop_file)
            res = xx.meta['transform'][0]
            tempD = self.inD.to_crs(xx.crs)
            shapes = ((row['geometry'], 1) for idx, row in tempD.iterrows())
            burned = features.rasterize(shapes=shapes, out_shape=xx.shape, fill=0, transform=xx.meta['transform'], dtype='int16')
            meta = xx.meta.copy()
            meta.update(dtype=burned.dtype)
            with rasterio.open(self.admin_file, 'w', **meta) as outR:
                outR.write_band(1, burned)
Esempio n. 4
0
    def prepMappingData(self, floodFolder=''):
        '''convert all the data to shapefiles for use in mapping
           1. Health / Education points
           2. Health / Education access / routes
           3. OSM Summary
           4. LC Summary / Landcover
           5. GHSL Summary / GHSL
           
           Clip input raster datasets           
        '''
        def createPoints(inFile, outFile):
            #Map OSM point files
            if not os.path.exists(outFile):
                inDF = pd.read_csv(inFile)
                geoms = [Point(xy) for xy in zip(inDF.Lon, inDF.Lat)]
                inDF = inDF.drop(['Lat', 'Lon'], axis=1)
                inGDF = gpd.GeoDataFrame(inDF, geometry=geoms, crs=self.wgs84)
                inGDF.to_file(outFile)

        def mergeFile(inD, inFile, prefix):
            try:
                mData = pd.read_csv(inFile)
                mData.columns = [
                    prefix + s.replace(".", "") for s in mData.columns
                ]
                inD = inD.merge(mData,
                                how='left',
                                left_on="FID",
                                right_on=mData.columns[0])
            except:
                logging.info("could not process %s" % inFile)
            return (inD)

        createPoints(self.healthFacilities, self.healthMapping)
        createPoints(self.eduFacilities, self.eduMapping)
        if not os.path.exists(self.gridMapping):
            #A number of attributes need to be attached to the grid file
            inGrid = self.gridGPD
            #Merge the market access files, the osm summary, the ghsl summary
            inGrid = mergeFile(inGrid, self.healthMA, "H_")
            inGrid = mergeFile(inGrid, self.eduMA, "E_")
            inGrid = mergeFile(inGrid, self.osmSummary, "OSM_")
            inGrid = mergeFile(inGrid, self.ghslSummary, "GHSL_")
            inGrid = mergeFile(inGrid, self.lcFile, "LC_")
            inGrid = mergeFile(inGrid, self.ndviSummary, "NDVI_")
            inGrid = mergeFile(inGrid, self.baiSummary, "BAI_")
            inGrid = mergeFile(inGrid, self.srtmSummary, "ELEV_")
            inGrid = mergeFile(inGrid, self.SSBNReturn, "SSBN_")
            inGrid.to_file(self.gridMapping)

        #Clip input raster datasets
        urbanParams = misc.getUrbanParams()
        #Get reference to combined flood data
        curRasters = [
            urbanParams['afriCover20'], urbanParams['ghspop15'],
            urbanParams['ghslVRT'], urbanParams['worldPopAfrica']
        ]
        if floodFolder != '':
            cFloodFolder = os.path.join(floodFolder, "CombinedFloodData")
            floodRasters = os.listdir(cFloodFolder)
            for r in floodRasters:
                try:
                    curR = rasterio.open(os.path.join(cFloodFolder, r), 'r')
                    floodAOI = self.inputAOI.to_crs(curR.crs)
                    intersects = box(curR.bounds.left, curR.bounds.bottom,
                                     curR.bounds.right,
                                     curR.bounds.top).intersects(
                                         floodAOI.unary_union)
                    if intersects:
                        curRasters.append(os.path.join(cFloodFolder, r))
                except:
                    pass
        for inRaster in curRasters:
            outRaster = os.path.join(
                self.mappingFolder,
                os.path.basename(inRaster).replace(".vrt", ".tif"))
            if not os.path.exists(outRaster):
                rasterMisc.clipRaster(rasterio.open(inRaster), self.gridGPD,
                                      outRaster)
Esempio n. 5
0
 def clipGHSL(self):
     urbanParams = misc.getUrbanParams()
     ghslVRT = urbanParams["ghslVRT"]
     rasterMisc.clipRaster(rasterio.open(ghslVRT), self.gridGPD,
                           self.ghsl_raw)
Esempio n. 6
0
    def create_urban_data(self,
                          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 = self.country_pop
        urban_pop = self.urban_pop
        urb_bounds = self.urb_bounds
        hd_urb_bounds = self.hd_urb_bounds
        urban_fishnets = self.urban_fishnets
        hd_urban_fishnets = self.hd_urban_fishnets
        iso3 = self.iso3
        country_folder = self.out_folder
        country_bounds = self.country_bounds

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

        if not os.path.exists(urb_bounds) 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)
            if urban_vec.shape[0] > 0:
                urban_vec.to_file(urb_bounds)

        if not os.path.exists(hd_urb_bounds) 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)
            if urban_vec.shape[0] > 0:
                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)

        def create_fishnet(extents_file, out_folder, prefix):
            urban_extents = gpd.read_file(extents_file)
            sel_cities = urban_extents.sort_values(['Pop'],
                                                   ascending=False).iloc[0:5]
            try:
                sel_cities = misc.project_UTM(sel_cities)
            except:
                sel_cities = sel_cities.to_crs({"init": "epsg:3857"})

            for idx, row in sel_cities.iterrows():
                out_fishnet = os.path.join(out_folder,
                                           "%s_%s.shp" % (prefix, row['ID']))
                b = row['geometry'].bounds
                crs_num = sel_cities.crs['init'].split(":")[-1]
                crs_num = int(crs_num)
                misc.createFishnet(out_fishnet,
                                   b[0],
                                   b[2],
                                   b[1],
                                   b[3],
                                   1000,
                                   1000,
                                   crsNum=crs_num)
                fishnet = gpd.read_file(out_fishnet)
                fishnet = fishnet[fishnet.intersects(row['geometry'])]
                fishnet = fishnet.to_crs({'init': 'epsg:4326'})
                fishnet.to_file(out_fishnet)
                if verbose:
                    misc.tPrint("%s: %s" % (prefix, row['ID']))

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