Use a Python script to automatically publish the ArcGIS Service

Source: Internet
Author: User
Tags vars

Generally, we use graphical tools such as arccatalog or ArcGIS Server Manager to publish the ArcGIS service. However, in some cases, for example, when the number of services to be released is large and the service needs to be released occasionally, using these tools for manual operations is far from enough. The powerful feature of ArcGIS is that it provides a variety of options for us. We can use python to write scripts and call the functions in the arcpy module to automatically scan folders and publish the map documents as map services.

This article will create a publishhelper. py file and write two methods in it. One is publishall, which is used to traverse the folder and call the other method publishmxd. The latter completes the specific service release work.

Before publishing a service, you must first check the mxd document path, read the mxd document, convert it to MSD, analyze the document, and finally publish the service.

#-*-Coding: UTF-8-*-import arcpy, OS _ name _ = 'hhhelper '# All. mxd document is published as map service # Folder: folder path containing mxd Document # servicedir: Service Directory URL, such as http: // localhost/ArcGIS/rest/services # servicefolder: the folder where the service is located. If it is null, it indicates the root directory def publishall (folder, servicedir, servicefolder): Print "Check the folder path ...... "If OS. Path. isdir (folder) = false: Print" the Input Folder path is invalid! "Return print" traverse folders ...... "Files = OS. listdir (folder) for F in files: If F. endswith (". mxd "): mxdpath = OS. path. join (folder, f) print "publishing:" + F publishmxd (mxdpath, servicedir, servicefolder) else: Continue # publish the mxd document as a service: 1. convert mxd to MSD; 2. analyze MSD; 3. publish msddef publishmxd (mxdpath, servicedir, servicefolder): # Check whether print exists in mxd and MSD Files "check file path ...... "If OS. Path. exists (mxdpath) = false: Print" The mxd document in the specified path does not exist! "Return # Open mxd document try: Print" Opening mxd document ...... "Mxd = arcpy. Mapping. mapdocument (mxdpath) failed t exception, E: Print" Open mxd error: ", e return else: Print" mxd file opened successfully ...... "# Getting the default data box print" reading the mxd document default data box ...... "Df =" "try: frames = arcpy. mapping. listdataframes (mxd, "layer") if Len (frames) = 0: frames = arcpy. mapping. listdataframes (mxd, "layers") df = frames [0] failed t exception, E: Print "An error occurred while reading the default data box of mxd documents :", E return # construct MSD document name msdpath = mxdpath. replace (". mxd ",". MSD ") # convert mxd to MSD print" converting mxd document to MSD document ...... "Arcpy. Mapping. converttomsd (mxd, msdpath, DF," normal "," normal ") # analyze MSD print" the document is being analyzed ...... "Analysis = arcpy. mapping. analyzeformsd (mxd) # list analysis result information 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, print # retrieve Server Information servicename = OS. path. basename (msdpath ). replace (". MSD "," ") serverna Me = servicedir. Split ("/") [2] Try: # publish MSD print "publish service ...... "Arcpy. mapping. publishmsdtoserver (msdpath, servicedir, servername, servicename, servicefolder, ["WMS", "kml"]) failed t exception, E: Print "publish service failed:", e else: print "Service released successfully!"

Call code:

Import syssys. path. append ("e :\\ codes \ Python") from publishhelper import publishall # You must have the correct permission. Otherwise, the Import fails. publishall ("D: \ testdata ", "http: // localhost/ArcGIS/rest/services", "Sichuan ")

In this way, you can use the Python script to traverse the folder and publish the mxd document as the map service. It can also be used as a Windows service to automatically run as soon as possible.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.