7

I have multiple shapefiles to which I have added a field called "gname1". For each shapefile, I want "gname1" to be set to the name of that particular shapefile, for every feature within the shapefile. I realize there are multiple ways to do this, but I want to know how to do this within the "Field Calculator" tool. So, in the "gname1=" box under Field Calculator, what is the expression I use to denote that I want the field to be set to the shapefile name?

I am very new to python, and not entirely familiar with the procedure for using python scripts to calculate fields through this tool in Arc10. I have seen other questions similar to this one, but I was confused on how to do this within the field calculator tool.

0

3 Answers 3

6

If you're committed to using the Field Calculator tool your best bet may be a very short python script within the "Pre-Logic Script Code":

def listShpName(yourShp):
  desc=arcpy.Describe(yourShp)
  return desc.Name

...and then your expression will be

listShpName("LayerInArcMapToC") 

Where LayerInArcMapToC is the name of your layer in ArcMap

I've attached a screen capture to illustrate this. Of course, you'll have to do this on a shapefile-by-shapefile basis if you want to use field calculator. Here's a link to the ESRI documentation on describing data like I've illustrated above: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002z00000018000000

An additional link to some simple field calculator operations: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//005s0000002m000000

enter image description here

3

I know nothing about Python, this is my first attempt at even trying it, but Cody's code didn't work for me. I fiddled with it a bit and this is what ended up working.

mxd = arcpy.mapping.MapDocument("Current")
layerlist = arcpy.mapping.ListLayers(mxd)

x = 0
while x < len(layerlist):                  
    layer = layerlist[x]
    layername = str(layer.name)             
    expression = "'" + layername + "'"
    arcpy.AddField_management(layer, "identify", "TEXT")
    arcpy.CalculateField_management (layer, "identify", expression, "PYTHON_9.3", "#")
    x = x + 1

The result was a new field in each layer, named "identify", which identified the name of its layer. You shouldn't have to edit any of the code, unless you want to call your column something different (then edit "identify" in the Addfield and Calculatefield bits to whatever you want it to be).

1
  • This worked perfectly for me, thanks BrainofMatter! For me my filenames were years, so I changed the field type from "TEXT" to "SHORT" (Just FYI in case that's useful for others).
    – cms
    May 6, 2020 at 23:36
2

If you open the python interpreter window you can do this pretty easily.

First you'll need a list of your shapefiles. You can get this by returning it from the data frame. So you'll need to have an MXD with all your shapefiles on it. Then in the python interpreter type the following:

mxd = arcpy.mapping.MapDocument("Current") #Returns your mxd
layerlist = arcpy.mapping.ListLayers(mxd)  #Returns your list of layer

That will produce a list of all your layers. Now we need to create a loop that will go through all the layers, find their name that add it to the field.

x = 0
while x < len(layerlist):                  #While x is less than the number of layers you have
    layer = layerlist[x]
    desc = arcpy.describe(layer)           #Describe a layer in the layer list
    layername = str(desc.name)             #Get the layer name
    expression = "'" + layername + "'"     #Setup the calculator expression
    arcpy.CalculateField_management (layer, "gname1", expr, "PYTHON_9.3", "#")
    x = x + 1

This will do what your looking for, but since your new at python it's going to seem pretty daunting and may be a little tricky. So if you have some questions post them and I'll try to help.

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.