Python script to separate shapefile based on type (Point, Line and Polygon)

Опубликовано: 16 Июль 2026
на канале: Umar Yusuf
636
3

Move Shapefile into sub folders based on type (Point, Line and Polygon) with Python.
Related Blog post: https://umar-yusuf.blogspot.com/2021/...

The Code
--------------
import glob
import shapefile # pip install pyshp
import shutil

Paths to respective shp folders...
point_folder = r'C:\Users\Yusuf_08039508010\Desktop\Move SHP\SHP folder_2\points SHP'
line_folder = r'C:\Users\Yusuf_08039508010\Desktop\Move SHP\SHP folder_2\lines SHP'
polygon_folder = r'C:\Users\Yusuf_08039508010\Desktop\Move SHP\SHP folder_2\polygon SHP'

Read all the shp into a list...
shp_files = glob.glob(r'C:\Users\Yusuf_08039508010\Desktop\Move SHP\SHP folder_1\*.shp')

for shp in shp_files:
Read the first shp from the list...
sf = shapefile.Reader(shp) # Can also use: open(shp_files[0], "rb")
print('Copying ', sf.shapeTypeName, 'shapefile...')

Check for shapefile type and copy to appropriate folder...
if sf.shapeTypeName == 'POINT':
shutil.copy(shp, point_folder)
shutil.copy(shp.replace('.shp', '.dbf'), point_folder)
shutil.copy(shp.replace('.shp', '.shx'), point_folder)
shutil.copy(shp.replace('.shp', '.prj'), point_folder)
shutil.copy(shp.replace('.shp', '.cpg'), point_folder)

elif sf.shapeTypeName == 'POLYLINE':
shutil.copy(shp, line_folder)
shutil.copy(shp.replace('.shp', '.dbf'), line_folder)
shutil.copy(shp.replace('.shp', '.shx'), line_folder)
shutil.copy(shp.replace('.shp', '.prj'), line_folder)
shutil.copy(shp.replace('.shp', '.cpg'), line_folder)

elif sf.shapeTypeName == 'POLYGON':
shutil.copy(shp, polygon_folder)
shutil.copy(shp.replace('.shp', '.dbf'), polygon_folder)
shutil.copy(shp.replace('.shp', '.shx'), polygon_folder)
shutil.copy(shp.replace('.shp', '.prj'), polygon_folder)
shutil.copy(shp.replace('.shp', '.cpg'), polygon_folder)

else:
print('This is niether point, line or polygon shapefile...')

print('Done...')