3

I have a group of shapefiles that are stored in a Windows folder. There are multiple users that will be adding shapefiles to this folder or editing existing shapefiles in this folder on a continual basis. Currently, the shapefiles are manually merged together. I would like to build a very simple model that references all of these shapefiles and merges them on a continuous basis- perhaps 1 time per start of business day. The goal is to have a single layer with all of the shapefiles that is updated automatically as new shapefiles are created. All of the shapefiles created will have the same number of fields, the same field names, and same shape (polygon).

I could use Modelbuilder or possibly create a Python script that runs every morning. I am a beginner with Python.

I am using ArcGIS 10.1.

Update #2- My Python code is as follows:

import arcpy, os, time

arcpy.env.workspace = r'C:\Temp\Planning\Planning_In'
fcs = arcpy.ListFeatureClasses(feature_type='Polygon')

fcOut = r'C:\Temp\Planning\Planning_Out\ProjectMerge.shp'
if fcOut in fcs:fcs.remove(fcOut)


if len(fcs) > 0:
    if arcpy.Exists(fcOut):
        fcArchived = os.path.join(r'C:\Temp\Planning\Planning_Out', 'Archived_{}.shp'.format(time.strftime('%Y%m%d')))
        os.makedirs(os.path.dirname(fcArchived))
        arcpy.Copy_management(fcOut, fcArchived)
        arcpy.Delete_management(fcOut)
    arcpy.Merge_management(fcs, fcOut)

print fcs

My shapefiles are named: 13_012, 13_013, etc.

*** My error message is:

Traceback (most recent call last):
  File "C:\Temp\Planning\import arcpy.py", line 16, in <module>
    arcpy.Merge_management(fcs, fcOut)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\management.py", line 4221, in Merge
    raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000732: Input Datasets: Dataset C:\Temp\Planning\Planning_In\13-004.shp;C:\Temp\Planning\Planning_In\1314-004.shp does not exist or is not supported
Failed to execute (Merge).

Additionally, when I remove the merge and just apply the following code, I receive an error message that 'fcArchived' is not defined:

    import arcpy, os, time

    arcpy.env.workspace = r'C:\Temp\Planning\Planning_In'
    fcs = arcpy.ListFeatureClasses(feature_type='Polygon')

    fcOut = r'C:\Temp\Planning\Planning_Out\ProjectMerge.shp'
    if fcOut in fcs:fcs.remove(fcOut)


    if len(fcs) > 0:
        if arcpy.Exists(fcOut):
            fcArchived = os.path.join(r'C:\Temp\Planning\Planning_Out', 'Archived_{}.shp'.format(time.strftime('%Y%m%d')))
            os.makedirs(os.path.dirname(fcArchived))
            arcpy.Copy_management(fcOut, fcArchived)

print fcArchived
  • 1
    Welcome to GIS SE, what version of ArcGIS are you using? – artwork21 Mar 25 '16 at 16:46
  • Do you mean a windows folder? and do you want to add them as additional features to your master shapefile? – whatahitson Mar 25 '16 at 17:00
  • 1
    Merge them to a new shapefile? How "continuous"? Real-time, hourly, daily? How will the merged data be used? If you want to merge them all to a common shapefile, keep in mind that that shapefile cannot be in use elsewhere while the merge is processing. – Tom Mar 25 '16 at 17:33
  • 1
    Keeping in mind my points above, you could schedule a Python script to run, say, hourly. Would something like that work for you? – Tom Mar 25 '16 at 17:34
  • 1
    Also, keep in mind that not all shapefiles can be merged together. You need similar schemas, points can't be merged with polygons, etc. – MaryBeth Mar 25 '16 at 17:57
3

So, it sounds like you just need a really simple Python script. The following should work, and I've included some options that you can un-comment to extend the functionality.

import arcpy, os, time

arcpy.env.workspace = r'C:\Data\ThePathToTheDirectoryInWhichTheShapefilesToBeMergedExist'
fcs = arcpy.ListFeatureClasses(feature_type='Polygon')

fcOut = r'C\Data\Output\ThePathToTheOutputShapefile\MyShapefile.shp'
# Note that if the output shapefile is in the same directory as the input shapefiles, you
# only need to put the file name and not the whole path here. However, you would also
# need to include the next two, commented-out lines:
##if fcOut in fcs:
##    fcs.remove(fcOut)

if len(fcs) > 0:
    if arcpy.Exists(fcOut):
        # If you wish to archive the merged shapefile in its current state, you could do something like:
        ##fcArchived = os.path.join(r'C\Data\PathToAnArchivDirectory', 'Archived_{}.shp'.format(time.strftime('%Y%m%d')))
        ##os.makedirs(os.path.dirname(fcArchived))
        ##arcpy.Copy_management(fcOut, fcArchived)
        arcpy.Delete_management(fcOut)
    arcpy.Merge_management(fcs, fcOut)

Simply copy the code to text file; set the paths to those relevant to you; name the file with a .py extension; and then schedule a Windows task to run the script at your desired interval.

EDIT: In response to your code:

Okay, there are a few issues. Let's clean those up and see if it can lead us to the source of the error.

For one, your merged shapefile and your archive shapefiles are all going to the same directory as your input shapefiles. This isn't a deal-breaker, but you must ensure that those features aren't accumulating duplicates in your output. It would be easiest just to use a different directory for your archives (where I have "PathToAnArchiveDirectory" in my sample code, just put anything other than the exact same path your set for your workspace).

You also need to include the two lines where I have if fcOut in fcs:. This will remove the current merged shapefile from your list of shapefiles to merge. Without that line, you may be trying to merge a shapefile that you've deleted (in the line arcpy.Delete_management(fcOut))

Let me know what happens after your make those changes. You could also try adding a print statement just to ensure that everything in the variable fcs is correct. I would put print fcs in a new line immediately prior to the merge.

EDIT 2:

fcArchived may not exist at that point. It's contingent upon two different conditionals. You might want to move its print statement inside those conditional statements.

Print fcs before the merge line--and indented to the same level as the merge line. Then share those results (i.e., exactly what it prints) with us. You're apparently getting some non-existent feature class in your list of feature classes. Are you putting in something manually that you aren't including in your code here? It appears that you're doing something with a semi-colon, somewhere. Even further evidence that you're doing something other than what you're sharing here is that you said that your shapefile names have underscores, but the error message says that you're looking for shapefiles with dashes. Something doesn't add up.

Also, please format your code after you paste it to your question. Simply select it and press ctrl+k. You can ensure that it's highlighted as Python by putting <!-- language: lang-py --> above your code.

  • Thanks Tom. When I tried to run this script I got the following error: Traceback (most recent call last): File "C:\Temp\Planning\import arcpy.py", line 15, in <module> arcpy.Merge_management(fcs, fcOut) File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\management.py", line 4221, in Merge raise e ExecuteError: Failed to execute. Parameters are not valid. ERROR 000732: Input Datasets: Dataset project1.shp;project2.shp; does not exist or is not supported Failed to execute (Merge). – Jessica Marie Mar 28 '16 at 15:07
  • I have no spaces or slashes in my shapefile names. I also have tried disabling background geoprocessing, adding double backslashes to file paths, and changing the direction of backslash to frontslash in the file paths. – Jessica Marie Mar 28 '16 at 15:30
  • @JessicaMarie, I've added information to my answer to address your specific code. – Tom Mar 28 '16 at 16:19
  • Thanks Tom... I updated my question. Still getting the error message. – Jessica Marie Mar 28 '16 at 17:22
  • @JessicaMarie, I've updated my answer. – Tom Mar 28 '16 at 18:21

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.