Use ArcPy Mapping to Publish Map Documents to ArcGIS Server
The ArcPy Mapping module, released with ArcGIS 10 provides a number of capabilities including the management of map documents and layers, printing, exporting, and ArcGIS Server publishing as well as map automation and the creation of PDF map books. In this post you will learn how to use Python and the ArcPy Mapping module to convert a map document file to an ArcGIS Server service. At the end of this post you will have learned the following related to publishing your map document to ArcGIS Server:
- How to analyze a map document for warnings and errors
- Convert a map document file to a map service definition file (MSD)
- Publish a MSD file to ArcGIS Server as a service
Publication of a map document to ArcGIS Server with Python involves a three step process. The goal of the first two steps is to convert a map document file to a map service definition file (MSD). The analysis process scans the map document file for any warnings or errors. Errors must be resolved before publication to an MSD file and warnings should also be carefully examined because they can lead to performance problems in ArcGIS Server. Once errors and warnings have been resolved, the ConvertToMSD() function can then be called against the file. This function will create a new MSD file which can then be published to ArcGIS Server through the PublishMSDToServer() function.
Analyze the Map Document for Warnings and Errors
The AnalyzeForMSD() function analyzes the map document for suitability and performance issues before conversion to MSD. This function returns a Python dictionary containing the warnings, errors, and general messages. All errors must be repaired before publication. The code sample below shows an example of calling the AnalyzeForMSD() function.
The AnalyzeForMSD() function takes a map document as a parameter. It analyzes the document and returns information, warnings, and errors in a Python dictionary which in this case is stored in the variable ‘analysis’. We then loop through this dictionary variable and print out the information contained within. You can see an example of the information returned in the figure below.
You will want to pay particular attention to the ERRORS section. Each error that has been found must be fixed before the document can be published to ArcGIS Server. In this case the background symbol for our data frame is not a solid fill. A solid fill is required for background symbols. You can change that in ArcMap by clicking the active data frame and selecting Properties. All errors listed in the ERRORS section must be fixed before publication can occur.
Once you’ve fixed all errors that have been identified you can convert to your MXD file to an MSD file through the ConvertToMSD() function. You can specify a specific data frame as the active data frame for the service that will eventually be published to ArcGIS Server. Again, no errors can be present in the MSD file. The code example below shows how to convert an MXD file to an MSD.
ConvertToMSD() accepts several arguments including object variables for the mxd and msd files, the data frame to be used as the active data frame for the published service, and two parameters that deal with anti-aliasing. In this case we want the active data frame to be used as the active data frame in the published map service. Therefore, we can simply use a double quote (“”) as the third parameter of ConvertToMSD(). Anti-aliasing is a method for creating a better visualization of text and vector features. I normally just use “Normal” for both these parameters.
After checking your map document for errors, resolving any errors, and converting the map document to an MSD file you are finally ready to publish the document to ArcGIS Server. There are several administrative tasks that you must do before performing this final step.
- ArcGIS Server 10 must be installed
- Your data and MSD file must be in a location accessible to ArcGIS Server. More information about this can be found here
- The username running the publish script must be a member of the ArcGIS Server admin group. More information about that here.
In your script you’ll need to call PublishMSDToServer() to actually publish the MSD file as a service in ArcGIS Server 10. You must supply connection information and information about the service to this function.
The code example below represents the general nature of the script you will need to write. Obviously the path to the MSD file and your server names and service name and folder will be different.
Want to learn more about the ArcPy Mapping module and other programming ArcGIS Desktop with Python topics? The next session of our Internet based, instructor led course “GIS Programming 101: Mastering Python for Geoprocessing in ArcGIS” begins February 21st. We also provide a self-paced version of this course which you can find here.