1

I have a dataset containing 171 shapefiles, their names as "shp0_0" to "shp0_171". I need to merge them into a single shapefile. When I use the merge tool and load all the .shp from the folder, it gives me random order, they are not listed by the filename order.

I need the output attribute table's FIDs to correspond with the input shapefiles: shp1 to be in the 1st row and have FID 0, shp2 to be in the 2nd row and have FID 1 and so on.

Is there a way to list all shapefiles to be merged numerically sorted by filename?

I work in arcmap 10.3 (using arcpy).

  • 1
    What GIS software are you using? – PolyGeo Nov 28 '17 at 13:53
  • 4
    It's not a great idea to use the FID for this purpose as you don't generally have any control over them. A better solution would be to add and populate a new field called filenum or rownum to each of the 171 shapefiles, then merge. – Alexander Nov 28 '17 at 14:03
  • I just wanted to emphasize @Alexander 's comment (more than I did by upvoting it): you should absolutely NOT USE FIDs as an ID field. Those numbers can and do change when a shapefile is edited. You should add a dedicated field for ID values. That would also make this question unnecessary. – Dan C Nov 28 '17 at 18:36
  • Yes, in the end I had to resort to Alexander's suggestion because I needed to do few more tasks with the shapefiles. Thank you. – M. Tomas Nov 28 '17 at 18:45
2

This will list the shapefiles, sort by number and merge. I dont know if it will give you the results you want though, you will have to try:

import arcpy

shapefile_folder = r'C:\folder' #Change to your folder containing the shapefiles
arcpy.env.workspace = shapefile_folder
shapelist = sorted(arcpy.ListFeatureClasses(), key = lambda x: int(x.split('.')[0][5:]))

arcpy.Merge_management(inputs=shapelist, output='Shapefiles_merge.shp')

The code can be executed in the Python window.

|improve this answer|||||
  • I tried to run the script and it failed saying: Runtime error Traceback (most recent call last): File "<string>", line 5, in <module> File "<string>", line 5, in <lambda> ValueError: invalid literal for int() with base 10: '0_1' The real filenames of the shapefiles are from "GPL0.shp" to "GPL0_170.shp" – M. Tomas Nov 28 '17 at 14:25
  • Rename GPL0 to GPL0_0 and try again, i have changed the code – BERA Nov 28 '17 at 14:27
  • This time it worked perfectly. Thank you very much. I'd like to learn from this, what does this line do? shapelist = sorted(arcpy.ListFeatureClasses(), key = lambda x: int(x.split('.')[0][5:])) Especially after key = – M. Tomas Nov 28 '17 at 14:33
  • Nice! The sorting of the shapefile list is done using a lambda function. See for example stackoverflow.com/questions/8966538/… – BERA Nov 28 '17 at 14:49
  • Without it it would sort the shapefiles alphabetical and not by the numbers – BERA Nov 28 '17 at 14:56

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.