Skip to main content

exportToSDDraft

汇总

FeatureSharingDraft 类允许您为 Web 要素图层 创建服务定义草稿文件 (.sddraft),该文件将所有数据复制到 ArcGIS Enterprise 或 ArcGIS Online。 要配置引用注册数据的 Web 要素图层的属性,请修改 MapImageSharingDraft 类以 包含要素图层

讨论

要创建 FeatureSharingDraft 对象,使用 Map 对象的 getWebLayerSharingDraft 方法并将 service_type 参数设置为 FEATURE 。 随后即可通过设置服务级别属性和元数据属性来配置 FeatureSharingDraft 对象。

您也可以通过修改 .sddraft 文件设置 Web 图层属性。 如果没有为属性指定值,则会设置默认值。 即使要覆盖具有已配置属性的 web 图层,也必须设置相应的属性值。

注:

如果未设置元数据属性( creditsdescriptionsummarytagsuseLimitations )或其具有空字符串,则 Web 图层项目将从地图或图层中获取元数据,具体取决于共享的内容。 有关 Web 图层如何获取元数据的详细信息,请参阅 Web 图层元数据

配置 FeatureSharingDraft 对象后,可以使用 exportToSDDraft 方法将其保存到服务定义草稿文件 (.sddraft)。 随后即可使用 过渡服务上传服务定义 工具将其过渡和共享给 ArcGIS Enterprise 或 ArcGIS Online。 有关详细信息,请参阅 什么是 arcpy.sharing 模块

代码示例可用于以下用途:

语法

exportToSDDraft()

属性

名称 说明 数据类型

allowExporting

(读取和写入)

指定 Web 图层是否可以导出为不同的格式。

Boolean

allowUpdateWithoutMValues

(读取和写入)

指定是否允许几何在不指定 m 值的情况下更新为启用了 m 值的要素。 默认值为 True

Boolean

approvePublicDataCollection

(读取和写入)

指定是否允许公开编辑 Web 要素图层。

Boolean

checkUniqueIDAssignment

(读取和写入)

指定是否分析地图以确认 地图属性 中的 允许向共享 web 图层分配唯一数字 ID 选项是否已启用。 有关详细信息,请参阅 分配图层 ID

Boolean

credits

(读取和写入)

web 图层制作者名单。

String

description

(读取和写入)

web 图层的描述。

String

featureCapabilities

(读取和写入)

已启用的功能,以逗号分隔。 支持以下功能:

  • Query

  • Create

  • Update

  • Delete

  • Sync

  • SharedTemplateEditing

  • Append

  • Extract

  • Editing

String

maxRecordCount

(读取和写入)

服务器在查询期间返回的最大记录数。 默认值为 2000。

Integer

offline

(读取和写入)

指定是否需要门户连接。 如果设置为 False ,则必须登录门户才能使用 exportToSDDraft 函数创建服务定义草稿文件 (.sddraft)。 如果设置为 True ,则无需登录到门户即可创建服务定义草稿文件。

Boolean

offlineTarget

(读取和写入)

将发布服务定义的目标门户版本。 指定版本,确保服务定义包含与门户兼容的内容。 仅在属性 offline 设置为 True 时支持此属性。

  • ENTERPRISE_10x - 内容将与 ArcGIS Enterprise 10.9.1 或较早版本兼容。 这是默认设置。

  • ENTERPRISE_11 - 内容将与 ArcGIS Enterprise 11.0 或更高版本兼容。

  • ONLINE - 内容将与 ArcGIS Online 兼容。

String

overwriteExistingService

(读取和写入)

指定是否覆盖现有的 Web 图层。

Boolean

portalFolder

(读取和写入)

将发布 Web 图层的门户文件夹的名称。 默认文件夹是“我的内容”中的根文件夹。

String

preserveEditUsersAndTimestamps

(读取和写入)

指定是否将保留编辑者追踪信息。 默认值为 False

Boolean

serviceName

(读取和写入)

