Feature Class Check Python/Modelbuilder

1363
8
Jump to solution
05-23-2014 07:28 AM
BrettAuger1
New Contributor
Hi Folks,

I am trying to build a model that inputs one geodatabase into an other. The problem I am having is the schema for the geodatabases is the same however there may or may not always be a input file to send to the output database and this is causing errors. Right now my model searches for a featureclass then appends it to another. Right now if it does not find the correct class is outputs and error and stops my model. I found this bit of code, however it is searching for a field rather than a featureclass. Is there a piece of code where I can use a similar tool but for searching if a feature class exists inside a dataset.

Many Thanks,

Brett


#********************************************************************** # Description: # Tests if a field exists and outputs two booleans: #   Exists - true if the field exists, false if it doesn't exist #   Not_Exists - true if the field doesn't exist, false if it does exist #                (the logical NOT of the first output). # # Arguments: #  0 - Table name #  1 - Field name #  2 - Exists (boolean - see above) #  3 - Not_Exists (boolean - see above) # # Created by: ESRI #**********************************************************************  # Standard error handling - put everything in a try/except block # try:      # Import system modules     import sys, string, os, arcgisscripting      # Create the Geoprocessor object     gp = arcgisscripting.create()      # Get input arguments - table name, field name     #     in_Table = gp.GetParameterAsText(0)     in_Field = gp.GetParameterAsText(1)      # First check that the table exists     #     if not gp.Exists(in_Table):         raise Exception, "Input table does not exist"      # Use the ListFields function to return a list of fields that matches     #  the name of in_Field. This is a wildcard match. Since in_Field is an     #  exact string (no wildcards like "*"), only one field should be returned,     #  exactly matching the input field name.     #     fields = gp.ListFields(in_Table, in_Field)      # If ListFields returned anything, the Next operator will fetch the     #  field. We can use this as a Boolean condition.     #     field_found = fields.Next()      # Branch depending on whether field found or not. Issue a     #  message, and then set our two output variables accordingly     #     if field_found:         gp.AddMessage("Field %s found in %s" % (in_Field, in_Table))         gp.SetParameterAsText(2, "True")         gp.SetParameterAsText(3, "False")     else:         gp.AddMessage("Field %s not found in %s" % (in_Field, in_Table))         gp.SetParameterAsText(2, "False")         gp.SetParameterAsText(3, "True")   # Handle script errors # except Exception, errMsg:      # If we have messages of severity error (2), we assume a GP tool raised it,     #  so we'll output that.  Otherwise, we assume we raised the error and the     #  information is in errMsg.     #     if gp.GetMessages(2):            gp.AddError(GP.GetMessages(2))     else:         gp.AddError(str(errMsg))  
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
markdenil
Occasional Contributor III
as I understand this you have two GDBs (one.gdb and two.gdb)
each with, say, one data set
and each data set has a bunch of feature classes

one.gdb\dataset1\
has
fc1
fc2
fc3
fc4
fc5
fc6

and
two.gdb\dataset1\
has
fc1
fc2
fc4
fc5
fc6
(no fc3)

you want to append from gdb one to two

so you would use listfeatureclasses to list what is in one.gdb\dataset1\
call it fcONElist
loop through that list of fc names, testing for each in two.gdb\dataset1\
to wit:
for fc in fcONElist:     if not arcpy.Exists(r"path\two.gdb\dataset1\%s" % (fc)):         print "not found: ", fc     else:         -- do the append --


I am sure it is more complex than this, but this is essentially the thing you need to do, eh?

View solution in original post

0 Kudos
8 Replies
markdenil
Occasional Contributor III
Just how does

if arcpy.Exists(r"path\dataset\featureclass"):

not allow you to avoid an error?
0 Kudos
BrettAuger1
New Contributor
My thoughts are that if the IF statement returns false then that section of the model will not continue and will move on to validate the next feature and so on. I am new to the implentation of python but this seemed like it may do the trick. I am not set up one piece of code, rather the idea. Ideally, if a code could verify if data existed then if "true" continue on with that section of the model and if "false" end that section and move on.

Brett

Just how does

if arcpy.Exists(r"path\dataset\featureclass"):

not allow you to avoid an error?
0 Kudos
DuncanHornby
MVP Notable Contributor
Brett,

