思路
- 通过arcgis server发布,连接arcgis server
- 发布要素服务,需要先创建草稿文件
- 创建草稿文件之后,需要修改草稿文件的配置,将要素服务开启以及去掉创建功能
- 分析草稿文件
- 编译资源
- 发布服务
代码
import arcpy, sys
import xml.dom.minidom as DOM
import os
workSpace = r"D:\testpublish"
gisServer = 'localhost'
gisPort = 6443
username = 'arcgis'
password = 'arcgis'
serverUrl = 'https://{0}:{1}/arcgis/admin'.format(gisServer, gisPort)
print(serverUrl)
outAGSName = 'arcgis_admin.ags'
def createAGSConnection(workSpace, outAGSName, serverUrl, username, password):
path = workSpace + '\\' + outAGSName
if os.path.exists(path):
os.remove(path)
print("Existing ArcGIS Server connection file deleted ... ")
arcpy.mapping.CreateGISServerConnectionFile(connection_type="ADMINISTER_GIS_SERVICES",
out_folder_path=workSpace,
out_name=outAGSName,
server_url=serverUrl,
server_type="ARCGIS_SERVER",
use_arcgis_desktop_staging_folder=False,
staging_folder_path=workSpace,
username=username,
password=password,
save_username_password=True)
print('ArcGIS Server connection file created: {0}'.format(outAGSName))
return
def publishingFeatureService(workSpace, mxdFile, outAGSName):
mapDoc = arcpy.mapping.MapDocument(os.path.join(workSpace, mxdFile))
print('Map Document: {0}'.format(mapDoc.filePath))
service = mxdFile.split(".")[0]
sddraft = os.path.join(workSpace + '\\', service + '.sddraft')
sd = os.path.join(workSpace + '\\', service + '.sd')
summary = mapDoc.summary
tags = mapDoc.tags
newSDdraft = os.path.join(workSpace + '\\', service + 'FS.sddraft')
analysis = arcpy.mapping.CreateMapSDDraft(map_document=mapDoc,
out_sddraft=sddraft,
service_name=service,
server_type='ARCGIS_SERVER',
connection_file_path=outAGSName,
copy_data_to_server=False,
folder_name='test',
summary=summary,
tags=tags)
doc = DOM.parse(sddraft)
tagsType = doc.getElementsByTagName('Type')
for tagType in tagsType:
if tagType.parentNode.tagName == 'SVCManifest':
if tagType.hasChildNodes():
tagType.firstChild.data = "esriServiceDefinitionType_Replacement"
tagsState = doc.getElementsByTagName('State')
for tagState in tagsState:
if tagState.parentNode.tagName == 'SVCManifest':
if tagState.hasChildNodes():
tagState.firstChild.data = "esriSDState_Published"
services___ = doc.getElementsByTagName('TypeName')
for service__ in services___:
if service__.firstChild.data == "FeatureServer":
service__.parentNode.getElementsByTagName('Enabled')[0].firstChild.data = 'true'
typeNames = doc.getElementsByTagName('TypeName')
for typeName in typeNames:
if typeName.firstChild.data == 'FeatureServer':
typeName.parentNode.getElementsByTagName('Value')[1].firstChild.data = 'Query,Update,Delete,Uploads,Editing'
print typeName.parentNode.getElementsByTagName('Value')[1].firstChild.data
f = open(newSDdraft, 'w')
doc.writexml(f)
f.close()
analysis = arcpy.mapping.AnalyzeForSD(newSDdraft)
if analysis['errors'] == {}:
if os.path.exists(sd):
os.remove(sd)
print("{0} already exists! Deleted! ".format(sd))
arcpy.StageService_server(newSDdraft, sd)
arcpy.UploadServiceDefinition_server(sd, outAGSName)
print "Uploaded and overwrote feature service success"
else:
print '----------------------'
print analysis['errors']
if os.path.isdir(workSpace) == False:
print "Not valid path..."
else:
createAGSConnection(workSpace, outAGSName, serverUrl, username, password)
files = os.listdir(workSpace)
for f in files:
if f.endswith(".mxd"):
reload(sys)
sys.setdefaultencoding('utf-8')
print "publishing: " + f
publishingFeatureService(workSpace, f, outAGSName)
else:
continue