Simple Merge of Shapefiles in a single folder

16081
19
12-05-2012 09:00 AM
TylerJones2
New Contributor
Hello,

    I am very new to Python scripting and apparently not very good at it. I have a single folder with many shapefiles that I need to merge into a single shapefile. They are in the same projection and will not hit the 2gb limit so I assume this would be the easiest type of script to write. I have been trying to execute from PythonWin since that is the IDE the ESRI training uses but at this point I don't care where I execute it just that it works. I assume the workflow to be something like: import arcpy, set the environment to that particular folders location, apply overwrite to true, create a list of the shapefiles in the folder (i assume by telling it to look for all files with the ending ".shp"), the using the Merge_management to execute the merge.
    So with that said, could anyone offer some help. I have found a few other scripts online but they have some extra arguements for parsing and what not and I could not get them to work. All help appreciated
Tags (2)
0 Kudos
19 Replies
TylerJones2
New Contributor
You are right. Im missing a forward slash after "D:". Now I just feel silly. So I was able to print the Listing file but now at the mergeManagment I get the same error but with the actual filenames I was looking for at least.
arcpy.Merge_management(Listing,"MergedFile.shp")
Runtime error <class 'arcgisscripting.ExecuteError'>: Failed to execute. Parameters are not valid. ERROR 000732: Input Datasets: Dataset campus_ortho.tiles.tile00.v5.shp;campus_ortho.tiles.tile01.v8.shp;campus_ortho.tiles.tile02.v5.shp;campus_ortho.tiles.tile03.v6.shp;campus_ortho.tiles.tile04.v5.shp;campus_ortho.tiles.tile05.v9.shp;campus_ortho.tiles.tile06.v9.shp;campus_ortho.tiles.tile07.v9.shp;campus_ortho.tiles.tile08.v10.shp;campus_ortho.tiles.tile09.v8.shp;campus_ortho.tiles.tile10.v8.shp;campus_ortho.tiles.tile11.v8.shp;campus_ortho.tiles.tile12.v8.shp;campus_ortho.tiles.tile13.v9.shp;campus_ortho.tiles.tile14.v9.shp;campus_ortho.tiles.tile15.v8.shp;campus_ortho.tiles.tile17.v8.shp;campus_ortho.tiles.tile18.v9.shp does not exist or is not supported Failed to execute (Merge). 
>>> 

An example of the shapefile names would be "campus_ortho.tiles.tile00.v5.shp" whereas each tile from my project was assigned a unique name upon export. Does this name have anything to do with the error I am recieving? I am not sure what characters are reserved in filenames when it comes to this.
0 Kudos
by Anonymous User
Not applicable
It may have something to do with the periods before the .shp extension on the shapefiles.  I would rename them by replacing the periods with underscores.  Try this:

import arcpy, os
arcpy.env.workspace = "D:/eCognition Results/Landcover/Buildings"
for file in arcpy.ListFiles('*.shp'):
    os.rename(os.path.join(ws,file), os.path.join(ws,file.replace('.','_')[:-4] + '.shp'))
Listing = arcpy.ListFeatureClasses()
arcpy.Merge_management(Listing,"MergedFile.shp")


If this doesn't work, I'm stumped.
0 Kudos
MichaelVolz
Esteemed Contributor
Are these raster datasets?  The merge_management documentation says that this data type is not supported.
0 Kudos
TylerJones2
New Contributor
Caleb,

   I went and renamed the files by hand and it worked like a charm. Thanks for the help. Now I just have to get the rename code to work so I don't have to do them all by hand.
0 Kudos
by Anonymous User
Not applicable
Caleb,

   I went and renamed the files by hand and it worked like a charm. Thanks for the help. Now I just have to get the rename code to work so I don't have to do them all by hand.


I guess I should have mentioned that code was not thoroughly tested.  I copied several of the names of your shapefiles and renamed them as text files and ran this code.  It did successfully convert those txt files to shp, but I do not know how that would work with real shapefiles.  It may work with taking out the wild card that I had in the first time. I also changed it to ListFeatureClasses() which I do not know why I didn't do that the first time.

import arcpy, os
arcpy.env.workspace = "D:/eCognition Results/Landcover/Buildings"
for file in arcpy.ListFeatureClasses():
    os.rename(os.path.join(ws,file), os.path.join(ws,file.replace('.','_')[:-4] + '.shp'))
Listing = arcpy.ListFeatureClasses()
arcpy.Merge_management(Listing,"MergedFile.shp")
0 Kudos
by Anonymous User
Not applicable
If THAT doesn't work (renaming shapefiles may not have been a good idea to begin with), here is my last idea:

import arcpy, os
ws = "D:/eCognition Results/Landcover/Buildings"
arcpy.env.workspace = ws
out = "D:/eCognition Results/Landcover/Buildings/Outputs"
for file in arcpy.ListFeatureClasses():
    arcpy.CopyFeatures_management(file, os.path.join(out,file.replace('.','_')[:-4] + '.shp'))
arcpy.env.workspace = out   
Listing = arcpy.ListFeatureClasses()
arcpy.Merge_management(Listing,"MergedFile.shp")


If this does not work, Python: 1,  Me: 0
0 Kudos
TylerJones2
New Contributor
I tried the first one and the logic seems to make sense to me but I get an 'exeptions.NameError'>: name 'ws' is not defined' So I am looking over the rename function to see what those parameters are all about. Can't be far from it now. Again man thanks for all the help.
0 Kudos
by Anonymous User
Not applicable
I tried the first one and the logic seems to make sense to me but I get an   'exeptions.NameError'>: name 'ws' is not defined' So I am looking over the rename function to see what those parameters are all about. Can't be far from it now. Again man thanks for all the help.


Ah! That is my fault! I had a ws variable in my test script on my end. Try this:

import arcpy, os
ws = "D:/eCognition Results/Landcover/Buildings"
arcpy.env.workspace = ws
for file in arcpy.ListFeatureClasses():
    os.rename(os.path.join(ws,file), os.path.join(ws,file.replace('.','_')[:-4] + '.shp'))
Listing = arcpy.ListFeatureClasses()
arcpy.Merge_management(Listing,"MergedFile.shp")


The problem was I had a variable set for the workspace and used that to append the paths and forgot to do that in yours. If you copied and pasted what I had, that was probably the problem the whole time! Sorry!
0 Kudos
TylerJones2
New Contributor
Works!
Only thing is that it only renames the .shp files and none of the associated files (.dbf,.prj, ect.) so the merge gives an error saying it cannot be completed probably because it cannot see the associated filetypes. However, I assume that I can just rewrite this for each of the filetypes and once they are all renamed the merge should work as expected? Fingers crossed.
0 Kudos
by Anonymous User
Not applicable
Works!
Only thing is that it only renames the .shp files and none of the associated files (.dbf,.prj, ect.) so the merge gives an error saying it cannot be completed probably because it cannot see the associated filetypes. However, I assume that I can just rewrite this for each of the filetypes and once they are all renamed the merge should work as expected? Fingers crossed.


I would just copy the shapefiles with a different name into another folder, much safer:

import arcpy, os
ws = "D:/eCognition Results/Landcover/Buildings"
arcpy.env.workspace = ws
out = "D:/eCognition Results/Landcover/Buildings/Outputs"
for file in arcpy.ListFeatureClasses():
    arcpy.CopyFeatures_management(file, os.path.join(out,file.replace('.','_')[:-4] + '.shp'))
arcpy.env.workspace = out   
Listing = arcpy.ListFeatureClasses()
arcpy.Merge_management(Listing,"MergedFile.shp")


This *should work.