Example #1
0
def clear_map_layers():
    """Removes layers from maps within the ArcGIS Pro project template.
    Does not remove the layers if they are group layers or basemaps.
    """
    aprx = ArcGISProject(config.aprx)
    for map_ in aprx.listMaps():
        del_layers = [x for x in map_.listLayers(
        ) if not x.isGroupLayer and not x.isBasemapLayer]
        if del_layers:
            for d in del_layers:
                map_.removeLayer(d)
    aprx.save()
Example #2
0
    def add_to_aprx(self):
        """Adds the input layer to a .aprx Map based on the owner of the
        data. For example, the UTIL.wFitting feature would be added to the
        "UTIL" map of the designated .aprx file. The file path to the Pro
        project is set in the config file.
        """

        log.debug("Adding the layer to its edit aprx...")
        aprx = ArcGISProject(config.aprx)
        user_map = aprx.listMaps(f"{self.owner}")[0]
        user_map.addDataFromPath(self.aprx_connection)
        for map_lyr in user_map.listLayers():
            if self.feature_name in map_lyr.dataSource:
                layer = map_lyr
                break
        aprx.save()

        # Create group layer if it does not exist
        lyr_realpath = os.path.realpath(config.lyr)
        lyr_basename = os.path.basename(config.lyr)
        if self.version_name not in [x.name for x in user_map.listLayers()]:
            # Add to the map
            user_map.addLayer(LayerFile(lyr_realpath))
            aprx.save()
            # Rename the group layer to match the version name
            layer_name = lyr_basename.strip('.lyrx')
            for lyr in user_map.listLayers():
                if lyr.name == layer_name:
                    lyr.name = self.version_name
                    break

        # Move the data layer into the group layer
        group_layer = user_map.listLayers(self.version_name)[0]
        user_map.addLayerToGroup(group_layer, layer)
        user_map.removeLayer(layer)
        aprx.save()