Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

up vote 7 down vote accepted

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))
share|improve this answer
    
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. –  user two seven two three nine Apr 11 at 17:14
    
got it, thank you. –  user two seven two three nine Apr 13 at 0:20

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

share|improve this answer
    
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 Apr 24 at 21:33

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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