I have 138 layers and I like to add a field which contain the layer name for all records for each table.

Is there any way to automate this process instead of doing it layer by layer?

I have a bike station feature class (point) and bus stations feature class (point) I like to know that, in the radius of 500 meter of every bike station how many bus stations I have and I like to have them separately based on bike station Id.

For example, which bus stations (bus station id) are in the 500 meter of bike stations (bike station id). Problem: the bus and bike stations table have no fields in common. I first buffered the bike stations by 500m and then clipped the bus stations based on buffered layer but the clipped layer does not separate the bus stations based on bike station id!

  • 3
    This should be possible using ModelBuilder. Give it a try, and if you get stuck, then edit your question to provide details of what you have tried and where you are stuck. If you prefer ArcPy (which is what I would use for this), then start writing the code, and if you get stuck, then edit your question to include a code snippet that illustrates what you have tried and where you are stuck. – PolyGeo Sep 5 '16 at 2:44
  • I suspect that this question or near identical ones to it has been asked and answered previously on this site so make sure you search thoroughly, and as a new user be sure to take the Tour that is designed to introduce all users to the site and its protocols. – PolyGeo Sep 5 '16 at 3:40
  • i am guessing this is some intermediate step you are want to perform to obtain some final answer it would help if you tell what do you want final output maybe some can suggest a right way and better way based on their experience. – Abhijit Gujar Sep 5 '16 at 5:56

It is relatively straightforward to add a field and populate with the feature class name using arcpy.

The following example will go through every layer in your MXD, add a field and populate that field with the name of the feature class. It will also set the default value of that field so that any new features will also be populated with that value.

import arcpy

mxd = arcpy.mapping.MapDocument("CURRENT")
df = mxd.activeDataFrame

# Get list of layers in mxd
layers = arcpy.mapping.ListLayers(mxd, "", df)
for layer in layers: # Loop through the layers
    if layer.supports("DATASETNAME"):
        fc = layer.datasetName # Get the name of the feature class
        arcpy.AddField_management(layer, 'FCName', 'Text') # Add new field to layer
        arcpy.AssignDefaultToField_management(layer,"FCName","{}".format(fc)) # Set default value of new field to name of feature class
        # If the layer contains features, then set the value in the new field
        # to the name of the feature class for every record in the layer
        if int(arcpy.GetCount_management(layer).getOutput(0)) > 0: 
            with arcpy.da.UpdateCursor(layer, "FCName") as cursor:
                for row in cursor:
                    row[0] = fc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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