<?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>GIS Tips &#38; Tricks &#187; python</title>
	<atom:link href="http://www.aubreyrhea.net/gis/index.php/tag/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.aubreyrhea.net/gis</link>
	<description>for ESRI&#039;s ArcGIS suite</description>
	<lastBuildDate>Sun, 25 Jul 2010 14:42:03 +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>Removing Empty Feature Classes with Python</title>
		<link>http://www.aubreyrhea.net/gis/index.php/2009/07/removing-empty-feature-classes-with-python/</link>
		<comments>http://www.aubreyrhea.net/gis/index.php/2009/07/removing-empty-feature-classes-with-python/#comments</comments>
		<pubDate>Mon, 27 Jul 2009 17:32:47 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[attribute table]]></category>
		<category><![CDATA[automation]]></category>
		<category><![CDATA[geodatabase]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.aubreyrhea.net/gis/?p=1</guid>
		<description><![CDATA[The Problem: You have a geodatabase with a lot of empty feature classes This often happens if the geodatabase has been created from a template of standard themes, but this particular version doesn&#8217;t contain information to stick into all the available slots. It can be difficult to get a handle on what&#8217;s actually there with [...]]]></description>
			<content:encoded><![CDATA[<p><strong>The Problem: You have a geodatabase with a lot of empty feature classes</strong></p>
<p>This often happens if the geodatabase has been created from a template of standard themes, but this particular version doesn&#8217;t contain information to stick into all the available slots. It can be difficult to get a handle on what&#8217;s actually there with all the empty feature classes clogging up space. It can take up extra processing time or cause errors depending on what you&#8217;re doing with the data later. And, it&#8217;s just bad organization. The best practice is to get rid of them. You can do so by moving down the feature class list in ArcCatalog, checking for empties and deleting them one by one. But with large databases (the kind that need trimming down the most) this can take a lot of time.</p>
<p>The slow manual method:<br />
<img src="http://www.aubreyrhea.net/gis/images/delete.jpg" alt="Right click to delete feature class" /></p>
<p>The fast automated method: use this python code that an associate shared with me!</p>
<p>Copy and paste the following into any text editor and save it as a python .py file.<br />
<big> </big></p>
<pre><big>##############################################################
##  Delete Empty Feature Classes from Multiple Geodatabases
##
##  This script loops through all geodatabases within a directory
##  and removes empty featureclasses from each geodatabase.
##
##  User must hardcode directory.  Run this script in IDLE.
##
##  Python Version 2.4
##############################################################

# Create the Geoprocessor object
import arcgisscripting, os, sys, string
gp = arcgisscripting.create()

# Load required toolboxes...
gp.AddToolbox("C:\\Program Files\\ArcGIS\\ArcToolbox\\Toolboxes\\Conversion Tools.tbx")

# Set workspace
gp.workspace = "C:\\Documents and Settings\\user\\Desktop\\gis\\"

# list personal geodatabases in the current workspace
listmdb = gp.listworkspaces("*", "access")
mdb = listmdb.next()

# loop through the personal geodatabases
while mdb:
    print mdb
    gp.workspace = mdb
    # list feature datasets in personal geodatabase
    listfdset=gp.listdatasets()
    fdset=listfdset.next()
    if not fdset == None:
        gp.workspace = mdb + "\\" + fdset
    # list feature classes in the personal geodatabase
    fcs = gp.listfeatureclasses()
    fcs.reset()
    fc = fcs.next()

    # loop through the featureclasses
    while fc:
       # print fc

        # if the feature class table has no records, delete the featureclass
        if gp.GetCount_management(fc) == 0:
            gp.Delete_management(fc)
            print fc + "deleted from" + mdb
        fc = fcs.next()

    mdb = listmdb.next()</big></pre>
<p><big></big><br />
The only thing you will need to change is the directory under # Set workspace. Put the path to your geodatabase(s) in between the gp.workspace = &#8220;&#8221; quotes, remembering to use two slashes \\ in between folder names.</p>
<p>This works with personal geodatabases. If you are using file geodatabases, go to the next line under # list personal geodatabases&#8230; and replace &#8220;access&#8221; with &#8220;FileGBD&#8221; between the gp.listworkspaces(&#8220;*&#8221;, &#8220;&#8221;) quotes.</p>
<p>Now, start Python IDLE, which ships with the ArcGIS suite. Open up your .py file. Then, select Run &#8211;&gt; Run Module (or hit F5). This is what you will see:<br />
<a href="http://www.aubreyrhea.net/gis/images/script.jpg"><img src="http://www.aubreyrhea.net/gis/images/script-small.jpg" alt="Python module in IDLE" />&#8220;</a><br />
<a href="http://www.aubreyrhea.net/gis/images/results.jpg"><img src="http://www.aubreyrhea.net/gis/images/results-small.jpg" alt="Python results in IDLE" />&#8220;</a><br />
It may take some time to run, but that is time you can spend doing other things. When it&#8217;s done, those empty feature classes are history.</p>
<p>You can edit, save and rerun the .py file as many times as you need if your geodatabases are spread between different directories. (Or, for even more automation, copy them all into the same directory before you start, and let the script loop through them).</p>
<p>Python is an extremely handy tool. If you are interested in learning more about it, see the resources below.</p>
<ul>
<li><a href="http://support.esri.com/index.cfm?fa=knowledgebase.techarticles.articleShow&amp;d=30028" target="_blank">HowTo: Get started writing Python geoprocessing scripts in ArcGIS 9x (ESRI Support)</a></li>
<li><a href="http://www.esri.com/news/arcuser/0405/files/python.pdf" target="_blank">A Guide to the Python Universe for ESRI Users (PDF File)</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.aubreyrhea.net/gis/index.php/2009/07/removing-empty-feature-classes-with-python/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
