ArcGis and Python (2 ): for a good start

Have you decided to get started? It’s time to put all the assets on your
side . It’s already enough complicated to learn a new language, it is useless
to add pitfalls! This is a checklist and a few tips to start smoothly. Firstly
, if you are accustomed to program and Python or another language leaves you
completely indifferent , just take a look at the few traps described below and tackle
down the Python tutorial of your choice .

On the other side,  if you back
off, start by convincing yourself that it is not necessary to master Python in
order to use it for our tasks with ArcGis. It is quite possible to link the
processing in a Python script by only knowing the basics of the language .

Get rid of the syntax

Firstly,  we will rely on the predictive code features
by using the Python console or an integrated development environment . This allows
you to have the exact syntax without having to know them by heart .  

If you have not, yet,  read our article
ArcGis and Python before making your first steps  please read it to understand the difference
between python consoles that allows you to write and execute a single command
line at a time, and the scripts publishers.

In ArcGis you have installed by default, the Python Console. On the
other side,  you have no integrated development
environment by default . Therefore, you will have to install one . On the
ArcGis installation support you have PythonWin . You can install it from ArrGis
support. If you do not have the support at hand, you can install it from this address .

Using an IDE provides you with:

  • An editor with code completion
  • The syntax highlighting
  • Codes of models
  • A code explorer for functions and classes
  • Testing tools and debugging scripts

Here we will install another IDE, PyScripter . You can download it at this address .
Why this one? Because you have to choose one and when using them, they are all quite
similar . By cons it is easier find the first big problem. Do you do not rush when
downloading the pages ! Read the following lines …

32 or64 bit ???

If you have a 32-bit machine, no problem. You can only use 32-bit
software versions .
By cons, if you have a 64-bit machine, depending on the installations chosen you
can you find, for a same software, versions 32 or 64. For example Python! And
when you go to the download page for PyScripter or PythonWin , the first thing
you have to do is to choose between the 32 or 64 bits. As it is ArcGis that installs
Python, in general you do not know which version you have . Yes, because for
ArcGIS Desktop the normally installed version, even on a 64-bit machine, is the
Python 32-bit version! By cons, if you install the 64-bit, ArcGis will install
the Python 64-bit version …

So which version do I have?
Open ArcMap, click the Python Console button, and enter both lines following : importsys
print (sys.version)

Then,
you will see not only if it is 32 or 64 bits, but also the version number
(2.7.x). You will need , if you install PyScrypter , at launching  time because if you start with the default
option, it will use the latest available version of Python (3.4). On the other
side for ArcGis you must, absolutely, use version 2.7   

For the curious who want to know why , here is a link Python page .

Now you have all the necessary information to download your IDE and to
be able to run it for ArcGis.

How to proceed with your IDE?

When you open PyScripter you have the following interface:
  

The goal is to describe a script that will be executed in ArcGis. The
script, the succession of code lines of linking the different operations and processes
, will be written in the main window.

The bottom window corresponds to the Python console. It allows you to
enter a code line and run it immediately . This allows you to test the code
before adding it in the main window.

Once completed and validated , you will include the script in the ArcGis
Toolbox or you will create an ArcGis add-in. Theoretically,  PyScripter will only be used during the
development phase of the script.

Save time: Get the code samples

Once you have found which tool you want to use, go
to ArcGIS support and type the name of the tool . In a previous article, we have
used, as example, the FeatureToLine tool . Here is what is find at the bottom
of the tool support page (and this is true for all tools):

You have always a line example command with all the options and that a
little script or one can see how to include this tool in a real script. Do not
hesitate to “inspire” by copying and pasting the examples.

A few specifics of Python for ArcGis

Here are a few items you will not find in books or classic Python
tutorials .

1- In order for your script to use ArcGis tools , start all your scripts
with the line

arcpy import

2- ArcGis tools respect a Rule name strict

arcpy .    the name
of
the tool   _   the name of the toolbox that
contains it

For example   arcpy.FeatureToLine _management

3-In the tool syntax display, its settings are displayed in brackets . If
the parameter is mandatory he will be surrounded by <> , if he is optional
by {} . If the tool has multiple settings optional and you want , for
example , to inform the fourth parameter optional , you are obliged to inform
the three previous ones , even with chains empty .

4-Be careful with the file paths. If you use \ in the paths ,
that will be a problem because Python uses this symbol in her syntax . You can opt
by / instead of \ ( “C: / ArcGIS / Data”)
or you can double \\ in the paths ( “C; \\ ArcGis \\ Data”
). You can also to precede the chain containing a file path with r ( r
“C ; \ ArcGis \ Data” ).

Leave a Reply

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