Ejemplo n.º 1
0
def test_calculate_slope_raster(raster):
    from gemgis.raster import calculate_slope

    slope = calculate_slope(raster)

    assert isinstance(raster, rasterio.io.DatasetReader)
    assert raster.read(1).shape == (275, 250)
    assert raster.read(1).ndim == 2
    assert isinstance(slope, np.ndarray)
    assert slope.ndim == 2
    assert slope.shape == (275, 250)
Ejemplo n.º 2
0
def test_calculate_slope_array(array):
    from gemgis.raster import calculate_slope

    slope = calculate_slope(array, [0, 972, 0, 1069])

    assert isinstance(array, np.ndarray)
    assert array.shape == (1069, 972)
    assert array.ndim == 2
    assert isinstance(slope, np.ndarray)
    assert slope.ndim == 2
    assert slope[0][0] == 11.598369665181522
    assert slope.shape == (1069, 972)
Ejemplo n.º 3
0
def test_calculate_slope(raster):
    from gemgis.raster import calculate_slope

    slope = calculate_slope(raster)

    assert isinstance(raster, rasterio.io.DatasetReader)
    assert raster.read(1).shape == (1000, 1000)
    assert raster.read(1).ndim == 2
    assert isinstance(slope, np.ndarray)
    assert slope.ndim == 2
    assert slope[0][0] == 45
    for i in np.arange(0, 1000, 100):
        for j in np.arange(0, 1000, 100):
            assert round(slope[i][j], 10) == 45
    assert slope.shape == (1000, 1000)
Ejemplo n.º 4
0
def test_calculate_slope(raster):
    from gemgis.raster import calculate_slope

    with pytest.raises(TypeError):
        calculate_slope([raster])
