3

I have 30000 shapefiles (a1.shp to a30000.shp). They are all polygons. Each polygons has one and only one polygon feature in it. I need to Merge the polygons into 1000 shapefiles. My output would be 1000 polygons (each with 30 sequential polygons in it)

So output b1.shp = Merged polygons a1.shp to a30.shp

So output b2.shp = Merged polygons a31.shp to a60.shp

So output b3.shp = Merged polygons a61.shp to a90.shp

and so on up to...

b1000.shp = Merged polygons a29970.shp to a30000.shp

Looking for an ArcGIS model builder or GDAL solution.

3 Answers 3

8

It would be easier with .

for i in range(10000):
    pol_list = []
    for j in range(30):
        pol_list.append("a" + str(i*30 + j + 1) + ".shp")
    arcpy.Merge_management(pol_list, "b" + str(i+1) + ".shp")

EDIT:

for a feature class inside a geodatabase, you don't need the + ".shp" anymore, and you can define the workspace using :

arcpy.env.workspace = "C:/data/geodatabase.gdb"

or you can concatenate the full paths

outgdb = "C:/data/outgeodatabase.gdb/"
 ...
     arcpy.Merge_management(pol_list,outgdb + "b" + str(i+1))
1
  • Thanks, this is excellent. Could you possibly show me how to amend the code above so it would work with polygon features in a file geodatabase as opposed to shapefiles? Basically everything stays the same but now into and out of the scratch geodatabase as opposed to shapefiles. Commented Apr 11, 2014 at 17:14
0

In ModelBuilder, you use Iterate Feature Classes to loop through the shapefiles, then use Collect Values to send the iterator values to Merge.

It will look like this:

model_builder

1
  • I believe this model is incorrect despite the fact that it will work (but very inefficiently).The Iterator and Collects tool should be in a sub model with the Output Values exposed as a parameter. This sub model should then be in a master model feeding into the merge tool. As it stands Merge is being executed X number of times rather than just once.
    – Hornbydd
    Commented Apr 24, 2014 at 21:33
0

Modelbuilder!

Add iterator: FOR Set it to start at 10, iterate from 0-350 or so, in iterations of 30. Output of iterator will include Value variable.

Select by attribute, as so: FID = %Value%+1 or FID = %Value%+2 or FID = %Value%+3 or etc.

For the FIDS: First iteration: 0+1, 0+2, 0+3 Second iteration: 30+1, 30+2, 30+3 Third interation: 90+1, 90+2, 90+3

After the select features, merge them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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