Solved

List of layers from Esri webmap

  • 15 December 2023
  • 5 replies
  • 82 views

Badge +6

Hi,

I am trying to produce a list of all layers that are in Esri webmap. I have the private url of the webmap and I found this Python code

from arcgis.gis import GIS
from arcgis.mapping import WebMap
gis = GIS('home')# if running this from Notebook for ArcGIS in AGOL/Enterprise, replace this line with gis = GIS('home')
wmItemId = "myid" #put the id of the webmap in here
wmItem = gis.content.get(wmItemId)
wm = WebMap(wmItem)
for lyr in wm.layers:
    print(lyr.title)
    print(lyr.url)

Could some please tell me how to use this code in FME?

icon

Best answer by nielsgerrits 16 April 2024, 09:12

View original

5 replies

Userlevel 6
Badge +33

This works for me in ArcGIS Online, you need to replace <UserName>, <Password> and <ItemId>. Put it in a PythonCaller and it will return a feature with a list called "layer_names".

import fme
import fmeobjects
from arcgis.gis import GIS
from arcgis.mapping import WebMap
 
class FeatureProcessor(object):
 
    def __init__(self):
        pass
 
    def has_support_for(self, support_type: int):
        return support_type == fmeobjects.FME_SUPPORT_FEATURE_TABLE_SHIM
 
    def input(self, feature: fmeobjects.FMEFeature):
 
        # Connect to your ArcGIS Online organization or Portal for ArcGIS
        gis = GIS("https://www.arcgis.com", "<UserName>", "<Password>")
 
        # Get the web map item by its ID
        web_map_id = "<ItemId>"
        web_map_item = gis.content.get(web_map_id)
        web_map = WebMap(web_map_item)
 
        # Create an empty list to store layer names
        layer_names = []
 
        # Append each layer name to the list
        for layer in web_map.layers:
            layer_names.append(layer.title)
        
        # Create a feature with the list of layer names
        feature_out = fmeobjects.FMEFeature()
        feature_out.setAttribute("layer_names", layer_names)
        self.pyoutput(feature_out)
 
    def close(self):
        pass
 
    def process_group(self):
        pass

 

Badge +6

Hi, I am getting an error

 

2024-04-08 14:26:28|  11.6|  4.6|ERROR |Python Exception <Exception>: A general error occurred: Could not login. Please ensure you have valid credentials and set your security login question.
2024-04-08 14:26:28|  11.6|  0.0|ERROR |Traceback (most recent call last):
2024-04-08 14:26:28|      |     |ERROR |  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\auth\_auth\_token.py", line 841, in token
2024-04-08 14:26:28|      |     |ERROR |    self._init_response_type_token()
2024-04-08 14:26:28|      |     |ERROR |  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\auth\_auth\_token.py", line 540, in _init_response_type_token
2024-04-08 14:26:28|      |     |ERROR |    raise Exception("Unable to generate oauth token")
2024-04-08 14:26:28|      |     |ERROR |Exception: Unable to generate oauth token
 

Badge +6

  

Badge +6

I have opened fresh FME 2023.0 workbend and have only Creator and PythonCaller with:

  • Esri ArcGIS Python 3.7+
  • and following script
import fme
import fmeobjects
from arcgis.gis import GIS
from arcgis.mapping import WebMap

class FeatureProcessor(object):

def __init__(self):
pass

def has_support_for(self, support_type: int):
return support_type == fmeobjects.FME_SUPPORT_FEATURE_TABLE_SHIM

def input(self, feature: fmeobjects.FMEFeature):

# Connect to your ArcGIS Online organization or Portal for ArcGIS
gis = ("https://maps-eu.ramboll.com/portal", "USERNAME", "PASSWORD")

# Get the web map item by its ID
web_map_id = "ITEMID"
web_map_item = gis.content.get(web_map_id)
web_map = WebMap(web_map_item)

# Create an empty list to store layer names
layer_names = []

# Append each layer name to the list
for layer in web_map.layers:
layer_names.append(layer.title)

# Create a feature with the list of layer names
feature_out = fmeobjects.FMEFeature()
feature_out.setAttribute("layer_names", layer_names)
self.pyoutput(feature_out)

def close(self):
pass

def process_group(self):
pass

Now for a change I get following error

2024-04-12 14:23:00| 8.3| 0.0|ERROR |Python Exception <AttributeError>: 'tuple' object has no attribute 'content'2024-04-12 14:23:00| 8.3| 0.0|ERROR |Traceback (most recent call last):2024-04-12 14:23:00| | |ERROR | File "<string>", line 22, in input2024-04-12 14:23:00| | |ERROR |AttributeError: 'tuple' object has no attribute 'content'2024-04-12 14:23:00| 8.3| 0.0|ERROR |Error encountered while calling method `input'2024-04-12 14:23:00| 8.3| 0.0|FATAL |PythonCaller_7 (PythonFactory): PythonFactory failed to process feature

I trully do not understand what is going on. In general I need to get a list of all layers that are included in the webmap in Enterpise. I use SingleSingOn to log in to Esri Portal. I would appreciate help as noone seem to know the answer. I thouhg that maybe I can use Esri ArcGIS Online Connector to get this information but I apart from getting private_url of the webmap I cannot extract the information about the content of the webmap

Userlevel 6
Badge +33

A different way to do this is to use the ArcGISOnlineConnector to find the ItemId of the webmap, then use that ID to get the JSON of the WebMap.

Portal:

https://YourPortalUrl/portal/sharing/rest/content/items/YourItemId/data?f=json

ArcGIS Online:

https://www.arcgis.com/sharing/rest/content/items/YourItemId/data?f=json

Then put the json through a JSONFragmenter. Query is

json["operationalLayers"][*]

and set Flattening = Yes. Then expose what you need (id, title, url) using an AttributeExposer.

Reply