2

I am trying to merge lots of point shapefiles together to form 1 large shapefile. What i want to be able to do is get the name of each of the point files to appear in the attribute table either during the merge or carrying out a post processing task. How can i do this in ArcMap 10.3? I also have no experience with python code etc.

1 Answer 1

6

Without coding, it could be time-consuming. Either way, the steps would be to add a field to each shapefile and populate each feature with the shapefile's name. Here's a sample script, to give you an idea, if you'd like to try coding.

import arcpy
import os

# Set the ArcPy workspace to the directory that contains your input shapefiles
arcpy.env.workspace = r'C:\data\WhereTheShapefilesAre'

# Get a list of shapefiles in the workspace
fcs_in = arcpy.ListFeatureClasses()
print 'fcs_in:', fcs_in

fn_source_field = 'SOURCE_SHP'

# Iterate over the shapefiles
for fc in fcs_in:
    print 'fc:', fc
    # Get the shapefile's name without the extension
    name = os.path.splitext(fc)[0]
    # Add the source field to the shapefile
    arcpy.AddField_management(fc, fn_source_field, 'TEXT')
    # Iterate over the rows, populating the source field with the shapefile's
    #   name
    with arcpy.da.UpdateCursor(fc, fn_source_field) as cur:
        for row in cur:
            row[0] = name
            cur.updateRow(row)

# The path/name of your output, merged shapefile
fc_output = r'C:\data\MyOutputDirectory\MyOutputShapefile.shp'
# Merge the shapefiles
arcpy.Merge_management(fcs_in, fc_output)

You shouldn't have to change much for your situation. Change C:\data\WhereTheShapefilesAre to the directory that contains your input shapefiles. Change C:\data\MyOutputDirectory\MyOutputShapefile.shp to wherever you'd like the merged shapefile to be.

After you make those changes, put it into a text file and give it a .py extension. Then, you can simply double click the file from Windows Explorer, and it should run.

Note that this script will alter your original files, by adding a field named 'SOURCE_SHP' and populating it with the shapefile's name.

==EDIT==

I added a couple print statements to the code to help you identify the issue.

11
  • I've tried using this code and have just changed the source and output locations and it opens a cmd prompt style window for about 10-20 seconds and then this closes. Nothing has been generated in my selected output folder and none of the original shapefiles have been altered...
    – Stephen
    Jun 14, 2016 at 12:51
  • It results in this error: Traceback (most recent call last): File "F:\Flood Mapping Project\ShapefileMergeCode2.py", line 28, in <module> arcpy.Merge_management(fcs_in, fc_output) File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\management.py", line 4217, in Merge raise e ExecuteError: ERROR 999999: Error executing function. Failed to execute (Merge).
    – Stephen
    Jun 14, 2016 at 12:59
  • @Stephen, Yeah, don't you love ESRI's uselessly vague errors? How many and how large are the original shapefiles? You may be hitting a size limit with the output. This is why it's generally better to use geodatabases. Are you sure your shapefiles aren't being updated with the new field/values? I used this exact same code yesterday, and it worked as expected.
    – Tom
    Jun 14, 2016 at 16:10
  • @Stephen Oh! I bet I know what it is. Check your workspace path. Make sure that there is an r prior to the opening quotation mark. Also, make sure that that path points directly to the directory that contains your shapefiles. If it's running that fast and not giving you an error until the merge, it's probably not finding any shapefiles. Also, the shapefile search may be case-sensitive, but you could just remove the '*.shp' bit. So that line would just be fcs_in = arcpy.ListFeatureClasses()
    – Tom
    Jun 14, 2016 at 16:15
  • 1
    As a last ditch attempt I tried all of this on a different computer and it worked straight away... very strange.
    – Stephen
    Jun 15, 2016 at 17:07

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