web 图层名称。 用户可以看到该名称并使用该名称来识别 web 图层。 名称只能包含字母数字字符和下划线。 不允许使用空格或特殊字符。 名称不能超过 120 个字符。

String

sharing.groups

(读取和写入)

以逗号分隔的组名称。

String

sharing.sharingLevel

(读取和写入)

Web 图层的共享级别。

  • 所有者 - 只有所有者(以及有权查看其他成员所拥有内容的组织成员)才能访问项目。

  • 组织 - 与组织中所有经过身份验证的用户共享您的内容。

  • 所有人 - 与公众分享您的内容。 任何人均可访问并进行查看。

String

summary

(读取和写入)

Web 图层的摘要。

String

tags

(读取和写入)

Web 图层的标签。 可以添加多个标签,标签之间用逗号分隔。

String

useLimitations

(读取和写入)

Web 图层的使用限制。

String

serverType

(只读)

根据 map 类中的 getWebLayerSharingDraft 函数创建共享草稿时指定的服务器类型。 从 FeatureSharingDraftserverType 中唯一可能返回的值为 HOSTING_SERVERHOSTING_SERVERserverType 值表示支持共享至 ArcGIS Enterprise 或 ArcGIS Online。

String

timezone.ID

(读取和写入)

将存储日期值的时区。 字符串值必须与 Windows 操作系统识别的官方时区 ID 之一匹配。 有关时区的列表,请参阅 Microsoft 文档中的 时区

注:

该设置适用于以下情景:

  • 您的数据已设置编辑器追踪字段以记录数据库时间中的日期。

  • 您正在将包含日期字段的数据共享到 ArcGIS Enterprise 11.5 和更低版本。

  • 您正在共享至 ArcGIS Online 或 ArcGIS Enterprise 12.0 及更高版本,并且 图层属性 中未定义时区。

String

timezone.DaylightSavingTime

(读取和写入)

指定时区是否考虑夏令时。 默认值为 False

Boolean

timezone.preferredTimezoneID

(读取和写入)

显示 Web 要素图层中的查询时 ArcGIS Pro 将使用的时区。 字符串值必须与 Windows 操作系统识别的官方时区 ID 之一匹配。 有关时区的列表,请参阅 Microsoft 文档中的 时区

String

timezone.preferredTimezoneIDDaylightSavingTime

(读取和写入)

指定首选时区是否考虑夏令时。 默认值为 False

Boolean

useCIMSymbols

(读取和写入)

指定是否将使用 Esri 制图信息模型 (CIM) 符号(也称为 ArcGIS Pro 符号)。 设置为 False 时,将使用所有客户端都能渲染的符号。 设置为 True 时,将使用新客户端(例如 ArcGIS API for JavaScript 4.x)支持的 CIM 符号。

此属性仅在服务定义发布到 ArcGIS Online 或 ArcGIS Enterprise 11.3 或更高版本时有效。

Boolean

zDefault.enable

(读取和写入)

指定是否允许编辑者通过不允许指定 z 值的客户端添加或更新要素。 默认值为 True

Boolean

zDefault.value

(读取和写入)

插入或更新要素时将使用的默认 z 值。 仅在属性 zDefault.enable 设置为 True 时支持此属性。 默认值为 0。

Double

方法

exportToSDDraft(out_sddraft)

FeatureSharingDraft 转换为服务定义草稿 (.sddraft) 文件。

名称 说明 数据类型

out_sddraft

用于表示输出服务定义草稿 (.sddraft) 文件的路径和文件名的字符串。

String

代码示例

将 Web 要素图层发布到门户文件夹

以下脚本为地图创建 Web 要素图层 服务定义草稿文件 (.sddraft),并设置元数据、导出数据属性和 CIM 符号。 Web 要素图层将发布到 ArcGIS Online 或 ArcGIS Enterprise 的文件夹中并与所有人(公共)共享。 门户信息可从 SignInToPortal 函数中获得。

import arcpy
import os

