Merge Multiple Shapefiles into a Single File
This is a Python script that automates the task of merging multiple shapefiles into a single file. This can be useful if you have multiple shapefiles that represent different features or layers, and you want to combine them into a single file for easier analysis or visualization.
import pandas as pd
import fiona
from shapely.geometry import shape
# Set the directory where the shapefiles are located
shapefile_dir = '/path/to/shapefiles/'
# Create an empty list to store the merged shapefile
merged_shapes = []
# Iterate through the shapefiles in the directory
for shapefile in os.listdir(shapefile_dir):
if shapefile.endswith('.shp'):
# Open the shapefile using Fiona
with fiona.open(shapefile_dir + shapefile) as f:
# Iterate through the features in the shapefile
for feature in f:
# Convert the feature to a Shapely object
feature_shape = shape(feature['geometry'])
# Add the feature to the list of merged shapes
merged_shapes.append(feature_shape)
# Create a new Shapefile using Fiona
with fiona.open('merged_shapefile.shp', 'w', driver='ESRI Shapefile', crs=f.crs, schema=f.schema) as f:
# Iterate through the list of merged shapes
for shape in merged_shapes:
# Convert the Shapely shape back to a Fiona feature and write it to the Shapefile
f.write({
'geometry': shape.__geo_interface__,
'properties': {}
})
print('Shapefile merge complete!')
To use this script, you will need to have the pandas
, fiona
, and shapely
libraries installed. You will also need to have the shapefiles that you want to merge saved in a directory on your computer.
To use this script, simply change the shapefile_dir
variable to the directory where your shapefiles are stored, and run the script. The merged shapefile will be saved in the same directory as the script, with the name merged_shapefile.shp
.
The first thing we do is import three libraries that we will need for this script. pandas
is a library for working with data, fiona
is a library for reading and writing geospatial data, and shapely
is a library for working with geometric shapes.
Next, we set a variable called shapefile_dir
to the directory where our shapefiles are stored. This is the location on our computer where the script will look for the shapefiles that we want to merge.
We also create an empty list called merged_shapes
, which we will use to store all of the shapes that we want to merge.
Next, we use a for
loop to iterate through all of the files in the shapefile_dir
directory. We only want to look at shapefiles, so we use an if
statement to check if each file ends with the .shp
file extension. If it does, we open the shapefile using Fiona.
Inside the with
block, we use another for
loop to iterate through all of the features in the shapefile. Each feature represents a single shape, such as a polygon or a line. We use the shape
function from the shapely
library to convert each feature to a Shapely object. Then, we add the Shapely object to our merged_shapes
list.
After we have iterated through all of the shapefiles and added all of the shapes to the merged_shapes
list, we use Fiona again to create a new shapefile called merged_shapefile.shp
.
Inside the with
block, we use another for
loop to iterate through the list of merged shapes. For each shape, we use the __geo_interface__
attribute to convert the Shapely shape back to a Fiona feature. Then, we write the feature to the new shapefile using the write
method.
Finally, we print a message to the console to let the user know that the shapefile merge is complete.