I am trying to clean up my script so that I don't have to change the variables each time I want to use it.

I have arcgis tables for each US state (plus DC and Puerto Rico). I want to iterate through those tables in a folder, join each one at a time to a shapefile, copy the joined features to a new featureclass in a different geodatabase, name that featureclass after the respective state name, then remove the join and move on to the next state.

I would consider myself a novice when it comes to python. I've been trying to teach myself for years, but haven't had a good chance to take an in depth class and I don't have anyone I work with that knows it too well. I know there are more efficient ways for scripting like loops, functions, and conditional statements, but I don't know how to set them up correctly.

So I created a script that did what I needed to get done for today, but I'd like to make the script more dynamic. I don't want to have to change each and every table name or new featureclass name. I was trying to look up how to either create a custom function for part of the code, but then also have a loop so it knew to iterate through each table in the folder. I wasn't sure if I need to have the loop first, then the function, or have the loop inside the function. Then I wasn't sure how to get the correct names for the output feature classes, I know theres a way to reference dynamic variable with %s but not sure how to incorporate it here.

layer = arcpy.GetParameterAsText(0) inField = "GEOID" jTable = r'k:\geospatial\data\census\national\census_fact_finder_data\census_tract_year_built\aa_by_state\xls_pcts\tables' jField = "GEOID" outFC = r'K:\GEOSPATIAL\DATA\Census\National\Census_Fact_Finder_Data\Shapefiles\CFF_Census_Tracts\PCTs\FCC_CT_YB_PCT.gdb' arcpy.AddMessage("Processing Arizona...") #join table to census tract layer arcpy.AddMessage("Joining Arizona table to Census Tracts...") tract_join = arcpy.AddJoin_management(layer, inField,jTable + "\\az_pcts", jField, "KEEP_COMMON") #Copy joined features to new feature class in geodatabase arcpy.AddMessage("Exporting joined features to FCC_CT_YB_PCT geodatabase...") arcpy.CopyFeatures_management(tract_join, outFC + "\Arizona_PCT") #remove all joins arcpy.AddMessage("Removing joins to process next table...") arcpy.RemoveJoin_management(layer) arcpy.AddMessage("Arizona Complete") 

So with this example above, it joined the Arizona table (az_pcts) to the census tracts layer (layer = arcpy.GetParameterAsText(0)), copied the joined features to the new database and named it Arizona_PCT, then removed the join and continued on to the next table. I repeated this same exact structure for each state table and changed all the path endings to what I wanted them to be. If anyone has any advice, even pieces, that would be greatly appreciated.

1

1 Answer

First of all, Welcome to Stack Overflow. You can do all of these using ModelBuilder. See: What is model builder and quick tutorial

But what i don't understand what kind of tables you have locating in folder? Normally, you need to specify your folder in a file geodatabase, personal geodatabase or RDBMS table instead of a folder. But i skipped that question and gave a sample code for you above.

Anyway, I prepare some code for you. You can follow it:

import arcpy # iterating all tables in an environment , and make join them with a shapefile # these are constant variables shapefilepath = r"c:\users\someplace\someshape.shp" commoncolumn = "SAMECOLUMN" # this column must be same in other shapefiles too # If all shapefile samecolumns are different each other, you need to make a list like this commoncolumns_ordered = ['SAMECOLUMN1', 'SAMECOLUMN2', ] # .. goes away mainfolder = r"c:\users\someplace" tablegdb = r"c:\users\someplace\somegdb.gdb" # we'll search our tables here arcpy.env.workspace = tablegdb # we will work on here mytables = arcpy.ListTables("*") # you can filter your tables as starting or ending with a letter. for table in mytables: # you need to make view from all tables name = arcpy.Describe(table).name.encode('utf-8') # my table name table_view = arcpy.MakeTableView_management(table, name) # ok so we have our view. Otherwise, we would not be able to use this as an input for add join tool """ There are couple differences between add join and join field tools. Read them: Add join help : Join field help : * We don't have to make table view if we use join field """ # i assume that both common columns, # fields are same. out_join = arcpy.AddJoin_management(table_view, commoncolumn, shapefilepath, commoncolumn) # extracting them is not useful. But I'll write it down: arcpy.Copy_management(out_join, out_data="%s\\%s" % (out_gdb, name)) # some notes: # If your samecolumn fields are different between each other in tables # you need to iterate them like this: for table, column in zip(mytables, commoncolumns_ordered): print (table) print (column) # do others 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.