HOW TO
Sometimes, we want to match the used source enterprise geodatabase feature classes and enterprise geodatabase tables in the published referenced feature layer. This layer may have multiple sublayers and tables used. The below script will retrieve the sublayer/table name, its ID/index, and its source table/feature class name in the .sde connection.
Run the below script using the ArcGIS Pro Python environment:
from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection
import arcpy
import re
#Log in to ArcGIS Enterprise
portalurl = 'https://hostname.esri.com/portal'
username = 'admin'
password = 'admin123'
gis = GIS(portalurl,username,password)
arcpy.SignInToPortal(portalurl,username,password)
#Specify the referenced Feature Layer item ID
featurelayeritem = gis.content.get('c800e0fe96b844cfb7e6a684383da43f')
#Specify the workspace to be the REST URL of the feature layer
arcpy.env.workspace = f"{featurelayeritem.url}"
#Identify a list of all source feature classes and tables
featureclasses = arcpy.ListFeatureClasses()
sourcetables = arcpy.ListTables()
"""The above will create a list of strings with source feature
classes and another for source tables used in the layer.
The result will be ['L<id><featureclass>']. For examble, if
the feature class is called 'Addresses', and the associated
layer id is '3', it will be ['L3Addresses']."""
#The below is to retrieve layers and tables information from the web feature layer
featurelayercollection = FeatureLayerCollection.fromitem(featurelayeritem)
layers = featurelayercollection.properties['layers']
tables = featurelayercollection.properties['tables']
#Looping through each layer
for layer in layers:
#Retrieving the layer name and its id
layername = layer['name']
layerid = layer['id']
#Looping through the source feature classes
for featureclass in featureclasses:
#Retrieving only the feature class name that starts with "L<layerid>"
if featureclass.startswith(f'L{layerid}'):
"""Making sure that, for example, the layer wth id 33
doesn't get retrieved with the one with 3 id. So we
make sure that the character after L<layerid> is a
letter not a number as feature classes can only start
with letters"""
prefixlength = len(f'L{layerid}')
if re.match(r'[A-Z,a-z]',featureclass[prefixlength:]):
#Identifying the sourcename without 'l<layerid>'
sourcename = featureclass[prefixlength:]
#Printing the result
print(f'Layer name: {layername}')
print(f'Layer ID: {layerid}')
print(f'Source Feature Class: {sourcename}')
print('-----------------------------------')
#Looping through each table
for table in tables:
#Retrieving the table name and its id
tablename = table['name']
tableid = table['id']
#Looping through the source tables
for sourcetable in sourcetables:
#Retrieving only the table name that starts with "L<tableid>"
if sourcetable.startswith(f'L{tableid}'):
"""Making sure that, for example, the table wth id 33
doesn't get retrieved with the one with 3 id. So we
make sure that the character after L<tableid> is a
letter not a number as feature classes can only start
with letters"""
prefixlength = len(f'L{tableid}')
if re.match(r'[A-Z,a-z]',sourcetable[prefixlength:]):
#Identifying the sourcename without 'l<tableid>'
sourcename = sourcetable[prefixlength:]
#Printing the result
print(f'Table name: {tablename}')
print(f'Table ID: {tableid}')
print(f'Source Table: {sourcename}')
print('-----------------------------------')
Article ID: 000032337
Get help from ArcGIS experts
Start chatting now