# Sign in to portal
arcpy.SignInToPortal("https://www.arcgis.com", "MyUserName", "MyPassword")

# Set output file names
outdir = r"C:\Project\Output"
service_name = "FeatureSharingDraftExample"
sddraft_filename = service_name + ".sddraft"
sddraft_output_filename = os.path.join(outdir, sddraft_filename)
sd_filename = service_name + ".sd"
sd_output_filename = os.path.join(outdir, sd_filename)

# Reference map to publish
aprx = arcpy.mp.ArcGISProject(r"C:\Project\World.aprx")
m = aprx.listMaps('World')[0]

# Create FeatureSharingDraft and set metadata, portal folder, export data properties, and CIM symbols
server_type = "HOSTING_SERVER"
sddraft = m.getWebLayerSharingDraft(server_type, "FEATURE", service_name)
sddraft.credits = "These are credits"
sddraft.description = "This is description"
sddraft.summary = "This is summary"
sddraft.tags = "tag1, tag2"
sddraft.useLimitations = "These are use limitations"
sddraft.portalFolder = "my folder name"
sddraft.featureCapabilities = "Extract"
sddraft.useCIMSymbols = True
sddraft.sharing.sharingLevel = "EVERYONE"
sddraft.sharing.groups = ""  # Group names = "group1,group2"

# Create Service Definition Draft file
sddraft.exportToSDDraft(sddraft_output_filename)

# Stage Service
print("Start Staging")
arcpy.server.StageService(sddraft_output_filename, sd_output_filename)

# Share to portal
print("Start Uploading")
result = arcpy.server.UploadServiceDefinition(sd_output_filename, server_type)

value_table_op = arcpy.ValueTable(2)
value_table_op.loadFromString(result[11])
for i in range(0, value_table_op.rowCount):
   print(value_table_op.getRow(i))

print("Finish Publishing")
覆盖 Web 要素图层

以下脚本将 覆盖 Web 要素图层 。 如果服务名称已存在,将覆盖该服务;否则,将创建新服务。 如果希望 Web 图层具有特定的属性,则必须设置属性值,即使要覆盖的 Web 图层已配置了属性。 否则,现有属性将被默认设置覆盖。

import arcpy
import os

# Sign in to portal
arcpy.SignInToPortal("https://www.arcgis.com", "MyUserName", "MyPassword")

# Set output file names
outdir = r"C:\Project\Output"
service_name = "FeatureSharingDraftExample"
sddraft_filename = service_name + ".sddraft"
sddraft_output_filename = os.path.join(outdir, sddraft_filename)
sd_filename = service_name + ".sd"
sd_output_filename = os.path.join(outdir, sd_filename)

# Reference map to publish
aprx = arcpy.mp.ArcGISProject(r"C:\Project\World.aprx")
m = aprx.listMaps('World')[0]

# Create FeatureSharingDraft and set overwrite property
server_type = "HOSTING_SERVER"
sddraft = m.getWebLayerSharingDraft(server_type, "FEATURE", service_name)
sddraft.overwriteExistingService = True

# Create Service Definition Draft file
sddraft.exportToSDDraft(sddraft_output_filename)

# Stage Service
print("Start Staging")
arcpy.server.StageService(sddraft_output_filename, sd_output_filename)

# Share to portal
print("Start Uploading")
arcpy.server.UploadServiceDefinition(sd_output_filename, server_type)

print("Finish Publishing")
设置创建和同步功能

以下脚本将创建 Web 要素图层 服务定义草稿文件 (.sddraft) 并在 Web 要素图层上设置创建和同步功能。 然后,会将 .sddraft 文件过渡并发布到门户(可以选择使用指定项目 ID)。

import arcpy
import os

# Sign in to portal
arcpy.SignInToPortal("https://organization.example.com/webadaptorname",
                     "MyUserName", "MyPassword")