When you say "model searches for a featureclass" are you using an Iterator? If you use a FeatureClass iterator that will return all the FeatureClasses in a workspace, so missing ones are never an issue. Have a look at that, if that is not how you are searching for FeatureClasses the I suggest you upload an image of your model for us to understand.

Duncan
0 Kudos
BrettAuger1
New Contributor
Right now I have a nested iterator model inside my working model. In the image below I have an input GDB and a destination GDB, the destination uses a select select data function which is simple because this does not change. The input GDB goes thru an iterator to search based on a wildcard statment. My problem is that sometimes the feature class does not exist but it tries to run the append tool anyway even tho the iterator didnt find what it needed. My ask is for a function to go between the append and the iterator function to stop the append if the data does not exist. If the iterator does not find the feature class based on the wildcard statment it spits out a feature class with a made up name. This made up feature class does not exist and that hangs up the append function.

I appreciate all the help, these things get complicated to explain via forum post.


Brett,

When you say "model searches for a featureclass" are you using an Iterator? If you use a FeatureClass iterator that will return all the FeatureClasses in a workspace, so missing ones are never an issue. Have a look at that, if that is not how you are searching for FeatureClasses the I suggest you upload an image of your model for us to understand.

Duncan
0 Kudos
DuncanHornby
MVP Notable Contributor
Your sub-model Input Riparian Point- is it your intention to "spit out" a single FeatureClass at a time or a bunch of FeatureClasses based upon your wild card. If it is meant to be a single FeatureClass I see no reason for it to be a sub model unless its doing some other complex stuff so you could probably do away with the collects tool.  Normally the collects tool is the output parameter of a sub-model which you would link directly to the append tool. Have a look at this question I answered.

Try tweaking these first?
0 Kudos
BrettAuger1
New Contributor
What I copied for an image above is a one section of the model I need. The output database has ~23 feature classes that need to be appended however the input may not have a match for the ~23. So my model right now has 23 or so copies of the image I posted above. So each of the previously shared functions should produce 1/23 of the feature classes then move on to the next. When I put a stop function in or something similar it it stops all functions past that point.

Your sub-model Input Riparian Point- is it your intention to "spit out" a single FeatureClass at a time or a bunch of FeatureClasses based upon your wild card. If it is meant to be a single FeatureClass I see no reason for it to be a sub model unless its doing some other complex stuff so you could probably do away with the collects tool.  Normally the collects tool is the output parameter of a sub-model which you would link directly to the append tool. Have a look at this question I answered.

Try tweaking these first?
0 Kudos
markdenil
Occasional Contributor III
as I understand this you have two GDBs (one.gdb and two.gdb)
each with, say, one data set
and each data set has a bunch of feature classes

one.gdb\dataset1\
has
fc1
fc2
fc3
fc4
fc5
fc6

and
two.gdb\dataset1\
has
fc1
fc2
fc4
fc5
fc6
(no fc3)

you want to append from gdb one to two

so you would use listfeatureclasses to list what is in one.gdb\dataset1\
call it fcONElist
loop through that list of fc names, testing for each in two.gdb\dataset1\
to wit:
for fc in fcONElist:     if not arcpy.Exists(r"path\two.gdb\dataset1\%s" % (fc)):         print "not found: ", fc     else:         -- do the append --


I am sure it is more complex than this, but this is essentially the thing you need to do, eh?
0 Kudos
BrettAuger1
New Contributor
as I understand this you have two GDBs (one.gdb and two.gdb)
each with, say, one data set
and each data set has a bunch of feature classes

one.gdb\dataset1\
has
fc1
fc2
fc3
fc4
fc5
fc6

and
two.gdb\dataset1\
has
fc1
fc2
fc4
fc5
fc6
(no fc3)

you want to append from gdb one to two

so you would use listfeatureclasses to list what is in one.gdb\dataset1\
call it fcONElist
loop through that list of fc names, testing for each in two.gdb\dataset1\
to wit:
for fc in fcONElist:
    if not arcpy.Exists(r"path\two.gdb\dataset1\%s" % (fc)):
        print "not found: ", fc
    else:
        -- do the append --


I am sure it is more complex than this, but this is essentially the thing you need to do, eh?


That is correct, sorry for the late reply. This project got put aside for a bit.
0 Kudos