实现ArcGIS Server 10.x自动发布地图的解决方案

        首先来看解决方案的技术流程图:

实现ArcGIS Server 10.x自动发布地图的解决方案_第1张图片

        首先,我们先在D盘下建一个“MxdPublisher”的目录,然后创建一个python脚本文件:“MxdPublish.py”,里面代码如下:

#-*-coding:utf-8
__author__ = 'suwen'
import os
import arcpy
import xml.dom.minidom as DOM
def CreateContectionFile(wrkspc,userName,password,serverName):
    con="http://"+str(serverName)+":6080/arcgis/admin"
    connection_file_path=str(wrkspc)+"/tmp.ags"
    #
    if os.path.exists(connection_file_path):
        os.remove(connection_file_path)
    agsname=os.path.basename(connection_file_path)
    arcpy.mapping.CreateGISServerConnectionFile("ADMINISTER_GIS_SERVICES",
                                                    wrkspc,
                                                    agsname,
                                                    con,
                                                    "ARCGIS_SERVER",
                                                    username=userName,
                                                    password=password,
                                                    save_username_password=True)

    return connection_file_path


def PublishService(connection_file_path,wrkspc,mapDoc):
    #get the serviceName
    serviceName =os.path.basename(mapDoc).replace(".mxd","")

    #create the .sddraft path
    sddraftname=os.path.basename(mapDoc).replace(".mxd",".sddraft")
    sddraft =str(wrkspc)+"/"+str(sddraftname)
    #create the .sd file path
    sdname=os.path.basename(mapDoc).replace(".mxd",".sd")
    sd=str(wrkspc)+"/"+str(sdname)
    #check the file exists or not
    if(os.path.exists(sd)):
        os.remove(sd)

    analysis = arcpy.mapping.CreateMapSDDraft(mapDoc, sddraft, serviceName, 'ARCGIS_SERVER',
                                              connection_file_path, True, 'ProjectData',None,None)
    #启用WMS
    doc = DOM.parse(sddraft)
    typeNames = doc.getElementsByTagName('TypeName')
    for typeName in typeNames:
      if typeName.firstChild.data == 'WMSServer':
        extention = typeName.parentNode
        for extElement in extention.childNodes:
            if extElement.tagName == 'Enabled':
                extElement.firstChild.data = 'true'

    f = open(sddraft,'w')
    doc.writexml(f, encoding='utf-8')
    f.close()
    
    arcpy.StageService_server(sddraft, sd)
    arcpy.UploadServiceDefinition_server(sd, connection_file_path)

connection_file_path="D:\MxdPublisher"
mxdfolder=u"D:\MxdPublisher"
wrkspc=r"D:\MxdPublisher"
serverName="192.168.173.100"
userName="lixw"
password="info12345"

connection_file_path=CreateContectionFile(wrkspc,userName,password,serverName)
# the mxd file list of the mxdPath

mxds=os.listdir(mxdfolder)
mxd_files=[]
for file in mxds:
    if file.endswith(".mxd"):
        mxdpath=os.path.join(mxdfolder,file)
        mxd_files.append(mxdpath)

for mxd in mxd_files:
    PublishService(connection_file_path,wrkspc,mxd)

        注意,代码里下面的几个变量根据实际情况进行修改:

connection_file_path="D:\MxdPublisher"
mxdfolder=u"D:\MxdPublisher"
wrkspc=r"D:\MxdPublisher"
serverName="192.168.173.100"
userName="lixw"
password="info12345"

    接下来,可以往“D:\MxdPublisher”目录下手动拷贝一个mxd及其相关文件进去(注意,mxd须保存为相对路径),然后执行“MxdPublish.py”脚本即可进行测试(机器需安装ArcGIS Desktop 10.x环境),脚本执行过程可能有点慢,请耐心等待,执行完后可能会报错,这个不用管,登录arcgis server 10.x可查看发布是否成功。

    如果上面手动测试OK,后面就是在C#里调用“MxdPublish.py”脚本进行动态发布了,backgroundWorker1_DoWork里的代码如下:

            string mxdPath = e.Argument as string;

            //创建临时文件夹
            string tempDir = @"D:\MxdPublisher";
            if (Directory.Exists(tempDir) == false)
            {
                Directory.CreateDirectory(tempDir);
            }
            else
            {
                this.ClearMxdPublishTempDir(tempDir);
            }
            //拷贝mxd及其所在文件夹下所有文件及其子文件夹到临时文件夹
            string mapServerName = this.CopyMxdDocument2TempDir(mxdPath, tempDir);

            //执行脚本
            string pyPath = Path.Combine(Application.StartupPath, "MxdPublish.py");
            try
            {
                ProcessStartInfo info = new ProcessStartInfo(pyPath);
                info.WindowStyle = ProcessWindowStyle.Hidden;

                Process pro = new Process();
                pro.StartInfo = info;
                pro.Start();

                while (!pro.HasExited)
                {
                    System.Threading.Thread.Sleep(500);
                    Application.DoEvents();
                }

                string wmsUrl = string.Format("http://192.168.173.100:6080/arcgis/services/ProjectData/{0}/MapServer/WMSServer?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetCapabilities", mapServerName);
                e.Result = wmsUrl;
            }
            catch (Exception ex)
            {
                e.Result = null;
                MessageBox.Show(ex.Message);
            }


你可能感兴趣的:(server,自动,发布,地图,arcgis,10.1,10.2,mxd)