# Set output file names
outdir = r"C:\Project\Output"
service_name = "FeatureSharingDraftExample"
sddraft_filename = service_name + ".sddraft"
sddraft_output_filename = os.path.join(outdir, sddraft_filename)
sd_filename = service_name + ".sd"
sd_output_filename = os.path.join(outdir, sd_filename)

# Reference map to publish
aprx = arcpy.mp.ArcGISProject(r"C:\Project\World.aprx")
m = aprx.listMaps('World')[0]

# Create FeatureSharingDraft
server_type = "HOSTING_SERVER"
sddraft = m.getWebLayerSharingDraft(server_type, "FEATURE", service_name)
sddraft.featureCapabilities = "Create,Sync,Query"

# Create Service Definition Draft file
sddraft.exportToSDDraft(sddraft_output_filename)

# Stage Service
print("Start Staging")
arcpy.server.StageService(sddraft_output_filename, sd_output_filename)

# Replace "itemid" with desired item ID
item_id = "web_feature_layer_id itemid"

# Share to portal
print("Start Uploading")
arcpy.server.UploadServiceDefinition(sd_output_filename, server_type, in_item_id=item_id)

print("Finish Publishing")
发布图层和表

以下脚本将地图中的图层和表作为 Web 要素图层发布至门户。

import arcpy
import os

# Sign in to portal
arcpy.SignInToPortal("https://www.arcgis.com", "MyUserName", "MyPassword")

# Set output file names
outdir = r"C:\Project\Output"
service_name = "FeatureSharingDraftExample"
sddraft_filename = service_name + ".sddraft"
sddraft_output_filename = os.path.join(outdir, sddraft_filename)
sd_filename = service_name + ".sd"
sd_output_filename = os.path.join(outdir, sd_filename)

# Reference layers to publish
aprx = arcpy.mp.ArcGISProject(r"C:\Project\World.aprx")
m = aprx.listMaps('World')[0]
selected_layer = m.listLayers('States')[0]
selected_table = m.listTables('Capitals')[0]

# Create FeatureSharingDraft
server_type = "HOSTING_SERVER"
sddraft = m.getWebLayerSharingDraft(server_type, "FEATURE", service_name, [selected_layer, selected_table])

# Create Service Definition Draft file
sddraft.exportToSDDraft(sddraft_output_filename)

# Stage Service
print("Start Staging")
arcpy.server.StageService(sddraft_output_filename, sd_output_filename)

# Share to portal
print("Start Uploading")
arcpy.server.UploadServiceDefinition(sd_output_filename, server_type)

print("Finish Publishing")
创建并发布离线服务定义

以下脚本将为目标门户版本创建 离线服务定义 ,并将其发布为 Web 要素图层。

import arcpy
import os

# Set output file names
outdir = r"C:\Project\Output"
service_name = "FeatureSharingDraftExample"
sddraft_filename = service_name + ".sddraft"
sddraft_output_filename = os.path.join(outdir, sddraft_filename)
sd_filename = service_name + ".sd"
sd_output_filename = os.path.join(outdir, sd_filename)

# Reference map to publish
aprx = arcpy.mp.ArcGISProject(r"C:\Project\World.aprx")
m = aprx.listMaps('World')[0]

# Create FeatureSharingDraft and set offline and offlineTarget properties
server_type = "HOSTING_SERVER"
sddraft = m.getWebLayerSharingDraft(server_type, "FEATURE", service_name)
sddraft.offline = True
# Set offlineTarget property to "ONLINE" for ArcGIS Online or "ENTERPRISE_11" for ArcGIS Enterprise 11.0 or later
# The default is ArcGIS Enterprise 10.9.1 or earlier
sddraft.offlineTarget = "ENTERPRISE_10x"

# Create Service Definition Draft file
sddraft.exportToSDDraft(sddraft_output_filename)

# Sign in to portal to upload and publish
arcpy.SignInToPortal("https://organization.example.com/webadaptorname",
"MyUserName", "MyPassword")

# Stage Service
print("Start Staging")
arcpy.server.StageService(sddraft_output_filename, sd_output_filename)

