Arcpy发布追踪网络服务

0
分享 2025-03-10
前提:
首先有加载追踪网络图层的GeoScene Pro工程文件。
注册数据存储:向server或者portal注册追踪网络数据所在sde
然后通过发布关联要素的地图影像图层的形式,修改sddraft文件启用要素图层,启用版本管理的方式发布追踪网络服务。
import arcpy
import os
import xml.dom.minidom as DOM

def configure_featureserver_capabilities(sddraftPath, capabilities):
"""Function to configure FeatureServer properties"""
# Read the .sddraft file
doc = DOM.parse(sddraftPath)

# Find all elements named TypeName
# This is where the additional layers and capabilities are defined
typeNames = doc.getElementsByTagName('TypeName')
for typeName in typeNames:
# Get the TypeName to enable
if typeName.firstChild.data == "FeatureServer":
extension = typeName.parentNode
for extElement in extension.childNodes:
if extElement.tagName == 'Info':
for propSet in extElement.childNodes:
for prop in propSet.childNodes:
for prop1 in prop.childNodes:
if prop1.tagName == "Key":
if prop1.firstChild.data == 'WebCapabilities':
if prop1.nextSibling.hasChildNodes():
prop1.nextSibling.firstChild.data = capabilities
else:
txt = doc.createTextNode(capabilities)
prop1.nextSibling.appendChild(txt)
# Write to the .sddraft file
f = open(sddraftPath, 'w')
doc.writexml(f)
f.close()

def configure_mapserver_capabilities(sddraftPath, capabilities):
"""Function to configure MapServer properties"""
# Read the .sddraft file
doc = DOM.parse(sddraftPath)

# Find all elements named TypeName
# This is where the additional layers and capabilities are defined
typeNames = doc.getElementsByTagName('TypeName')
for typeName in typeNames:
# Get the TypeName to enable
if typeName.firstChild.data == "MapServer":
extension = typeName.parentNode
for extElement in extension.childNodes:
if extElement.tagName == 'Definition':
for propArray in extElement.childNodes:
if propArray.tagName == 'Info':
for propSet in propArray.childNodes:
for prop in propSet.childNodes:
for prop1 in prop.childNodes:
if prop1.tagName == "Key":
if prop1.firstChild.data == 'WebCapabilities':
if prop1.nextSibling.hasChildNodes():
prop1.nextSibling.firstChild.data = capabilities
else:
txt = doc.createTextNode(capabilities)
prop1.nextSibling.appendChild(txt)
# Write to the .sddraft file
f = open(sddraftPath, 'w')
doc.writexml(f)
f.close()

if __name__ == "__main__":
# Sign in to portal
arcpy.SignInToPortal("https://geo41.geoscene.cn/geoscene", "username", "password")

# Set output file names
outdir = r"E:\ArcGISproj\tracenetwork\Output"
service_name = "MapImageSharingDraftExample1"
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"E:\ArcGISproj\tracenetwork\tracenetwork.aprx")
m = aprx.listMaps('world')[0]

# Create MapImageSharingDraft, set copyDataToServer property to False to reference registered data, and set CIM symbols
server_type = "FEDERATED_SERVER"
federated_server_url = "https://geo41.geoscene.cn/server"
sddraft = m.getWebLayerSharingDraft(server_type, "MAP_IMAGE", service_name)
sddraft.federatedServerUrl = federated_server_url
sddraft.copyDataToServer = False
sddraft.useCIMSymbols = True

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

"""Modify the .sddraft file to include a feature layer and set map image layer and feature layer properties"""

# Modify the .sddraft file to change map image layer properties
# Defaults are Map,Query,Data
# Comment out the line below if you do not want to modify map image layer properties
configure_mapserver_capabilities(sddraft_output_filename, "Map,Data")

# Modify the .sddraft file to include a feature layer
# Read the file
doc = DOM.parse(sddraft_output_filename)

# Find all elements named TypeName
# This is where the extensions are defined
typeNames = doc.getElementsByTagName('TypeName')
for typeName in typeNames:
# Get the TypeName to enable
if typeName.firstChild.data == "FeatureServer":
extension = typeName.parentNode
for extElement in extension.childNodes:
# Include a feature layer
if extElement.tagName == 'Enabled':
extElement.firstChild.data = 'true'

if typeName.firstChild.data == "VersionManagementServer":
extension = typeName.parentNode
for extElement in extension.childNodes:
# Include a feature layer
if extElement.tagName == 'Enabled':
extElement.firstChild.data = 'true'


# Write to new .sddraft file
sddraft_mod_xml = service_name + '_mod_xml' + '.sddraft'
sddraft_mod_xml_file = os.path.join(outdir, sddraft_mod_xml)
f = open(sddraft_mod_xml_file, 'w')
doc.writexml(f)
f.close()

# Modify the .sddraft file to change feature layer properties
# Defaults are Query,Create,Update,Delete,Uploads,Editing
# Comment out the line below if you don't want to modify feature layer properties
configure_featureserver_capabilities(sddraft_mod_xml_file, "Create,Sync,Query")

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

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

print("Finish Publishing")



image.png


 
 

0 个评论

要回复文章请先登录注册