Archive for November, 2010
Through December 31st you can save 20% on all e-learning courses offered by GeoSpatial Training Services. This includes both self-paced and Internet based, instructor led courses. Simply enter the discount code ‘2010holiday‘ when checking out.
Most popular courses:
- GIS Programming 101: Mastering Python for Geoprocessing in ArcGIS 10
- Programming the ArcGIS Server API for Flex
- ArcGIS Server Bootcamp
- Google Earth and Google Maps Bundle
- Mastering KML for Google Earth
- Working with Geodatabases and Linear Referencing
- Programming ArcObjects with .NET
- ArcGIS Desktop Bundle
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.Read Full Post | Make a Comment ( None so far )
Printing and exporting maps is obviously one of the most commonly performed ArcGIS tasks. The new Arcpy.mapping module provides several functions that you can use to automate your routine print and export tasks. In this post we’ll examine Arcpy.mapping functions you can use for printing and exporting either maps from either the data or layout view in ArcMap.
Arcpy.mapping provides a PrintMap() function for printing page layouts or data frames within ArcMap and a host of Export functions that export the page layout or a data frame to various image formats and PDF files. Before calling PrintMap() it is common practice to call the ListPrinterNames() function which returns a list of the available printers for the local computer. A particular printer can then be found by iterating the list of printers and used as input to the PrintMap() function.
PrintMap() can print either a specific data frame or the page layout of a map document. By default this function will use the printer saved with the map document or, if not present in the map document, the default system printer. As I mentioned you can also use ListPrinterNames() to get a list of the available printers and select one of these printers as input to PrintMap(). In the code example on below PrintMap() uses the default printer as specified through the use of double quotes as the second parameter to PrintMap().
You can use one of the many Export functions provided by Arcpy.mapping to export a data frame or page layout to an image file. Available formats include AI, BMP, EMF, EPS, GIF, JPEG, SVG, and TIFF. The parameters provided for each function will vary depending upon the type of image. Examples of these function names include ExportToJPEG, ExportToGIF, ExportToBMP, and all the others you see listed here.
You can also export your data frames or page layout to PDF format with ExportToPDF(). PDF is obviously a very popular interchange format so you will likely need to become familiar with this function. Calling ExportToPDF() is much the same as calling any of the image export functions since it takes an mxd and path to output the file along with optional properties for the PDF.
In addition to exporting your maps to PDF you can also manipulate existing PDF documents or create new PDF documents. You can merge pages, set document open behavior, add file attachments, and create or change document security settings. The PDFDocumentOpen() function is used to open an existing PDF file for manipulation. PDFDocumentCreate() creates a new PDF document. These functions are often used in the creation of map books.
You’ll need to use PDFDocumentCreate to create a new PDF document by providing a path and filename for the document. The PDF is not actually created on disk until you insert or append pages and then call PDFDocument.saveAndClose(). The appendPages() and insertPages() functions are used to insert and append pages.
PDFDocumentOpen accepts a parameter that specifies the path to a PDF file and returns an instance of PDFDocument. Once open you can make modifications to PDF file properties, add or insert files, and attach documents. Make sure you call PDFDocument.saveAndClose() after all operations to save the changes to disk.
A number of properties can be set on a PDF Document through the PDFDocument object including getting a page count, attaching files, updating title, author, subject, keywords, open behavior, and the layout. You can also update the document security by calling PDFDocument.updateDocSecurity() to set a password, encryption, and security restrictions.
Other ArcPy Mapping articles.
- Using ArcPy Mapping to Add and Remove Layers in a Map Document
- Working with Layout Elements in the ArcPy Mapping Module
- Introducing the ArcPy Mapping Module in ArcGIS 10
- Free E-Learning Module from the ArcGIS 10 Version of our GIS Programming 101 Course
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 October 25th, 2010. You can still register even though class has already begun.Read Full Post | Make a Comment ( None so far )