Sie sind auf Seite 1von 4

1.4.2 What is Python?

Print

Python is a language that is used to automate computing tasks through programs called scripts. In
the introduction to this lesson, you learned that automation makes work easier, faster, and more
accurate. This applies to GIS and many other areas of computer science. Learning Python will
make you a more effective GIS analyst, but Python programming is a technical skill that can be
beneficial to you even outside the field of GIS.

Python is a good language for beginning programming. Python is a high-level language, meaning
you don’t have to understand the “nuts and bolts” of how computers work in order to use it.
Python syntax (how the code statements are constructed) is relatively simple to read and
understand. Finally, Python requires very little overhead to get a program up and running.

Python is an open-source language and there is no fee to use it or deploy programs with it.
Python can run on Windows, Linux, and Unix operating systems.

In ArcGIS, Python can be used for coarse-grained programming, meaning that you can use it to
easily run geoprocessing tools such as the Buffer tool that we just worked with. You could code
all the buffer logic yourself, using more detailed, fine-grained programming with ArcObjects,
but this would be time consuming and unnecessary in most scenarios; it’s easier just to call the
Buffer tool from a Python script using one line of code.

In addition to the esri help which describes all of the parameters of a function and how to access
them from Python you can also get Python syntax (the structure of the language) for a tool like
this :

1. Run the tool interactively (e.g. buffer) with your input data, output data and any other relevant
parameters (e.g. distance to buffer)

2. Go to the Geoprocessing -> Results window and right click the completed tool run.

3. Pick "Copy Python Snippet"

4. Paste the code into PythonWin or the Python code window to see how you would code the
same operation you just ran in Desktop in Python.
1.4.1 Introducing Python using the Python
window in ArcGIS
Print

The best way to introduce Python may be to look at a little bit of code. Let’s take the Buffer tool
which you recently ran from the ArcToolbox GUI and run it in the ArcGIS Python window. This
window allows you to type a simple series of Python commands without writing full permanent
scripts. The Python Window is a great way to get a taste of Python.

This time, we’ll make buffers of 15 miles around the cities.

1. Open ArcMap to a new empty map.


2. Add the us_cities.shp dataset from the Lesson 1 data.
3. On the Standard toolbar, click the Python window button . Once the window appears, drag
it over to the side or bottom of the screen to dock it.
4. Type the following in the Python window (Don't type the >>>. These are just included to
show you where the new lines begin in the Python window.)
5. >>> import arcpy
>>> arcpy.Buffer_analysis("us_cities", "us_cities_buffered", "15
miles", "", "", "ALL")

6. Zoom in and examine the buffers that were created.

You’ve just run your first bit of Python. You don’t have to understand everything about the code
you wrote in this window, but here are a few important things to note.

The first line of the script import arcpy tells the Python interpreter (which was installed when
you installed ArcGIS) that you’re going to work with some special scripting functions and tools
included with ArcGIS. Without this line of code, Python knows nothing about ArcGIS, so you'll
put it at the top of all ArcGIS-related code that you write in this class. You technically don't need
this line when you work with the Python window in ArcMap because arcpy is already imported,
but I wanted to show you this pattern early; you'll use it in all the scripts you write outside the
Python window.

The second line of the script actually runs the tool. You can type arcpy, plus a dot, plus any tool
name to run a tool in Python. Notice here that you also put an underscore followed by the name
of the toolbox that includes the buffer tool. This is necessary because some tools in different
toolboxes actually have the same name (like Clip, which is a tool for clipping vectors in the
Analysis toolbox or tool for clipping rasters in the Data Management toolbox).

After you typed arcpy.Buffer_analysis, you typed all the parameters for the tool. Each parameter
was separated by a comma, and the whole list of parameters was enclosed in parentheses. Get
used to this pattern, since you'll follow it with every tool you run in this course.
In this code, we also supplied some optional parameters, leaving empty quotes where we wanted
to take the default values, and truncating the parameter list at the final optional parameter we
wanted to set.

How do you know the syntax, or structure, of the parameters to enter? For example, for the
buffer distance, should you enter 15MILES, ‘15MILES’, 15 Miles, or ’15 Miles’? The best way
to answer questions like these is to return to the Geoprocessing tool reference help topic for the
Buffer tool. All of the topics in this reference section have a command line usage and example
section to help you understand how to structure the parameters. Optional parameters are enclosed
in braces, while the required parameters are not. From the example in this topic, you can see that
the buffer distance should be specified as ’15 miles’. Because there is a space in this text, or
string, you need to surround it with single quotes.

You might have noticed that the Python window helps you by popping up different options you
can type for each parameter. This is called autocompletion, and it can be very helpful if you're
trying to run a tool for the first time and you don't know exactly how to type the parameters.

There are a couple of differences between writing code in the Python window and writing code
in some other program, such as Notepad or PythonWin. In the Python window, you can reference
layers in the map document by their names only, instead of their file paths. Thus, we were able to
type "us_cities" instead of something like "C:\\data\\us_cities.shp". We were also able to make
up the name of a new layer "us_cities_buffered" and get it added to the map by default after the
code ran. If you're going to use your code outside the Python window, make sure you use the full
paths.

When you write more complex scripts, it will be helpful to use an integrated development
environment (IDE), meaning a program specifically designed to help you write and test Python
code. Later in this course we’ll explore the PythonWin IDE.

Earlier in this lesson you saw how tools can be chained together to solve a problem using
ModelBuilder. The same can be done in Python, but it’s going to take a little groundwork to get
to that point. For this reason we’ll spend the rest of Lesson 1 covering some of the basics of
Python.
What is Map Algebra?
ArcMap 10.4

Other versions

Available with Spatial Analyst license.

Map Algebra is a simple and powerful algebra with which you can execute all Spatial Analyst
tools, operators, and functions to perform geographic analysis. Map Algebra is available through
the Spatial Analyst module; an extension of the ArcPy Python site package. As Map Algebra has
been integrated in Python, all the functionality of Python and ArcPy and its extensions (modules,
classes, functions, and properties) is available to you.

Spatial Analyst tools are accessed through an algebraic format. That is, an object whose name is
identified to the left of an equal sign is created based on a tool or operator stated to the right of
the equal sign.

from arcpy.sa import *


outRas = Slope("indem")

The above statement calculates the slope for each cell in the indem dataset and creates a Raster
object called outRas to store the results. Specific information on importing the Spatial Analyst
module to utilize Map Algebra can be found at Importing the Spatial Analyst module.

Map Algebra within Python is composed of tools, operators, functions, and classes. For more
information on the syntax rules of Map Algebra, refer to the following resources:

Das könnte Ihnen auch gefallen