forked from Automating-GIS-processes/2016
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLesson6-arcpy-script.html
586 lines (441 loc) · 47 KB
/
Lesson6-arcpy-script.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-88382509-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Writing arcpy scripts — GeoPython - AutoGIS 1 documentation</title>
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
<link rel="index" title="Index"
href="genindex.html"/>
<link rel="search" title="Search" href="search.html"/>
<link rel="top" title="GeoPython - AutoGIS 1 documentation" href="index.html"/>
<link rel="next" title="Running the Python script from ArcGIS" href="Lesson6-run-the-tool.html"/>
<link rel="prev" title="ArcGIS Toolbox" href="Lesson6-toolbox.html"/>
<script src="_static/js/modernizr.min.js"></script>
</head>
<body class="wy-body-for-nav" role="document">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search">
<a href="index.html" class="icon icon-home"> GeoPython - AutoGIS
<img src="_static/logo_hy_geo_135.png" class="logo" />
</a>
<div class="version">
2016 Autumn
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<p class="caption"><span class="caption-text">Course information</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="course-info.html">General info</a></li>
<li class="toctree-l1"><a class="reference internal" href="Installing_Anacondas_GIS.html">Installing Python + GIS</a></li>
<li class="toctree-l1"><a class="reference internal" href="License-terms.html">License and terms of usage</a></li>
</ul>
<p class="caption"><span class="caption-text">Lesson 1</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="Lesson1-Intro-Python-GIS.html">Introduction to Python GIS</a></li>
<li class="toctree-l1"><a class="reference internal" href="Lesson1-Geometric-Objects.html">Geometric Objects - Spatial Data Model</a></li>
<li class="toctree-l1"><a class="reference internal" href="Exercise-1.html">Exercise 1</a></li>
</ul>
<p class="caption"><span class="caption-text">Lesson 2</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="Lesson2-overview.html">Lesson 2 Overview</a></li>
<li class="toctree-l1"><a class="reference internal" href="Lesson2-download-data.html">Download datasets</a></li>
<li class="toctree-l1"><a class="reference internal" href="Lesson2-overview-pandas-geopandas.html">Pandas and Geopandas -modules</a></li>
<li class="toctree-l1"><a class="reference internal" href="Lesson2-pandas-intro.html">Introduction to Pandas</a></li>
<li class="toctree-l1"><a class="reference internal" href="Lesson2-geopandas-basics.html">Introduction to Geopandas</a></li>
<li class="toctree-l1"><a class="reference internal" href="Exercise-2.html">Exercise 2</a></li>
</ul>
<p class="caption"><span class="caption-text">Lesson 3</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="Lesson3-overview.html">Lesson 3 Overview</a></li>
<li class="toctree-l1"><a class="reference internal" href="Lesson3-geocoding.html">Geocoding</a></li>
<li class="toctree-l1"><a class="reference internal" href="Lesson3-table-join.html">Table join</a></li>
<li class="toctree-l1"><a class="reference internal" href="Lesson3-projections.html">Re-projecting data</a></li>
<li class="toctree-l1"><a class="reference internal" href="Lesson3-point-in-polygon.html">Point in Polygon & Intersect</a></li>
<li class="toctree-l1"><a class="reference internal" href="Lesson3-spatial-join.html">Spatial join</a></li>
<li class="toctree-l1"><a class="reference internal" href="Exercise-3.html">Exercise 3</a></li>
</ul>
<p class="caption"><span class="caption-text">Lesson 4</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="Lesson4-overview.html">Lesson 4 Overview</a></li>
<li class="toctree-l1"><a class="reference internal" href="Lesson4-download-data.html">Download datasets</a></li>
<li class="toctree-l1"><a class="reference internal" href="Lesson4-geometric-operations.html">Geometric operations</a></li>
<li class="toctree-l1"><a class="reference internal" href="Lesson4-reclassify.html">Data reclassification</a></li>
<li class="toctree-l1"><a class="reference internal" href="Lesson4-nearest-neighbour.html">Nearest Neighbour Analysis</a></li>
<li class="toctree-l1"><a class="reference internal" href="Exercise-4.html">Exercise 4</a></li>
</ul>
<p class="caption"><span class="caption-text">Lesson 5</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="Lesson5-overview.html">Lesson 5 Overview</a></li>
<li class="toctree-l1"><a class="reference internal" href="Lesson5-download-data.html">Download datasets</a></li>
<li class="toctree-l1"><a class="reference internal" href="Lesson5-static-maps.html">Static maps</a></li>
<li class="toctree-l1"><a class="reference internal" href="Lesson5-interactive-map-bokeh.html">Interactive maps with Bokeh</a></li>
<li class="toctree-l1"><a class="reference internal" href="Lesson5-share-on-github.html">Sharing interactive plots on GitHub</a></li>
<li class="toctree-l1"><a class="reference internal" href="Lesson5-interactive-map-folium.html">Interactive maps on Leaflet</a></li>
<li class="toctree-l1"><a class="reference internal" href="Lesson5-World-3D.html">Inspiration: World 3D</a></li>
<li class="toctree-l1"><a class="reference internal" href="Exercise-5.html">Exercise 5</a></li>
</ul>
<p class="caption"><span class="caption-text">Lesson 6</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="Lesson6-overview.html">Lesson 6 Overview</a></li>
<li class="toctree-l1"><a class="reference internal" href="Lesson6-arcpy.html">Python in ArcGIS</a></li>
<li class="toctree-l1"><a class="reference internal" href="Lesson6-toolbox.html">ArcGIS Toolbox</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Writing arcpy scripts</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#importing-arcpy">Importing arcpy</a></li>
<li class="toctree-l2"><a class="reference internal" href="#getting-parameters-from-the-toolbox">Getting parameters from the toolbox</a></li>
<li class="toctree-l2"><a class="reference internal" href="#adding-a-new-field-into-attribute-table">Adding a new field into attribute table</a></li>
<li class="toctree-l2"><a class="reference internal" href="#updating-column-with-field-calculator">Updating column with Field Calculator</a></li>
<li class="toctree-l2"><a class="reference internal" href="#iterating-over-values-in-attribute-table">Iterating over values in attribute table</a></li>
<li class="toctree-l2"><a class="reference internal" href="#selecting-data">Selecting data</a></li>
<li class="toctree-l2"><a class="reference internal" href="#convert-polygons-to-raster">Convert Polygons to raster</a></li>
<li class="toctree-l2"><a class="reference internal" href="#sending-messages-to-the-script-tool">Sending messages to the Script tool</a></li>
<li class="toctree-l2"><a class="reference internal" href="#the-full-script">The full script</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="Lesson6-run-the-tool.html">Running the Python script from ArcGIS</a></li>
</ul>
<p class="caption"><span class="caption-text">Lesson 7</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="Lesson7-overview.html">Lesson 7 Overview</a></li>
<li class="toctree-l1"><a class="reference internal" href="Lesson7-download.html">Download data</a></li>
<li class="toctree-l1"><a class="reference internal" href="Lesson7-read-raster.html">Reading raster files with GDAL</a></li>
<li class="toctree-l1"><a class="reference internal" href="Lesson7-read-raster-array.html">Reading raster as a numerical array</a></li>
<li class="toctree-l1"><a class="reference internal" href="Lesson7-gdal-utilities.html">GDAL command line tools</a></li>
<li class="toctree-l1"><a class="reference internal" href="Exercise-7.html">Exercise 7</a></li>
</ul>
<p class="caption"><span class="caption-text">Lesson 8</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="Lesson8-network-analysis.html">Network analysis in Python</a></li>
</ul>
<p class="caption"><span class="caption-text">Final Assignment</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="Final-assignment.html">Final assignment</a></li>
</ul>
<p class="caption"><span class="caption-text">Map Challenge 2016</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="map-challenge.html">Map Challenge 2016</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" role="navigation" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="index.html">GeoPython - AutoGIS</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="index.html">Docs</a> »</li>
<li>Writing arcpy scripts</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/Automating-GIS-processes/2016/blob/master/source/Lesson6-arcpy-script.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="writing-arcpy-scripts">
<h1>Writing arcpy scripts<a class="headerlink" href="#writing-arcpy-scripts" title="Permalink to this headline">¶</a></h1>
<p>At this point we have created a nice interface for our Python tool in ArcGIS:</p>
<div class="figure">
<a class="reference internal image-reference" href="_images/arcgis-my-script.png"><img alt="_images/arcgis-my-script.png" src="_images/arcgis-my-script.png" style="width: 732.0px; height: 358.40000000000003px;" /></a>
</div>
<p>Now we need to write the functionalities for our tool. The aim of our tool is to convert Shapefiles into rasters and give the raster a value that the user specifies in the user interface.
Let’s see how this can be done.</p>
<p>First, we need start the ArcGIS IDLE (if it is not open already) from <strong>All Programs</strong> –> <strong>ArcGIS</strong>:</p>
<div class="figure">
<img alt="_images/arcgis-idle-location.PNG" src="_images/arcgis-idle-location.PNG" />
</div>
<p>Create a new script called <code class="docutils literal"><span class="pre">PolyToRaster.py</span></code> in IDLE (<strong>File</strong> –> <strong>New File</strong>) and save it to your computer:</p>
<div class="figure">
<a class="reference internal image-reference" href="_images/arcgis-idle-new.png"><img alt="_images/arcgis-idle-new.png" src="_images/arcgis-idle-new.png" style="width: 430.0px; height: 222.0px;" /></a>
</div>
<p>Let’s start writing the functionalities of our tool to the script that we just created.</p>
<div class="section" id="importing-arcpy">
<h2>Importing arcpy<a class="headerlink" href="#importing-arcpy" title="Permalink to this headline">¶</a></h2>
<p>First thing to do is to import the arcpy module:</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="c1"># Import arcpy module so we can use ArcGIS geoprocessing tools</span>
<span class="kn">import</span> <span class="nn">arcpy</span>
<span class="kn">import</span> <span class="nn">os</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>Notice that arcpy can only be imported with Python interpreter that comes with ArcGIS. If you try to import it e.g. in Spyder, you will receive an error <code class="docutils literal"><span class="pre">ImportError:</span> <span class="pre">No</span> <span class="pre">module</span> <span class="pre">named</span> <span class="pre">'arcpy'</span></code>.</p>
<p class="last">Well...actually there is a way to <a class="reference external" href="http://gis.stackexchange.com/questions/86850/making-separate-python-installation-that-can-call-arcpy">import arcpy from other places</a> as well and also <a class="reference external" href="http://gis.stackexchange.com/questions/176879/importing-arcpy-in-spyder">import it into Spyder</a> but this is
not a topic of this course.</p>
</div>
</div>
<div class="section" id="getting-parameters-from-the-toolbox">
<h2>Getting parameters from the toolbox<a class="headerlink" href="#getting-parameters-from-the-toolbox" title="Permalink to this headline">¶</a></h2>
<p>Before we can do anything with our tool and our nice interface for it, we need to get those parameters into our script. This can be done by using arcpy’s function called <code class="docutils literal"><span class="pre">.GetParameterAsText()</span></code> where the index
value of the parameter is passed to the function (where number 0 is the first parameter). ArcGIS has a good documentation that should be used for searching the information about how different functions are used.</p>
<p>Let’s import those five parameters from the graphical interface into our Python script using <a class="reference external" href="http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-functions/getparameterastext.htm">GetParameterAsText()</a> -function:</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="c1"># 1. Get parameters from the toolbox using 'GetParametersAsText' method</span>
<span class="c1">#----------------------------------------------------------------------</span>
<span class="c1"># --> check ArcGIS help for info how to use methods</span>
<span class="c1"># Method info: http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-functions/getparameterastext.htm</span>
<span class="n">input_species_shp</span> <span class="o">=</span> <span class="n">arcpy</span><span class="o">.</span><span class="n">GetParameterAsText</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="n">output_folder</span> <span class="o">=</span> <span class="n">arcpy</span><span class="o">.</span><span class="n">GetParameterAsText</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="n">species_attribute</span> <span class="o">=</span> <span class="n">arcpy</span><span class="o">.</span><span class="n">GetParameterAsText</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="n">attribute_name</span> <span class="o">=</span> <span class="n">arcpy</span><span class="o">.</span><span class="n">GetParameterAsText</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="n">presence_value</span> <span class="o">=</span> <span class="n">arcpy</span><span class="o">.</span><span class="n">GetParameterAsText</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="adding-a-new-field-into-attribute-table">
<h2>Adding a new field into attribute table<a class="headerlink" href="#adding-a-new-field-into-attribute-table" title="Permalink to this headline">¶</a></h2>
<p>Next, we need to add a new field that is called in a way that the user wants it. The field name is stored in the <code class="docutils literal"><span class="pre">attribute_name</span></code> variable. Adding new column can be done by using a function called <code class="docutils literal"><span class="pre">AddField_management()</span></code>
(<a class="reference external" href="http://desktop.arcgis.com/en/arcmap/latest/tools/data-management-toolbox/add-field.htm">see help</a>).</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="c1"># 2. Add a new field into the input shapefile with 'AddField_management' method</span>
<span class="c1">#------------------------------------------------------------------------------</span>
<span class="c1"># Method info: http://desktop.arcgis.com/en/arcmap/latest/tools/data-management-toolbox/add-field.htm</span>
<span class="n">arcpy</span><span class="o">.</span><span class="n">AddField_management</span><span class="p">(</span><span class="n">in_table</span><span class="o">=</span><span class="n">input_species_shp</span><span class="p">,</span> <span class="n">field_name</span><span class="o">=</span><span class="n">attribute_name</span><span class="p">,</span> <span class="n">field_type</span><span class="o">=</span><span class="s2">"SHORT"</span><span class="p">)</span> <span class="c1"># Other possible parameters can be left as default</span>
</pre></div>
</div>
</div>
<div class="section" id="updating-column-with-field-calculator">
<h2>Updating column with Field Calculator<a class="headerlink" href="#updating-column-with-field-calculator" title="Permalink to this headline">¶</a></h2>
<p>Let’s update the newly created column with the <code class="docutils literal"><span class="pre">Presence</span> <span class="pre">value</span></code> that was asked from the user and will be assigned to the raster cells. We can do calculations in attribute table with <a class="reference external" href="http://desktop.arcgis.com/en/arcmap/latest/tools/data-management-toolbox/calculate-field.htm">CalculateField_management()</a> -function. Let’s update the column with value that is stored in <code class="docutils literal"><span class="pre">presence_value</span></code> variable.</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="c1"># 3. Update the presence value for our newly created attribute with 'CalculateField_management' method</span>
<span class="c1">#-----------------------------------------------------------------------------------------------------</span>
<span class="c1"># Method info: http://desktop.arcgis.com/en/arcmap/latest/tools/data-management-toolbox/calculate-field.htm</span>
<span class="n">arcpy</span><span class="o">.</span><span class="n">CalculateField_management</span><span class="p">(</span><span class="n">in_table</span><span class="o">=</span><span class="n">input_species_shp</span><span class="p">,</span> <span class="n">field</span><span class="o">=</span><span class="n">attribute_name</span><span class="p">,</span> <span class="n">expression</span><span class="o">=</span><span class="n">presence_value</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="iterating-over-values-in-attribute-table">
<h2>Iterating over values in attribute table<a class="headerlink" href="#iterating-over-values-in-attribute-table" title="Permalink to this headline">¶</a></h2>
<p>As we wanted to save individual species into separate raster files, we need to determine the unique species in our attribute table. In Pandas / Geopandas there is a nice function called <code class="docutils literal"><span class="pre">.unique()</span></code>
for this purpose but unfortunately arcpy does not have such a function that would work with Shapefiles. Hence, we need to create the “unique” -function ourselves.</p>
<p>Let’s create a function that iterates over the values in a column and returns a list of unique values that are present in that column. We can iterate over the rows in attribute table by using
<a class="reference external" href="http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-data-access/searchcursor-class.htm">SearchCursor()</a> -function (read-only) in arcpy.</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="c1">#-----------------------------------------------------------------------------------------------------------------------------------</span>
<span class="c1"># 4. Get a list of unique species in the table using 'SearchCursor' method</span>
<span class="c1"># Method info: http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-data-access/searchcursor-class.htm</span>
<span class="c1"># More elegant version of the function in ArcPy Cafe: https://arcpy.wordpress.com/2012/02/01/create-a-list-of-unique-field-values/</span>
<span class="c1"># ----------------------------------------------------------------------------------------------------------------------------------</span>
<span class="c1"># 4.1 CREATE a function that returns unique values of a 'field' within the 'table'</span>
<span class="k">def</span> <span class="nf">unique</span><span class="p">(</span><span class="n">table</span><span class="p">,</span> <span class="n">field</span><span class="p">):</span>
<span class="c1"># Create a cursor object for reading the table</span>
<span class="n">cursor</span> <span class="o">=</span> <span class="n">arcpy</span><span class="o">.</span><span class="n">da</span><span class="o">.</span><span class="n">SearchCursor</span><span class="p">(</span><span class="n">table</span><span class="p">,</span> <span class="p">[</span><span class="n">field</span><span class="p">])</span> <span class="c1"># A cursor iterates over rows in table</span>
<span class="c1"># Create an empty list for unique values</span>
<span class="n">unique_values</span> <span class="o">=</span> <span class="p">[]</span>
<span class="c1"># Iterate over rows and append value into the list if it does not exist already</span>
<span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">cursor</span><span class="p">:</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">row</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="ow">in</span> <span class="n">unique_values</span><span class="p">:</span> <span class="c1"># Append only if value does not exist</span>
<span class="n">unique_values</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">row</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="k">return</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">unique_values</span><span class="p">)</span> <span class="c1"># Return a sorted list of unique values</span>
</pre></div>
</div>
<p>Let’s apply our function in following manner:</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="c1"># 4.2 USE the function to get a list of unique values</span>
<span class="n">unique_species</span> <span class="o">=</span> <span class="n">unique</span><span class="p">(</span><span class="n">table</span><span class="o">=</span><span class="n">input_species_shp</span><span class="p">,</span> <span class="n">field</span><span class="o">=</span><span class="n">species_attribute</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">If your data is in Geodatabase, you can use <code class="docutils literal"><span class="pre">DISTINCT</span></code> operator in a <code class="docutils literal"><span class="pre">sql_clause</span></code> that you can pass to the SearchCursor (see <a class="reference external" href="http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-data-access/searchcursor-class.htm">help</a>).</p>
</div>
<div class="admonition hint">
<p class="first admonition-title">Hint</p>
<p><strong>Updating rows</strong></p>
<p class="last">If you need to update rows using similar iteration approach, it is possible to do with <code class="docutils literal"><span class="pre">UpdateCursor()</span></code> -function
(see <a class="reference external" href="http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-data-access/updatecursor-class.htm">help</a>).</p>
</div>
</div>
<div class="section" id="selecting-data">
<h2>Selecting data<a class="headerlink" href="#selecting-data" title="Permalink to this headline">¶</a></h2>
<p>Now that we have a list of unique species values we can iterate over that list and select all rows that correspond to a selected species and then rasterize those rows (polygons).</p>
<p>Before we can do selections in arcpy, we need to “prepare” the selection by creating a temporary feature layer (enables to make selections) using <code class="docutils literal"><span class="pre">MakeFeatureLayer_management()</span></code> -function
(see <a class="reference external" href="http://desktop.arcgis.com/en/arcmap/latest/tools/data-management-toolbox/make-feature-layer.htm">help</a>):</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="c1">#--------------------------------------------------------------------------------------------------------------------------------</span>
<span class="c1"># 5. Create a feature layer from the shapefile with 'MakeFeatureLayer_management' method that enables us to select specific rows</span>
<span class="c1"># Method info: http://desktop.arcgis.com/en/arcmap/latest/tools/data-management-toolbox/make-feature-layer.htm</span>
<span class="c1">#--------------------------------------------------------------------------------------------------------------------------------</span>
<span class="n">species_lyr</span> <span class="o">=</span> <span class="n">arcpy</span><span class="o">.</span><span class="n">MakeFeatureLayer_management</span><span class="p">(</span><span class="n">in_features</span><span class="o">=</span><span class="n">input_species_shp</span><span class="p">,</span> <span class="n">out_layer</span><span class="o">=</span><span class="s2">"species_lyr"</span><span class="p">)</span>
</pre></div>
</div>
<p>Now the feature layer “lives” temporarily in the variable <code class="docutils literal"><span class="pre">species_lyr</span></code> that we use for making the selections.</p>
<p>Next, we can start iterating over those unique species that are stored in <code class="docutils literal"><span class="pre">unique_species</span></code> -list and select rows with <code class="docutils literal"><span class="pre">SelectLayerByAttribute_management()</span></code> -function (see <a class="reference external" href="http://desktop.arcgis.com/en/arcmap/latest/tools/data-management-toolbox/select-layer-by-attribute.htm">help</a>)
based on the species name (in a similar manner that you would do with <code class="docutils literal"><span class="pre">SelectByAttributes</span></code> -query
in ArcGIS, and save those selections into separate Shapefiles using <code class="docutils literal"><span class="pre">CopyFeatures_management()</span></code> -function (see <a class="reference external" href="http://desktop.arcgis.com/en/arcmap/latest/tools/data-management-toolbox/copy-features.htm">help</a>).</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="c1">#---------------------------------------------------</span>
<span class="c1"># 6. Iterate over unique_species list and:</span>
<span class="c1"># 6.1) export individual species as Shapefiles and</span>
<span class="c1"># 6.2) convert those shapefiles into Raster Datasets</span>
<span class="c1">#---------------------------------------------------</span>
<span class="k">for</span> <span class="n">individual</span> <span class="ow">in</span> <span class="n">unique_species</span><span class="p">:</span>
<span class="c1"># 6.1):</span>
<span class="c1"># Create an expression for selection using Python String manipulation</span>
<span class="n">expression</span> <span class="o">=</span> <span class="s2">"</span><span class="si">%s</span><span class="s2"> = '</span><span class="si">%s</span><span class="s2">'"</span> <span class="o">%</span> <span class="p">(</span><span class="n">species_attribute</span><span class="p">,</span> <span class="n">individual</span><span class="p">)</span>
<span class="c1"># Select rows based on individual breed using 'SelectLayerByAttribute_management' method</span>
<span class="c1"># Method info: http://desktop.arcgis.com/en/arcmap/latest/tools/data-management-toolbox/select-layer-by-attribute.htm</span>
<span class="n">arcpy</span><span class="o">.</span><span class="n">SelectLayerByAttribute_management</span><span class="p">(</span><span class="n">species_lyr</span><span class="p">,</span> <span class="s2">"NEW_SELECTION"</span><span class="p">,</span> <span class="n">where_clause</span><span class="o">=</span><span class="n">expression</span><span class="p">)</span>
<span class="c1"># Create an output path for Shapefile</span>
<span class="n">shape_name</span> <span class="o">=</span> <span class="n">individual</span> <span class="o">+</span> <span class="s2">".shp"</span>
<span class="n">individual_shp</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">output_folder</span><span class="p">,</span> <span class="n">shape_name</span><span class="p">)</span>
<span class="c1"># Export the selection as a Shapefile into the output folder using 'CopyFeatures_management' method</span>
<span class="c1"># Method info: http://desktop.arcgis.com/en/arcmap/latest/tools/data-management-toolbox/copy-features.htm</span>
<span class="n">arcpy</span><span class="o">.</span><span class="n">CopyFeatures_management</span><span class="p">(</span><span class="n">in_features</span><span class="o">=</span><span class="n">species_lyr</span><span class="p">,</span> <span class="n">out_feature_class</span><span class="o">=</span><span class="n">individual_shp</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="convert-polygons-to-raster">
<h2>Convert Polygons to raster<a class="headerlink" href="#convert-polygons-to-raster" title="Permalink to this headline">¶</a></h2>
<p>Now we are saving the species into separate Shapefiles which we can convert to rasters using <code class="docutils literal"><span class="pre">PolygonToRaster_conversion()</span></code> -function (see <a class="reference external" href="http://desktop.arcgis.com/en/arcmap/latest/tools/conversion-toolbox/polygon-to-raster.htm">help</a>).
Let’s also send information to the user about the process with <code class="docutils literal"><span class="pre">AddMessage()</span></code> -function (see <a class="reference external" href="http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-functions/addmessage.htm">help</a>). Let’s add the following lines in the same loop that we started previously:</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="c1"># 6.2):</span>
<span class="c1"># Create an output path for the Raster Dataset (*.tif)</span>
<span class="n">tif_name</span> <span class="o">=</span> <span class="n">individual</span> <span class="o">+</span> <span class="s2">".tif"</span>
<span class="n">individual_tif</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">output_folder</span><span class="p">,</span> <span class="n">tif_name</span><span class="p">)</span>
<span class="c1"># Convert the newly created Shapefile into a Raster Dataset using 'PolygonToRaster_conversion' method</span>
<span class="c1"># Method info: http://desktop.arcgis.com/en/arcmap/latest/tools/conversion-toolbox/polygon-to-raster.htm</span>
<span class="n">arcpy</span><span class="o">.</span><span class="n">PolygonToRaster_conversion</span><span class="p">(</span><span class="n">in_features</span><span class="o">=</span><span class="n">individual_shp</span><span class="p">,</span> <span class="n">value_field</span><span class="o">=</span><span class="n">attribute_name</span><span class="p">,</span> <span class="n">out_rasterdataset</span><span class="o">=</span><span class="n">individual_tif</span><span class="p">)</span>
<span class="c1"># Print progress info for the user</span>
<span class="n">info</span> <span class="o">=</span> <span class="s2">"Processed: "</span> <span class="o">+</span> <span class="n">individual</span>
<span class="n">arcpy</span><span class="o">.</span><span class="n">AddMessage</span><span class="p">(</span><span class="n">info</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="sending-messages-to-the-script-tool">
<h2>Sending messages to the Script tool<a class="headerlink" href="#sending-messages-to-the-script-tool" title="Permalink to this headline">¶</a></h2>
<p>It is possible to “print” stuff to the user from arcpy scripts as well. We can use <code class="docutils literal"><span class="pre">AddMessage()</span></code> -function (see <a class="reference external" href="http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-functions/addmessage.htm">help</a>) to send
any kind of messages to the user who uses the Python tool that we have created and use from ArcGIS Toolbox.</p>
<p>Let’s add a final message for the user that the process was successful.</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="c1"># 7. Print that the process was finished successfully</span>
<span class="n">info</span> <span class="o">=</span> <span class="s2">"Process was a great success! Wuhuu!"</span>
<span class="n">arcpy</span><span class="o">.</span><span class="n">AddMessage</span><span class="p">(</span><span class="n">info</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="the-full-script">
<h2>The full script<a class="headerlink" href="#the-full-script" title="Permalink to this headline">¶</a></h2>
<p>Here is the full script that we prepared previously:</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="c1"># Import arcpy module so we can use ArcGIS geoprocessing tools</span>
<span class="kn">import</span> <span class="nn">arcpy</span>
<span class="kn">import</span> <span class="nn">sys</span><span class="o">,</span> <span class="nn">os</span>
<span class="n">input_species_shp</span> <span class="o">=</span> <span class="n">arcpy</span><span class="o">.</span><span class="n">GetParameterAsText</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="n">output_folder</span> <span class="o">=</span> <span class="n">arcpy</span><span class="o">.</span><span class="n">GetParameterAsText</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="n">species_attribute</span> <span class="o">=</span> <span class="n">arcpy</span><span class="o">.</span><span class="n">GetParameterAsText</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="n">attribute_name</span> <span class="o">=</span> <span class="n">arcpy</span><span class="o">.</span><span class="n">GetParameterAsText</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="n">presence_value</span> <span class="o">=</span> <span class="n">arcpy</span><span class="o">.</span><span class="n">GetParameterAsText</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span>
<span class="c1"># 2. Add a new field into the table using 'AddField_management' method</span>
<span class="n">arcpy</span><span class="o">.</span><span class="n">AddField_management</span><span class="p">(</span><span class="n">in_table</span><span class="o">=</span><span class="n">input_species_shp</span><span class="p">,</span> <span class="n">field_name</span><span class="o">=</span><span class="n">attribute_name</span><span class="p">,</span> <span class="n">field_type</span><span class="o">=</span><span class="s2">"SHORT"</span><span class="p">)</span>
<span class="c1"># 3. Update the presence value for our newly created attribute with 'CalculateField_management' method</span>
<span class="n">arcpy</span><span class="o">.</span><span class="n">CalculateField_management</span><span class="p">(</span><span class="n">in_table</span><span class="o">=</span><span class="n">input_species_shp</span><span class="p">,</span> <span class="n">field</span><span class="o">=</span><span class="n">attribute_name</span><span class="p">,</span> <span class="n">expression</span><span class="o">=</span><span class="n">presence_value</span><span class="p">)</span>
<span class="c1"># 4. Get a list of unique species in the table using 'SearchCursor' method</span>
<span class="c1"># 4.1 CREATE a function that returns unique values of a 'field' within the 'table'</span>
<span class="k">def</span> <span class="nf">unique_values</span><span class="p">(</span><span class="n">table</span><span class="p">,</span> <span class="n">field</span><span class="p">):</span>
<span class="c1"># Create a cursor object for reading the table</span>
<span class="n">cursor</span> <span class="o">=</span> <span class="n">arcpy</span><span class="o">.</span><span class="n">da</span><span class="o">.</span><span class="n">SearchCursor</span><span class="p">(</span><span class="n">table</span><span class="p">,</span> <span class="p">[</span><span class="n">field</span><span class="p">])</span> <span class="c1"># A cursor iterates over rows in table</span>
<span class="c1"># Create an empty list for unique values</span>
<span class="n">unique_values</span> <span class="o">=</span> <span class="p">[]</span>
<span class="c1"># Iterate over rows and append value into the list if it does not exist already</span>
<span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">cursor</span><span class="p">:</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">row</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="ow">in</span> <span class="n">unique_values</span><span class="p">:</span> <span class="c1"># Append only if value does not exist</span>
<span class="n">unique_values</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">row</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="k">return</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">unique_values</span><span class="p">)</span> <span class="c1"># Return a sorted list of unique values</span>
<span class="c1"># 4.2 USE the function to get a list of unique values</span>
<span class="n">unique_species</span> <span class="o">=</span> <span class="n">unique_values</span><span class="p">(</span><span class="n">table</span><span class="o">=</span><span class="n">input_species_shp</span><span class="p">,</span> <span class="n">field</span><span class="o">=</span><span class="n">species_attribute</span><span class="p">)</span>
<span class="c1"># 5. Create a feature layer from the shapefile with 'MakeFeatureLayer_management' method that enables us to select specific rows</span>
<span class="n">species_lyr</span> <span class="o">=</span> <span class="n">arcpy</span><span class="o">.</span><span class="n">MakeFeatureLayer_management</span><span class="p">(</span><span class="n">in_features</span><span class="o">=</span><span class="n">input_species_shp</span><span class="p">,</span> <span class="n">out_layer</span><span class="o">=</span><span class="s2">"species_lyr"</span><span class="p">)</span>
<span class="c1"># 6. Iterate over unique_species list and:</span>
<span class="c1"># 6.1) export individual species as Shapefiles and</span>
<span class="c1"># 6.2) convert those shapefiles into Raster Datasets</span>
<span class="k">for</span> <span class="n">individual</span> <span class="ow">in</span> <span class="n">unique_species</span><span class="p">:</span>
<span class="c1"># 6.1):</span>
<span class="c1"># Create an expression for selection using Python String manipulation</span>
<span class="n">expression</span> <span class="o">=</span> <span class="s2">"</span><span class="si">%s</span><span class="s2"> = '</span><span class="si">%s</span><span class="s2">'"</span> <span class="o">%</span> <span class="p">(</span><span class="n">species_attribute</span><span class="p">,</span> <span class="n">individual</span><span class="p">)</span>
<span class="c1"># Select rows based on individual breed using 'SelectLayerByAttribute_management' method</span>
<span class="n">arcpy</span><span class="o">.</span><span class="n">SelectLayerByAttribute_management</span><span class="p">(</span><span class="n">species_lyr</span><span class="p">,</span> <span class="s2">"NEW_SELECTION"</span><span class="p">,</span> <span class="n">where_clause</span><span class="o">=</span><span class="n">expression</span><span class="p">)</span>
<span class="c1"># Create an output path for Shapefile</span>
<span class="n">shape_name</span> <span class="o">=</span> <span class="n">individual</span> <span class="o">+</span> <span class="s2">".shp"</span>
<span class="n">individual_shp</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">output_folder</span><span class="p">,</span> <span class="n">shape_name</span><span class="p">)</span>
<span class="c1"># Export the selection as a Shapefile into the output folder using 'CopyFeatures_management' method</span>
<span class="n">arcpy</span><span class="o">.</span><span class="n">CopyFeatures_management</span><span class="p">(</span><span class="n">in_features</span><span class="o">=</span><span class="n">species_lyr</span><span class="p">,</span> <span class="n">out_feature_class</span><span class="o">=</span><span class="n">individual_shp</span><span class="p">)</span>
<span class="c1"># 6.2):</span>
<span class="c1"># Create an output path for the Raster Dataset (*.tif)</span>
<span class="n">tif_name</span> <span class="o">=</span> <span class="n">individual</span> <span class="o">+</span> <span class="s2">".tif"</span>
<span class="n">individual_tif</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">output_folder</span><span class="p">,</span> <span class="n">tif_name</span><span class="p">)</span>
<span class="c1"># Convert the newly created Shapefile into a Raster Dataset using 'PolygonToRaster_conversion' method</span>
<span class="n">arcpy</span><span class="o">.</span><span class="n">PolygonToRaster_conversion</span><span class="p">(</span><span class="n">in_features</span><span class="o">=</span><span class="n">individual_shp</span><span class="p">,</span> <span class="n">value_field</span><span class="o">=</span><span class="n">attribute_name</span><span class="p">,</span> <span class="n">out_rasterdataset</span><span class="o">=</span><span class="n">individual_tif</span><span class="p">)</span>
<span class="c1"># Print progress info for the user</span>
<span class="n">info</span> <span class="o">=</span> <span class="s2">"Processed: "</span> <span class="o">+</span> <span class="n">individual</span>
<span class="n">arcpy</span><span class="o">.</span><span class="n">AddMessage</span><span class="p">(</span><span class="n">info</span><span class="p">)</span>
<span class="c1"># 7. Print that the process was finished successfully</span>
<span class="n">info</span> <span class="o">=</span> <span class="s2">"Process was a great success! Wuhuu!"</span>
<span class="n">arcpy</span><span class="o">.</span><span class="n">AddMessage</span><span class="p">(</span><span class="n">info</span><span class="p">)</span>
</pre></div>
</div>
<p>Now, we have a script that we can use from our Toolbox in ArcGIS! Let’s next see how it can be used.</p>
</div>
</div>
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="Lesson6-run-the-tool.html" class="btn btn-neutral float-right" title="Running the Python script from ArcGIS" accesskey="n">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="Lesson6-toolbox.html" class="btn btn-neutral" title="ArcGIS Toolbox" accesskey="p"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
© Copyright 2016, Henrikki Tenkanen.
Last updated on Feb 20, 2017.
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT:'./',
VERSION:'1',
COLLAPSE_INDEX:false,
FILE_SUFFIX:'.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script type="text/javascript" src="_static/js/theme.js"></script>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.StickyNav.enable();
});
</script>
<li><a href="http://project.invalid/">Project Homepage</a> »</li>
<div class="footer">
<img src="../img/GPLv3_Logo.svg">
</div>
</body>
</html>