-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheck_proj_all_shapefiles_in_folder_and_reproject.py
51 lines (41 loc) · 1.91 KB
/
Check_proj_all_shapefiles_in_folder_and_reproject.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import geopandas as gpd
import os
def reproject_shapefiles(folder_path, target_epsg):
"""
Reproject all shapefiles in a given folder to the target EPSG code.
Parameters:
folder_path (str): The path to the folder containing the shapefiles.
target_epsg (int): The EPSG code to which the shapefiles should be reprojected.
Returns:
None
"""
# List all shapefiles in the folder
shapefiles = [f for f in os.listdir(folder_path) if f.endswith('.shp')]
# Loop through each shapefile
for shapefile in shapefiles:
shapefile_path = os.path.join(folder_path, shapefile)
try:
# Load the shapefile using GeoPandas
gdf = gpd.read_file(shapefile_path)
# Check and print the current EPSG code
if gdf.crs is not None:
current_epsg = gdf.crs.to_epsg()
print(f"Shapefile: {shapefile} | Current EPSG: {current_epsg}")
# Reproject if necessary
if current_epsg != target_epsg:
print(f"Reprojecting {shapefile} to EPSG:{target_epsg}...")
gdf = gdf.to_crs(epsg=target_epsg)
# Save the reprojected shapefile back to the folder
new_shapefile_path = os.path.join(folder_path, f"reprojected_{shapefile}")
gdf.to_file(new_shapefile_path)
print(f"Saved reprojected shapefile: {new_shapefile_path}")
else:
print(f"{shapefile} is already in EPSG:{target_epsg}")
else:
print(f"Shapefile: {shapefile} | CRS: Not defined")
except Exception as e:
print(f"Error processing {shapefile}: {e}")
# Example usage:
folder_path=r'T:\Abteilung 7\Müller\TEMP_Timis_Gewässerachsen\VISDOM\Bank_line'
target_epsg=25832
reproject_shapefiles(folder_path,target_epsg)