Leave a comment

Joining a table to a feature class

I have had the need to join tables to a shapefile or feature class many times and after not finding anything on the web, I posted on the ESRI forum and got my solution.

The difference with joining a table is that it is a one way join.  That is, you can copy from the table to the feature but not the other way around.

gp.MakeFeatureLayer("temp.shp", "temp")
 joinTable = "temp2.dbf"
 gp.AddJoin_management("temp", "field 1", joinTable, "field 1")
 gp.CalculateField_management("temp", "Field_2", "[temp2.Field_2]")
 gp.RemoveJoin_management("temp", "temp2.dbf")

I originally tried making a tableview of the table but found that it wasn’t necessary to do so.  Notice that because it is a table, tou need the [] around the table that is being read from.

I find this function very useful as I often have the need to read from a table and copy values over to a shapefile.

In this instance I needed to join a table to a table.  The first table  (table 1) was an input table and the second table (table 2) was a copy of the .dbf from a shapefile.

gp.AddMessage("Making Layer & View...")
 # Make Feature Layer from Stops
 gp.Copy_management(sys.argv[3], "temp.dbf")
 gp.MakeTableView_management("temp2.dbf", "temp2")
 joinTable1 = "temp.dbf"
 joinTable2 = "temp2"
 gp.AddJoin_management(joinTable1, "field_1", joinTable2, "field_2")
 gp.AddMessage("Joined table views")
 gp.CalculateField_management("temp2", "TIME", "[temp.TIME]")
 gp.RemoveJoin_management("temp2", "temp.dbf")

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: