Arcpy basics-4. Advanced arcpy tools, arcpy-4

Source: Internet
Author: User

Arcpy basics-4. Advanced arcpy tools, arcpy-4

When using the tools in the arcmap toolbox, if the corresponding layers or data have been loaded, you can select from the drop-down list, that is to say, the tool automatically loads these layers by selecting them. This section describes how to automatically select layers and data.

1. Set the parameter type to Featurelayer, RasterLayer, and other parameters with Layer.

Let's look at an instance code:

Import arcpy

# Retrieving Layers

Fc = arcpy. GetParameter (0)

Arcpy. RepairGeometry_management (fc)

Follow the tutorial to create the arcpy tool. Set the parameters as follows:


You can automatically select layers, for example:

 

2. automatically obtain layers or data through code

Let's look at an instance code:

Import arcpy, OS

# Read parameters from dialog

MxdPath = arcpy. GetParameterAsText (0)

DfName = arcpy. GetParameterAsText (1)

LayerName = arcpy. GetParameterAsText (2)

LayerFile = arcpy. GetParameterAsText (3)

OutMXD = arcpy. GetParameterAsText (4)

# Update layer symbology

Mxd = arcpy. mapping. MapDocument (mxdPath)

Df = arcpy. mapping. ListDataFrames (mxd, dfName) [0]

UpdateLayer = arcpy. mapping. ListLayers (mxd, layerName) [0]

SourceLayer = arcpy. mapping. Layer (layerFile)

Arcpy. mapping. UpdateLayer (df, updateLayer, sourceLayer, True)

 

# Save changes to new MXD and automaticallyopen

Mxd. saveACopy (outMXD)

OS. startfile (outMXD)

Del mxd, sourceLayer

The tool parameters are set as follows:

The parameter setting is followed by a Validation


You can use the Edit button below to Edit the code. After editing, click Save and click the application button below. The paste code is as follows:

Class ToolValidator:

"Class for validating a tool's parameter values andcontrolling

Thebehavior of the tool's dialog ."""

 

Def _ init _ (self ):

"Setup the Geoprocessor and the list of toolparameters ."""

Import arcpy

Self. params = arcpy. GetParameterInfo ()

 

DefinitializeParameters (self ):

"Refine the properties of a tool's parameters. This method is

Called when the tool is opened ."""

Return

 

DefupdateParameters (self ):

"Modify the values and properties of parameters beforeinternal

Validation is completed MED. Thismethod is called whenever a parmater

Has been changed ."""

Import arcpy

 

# Update Data Frames list

If self. params [0]. value:

Mxd = arcpy. mapping. MapDocument (self. params [0]. value. value)

 

DataFrames = arcpy. mapping. ListDataFrames (mxd)

DfList = []

For df in dataFrames:

DfList. append (df. name)

DfList. sort ()

If not self. params [1]. altered:

Self. params [1]. filter. list = dfList

 

# Update Layer list (in selected data frame)

If self. params [1]. value:

Df = arcpy. mapping. ListDataFrames (mxd, (self. params [1]. value) [0]

Lyrs = arcpy. mapping. ListLayers (mxd, "", df)

LayerList = []

For lyr in lyrs:

LayerList. append (lyr. name)

LayerList. sort ()

If not self. params [2]. altered:

Self. params [2]. filter. list = layerList

Return

 

DefupdateMessages (self ):

"Modify the messages created by internal validation foreach tool

Parameter. This method is calledafter internal validation ."""

Return

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.