# Share to portal
print("Start Uploading")
arcpy.server.UploadServiceDefinition(sd_output_filename, server_type)

print("Finish Publishing")
分析以确认地图已设置为允许分配唯一数字 ID

以下脚本将创建启用了 checkUniqueIDAssignment 属性的 Web 要素图层 服务定义草案文件 (.sddraft)。 在过渡期间,将对 .sddraft 文件进行分析。 如果地图未设置为允许分配用于共享 web 图层的唯一数字 ID,则会返回分析器错误 00374 并打印一条消息。 有关详细信息,请参阅 分配图层 ID

import arcpy
import os

# Sign in to portal
arcpy.SignInToPortal("https://www.arcgis.com", "MyUserName", "MyPassword")

# Set output file names
outdir = r"C:\Project\Output"
service_name = "FeatureSharingDraftExample"
sddraft_filename = service_name + ".sddraft"
sddraft_output_filename = os.path.join(outdir, sddraft_filename)
sd_filename = service_name + ".sd"
sd_output_filename = os.path.join(outdir, sd_filename)

# Reference map to publish
aprx = arcpy.mp.ArcGISProject(r"C:\Project\World.aprx")
m = aprx.listMaps('World')[0]

# Create FeatureSharingDraft and set the check unique ID assignment property
server_type = "HOSTING_SERVER"
sddraft = m.getWebLayerSharingDraft(server_type, "FEATURE", service_name)
sddraft.checkUniqueIDAssignment = True

# Create Service Definition Draft file
sddraft.exportToSDDraft(sddraft_output_filename)

try:
    # Stage Service
    print("Start Staging")
    arcpy.server.StageService(sddraft_output_filename, sd_output_filename)
    # Share to portal
    print("Start Uploading")
    arcpy.server.UploadServiceDefinition(sd_output_filename, server_type)

    print("Finish Publishing")
except Exception as stage_exception:
    if "00374" in str(stage_exception):
        print("The map is not set to allow assignment of unique IDs")
    print("Analyzer errors encountered - {}".format(str(stage_exception)))

except arcpy.ExecuteError:
    print(arcpy.GetMessages(2))
使用与早期 ArcGIS Pro 版本兼容的字段类型发布表格

以下脚本用于设置 env 类的 useCompatibleFieldTypes 属性,将表格添加至地图,并将托管表格发布至 ArcGIS Enterprise。

import arcpy
import os

# Set useCompatibleFieldTypes property
arcpy.env.useCompatibleFieldTypes = True

data = r"C:\states.csv"

# Set output file names
outdir = r"C:\Project\Output"
service_name = "FeatureSharingDraftExample"
sddraft_filename = service_name + ".sddraft"
sddraft_output_filename = os.path.join(outdir, sddraft_filename)
sd_filename = service_name + ".sd"
sd_output_filename = os.path.join(outdir, sd_filename)

# Open existing project, create a map with a basemap, and add data
aprx = arcpy.mp.ArcGISProject(os.path.join(outdir, r"C:\Project\World.aprx"))
m = aprx.createMap(service_name, "Map")
m.addBasemap("Topographic")
m.addDataFromPath(data)
print(f"Data added: {data}")
aprx.save()
print(f"Project saved: {aprx.filePath}")

# Sign in to portal
arcpy.SignInToPortal("https://organization.example.com/webadaptorname",
                 "MyUserName", "MyPassword")

# Create FeatureSharingDraft
server_type = "HOSTING_SERVER"
sddraft = m.getWebLayerSharingDraft(server_type, "FEATURE", service_name)

# Create Service Definition Draft file
sddraft.exportToSDDraft(sddraft_output_filename)

# Stage Service
print("Start Staging")
arcpy.server.StageService(sddraft_output_filename, sd_output_filename)

# Share to portal
print("Start Uploading")
arcpy.server.UploadServiceDefinition(sd_output_filename, server_type)

print("Finish Publishing")