Exemplo n.º 1
0
"""
# import os
import numpy as np
from Hapi.gis.raster import Raster as R

Path = "data/GIS/ZonalStatistics/"
SavePath = Path
BaseMapF = Path + "Polygons.tif"

ExcludedValue = 0
Compressed = True
OccupiedCellsOnly = False

# one map
ExtractedValues, NonZeroCells = R.OverlayMap(Path + "data/Map1.zip", BaseMapF,
                                             ExcludedValue, Compressed,
                                             OccupiedCellsOnly)

MapPrefix = "Map"
# several maps
ExtractedValues, NonZeroCells = R.OverlayMaps(Path + "data", BaseMapF,
                                              MapPrefix, ExcludedValue,
                                              Compressed, OccupiedCellsOnly)

# save extracted values in different files
Polygons = list(ExtractedValues.keys())
for i in range(len(Polygons)):
    np.savetxt(SavePath + "/" + str(Polygons[i]) + ".txt",
               ExtractedValues[Polygons[i]],
               fmt="%4.2f")
Exemplo n.º 2
0
    def OverlayMaps(self, Path, BaseMapF, ExcludedValue, OccupiedCellsOnly,
                    SavePath):
        """
        ==================================================================
          OverlayMaps(self, Path, BaseMapF, ExcludedValue, OccupiedCellsOnly, SavePath)
        ==================================================================
        OverlayMaps method reads all the maps in the folder given by Path
        input and overlay them with the basemap and for each value in the basemap
        it create a dictionary with the intersected values from all maps

        Inputs:
            1-Path
                [String] a path to the folder includng the maps.
            2-BaseMapF:
                [String] a path includng the name of the ASCII and extention like
                path="data/cropped.asc"
            3-ExcludedValue:
                [Numeric] values you want to exclude from exteacted values
            5-OccupiedCellsOnly:
                [Bool] if you want to count only cells that is not ExcludedValue.
            6-SavePath:
                [String] a path to the folder to save a text file for each
                value in the base map including all the intersected values
                from other maps.
        Outputs:
            1- ExtractedValues:
                [Dict] dictonary with a list of values in the basemap as keys
                    and for each key a list of all the intersected values in the
                    maps from the path
            2- NonZeroCells:
                [dataframe] dataframe with the first column as the "file" name
                and the second column is the number of cells in each map
        """

        self.DepthValues, NonZeroCells = Raster.OverlayMaps(
            Path, BaseMapF, self.DepthPrefix, ExcludedValue, self.Compressed,
            OccupiedCellsOnly)

        # NonZeroCells dataframe with the first column as the "file" name and the second column
        # is the number of cells in each map

        NonZeroCells['days'] = [
            int(i[len(self.DepthPrefix):-4])
            for i in NonZeroCells['files'].tolist()
        ]
        # get the numbe of inundated cells in the Event index data frame
        self.EventIndex['cells'] = 0

        for i in range(len(NonZeroCells)):
            # get the location in the EventIndex dataframe
            try:
                loc = np.where(NonZeroCells.loc[i, 'days'] ==
                               self.EventIndex.loc[:, "id"])[0][0]
            except IndexError:
                # if it does not find the event in the eventindex table ignore
                continue
            # store number of cells
            self.EventIndex.loc[loc, 'cells'] = NonZeroCells.loc[i, 'cells']

        # save depths of each sub-basin
        inundatedSubs = list(self.DepthValues.keys())
        for i in range(len(inundatedSubs)):
            np.savetxt(SavePath + "/" + str(inundatedSubs[i]) + ".txt",
                       self.DepthValues[inundatedSubs[i]],
                       fmt="%4.2f")