Arne pointed out that all the code samples out there iterate through each feature in a shapefile and add them to the merged file. He says this method is slow. I agree to an extent (no pun intended). However, at some point the underlying shapefile library MUST iterate through each feature in order to generate the summary information, namely the bounding box, required to write a valid shapefile header. But it is theoretically slightly more efficient to wait until the merge is finished so there is only one iteration cycle. At the very least, waiting till the end requires less code.
The following example merges all the shapefiles in the current directory into one file and it is quite fast.
# Merge a bunch of shapefiles with attributes quickly! import glob import shapefile files = glob.glob("*.shp") w = shapefile.Writer() for f in files: r = shapefile.Reader(f) w._shapes.extend(r.shapes()) w.records.extend(r.records()) w.fields = list(r.fields) w.save("merged")
No comments:
Post a Comment