Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have a number of tables that contain known values in a field. For example, I have a soil type table with one field in it. That field contains a list of known soil types. I have another table that has a list of known vegetation types. I have various other tables that contain lists of other known types of other data. What I want to do is:

  • Read in each table, do a cursor search and add the values to a list via tableList.append

  • Now that I have built each list, I want to compare the values from a separate feature class table to the known values in the list I have just created using if row.SOIL_TYPE in SoiltableList:

  • In order to do this, I need to reset the workspace I guess, because you cannot set a path parameter for listing data in any of the arcpy Listing methods.

    env.workspace = r"C:\BASE_TABLES.gdb"
    
    SoiltableList = []
    VegtableList = []
    
    for table in arcpy.ListTables():   
       if table == "SOIL_TYPE":
          for row in arcpy.SearchCursor(table)
              SoiltableList.append(row.SOIL_TYPE)
       if table == "VEG_TYPE":
          for row in arcpy.SearchCursor(table)
              VegtableList.append(row.VEG_TYPE)
    
    # more iterations of table
    
    # Reset workspace
    env.workspace = r"C:\TEST_GDB.gdb"
    
    for fc in arcpy.ListFeatureClasses():
       if fc == "SOIL_FEATURECLASS":
          for row in arcpy.SearchCursor(fc):
             if row.SOIL_TYPE in SoiltableList:
                print "YES"
             else:
                print "No"
      # More iterations of if/else logic to compare values from fields from various feature classes to the known values in the list I built.
    

The 2 questions I have are:

  1. Do I need to reset the workspace the way I did above? is there a better way of doing this or is what I am doing completely acceptable?
  2. Does my code make sense? Am I over complicating what I am trying to do? I though that maybe I would define a function or 2 to simplify the code, but I'm still in the process of figuring out what else I want the code to do.

Thanks for any suggestions.

Mike

share|improve this question
1  
Are your tables actually named SOIL_TYPE and VEG_TYPE? Those look more like field names than table names. What exactly are you trying to accomplish? –  blah238 May 9 '13 at 20:20
    
Yes, both the table and the field in each table have the same name. I am writing some verification coding (i.e the feature classes are going to a client. I want to make sure the values in the feature classes are correct. I have people entering data into the feature class attribute tables by hand from some field surveys. I am writing some code to make sure the values that the data entry person entered is correct according to our known lists. I'll eventually be checking for capitalization, field length, etc) –  Mike May 9 '13 at 20:31
1  
Is there a reason you are using ListTables/ListFeatureClasses instead of just specifying the full path of the tables/feature classes directly? –  blah238 May 9 '13 at 20:37
    
Interesting. I was just questioning myself on that one. You're right. I can simplify the code by hard coding the argument without using the Lists. thanks :) –  Mike May 9 '13 at 20:43
2  
Do you really need ArcPy to meet the requirement in your first Comment? Your data is in a File Geodatabase so if you converted your *_TYPE tables to domains then you would be able to use Validate on your data. –  PolyGeo May 9 '13 at 21:27

1 Answer 1

There isn't much point in iterating over a list of tables if the number of them is small and you know exactly the names and locations of each one -- instead, just specify their paths in full:

e.g.:

soil_table = r"C:\BASE_TABLES.gdb\SOIL_TYPE"
veg_table = r"C:\BASE_TABLES.gdb\VEG_TYPE"
soil_fc = r"C:\TEST_GDB.gdb\SOIL_FEATURECLASS"

If the workspaces can change but the class names are static, you could join them with os.path.join.

e.g.:

table_workspace = arcpy.GetParameterAsText(0)
fc_workspace = arcpy.GetParameterAsText(1)

soil_table = os.path.join(table_workspace, "SOIL_TYPE")
veg_table = os.path.join(table_workspace, "VEG_TYPE")
soil_fc = os.path.join(fc_workspace, "SOIL_FEATURECLASS")

That way you can avoid messing with the arcpy list methods and the arcpy.env.workspace property.

share|improve this answer
1  
You could also put the tablenames into a list and then just iterate thru them, setting the path to each one using os.path.join. –  Chad Cooper May 10 '13 at 14:29
2  
Or, if you like one-liners: soil_table, veg_table, soil_fc = map(os.path.join, (table_workspace, table_workspace, fc_workspace), ("SOIL_TYPE", "VEG_TYPE", "SOIL_FEATURECLASS")) –  blah238 May 10 '13 at 18:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.