此内容是否有用?
如何进行改进?
联系我们的支持团队
可以使用 arcgis.gis.module 作为解决方法来覆盖托管要素服务。 借助 Python 脚本中的 ArcGIS Online 凭据,可以使用该模块来克服 ArcPy 模块的已知限制。 该 Python 脚本允许自动化此该过程。 但是,需要 ArcGIS Pro 安装中包含的 Python 3。
本文将提供说明以指定模块和凭据、创建服务定义 (SD) 文件并进行发布。
注: 此过程仅适用于具有现有服务定义文件的托管要素服务。
import arcpy import os, sys from arcgis.gis import GIS
prjPath = r"C:\Users\SampleFolder\Project_Name.aprx"
sd_fs_name = "hosted feature service name" portal = "http://www.arcgis.com" user = "USERNAME" password = "PASSWORD"
shrOrg = True shrEveryone = False shrGroups = ""
relPath = r'C:\Users\SampleFolder_1' sddraft = os.path.join(relPath, "temporary service name.sddraft") sd = os.path.join(relPath, "temporary service name.sd")
注: 在 sharing_draft = mp.getWebLayerSharingDraft("HOSTING_SERVER", "FEATURE", sd_fs_name) 部分中,保留参数 "HOSTING_SERVER" 和 "FEATURE" 作为默认参数。 仅编辑定义的参数,sd_fs_name,其与用户定义的变量名称相同。
print("Creating SD file") arcpy.env.overwriteOutput = True prj = arcpy.mp.ArcGISProject(prjPath) mp = prj.listMaps()[0] #define a new object to utilize the getWebLayerSharingDraft function with HOSTING_SERVER and FEATURE as the default parameters sharing_draft = mp.getWebLayerSharingDraft("HOSTING_SERVER", "FEATURE", sd_fs_name) sharing_draft.summary = "My Summary" sharing_draft.tags = "My Tags" sharing_draft.description = "My Description" sharing_draft.credits = "My Credits" sharing_draft.useLimitations = "My Use Limitations" sharing_draft.exportToSDDraft(sddraft) arcpy.StageService_server(sddraft, sd)
print("Connecting to {}".format(portal)) gis = GIS(portal, user, password)
print("Search for original SD on portal…") print(f"Query: {sd_fs_name}") sdItem = gis.content.search(query=sd_fs_name, item_type="Service Definition") i = 0 while sdItem[i].title != sd_fs_name: i += 1 print('"Item Found") print(f'item[i].title = {sdItem[i].title}, sd_fs_name = {sd_fs_name}') item = sdItem[i] item.update(data=sd) print("Overwriting existing feature service…") fs = item.publish(overwrite=True) if shrOrg or shrEveryone or shrGroups: print("Setting sharing options…") fs.share(org=shrOrg, everyone=shrEveryone, groups=shrGroups) print("Finished updating: {} – ID: {}".format(fs.title, fs.id))
以下显示了完整脚本:
import arcpy import os, sys from arcgis.gis import GIS prjPath = r"C:\Users\SampleFolder\Project_Name.aprx" sd_fs_name = "hosted feature service name" portal = "http://www.arcgis.com" # Can also reference a local portal user = "username" password = "password" shrOrg = True shrEveryone = False shrGroups = "" relPath = r'C:\Users\SampleFolder' sddraft = os.path.join(relPath, "temporary service name.sddraft") sd = os.path.join(relPath, "temporary service name.sd") print("Creating SD file") arcpy.env.overwriteOutput = True prj = arcpy.mp.ArcGISProject(prjPath) mp = prj.listMaps()[0] sharing_draft = mp.getWebLayerSharingDraft("HOSTING_SERVER", "FEATURE", sd_fs_name) sharing_draft.summary = "My Summary" sharing_draft.tags = "My Tags" sharing_draft.description = "My Description" sharing_draft.credits = "My Credits" sharing_draft.useLimitations = "My Use Limitations" sharing_draft.exportToSDDraft(sddraft) arcpy.StageService_server(sddraft, sd) print("Connecting to {}".format(portal)) gis = GIS(portal, user, password) # Find the SD, update it, publish /w overwrite and set sharing and metadata print("Search for original SD on portal…") print(f"Query: {sd_fs_name}") sdItem = gis.content.search(query=sd_fs_name, item_type="Service Definition") i=0 while sdItem[i].title != sd_fs_name: i += 1 print('Item Found') print(f'item[i].title = {sdItem[i].title}, sd_fs_name = {sd_fs_name}') item = sdItem[i] item.update(data=sd) print("Overwriting existing feature service…") fs = item.publish(overwrite=True) if shrOrg or shrEveryone or shrGroups: print("Setting sharing options…") fs.share(org=shrOrg, everyone=shrEveryone, groups=shrGroups) print("Finished updating: {} – ID: {}".format(fs.title, fs.id))
上次修改时间: 11/10/2021
文章 ID: 000023164
软件: ArcGIS Pro 2.4.3, 2.4.2, 2.4.1, 2.4, 2.3.3, 2.3.2, 2.3.1, 2.3, 2.2.4, 2.2.3, 2.2.2, 2.2.1, 2.2, 2.1.3, 2.1.2, 2.1.1, 2.1, 2.0.1, 2.0