<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>michalisavraam.org blog &#187; iteration</title>
	<atom:link href="http://michalisavraam.org/tag/iteration/feed/" rel="self" type="application/rss+xml" />
	<link>http://michalisavraam.org</link>
	<description>a spatial web presence</description>
	<lastBuildDate>Mon, 24 May 2010 17:17:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Geoprocessing Iteration with Python</title>
		<link>http://michalisavraam.org/2009/06/geoprocessing-iteration-with-python/</link>
		<comments>http://michalisavraam.org/2009/06/geoprocessing-iteration-with-python/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 02:05:05 +0000</pubDate>
		<dc:creator>Michalis Avraam</dc:creator>
				<category><![CDATA[GIS* Points]]></category>
		<category><![CDATA[Python Geoprocessing]]></category>
		<category><![CDATA[Python Points]]></category>
		<category><![CDATA[arcgisscripting]]></category>
		<category><![CDATA[geoprocessing]]></category>
		<category><![CDATA[iteration]]></category>
		<category><![CDATA[ListDatasets]]></category>
		<category><![CDATA[ListFeatureClasses]]></category>
		<category><![CDATA[ListWorkspaces]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://michalisavraam.org/teaching/36-pygeoprocessing/56-geoprocessing-iteration-with-python</guid>
		<description><![CDATA[There are multiple problems analysts face when they have to deal with processing multiple data files. There is the issue of identifying similarities and commonalities in files, and then of course how to automate the processing so they don&#8217;t have to run a program multiple times with the same parameters of separate files. In the [...]


Related posts:<ol><li><a href='http://michalisavraam.org/2009/08/using-the-geoprocessors-list-methods/' rel='bookmark' title='Permanent Link: Using the geoprocessor&#8217;s List* Methods'>Using the geoprocessor&#8217;s List* Methods</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>There are multiple problems analysts face when they have to deal with processing multiple data files. There is the issue of identifying similarities and commonalities in files, and then of course how to automate the processing so they don&#8217;t have to run a program multiple times with the same parameters of separate files. In the world of ESRI&#8217;s GIS analysis, this can be performed quite easily with the help of Geoprocessing, either in Python or the Model Builder. Below is sample code that allows the iteration over a number of datasets.<span id="more-18"></span></p>
<p>Geoprocessing in Python provides multiple methods that can help one iterate. Depending on the dataset types you have, you can access list of data easily and effortlessly. The following code will show you how this is done in version 9.3, although earlier versions are as easy to use as well.</p>
<p>In order to iterate a process, we need a list of files we will use, and then a series of commands to be run on those files. There are multiple ways to achieve this through the operating system (os and sys modules), but since we do have that pesky little problem of shapefiles having multiple files associated with them, or geodatabases being a single representation for the OS, we need a different solution. And here lies the power of the geoprocessor. It allows one to list data the way ESRI can recognize them: workspaces, (feature) datasets, feature classes, tables and rasters. So let&#8217;s begin.</p>
<p>Oftentimes, analysts store data in folders. If you are conducting a study in 10 different regions, it is often the case you save the data files for each region in a different folder. A folder structure like the following is common:</p>
<ul>
<li>\myStudy\</li>
<li>\myStudy\region1\</li>
<li>\myStudy\region2\</li>
<li>\myStudy\region3\</li>
<li>&#8230;</li>
</ul>
<p>We therefore need a method that allows us to go into the master folder (\myStudy\ in this case) and read the files in each folder. To achive this, we will read the names of folders first. The command <strong>ListWorkspaces(widcard, type)</strong> can help us do that, by accepting a wildcard as a first argument (any combination of * can help narrow things down) and the type of workspace we want (from Access, Coverage, FileGDB Folder, SDE or ALL). An example follows:</p>
<pre class="brush: python;">
import arcgisscripting # Import the geoprocessor capabilities
gp = arcgisscripting.create(9.3) # Create the geoprocessor object
masterFolder = r&quot;path\to\folder&quot; # Define the master folder
gp.workspace = masterFolder # Define the location to run the commands on
workspaces = gp.ListWorkspaces(&quot;*&quot;, &quot;Folder&quot;) # List all FOLDERS in master workspace
for workspace in workspaces: # Iterate through the workspaces one at a time
print workspace # Print the workspace name
# Do you work here
</pre>
<p>At this point, you have a Python list object that holds the names of all the subfolders that you may want to use. The next step is to identify a list of all data within that folder. To achieve that, we use one of the following commands, depending on the type of data we have: <strong>ListDatasets(wildcard, type)</strong>, <strong>ListFeatureClasses(wildcard, type)</strong>, <strong>ListTables(wildcard, type)</strong> or <strong>ListRasters(wildcard, type)</strong>. The wildcard is defined as above, but the type is different. Type actually refers to the type of listing. For datasets, the type is feature,TIN, raster, CAD or ALL. For feature classes the types are Annotation, arc, dimension, label, line, node, point, polygon, region, route or tic. Table types can be DBF, INFO or ALL. Similarly, raster types can be BMP, GIF, IMG, JP2, JPG, PNG, TIFF, GRID or ALL. We will contnue our example by searching for feature classes (shapefiles) in the folders we identified above.</p>
<pre class="brush: python;">
import arcgisscripting # Import the geoprocessor capabilities
gp = arcgisscripting.create(9.3) # Create the geoprocessor object
masterFolder = r&quot;path\to\folder&quot; # Define the master folder
gp.workspace = masterFolder # Define the location to run the commands on
workspaces = gp.ListWorkspaces(&quot;*&quot;, &quot;Folder&quot;) # List all FOLDERS in master workspace
for workspace in workspaces: # Iterate through the workspaces one at a time
gp.workspace = workspace # Change to the new workspace
fcs = gp.ListFeatureClasses() # No parameter defaults to all
for fc in fcs: # Iterate through each file in the folder now
print fc # print the feature class
# Do individual file work here
</pre>
<p>As you can see we managed to begin with a master folder and continue to analyze individual files within all subfolders of the initial master folder. This is all done in a few lines of code, minimize complexity and confussion for us. In less that 6 lines of Python code you can begin processing each file in your computer quickly and easily.</p>
<p>While listing of other types of files has not been shown, it is quite easy to do by using the appropriate command (as mentioned above). Dealing with databases is similar, as you can imagine and use the database as your master folder. Do not forget that &#8220;subfolders&#8221; in the database are called datasets, so you will need to use ListDatasets() instead of ListWorkspaces().</p>
<p>I hope this will be useful to some of you out there.</p>
Note: There is a rating embedded within this post, please visit this post to rate it.


<p>Related posts:<ol><li><a href='http://michalisavraam.org/2009/08/using-the-geoprocessors-list-methods/' rel='bookmark' title='Permanent Link: Using the geoprocessor&#8217;s List* Methods'>Using the geoprocessor&#8217;s List* Methods</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://michalisavraam.org/2009/06/geoprocessing-iteration-with-python/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
