accessmod/layer.py
def merge(self) -> np.ndarray:
        """Merge geographic layers into a single raster.

        Merging priorities between overlapping layers is provided through the
        `priorities` attribute at the class level.

        Raster values:
            * 0: no data
            * 1-999 range: land cover classes
            * 1000-1999: transport network classes
            * 2000: water cells
            * 3000-2999: barrier classes
        """
        logger.info("Started merge of input layers")
        land_cover = self.get(self.find(Role.LAND_COVER)[0]).read()
        stack = np.zeros(shape=self.meta.get("shape"), dtype="int16")
        self.labels = {}

        # 1 will be added each time we iterate over a barrier layer
        i_barrier = 0

        # populate the stack raster class per class starting from the lowest
        # priority
        for stack_class in reversed(self.priorities):

            layer = self.get(stack_class["name"])
            class_id = stack_class.get("class")
            if not layer:
                raise AccessModError(f"Layer '{layer}' not found.")

            if layer.role.value == Role.LAND_COVER.value:
                stack[land_cover == int(class_id)] = land_cover[
                    land_cover == int(class_id)
                ]
                self.labels[class_id] = layer.labels.get(class_id)

            elif layer.role.value == Role.TRANSPORT_NETWORK.value:
                data = layer.rasterize(
                    class_id,
                    self.meta.get("transform"),
                    self.meta.get("shape"),
                    self.meta.get("crs"),
                )
                if data is not None:
                    # e.g. class_value = 1001 for class_id = "primary"
                    class_value = layer.class_value(class_id) + 1000
                    stack[data == 1] = class_value
                    self.labels[class_value] = class_id

            elif layer.role.value == Role.WATER.value:
                data = layer.rasterize(
                    self.meta.get("transform"),
                    self.meta.get("shape"),
                    self.meta.get("crs"),
                )
                if data is not None:
                    stack[data == 1] = 2000
                    self.labels[2000] = layer.name

            elif layer.role.value == Role.BARRIER.value:
                data = layer.rasterize(
                    self.meta.get("transform"),
                    self.meta.get("shape"),
                    self.meta.get("crs"),
                )
                if data is not None:
                    stack[data == 1] = 3000 + i_barrier
                    self.labels[3000 + i_barrier] = layer.name
                    i_barrier += 1

            else:
                raise AccessModError("Unrecognized layer.")

        logger.info(f"Merged {len(self.priorities)} classes into stack layer")

        return stack
Merge geographic layers into a single raster.

        Merging priorities between overlapping layers is provided through the
        `priorities` attribute at the class level.

        Raster values:
            * 0: no data
            * 1-999 range: land cover classes
            * 1000-1999: transport network classes
            * 2000: water cells
            * 3000-2999: barrier classes
        
Merge geographic layers into a single raster.
Merging priorities between overlapping layers is provided through the
`priorities` attribute at the class level.


Merge geographic layers into a single raster . Merging priorities between overlapping layers is provided through the ` priorities ` attribute at the class level .
def merge(self) -> np.ndarray:
        logger.info("Started merge of input layers")
        land_cover = self.get(self.find(Role.LAND_COVER)[0]).read()
        stack = np.zeros(shape=self.meta.get("shape"), dtype="int16")
        self.labels = {}
        i_barrier = 0
        for stack_class in reversed(self.priorities):
            layer = self.get(stack_class["name"])
            class_id = stack_class.get("class")
            if not layer:
                raise AccessModError(f"Layer '{layer}' not found.")
            if layer.role.value == Role.LAND_COVER.value:
                stack[land_cover == int(class_id)] = land_cover[
                    land_cover == int(class_id)
                ]
                self.labels[class_id] = layer.labels.get(class_id)
            elif layer.role.value == Role.TRANSPORT_NETWORK.value:
                data = layer.rasterize(
                    class_id,
                    self.meta.get("transform"),
                    self.meta.get("shape"),
                    self.meta.get("crs"),
                )
                if data is not None:
                    class_value = layer.class_value(class_id) + 1000
                    stack[data == 1] = class_value
                    self.labels[class_value] = class_id
            elif layer.role.value == Role.WATER.value:
                data = layer.rasterize(
                    self.meta.get("transform"),
                    self.meta.get("shape"),
                    self.meta.get("crs"),
                )
                if data is not None:
                    stack[data == 1] = 2000
                    self.labels[2000] = layer.name
            elif layer.role.value == Role.BARRIER.value:
                data = layer.rasterize(
                    self.meta.get("transform"),
                    self.meta.get("shape"),
                    self.meta.get("crs"),
                )
                if data is not None:
                    stack[data == 1] = 3000 + i_barrier
                    self.labels[3000 + i_barrier] = layer.name
                    i_barrier += 1
            else:
                raise AccessModError("Unrecognized layer.")
        logger.info(f"Merged {len(self.priorities)} classes into stack layer")
        return stack
def merge ( self ) -> np . ndarray : logger . info ( "Started merge of input layers" ) land_cover = self . get ( self . find ( Role . LAND_COVER ) [ 0 ] ) . read ( ) stack = np . zeros ( shape = self . meta . get ( "shape" ) , dtype = "int16" ) self . labels = { } i_barrier = 0 for stack_class in reversed ( self . priorities ) : layer = self . get ( stack_class [ "name" ] ) class_id = stack_class . get ( "class" ) if not layer : raise AccessModError ( f"Layer '{layer}' not found." ) if layer . role . value == Role . LAND_COVER . value : stack [ land_cover == int ( class_id ) ] = land_cover [ land_cover == int ( class_id ) ] self . labels [ class_id ] = layer . labels . get ( class_id ) elif layer . role . value == Role . TRANSPORT_NETWORK . value : data = layer . rasterize ( class_id , self . meta . get ( "transform" ) , self . meta . get ( "shape" ) , self . meta . get ( "crs" ) , ) if data is not None : class_value = layer . class_value ( class_id ) + 1000 stack [ data == 1 ] = class_value self . labels [ class_value ] = class_id elif layer . role . value == Role . WATER . value : data = layer . rasterize ( self . meta . get ( "transform" ) , self . meta . get ( "shape" ) , self . meta . get ( "crs" ) , ) if data is not None : stack [ data == 1 ] = 2000 self . labels [ 2000 ] = layer . name elif layer . role . value == Role . BARRIER . value : data = layer . rasterize ( self . meta . get ( "transform" ) , self . meta . get ( "shape" ) , self . meta . get ( "crs" ) , ) if data is not None : stack [ data == 1 ] = 3000 + i_barrier self . labels [ 3000 + i_barrier ] = layer . name i_barrier += 1 else : raise AccessModError ( "Unrecognized layer." ) logger . info ( f"Merged {len(self.priorities)} classes into stack layer" ) return stack
Merge geographic layers into a single raster.
Merge geographic layers into a single raster .
"""Merge geographic layers into a single raster.

        Merging priorities between overlapping layers is provided through the
        `priorities` attribute at the class level.

        Raster values:
            * 0: no data
            * 1-999 range: land cover classes
            * 1000-1999: transport network classes
            * 2000: water cells
            * 3000-2999: barrier classes
        """ # 1 will be added each time we iterate over a barrier layer # populate the stack raster class per class starting from the lowest # priority # e.g. class_value = 1001 for class_id = "primary"
self