#coding=utf-8
import arcpy
import xml.dom.minidom
from xml.dom.minidom import parse
import os
reload(sys)
sys.setdefaultencoding('utf-8')
def GetAGSConnectionFile(out_folder_path):
out_name = 'connn.ags'
server_url = 'http://192.168.202.26:6080/arcgis/admin'
use_arcgis_desktop_staging_folder = False
staging_folder_path = out_folder_path
username = 'arcgis'
password = 'arcgis'
out_file_path = os.path.join(out_folder_path, out_name)
if os.path.exists(out_file_path):
os.remove(out_file_path)
print server_url
arcpy.mapping.CreateGISServerConnectionFile('ADMINISTER_GIS_SERVICES',
out_folder_path,
out_name,
server_url,
'ARCGIS_SERVER',
use_arcgis_desktop_staging_folder,
staging_folder_path,
username,
password,
True)
return out_file_path
# Define local variables
mxdfullpath =arcpy.GetParameterAsText(0).decode('gbk').encode('utf-8') #MXD所在文件夹
servicename=arcpy.GetParameterAsText(1).decode('gbk').encode('utf-8') #地图服务名称
xmlFullPath=arcpy.GetParameterAsText(2).decode('gbk').encode('utf-8') #地图服务描述配置文件路径
con_file_path=arcpy.GetParameterAsText(3).decode('gbk').encode('utf-8')
# mxdfullpath ='D:\\zx\\DKFW.mxd' #MXD所在文件夹
# servicename='DKFW_3502_2020_V001'
# xmlFullPath='D:\\mxd\\MxdPublishMapServise.xml' #地图服务描述配置文件路径
# con_file_path='D:\\mxd\\test.ags'
mxdfullpath = eval(repr(mxdfullpath).replace('\\', '/'))
xmlFullPath = eval(repr(xmlFullPath).replace('\\', '/'))
con_file_path = eval(repr(con_file_path).replace('\\', '/'))
print mxdfullpath+'\r'
print xmlFullPath+'\r'
print con_file_path+'\r'
open("F:\\MxdFile\\fail.txt", "a+").write('mxdfullpath:'+mxdfullpath+'\r')
open("F:\\MxdFile\\fail.txt", "a+").write('xmlFullPath:'+xmlFullPath+'\r')
open("F:\\MxdFile\\fail.txt", "a+").write('con_file_path:'+con_file_path+'\r')
if mxdfullpath.endswith('.mxd'):
mapDoc=mxdfullpath
# Provide path to connection file
# To create this file, right-click a folder in the Catalog window and
# click New > ArcGIS Server Connection
con = mxdfullpath + '/connn.ags'
# Provide other service details
service = servicename
# service = 'USA'
print service
sddraft = mxdfullpath + service + '.sddraft'
sd = mxdfullpath + service + '.sd'
try:
DOMTree = parse(xmlFullPath)
collection = DOMTree.documentElement
if collection.hasAttribute("Name"):
listDescrip = collection.getElementsByTagName("item")
for descrip in listDescrip:
if (descrip.getAttribute("MxdName")):
summary = descrip.getAttribute("Descrip")
# print("MxdName: %s" % descrip.getAttribute("MxdName"))
# print("Descrip: %s" % descrip.getAttribute("Descrip"))
summary = descrip.getAttribute("Descrip")
tags = ''
# Create service definition draft
arcpy.mapping.CreateMapSDDraft(mapDoc, sddraft, service, 'ARCGIS_SERVER', con_file_path, True, None, summary,
tags)
# Analyze the service definition draft
analysis = arcpy.mapping.AnalyzeForSD(sddraft)
# Print errors, warnings, and messages returned from the analysis
print "The following information was returned during analysis of the MXD:"
for key in ('messages', 'warnings', 'errors'):
print '----' + key.upper() + '---'
vars = analysis[key]
for ((message, code), layerlist) in vars.iteritems():
print ' ', message, ' (CODE %i)' % code
print ' applies to:',
for layer in layerlist:
print layer.name,
# Stage and upload the service if the sddraft analysis did not contain errors
if analysis['errors'] == {}:
# Execute StageService. This creates the service definition.
arcpy.StageService_server(sddraft, sd)
# Execute UploadServiceDefinition. This uploads the service definition and publishes the service.
arcpy.UploadServiceDefinition_server(sd, con_file_path)
print "Service successfully published"
else:
print "Service could not be published because errors were found during analysis."
if os.path.exists(sd):
os.remove(sd)
print arcpy.GetMessages()
# except:
# print
# pass
except Exception as e:
print(servicename + " Service could not be published because errors were found during analysis.")
print(e)
# file
open("F:\\MxdFile\\fail.txt", "a+").write(
servicename + " Service could not be published because errors were found during analysis.错误信息:" + e.message + "信息:" + arcpy.GetMessages() + '\r')
open("F:\\MxdFile\\fail.txt", "a+").write(e.message)
if os.path.exists(sd):
os.remove(sd)
if os.path.exists(sddraft):
os.remove(sddraft)
pass
#
上述脚本,有四个传入参数分别为:
1.mxdfullpath:mxd文件全路径
2.servicename:地图服务名称
3.xmlFullPath为服务说明文件
4.con_file_path:ags连接文件
其中:3.xmlFullPath为服务说明文件,主要是为了赋值地图服务的Descrip,格式如下:
<collection Name="pp">
<item MxdName="GGFWSSPZD" Descrip="公共服务设施配置(点)"/>
<item MxdName="GGFWSSPZM" Descrip="公共服务设施配置(面)"/>
<item MxdName="DKFW" Descrip="地块范围"/>
</collection>