Arcpy.mapping Combat-Thematic mapping automation
by Li Yuanxiang
Initially interested in arcpy.mapping because of a large-scale thematic map adjustment needs, due to the use of some units arcengine prepared by the thematic mapping system, there are some small problems, resulting in the location of the map decoration element error. Since the final production task has come to an end only to find some problems, it is necessary to use ArcMap outside the diagram system to modify, so that the thousands of documents to modify the results, is a small amount of work. Later, it was decided to use arcpy.mapping to recursively query the MXD document using Python, eventually automating the modification and re-drawing. Although only a few features were used at the time, basically the position and text of the finishing elements were adjusted, but Arcpy.mapping's help was really huge.
Later, because of another thing, Arcpy.mapping has a deeper insight, is to help a friend to make a thematic map. Due to the total total cause of the previous, resulting in multiple frequent changes, and each modification will result in more than 10 mxd files, each MXD document out of more than 10 topics, the figure includes the normal PNG and PDF format, the number of clicks is more than 300 times, each time to re-plot is more than two hours. So, as long as the map changes are involved, re-plot is a very troublesome thing. Later thought of the original help of the unit, using the arcpy.mapping to deal with, all the problems are solved.
First of all to see the author to do the thematic map, in fact, is very simple, is based on the soil sampling point data interpolation, the formation of soil elements distribution map. Take a look at some of the configuration of the MXD documentation,
This involves a variety of element interpolation data, each topic need to have a legend and title, of course, the title of the map needs to be changed according to the name of the layer, but it cannot include the brackets inside the unit. Just this one MXD document requires 14 themes, one for PNG and 28 for each PDF. A total of 14 areas, each region to export 28 charts, including the empty value layer display, modify the title name and choose Save Location, the name of the thematic map, and so on, it really takes some time.
However, many years of drawing experience has given a lot of help, is the standardization of map production. At first, I did not consider any of the normative issues, because basically a shot everywhere is the normative operation (please allow me to narcissism). The first is the name of the layer, with a standard name, such as a parenthesis behind each element, and a specific unit in the bread. The second is that each layer is a separate legend, rather than putting all the graph layers into a legend, because it is necessary to have a thematic map of each element, so that it is not associated with the other layers, the intuition tells me that I should not covet the convenience of all put in a legend (certainly put together also has its advantages, Management is easy, but the bad thing is the name of the legend is not good control). Finally, the specification of the decoration element is named, and the title and legend are given the name attribute. Such as
After these specifications, and in line with the previous chapters of the arcpy.mapping Big Three, basically can be peace of mind. Let's take a look at the code section below
#coding =GBK
Import arcpy
Mxd = Arcpy.mapping.MapDocument (r "C:\Users\lazy\Desktop\ Interpolation Analysis (spatial analysis) \ Elemental Analysis results \ Elemental Analysis feature map. MXD")
Quyu = "xx County "
Quyu=quyu. Decode (' GBK ')
DF = Arcpy.mapping.ListDataFrames (mxd, " layer ") [0]
Inlayer=arcpy.mapping.listlayers (Mxd, "", DF)
For layer in Inlayer:
#匹配图层名称
LAYERNAMESTR = layer. name
Danwei = "(mg/kg)"
Canfind = Layernamestr. Find (Danwei)
if canfind>0:
#获取前面字符串
Houzhui = " Elemental content distribution Map "
if (canfind==3):
Houzhui=houzhui. Decode (' GBK ')
Picname=layernamestr[0:canfind]
Picname=picname+ ". PNG"
Picname2=picname+ ". pdf"
TitleName =quyu+layernamestr[0:canfind] +houzhui
#设置标题
For Elm in arcpy.mapping.ListLayoutElements (Mxd, "text_element"):
if Elm. name = = "mapname":
Elm.text = TitleName
#显示图层
Layer.visible=true
#输出底图
FolderPath = "c:\\users\\lazy\\desktop\\ interpolation Analysis (spatial analysis) \ \" \ "
Folderpath=folderpath. Decode (' GBK ')
FullPath = Folderpath+picname
Arcpy.mapping.ExportToPNG (MXD, fullpath,resolution=200)
Arcpy.mapping.ExportToPDF (MXD, fullpath,resolution=200)
#关闭图层
Layer.visible=false
Now that you've defined the title, you just need to find the text element named Mapname. This process, of course, needs to be used comprehensively. Make full use of the above mentioned big three completion. Go to find Mxd via mapdocument, then point to layer using Layer object, use the Visible property of layers to switch layer, because each element is a layer corresponding to a legend, the legend will open when the layer opens. So here layer.visible is very important, the document initialization only need to open the basic background data, the special data do not open, or there will be multiple overlap; get the layer name and parse it after the layer is found, then get the "(mg/kg)" The preceding string (that is, the element name of the thematic map, which is useful for the title and legend), according to different units for code reuse, in order to lazy, I do not write judgment statement, directly using the first for code block reuse several times, just modified the judging unit only , using the obtained element name to build the name of the text element, for example, the format is "xx County x element content distribution map", see the code construction method, using Mapsurroundelement to modify, the legend name can also be modified; Directly using Arcpy.mapping.ExportToPNG and Arcpy.mapping.ExportToPDF to plot, the plot path has been built, that is, the main name can be customized, the name of the element extracted from the layer name as the file name.
When you are done, you are no longer afraid of the need to change, export map No more crazy click the mouse and keyboard. Just double-click on this Python script to make it easy to drink a good glass of tieguanyin and wait a few minutes. Compared to the traditional way of manual mapping, it is easier to save more worry. Take a look at the plot results for the following area
Summary: From this practical example can be seen, if you want to use arcpy.mapping to automate the diagram, map production must be very standardized, although this specification is not as harsh as the map template, but must add some map of the controllable means. If there is no standard naming and processing at the beginning, it will be very passive later on. Arcpy.mapping itself is not difficult, there are not many objects and methods to use, so it is very easy to learn and use. After mastering the four pieces of arcpy.mapping (sorry the fourth piece has not yet written out ^_^), in the face of the same problem can be juchongruoqing.
For the basics of arcpy.mapping, you can view previous content:
arcpy.mapping-know Arcpy.mapping
Arcpy.mapping Common Four pieces-mapdocument
Arcpy.mapping Common Four pieces-layer
Arcpy.mapping Common Four pieces-mapsurroundelement
Arcpy.mapping Combat-Thematic mapping automation