Ejemplo n.º 5
0
    def __init__(self,
                 model_name=None,
                 crs=None,
                 extent=None,
                 resolution=None,
                 interfaces=None,
                 orientations=None,
                 section_dict=None,
                 customsections=None,
                 dem=None,
                 stack=None,
                 surface_colors=None,
                 is_fault=None,
                 geolmap=None,
                 basemap=None,
                 faults=None,
                 tectonics=None,
                 raw_i=None,
                 raw_o=None,
                 raw_dem=None,
                 wms=None,
                 slope=None,
                 hillshades=None,
                 aspect=None,
                 contours=None):

        # Checking if data type are correct

        # Checking if model name was provided as string
        if isinstance(model_name, (type(None), str)):
            self.model_name = model_name
        else:
            raise TypeError("Model Name must be of type str")

        # Checking if CRS was provided as string
        if isinstance(crs, (type(None), str)):
            self.crs = crs
        else:
            raise TypeError("CRS must be of type str")

        # Checking if extent was provided as list of 6 elements (int/floats)
        if isinstance(extent, (type(None), list)):
            if isinstance(extent, list):
                if len(extent) == 6:
                    if all(isinstance(n, (int, (int, float))) for n in extent):
                        self.extent = extent
                    else:
                        raise TypeError(
                            'Coordinates for extent must be provided as integers or floats'
                        )
                else:
                    raise ValueError(
                        'Length of extent must be 6 [minx,maxx,miny,maxy,minz,maxz]'
                    )
            self.extent = extent
        else:
            raise TypeError("Extent must be of type list")

        # Checking if the resolution was provided as list of 3 integers
        if isinstance(resolution, (type(None), list)):
            if isinstance(resolution, list):
                if len(resolution) == 3:
                    if all(isinstance(n, int) for n in resolution):
                        self.resolution = resolution
                    else:
                        raise TypeError(
                            'Values for resolution must be provided as integers'
                        )
                else:
                    raise ValueError('Length of resolution must be 3 [x,y,z]')
            self.resolution = resolution
        else:
            raise TypeError("Resolution must be of type list")

        # Checking if the interfaces object is a Pandas df containing all relevant columns
        if isinstance(interfaces, (type(None), pd.core.frame.DataFrame)):
            if isinstance(interfaces, pd.core.frame.DataFrame):
                assert pd.Series(['X', 'Y', 'Z',
                                  'formation']).isin(interfaces.columns).all(
                                  ), 'Interfaces DataFrame is missing columns'
            self.interfaces = interfaces
        else:
            raise TypeError("Interfaces df must be a Pandas DataFrame")

        # Checking if the orientations object is Pandas df containing all relevant columns
        if isinstance(orientations, (type(None), pd.core.frame.DataFrame)):
            if isinstance(orientations, pd.core.frame.DataFrame):
                assert pd.Series(
                    ['X', 'Y', 'Z', 'formation', 'dip', 'azimuth',
                     'polarity']).isin(orientations.columns).all(
                     ), 'Orientations DataFrame is missing columns'
            self.orientations = orientations
        else:
            raise TypeError("Orientations df must be a Pandas DataFrame")

        # Setting the section_dict attribute
        if isinstance(section_dict, (type(None), dict)):
            self.section_dict = section_dict
        else:
            raise TypeError("Section Dict must be of type dict")

        # Checking if the provided stack is of type dict
        if isinstance(stack, (type(None), dict)):
            self.stack = stack
        else:
            raise TypeError("Layer Stack must be of type dict")

        # Checking if the provided DEM object is either an np array, a file loaded with rasterio or a string
        if isinstance(
                dem, (type(None), np.ndarray, rasterio.io.DatasetReader, str)):
            self.dem = dem
        else:
            raise TypeError(
                "Digital Elevation Model must be a np Array, a raster loaded with rasterio or a string"
            )

        # Checking if the provided surface colors object is of type dict
        if isinstance(surface_colors, (type(None), dict)):
            self.surface_colors = surface_colors
        elif isinstance(surface_colors, str):
            self.surface_colors = create_surface_color_dict(
                '../../gemgis/data/Test1/style1.qml')
        else:
            raise TypeError(
                "Surface Colors Dict must be of type dict or a path directing to a qml file"
            )

        # Checking that the provided geological map is a gdf containing polygons
        if isinstance(geolmap, (type(None), gpd.geodataframe.GeoDataFrame,
                                rasterio.io.DatasetReader, np.ndarray)):
            if isinstance(geolmap, gpd.geodataframe.GeoDataFrame):
                if all(geolmap.geom_type == "Polygon"):
                    self.geolmap = geolmap
                else:
                    raise TypeError("Geometry Type must be Polygon")
            elif isinstance(geolmap, rasterio.io.DatasetReader):
                self.geolmap = geolmap.read(1)
            else:
                self.geolmap = geolmap
        else:
            raise TypeError(
                "Geological Map must be a GeoDataFrame or NumPy Array")

        # Checking that the provided basemap is a np.ndarray or rasterio data set
        if isinstance(basemap,
                      (type(None), rasterio.io.DatasetReader, np.ndarray)):
            if isinstance(basemap, rasterio.io.DatasetReader):
                self.basemap = basemap.read(1)
            else:
                self.basemap = basemap
        else:
            raise TypeError(
                'Base Map must be a Raster loaded with rasterio or a NumPy Array'
            )

        # Checking the the provided faults are a gdf containing LineStrings
        if isinstance(faults, (type(None), gpd.geodataframe.GeoDataFrame)):
            if isinstance(faults, gpd.geodataframe.GeoDataFrame):
                if all(faults.geom_type == "LineString"):
                    self.faults = faults
                else:
                    raise TypeError("Geometry Type must be LineString")
            self.faults = faults
        else:
            raise TypeError("Faults must be a GeoDataFrame")

        # Checking that the provided is_fault object is a list containing strings
        if isinstance(is_fault, (type(None), list)):
            if isinstance(is_fault, list):
                if all(isinstance(n, str) for n in is_fault):
                    self.is_fault = is_fault
                else:
                    raise TypeError('Fault Names must be provided as strings')
            self.is_fault = is_fault
        else:
            TypeError('List of faults must be of type list')

        # Checking that the provided raw input data objects are of type gdf
        if isinstance(raw_i, (gpd.geodataframe.GeoDataFrame, type(None))):
            self.raw_i = raw_i
        if isinstance(raw_o, (gpd.geodataframe.GeoDataFrame, type(None))):
            self.raw_o = raw_o
        if isinstance(raw_dem,
                      (gpd.geodataframe.GeoDataFrame, np.ndarray, type(None))):
            self.raw_dem = raw_dem

        # Setting the slope attribute
        if isinstance(slope, np.ndarray):
            self.slope = slope
        elif isinstance(self.raw_dem, np.ndarray) and isinstance(
                slope, type(None)):
            self.slope = calculate_slope(self.raw_dem, self.extent)
        else:
            self.slope = slope

        # Setting the hillshades attribute
        if isinstance(hillshades, np.ndarray):
            self.hillshades = hillshades
        elif isinstance(self.raw_dem, np.ndarray) and isinstance(
                hillshades, type(None)):
            self.hillshades = calculate_hillshades(self.raw_dem, self.extent)
        else:
            self.hillshades = hillshades

        # Setting the aspect attribute
        if isinstance(aspect, np.ndarray):
            self.aspect = aspect
        elif isinstance(self.raw_dem, np.ndarray) and isinstance(
                aspect, type(None)):
            self.aspect = calculate_aspect(self.raw_dem, self.extent)
        else:
            self.aspect = aspect

        # Calculate model dimensions
        if not isinstance(self.extent, type(None)):
            self.model_width = self.extent[1] - self.extent[0]
            self.model_length = self.extent[3] - self.extent[2]
            self.model_depth = self.extent[5] - self.extent[4]
            self.model_area = self.model_width * self.model_length
            self.model_volume = self.model_area * self.model_depth

        # Calculate cell dimensions
        if not isinstance(self.resolution, type(None)):
            if not isinstance(self.extent, type(None)):
                self.cell_width = self.model_width / self.resolution[0]
                self.cell_length = self.model_length / self.resolution[1]
                self.cell_depth = self.model_depth / self.resolution[2]

        # Setting the wms attribute
        if isinstance(wms, np.ndarray):
            self.wms = wms
        else:
            self.wms = None

        # Setting the tectonics attribute
        self.tectonics = tectonics

        # Setting the customsections attribute
        if isinstance(customsections,
                      (type(None), gpd.geodataframe.GeoDataFrame)):
            if isinstance(customsections, gpd.geodataframe.GeoDataFrame):
                self.customsections = customsections
            else:
                self.customsections = customsections
        else:
            raise TypeError('Custom sections must be provided as GeoDataFrame')

        # Setting the contours attribute
        if isinstance(contours, (type(None), gpd.geodataframe.GeoDataFrame)):
            if isinstance(contours, gpd.geodataframe.GeoDataFrame):
                self.contours = contours
            else:
                self.contours = contours
        else:
            raise TypeError('Custom sections must be provided as GeoDataFrame')