-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02_GeoJsonConvertToWSG.py
112 lines (94 loc) · 3.96 KB
/
02_GeoJsonConvertToWSG.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import json
import sys
from pyproj import Transformer
def convert_coordinates(input_filename, output_filename=None):
"""
Convert GeoJSON coordinates from EPSG:26910 to EPSG:4326
Args:
input_filename (str): Path to input GeoJSON file
output_filename (str, optional): Path to output GeoJSON file. If None, will use input_filename with "_4326" suffix
"""
if output_filename is None:
output_filename = input_filename.replace('.geojson', '_4326.geojson')
# Create transformer from EPSG:26910 to EPSG:4326 (WGS84)
transformer = Transformer.from_crs("EPSG:26910", "EPSG:4326", always_xy=True)
# Read input GeoJSON
with open(input_filename, 'r') as f:
geojson_data = json.load(f)
# Update CRS property
if 'crs' in geojson_data:
geojson_data['crs'] = {
"type": "name",
"properties": {
"name": "EPSG:4326"
}
}
# Transform coordinates in all features
for feature in geojson_data['features']:
geometry = feature.get('geometry', {})
if not geometry:
continue
geom_type = geometry.get('type')
if geom_type == 'Point':
x, y = geometry['coordinates']
lon, lat = transformer.transform(x, y)
geometry['coordinates'] = [lon, lat]
elif geom_type == 'LineString':
new_coords = []
for coord in geometry['coordinates']:
x, y = coord
lon, lat = transformer.transform(x, y)
new_coords.append([lon, lat])
geometry['coordinates'] = new_coords
elif geom_type == 'Polygon':
new_rings = []
for ring in geometry['coordinates']:
new_coords = []
for coord in ring:
x, y = coord
lon, lat = transformer.transform(x, y)
new_coords.append([lon, lat])
new_rings.append(new_coords)
geometry['coordinates'] = new_rings
elif geom_type == 'MultiPoint':
new_points = []
for point in geometry['coordinates']:
x, y = point
lon, lat = transformer.transform(x, y)
new_points.append([lon, lat])
geometry['coordinates'] = new_points
elif geom_type == 'MultiLineString':
new_lines = []
for line in geometry['coordinates']:
new_coords = []
for coord in line:
x, y = coord
lon, lat = transformer.transform(x, y)
new_coords.append([lon, lat])
new_lines.append(new_coords)
geometry['coordinates'] = new_lines
elif geom_type == 'MultiPolygon':
new_polygons = []
for polygon in geometry['coordinates']:
new_rings = []
for ring in polygon:
new_coords = []
for coord in ring:
x, y = coord
lon, lat = transformer.transform(x, y)
new_coords.append([lon, lat])
new_rings.append(new_coords)
new_polygons.append(new_rings)
geometry['coordinates'] = new_polygons
# Write transformed GeoJSON to output file in condensed format
with open(output_filename, 'w') as f:
json.dump(geojson_data, f, separators=(',', ':'))
print(f"Converted {input_filename} from EPSG:26910 to EPSG:4326")
print(f"Output saved to {output_filename}")
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python geojson_converter.py input_file.geojson [output_file.geojson]")
sys.exit(1)
input_file = sys.argv[1]
output_file = sys.argv[2] if len(sys.argv) > 2 else None
convert_coordinates(input_file, output_file)