5

I need to add an attribute to each shapefile attribute table in ArcMap, I was thinking to do it in SQL, but I'm not sure how to go about doing it since I'm very new to ArcMap.

Can someone explain to me how I would go about doing this?

The added attribute would need each value populated based on another attribute in the table.

Thanks.

4 Answers 4

8

If you do not have experience in scripting I would suggest using ModelBuilder for this task.

  1. Open ModelBuilder via ArcCatalog or ArcMap
  2. Drag in any shapefile or feature class
  3. Add in the Calculate Field tool (assuming that each feature class has the same field names to run against the calculation)
  4. Add a Pre Logic Script Code in the expression button
  5. Make the first input layer a Model Parameter
  6. Run the model and browse for a feature class to update
  7. Run model again and browse for next feature class to update

If you are more fluent in scripting I would suggest writing a python script using the same method above, however you can define a common workspace where all the feature class exist that you want to update. You can use os.walk to iterate through each feature class in the directory and perform the field calculation in batch.

Hope that helps

1
  • 1
    Clear and concise answer.
    – Fezter
    Commented Feb 12, 2013 at 1:56
4

If you don't want to get into ModelBuilder and don't want to do it 100% manually, there is yet another option, that's by using the relevant ArcToolbox tools in Batch Processing, which is good for partially automating relatively simple tasks like this one:

  • Open ArcToolbox, Data Management, Fields, right-click the Add Field tool and click Batch. You'll get this window:

enter image description here

  • Each row you enter on this table (ArcGIS help refers to it as the Batch Grid) will, when you click OK, run the specified tool once, with the parameters you specify in that row. The Plus button on the right will add more rows to the grid, so you'd add one row for each field you want to add.

  • Enter the layer you want to add the field to under "Input Table" (the name should appear the way it appears in ArcMap, or you can supply the complete path to the shapefile if that's easier), the name of the new field under "Field Name", the field type (text, short or long integer, etc.), and so on. If you're adding the same exact field to multiple tables, you can just copy and paste the text into the grid cells. Actually you can even copy this grid to Excel or whatever, populate it there, then paste it back into this window to run it, that makes populating it easier and you can save the Excel file if you find yourself running the same tasks in batch mode time and again.

  • After the grid is full of your inputs, click OK and it will run through each row and add the new field to each table listed:

enter image description here

  • Then do the same thing with the Calculate Field tool, in the same toolbox, using the field you just added as the field to be calculated.
3

While I think @artwork21's answer is great, it could be a bit daunting for someone who is new to ArcGIS. I absolutely think that you should familiarise yourself with ModelBuilder and eventually Python scripting (if you aren't already). However, by the time you set it all up, you could probably have done it manually for all 30 files (maybe not if there were 300 files).

Here is the manual process:

  1. Open the Attribute Table for your shapefile.
  2. Click on the top-left button in the attribute table (Table Options).
  3. Select Add Field...
  4. Create a field and give it the appropriate type (text, float, short integer, etc).
  5. Right click on that field name and select Field Calculator...
  6. Enter the expression required.
  7. Click OK.

Repeat the above for each file.

1

Since you were considering using SQL here is another scripting option using R. This approach reads the data using rgdal, calculates a new column (in example log transform), and then overwrites original shapefile(s). The only things that you need to change are the working directory (setwd), calculating the new column and the name of the new column (in this example "NEW").

require(rgdal)
setwd("D:/data")
shps <- list.files(getwd(), pattern = "shp$")
  for(i in shps) { 
    shp <- readOGR(getwd(), unlist(strsplit(i, "\\."))[1])
      new.col <- shp@data$NEW=log(shp@data$VAR) # CAL NEW COLUMN HERE
      shp@data <- data.frame(shp@data,  NEW=new.col)
        writeORG(shp, getwd(), unlist(strsplit(i, "\\."))[1],
               driver="ESRI Shapefile")
  } 

Or, here is a more efficient R like way using apply and calculating a new column directly in the data.frame function.

require(rgdal)
setwd("D:/data") 
lapply(as.list(list.files(getwd(), pattern = "shp$")), FUN=function(x) {
    shp <- readOGR(getwd(), unlist(strsplit(x, "\\."))[1])
      shp@data <- data.frame(shp@data,  NEW=log(shp@data$VAR))
        writeORG(shp, getwd(), unlist(strsplit(x, "\\."))[1],
                 driver="ESRI Shapefile") } )

Your Answer

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

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