Arcpy.mapping Common Four pieces-layer

Source: Internet
Author: User

Arcpy.mapping Common Four pieces-layer

by Li Yuanxiang

Layers are a very important part of the map, and almost all cartographic displays are related to layers. First of all, it is an important bridge to connect data with symbolic rendering, and then in the publishing map, the layer is associated with the legend, and of course, the most important thing is the organization data, the classification of data, up and down, the display of annotations, and so on, all of which are related to the layer. In arcpy.mapping, layer is a very important type, if Mapdocument is the first entry of arcpy.mapping, then the layer is the VIP sitting in the mapping.

In arcpy.mapping, the layer has two places to enter, one through Mapdocument–>dataframe, and the other directly through the. lyr file. In general, the first case is more commonly used, and is basically an automated modification of one or more layers of a map document, which behaves as a detail modification. And through the LYR into the layer, it is more hope that through the desktop software to complete the configuration of a certain layer, and then the overall update or INSERT into the map document, its performance is extensive modification.

The simplest way to know what a layer can do is to look at its properties and methods. Properties are the most common way, and by looking at properties, you can basically know that there are some objects and functions associated with a layer. About the properties and methods of layer, just need to view ESRI official help on the line, specifically about layer help, not listed here, connect the address click here. The properties of a layer can be basically the same as the tab of the layers properties, including data source information (DatasetName, DataSource, Workspacepath are read-only), callouts (labelclasses, showlabels are read-only), General properties such as name, visible scale, etc. (name, Maxscale, Minscale are read and write), layer filtering (Definitionquery read/write), Layer type judgment (Isfeaturelayer, Isgrouplayer, Isnetworkanalystlayer, Israsterizinglayer, Israsterlayer, Isservicelayer are read only), symbology (symbology, SymbologyType are read-only), Tense (time is read-only). Of course, this is only a poor classification, and some of the more commonly used is visible, transparency.

The method is not much, it is simply a few such as get the Layer range (getextent), select the collection range (Getselectedextent, Setselectionset), set the data source (Findandreplaceworkspacepath, Replacedatasource), save layer (save, saveacopy), Update layer Action (Updatelayerfromjson), etc., the method is much less than the attribute, which is also related to the localization of arcpy coarse granularity, but nonetheless, These methods and properties also basically cover all the actions of the layer.

You can then look at the official example and go through the. lyr file to find the named highways layer, and modify the label to show

Import arcpy
LyrFile = Arcpy.mapping.Layer (r "C:\Project\Data\Streets.lyr")
For LYR in Arcpy.mapping.ListLayers (lyrFile):
    if lyr.name. Lower () = = "Highways":
        Lyr.showlabels = True
        Lyr.saveacopy (r "C:\Project\Data\StreetsWithLabels.lyr")
Del LyrFile

But in fact, we would like to directly modify the MXD document, so, the way to get the layer is not the way to use the LYR, but the use of dataframe into, of course, that also need to cooperate with Listdataframes get Dataframe, Get a list of layers with listlayers code as follows

#coding =GBK
Import arcpy
Mxd = Arcpy.mapping.MapDocument (r "C:\PythonTest\change.mxd")
DF = Arcpy.mapping.ListDataFrames (mxd, " layer ") [0]
#对第一个图层进行修改
Lyrs=arcpy.mapping.listlayers (Mxd, "", DF)
For layer in lyrs:
    if layer. name. Lower () = = "Highways":
        Layer.showlabels = True
Mxd.save ()
Del Mxd

Visible, it is relatively simple to modify the settings of a layer. Other properties, such as name, Maxscale, Minscale, visible, transparency, definitionquery, can be read and written, and can be set directly.

For example, a deeper setting for a layer callout, the Labelclasses property mentioned earlier, although the new Labelclasses object cannot be replaced as a whole, the properties of labelclasses itself can be modified, For example, to set the label for the first layer to be displayed by a combination of two fields by City_name_cntry_name, you can modify it by labelclasses expression. This expression is a read-write property in Labelclasses. The following code

#coding =GBK
# #修改label的显示字段
Import arcpy
Mxd = Arcpy.mapping.MapDocument (r "C:\PythonTest\change.mxd")
DF = Arcpy.mapping.ListDataFrames (mxd, " layer ") [0]
#对第一个图层进行修改
LYR = Arcpy.mapping.ListLayers (Mxd, "", DF) [0]
Lyr.showlabels = True
Lyr.labelclasses[0].expression  = ' [city_name] + '_' +[cntry_name] '
#保存结果
Mxd.save ()

Thus, the above mentioned layer of the properties, is not the traditional only to obtain information of the read-only mode, but that the property is forbidden to re-assign the object, but the Property object can be read-write settings inside can be effective. This gives the layer a lot of possible ways to set it up. For example, using the name as a unique value for rendering, now with the Dis field as a unique value, through the arcpy code is also very good production, the following code

The processing code is as follows

#coding =GBK
Import arcpy
Mxd = Arcpy.mapping.MapDocument (r "C:\PythonTest\change.mxd")
DF = Arcpy.mapping.ListDataFrames (mxd, " layer ") [0]
LYR = Arcpy.mapping.ListLayers (Mxd, "", DF) [1]
if lyr.symbologytype = = "unique_values":
  Lyr.symbology.valueField = "Dis"
  Lyr.symbology.addAllValues ()
arcpy. Refreshactiveview ()
arcpy. Refreshtoc ()
Mxd.save ()
Del Mxd

Looking at the results of the final execution, a unique value has been re-rendered according to Dis.

Symbologytype but similar to this type of setting to pay attention to, why should first Judge Symbologytype, that is because the previous also confessed, symbologytype and symbology for layer, are read-only properties, so, The layer's render type cannot be modified here, but it can modify the properties under these fixed types. So, if you want to make a unique value rendering, it is necessary that the layer itself is rendered with a unique value. Other render mode settings are the same, only modified in the set rendering mode, not directly changing the rendering mode. The example layer itself is a single symbol rendering and cannot be modified to be rendered as a unique value. It is estimated that many people do not understand this setting, including the author, the modification of the open rendering type is not difficult, and I do not know what ESRI is thinking in this regard.

There are Isfeaturelayer, Isgrouplayer, Isnetworkanalystlayer, Israsterizinglayer, Israsterlayer, IsServiceLayer These several properties of the layer type of interpretation, it does not seem to play a large role, in fact, with some of the operation of the docking, it is very good to control some of these operations can be performed. For example, using the Listlayers method to get a list of layers, you will get all the layers, if you need to set up a data source, the composition of the layer is not modified method, without judging the case, will cause the program directly error and crash. Therefore, do not underestimate these very subtle interpretation properties, which have a great effect on logical operations.

Summarize:

In the Layer object, you can basically make some minor adjustments to the layers ' settings to achieve some general modifications to the ArcMap interface. The part of the modification is more concentrated in its Properties section than its method. So, to use a good layer object, it is useful to know some of the settings of its associated Property object.

Arcpy.mapping Common Four pieces-layer

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.