File size: 882 Bytes
baf9496
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import geopandas as gpd
import sys

def clean_geometries(gdf):
    # Check and fix invalid geometries
    gdf['geometry'] = gdf['geometry'].buffer(0)  # This can help fix some topology issues
    gdf = gdf[gdf.is_valid]  # Remove invalid geometries
    return gdf

def union_shapefiles(shapefile1, shapefile2, output_shapefile):
    gdf1 = gpd.read_file(shapefile1)
    gdf2 = gpd.read_file(shapefile2)

    # Clean geometries
    gdf1 = clean_geometries(gdf1)
    gdf2 = clean_geometries(gdf2)

    # Perform the union
    union_gdf = gpd.overlay(gdf1, gdf2, how='union')

    # Save the result to a new shapefile
    union_gdf.to_file(output_shapefile)

# Example usage
if __name__ == "__main__":
    shapefile1 = sys.argv[1]
    shapefile2 = sys.argv[2]
    output_shapefile = sys.argv[3]
    union_shapefiles(shapefile1, shapefile2, output_shapefile)