ArcGis and Python: before starting

Getting started with Python in ArcGIS is easier than it looks. There is no need to master Python in order to use it in a basic way in our work with ArcGIS.
First of all, there are several options for executing our processes with Python code. So we will start by discussing the different options and how to make the right choice for our needs, whether it is simply testing a code line or a piece of script or the complete execution of a script.

The difference between running a code line and executing a script, in terms of programming, is that for the first time we will use an   INTERPRETER   and secondly, we will use an   EDITOR.

An interpreter will execute the code, order by order, while the editor translates the whole script code into the system’s machine code to create an executable file. It will be possible to run a program compiled directly into the machine, while the uncompiled code must be executed inside another application with the interpreter.

To see how the different options work we will use a simple example: we have a layer of polygons with the different councils of Finistère (France) and we want to obtain a layer of polylines. ArcGis geoprocessing tools have a task called, FeatureToLine_management, that executes this operation.

1-How to interpret Pyton

There are three ways to use Python through an interpreter:

  • From Python itself, in command line mode
  • From the ArcGis Python Console
  • From the console of another Integrated Development Environment (IDE) such as PyScripter, PythonWin, …

1-A: Python Command Line

Without opening ArcGis or any other software, just open a command window (In launching Windows programs,   run -> cmd )

 

You launch Python by simply typing

python

 

the three arrows (>>>) indicate that you can enter your Python command.

The first command is used to load the functions of ArcGis into Python. type

arcpy import

 

That’s all it takes to be able to execute our command that will transform our polygons into polylines. Then, we enter the order:

arcpy.FeatureToLine_management (“   C: \ ArcGIS \ com29.shp   »,«   C: \ ArcGIS \ com29_lignes.shp   “)

 

The <Result … line tells us that the operation was successful and that the new layer was created.

If we open ArcMap and we display this resulting layer we see:

 

Note that we have just done an ArcGis geoprocessing without having opened ArcGis and in a 5 to 10 fold less time than ArcMap takes to start …
Of course, you need to know the syntax and where the files are. Of course it is modest. But it’s still not as incomprehensible as what you thought it would be.

Now let’s go back one step above.

1-B: Run the Command Line in the ArcMap Python Console

Open the Python console by clicking on the corresponding key in the ArcMap window

 

We enter the command line to import the ArcGis library (import arcpy) and we start entering the second command line

arcpy.FeatureToLine_management (“   C: \ ArcGIS \ com29.shp   »,«   C: \ ArcGIS \ com29_lignes.shp   “)

Here, unlike the previous command window, we have an indicative input available. When you start typing a function, the list of functions is displayed and you can simply click on the one you are looking for:

 

When you start entering the parameters of the function, a descriptive help for the parameters is automatically displayed in the right side window:

 

If you have loaded the corresponding parameters layers  in ArcMap, you will have the drop-down list of the layers avoiding you to type the whole name.

When executing the command, besides the <Result … message you will observe that a detailed list of the processes is displayed in the right side window.

 

1-C: From the console of another IDE

The different integrated development environments are, in principle, intended to perform executables. But, of course, they have interpretation consoles equivalent to the ArcMap Python console.

For example,   PyScripter , allows you to enter and execute both command lines. The difference lies on the type of help for writing orders:

 

Generally you will have more features offered by an IDE than by theArcMap Python console. But if you keep using  the command line both are pretty equivalent. The main difference is that the IDE allows you to write, test, and debug complete Python scripts.

2- How to compile and execute Phyton

In the three previous examples, we enter the command lines one at a time. Each command is interpreted and executed.
But we can create a file, containing all the commands to execute, and then we can ask an application to compile and execute it. This is what we will call a   script.
We will create a text file, with a .py extension containing our two command lines:

 

We will call this example file .py

We have four possibilities to run a Python script:

  • run it with Python
  • run it with an IDE
  • run it as a tool in the ArcGis toolbox
  • run it as an ArcMap command button

2-A: Compile and run with Python

If we open a command window we can execute the example.py file directly in Python, simply typing

python.exe c: \ ArcGis \ example.py

 

Python compiles and executes the commands. No message is displayed on the progress and completion of the functions, but if there were an error we would have had the error messages displayed.

This option is ideal if you want to perform certain tasks from the Windows Task Scheduler.

2-B: Compile and run with an IDE

Of course you can run a script from an integrated development environment.
Usually, we execute a Python script from the IDE as we are developing it. This makes it possible to use the console to test certain functions, as well as to use step-by-step execution for debugging, tracking variable values ​​throughout the process, and so on.

 

2-C: Create a tool from the ArcGis toolbox

Once you have created and validated the script with your IDE, you have a script file that can be stored and run as a tool in the ArcGis Toolbox.

To do this, right-click on one of your personal toolboxes, select   Add -> Script

 

The add script window opens:

 

Give a name (without spaces or special characters) and a label (you are free of content).

Click Next to highlight the script file.

 

The third window is used to manage the script parameters. In our case there are no parameters.

Your script is now available as a tool in your toolbox and can be run in the same way as any other tool.

 

 

The window ”   Results   “allows to verify the good execution of the script.

 

2-D: Create a Button or Toolbar for ArcMap with a Python Addin

We will not keep on with the example of creating AddIn Python because it is much more complicated than the previous examples. The purpose of creating a button is to be able to interact with the content of ArcMap, with other scripts or tools, to manage forms, etc. Our example is far from what the buttons are supposed to do.
In any case, the approach is as follows:
You will use the python addins creation wizard ( download here )

 

Once you have completed the definition of the addin, you will create the addin file.

In the working folder, run the makeaddin.py script by double-clicking this script. All the files and folders needed by the add-in are copied to a compressed add-in file in the working folder.The file name is composed of the name of the working folder and an extension   .esriaddin.

You still have to install the addin:

Double-click the new add-in file to launch the ESRI ArcGIS Add-In Installation Utility. When this utility is retrieved, it analyzes the file   config.xml of the add-in file, the project settings entered during the creation of the add-in, and whether the add-in contains a reliable digital signature, as shown below.

 

Now you are able to use customization in the ArcGIS for Desktop application. Start the desktop application (ArcMap). The toolbar or custom menu may already be visible and ready for testing. Otherwise, in the menu   Customize , click   Add-In Manager . The Add-In Manager dialog lists the installed add-ins that target the current application. The add-in information, such as the name, description, and image entered in the project settings, should be displayed.

 

 

Si cet article vous a intéressé et que vous pensez qu'il pourrait bénéficier à d'autres personnes, n'hésitez pas à le partager sur vos réseaux sociaux en utilisant les boutons ci-dessous. Votre partage est apprécié !

Leave a Reply

Your email address will not be published. Required fields are marked *