操作方法

操作方法:检索已发布要素图层中使用的源要素类和表

Last Published: August 1, 2025

描述

有时,我们希望匹配已发布的引用要素图层中使用的源企业级地理数据库要素类和企业级地理数据库表。 此图层可能使用了多个子图层和表。 以下脚本将在 .sde 连接中检索子图层/表名称及其 ID/索引和源表/要素类名称。

解决方案或解决方法

使用 ArcGIS Pro Python 环境运行以下脚本:

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('-----------------------------------')

文章 ID: 000032337

获得人工智能支持

使用 Esri Support AI Chatbot 快速解决您的问题。

立即开始聊天

相关信息

发现关于本主题的更多内容

获取来自 ArcGIS 专家的帮助

联系技术支持部门

立即开始聊天

转至下载选项