Repository navigation
Expand file tree
/
Copy pathtempCodeRunnerFile.py
More file actions
206 lines (165 loc) · 7.71 KB
/
Copy pathtempCodeRunnerFile.py
File metadata and controls
206 lines (165 loc) · 7.71 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import json
from pathlib import Path
def load_data(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
return json.load(file)
def save_data(data, file_path):
with open(file_path, 'w', encoding='utf-8') as file:
json.dump(data, file, indent=4)
def find_and_rearrange(features):
if not features:
return []
ordered_features = []
remaining_features = features[:]
current_feature = features[-1]
for feature in features:
is_unique = True
for other in features:
# if feature['first_coordinate'] == other['last_coordinate']:
if abs(feature['first_coordinate'][0] - other['last_coordinate'][0]) < 0.001 and abs(feature['first_coordinate'][1] - other['last_coordinate'][1]) < 0.001:
is_unique = False
break
if is_unique:
current_feature = feature
break
while current_feature:
ordered_features.append(current_feature)
remaining_features.remove(current_feature)
next_feature = None
for feature in remaining_features:
if feature['first_coordinate'] == current_feature['last_coordinate']:
next_feature = feature
break
current_feature = next_feature
while current_feature:
ordered_features.append(current_feature)
remaining_features.remove(current_feature)
next_feature = None
for feature in remaining_features:
if abs(feature['first_coordinate'][0] - current_feature['last_coordinate'][0]) < 0.001 and abs(feature['first_coordinate'][1] - current_feature['last_coordinate'][1]) < 0.001:
next_feature = feature
break
current_feature = next_feature
if len(remaining_features) != 0:
ordered_features.reverse()
for feature in ordered_features:
temp = feature['first_coordinate']
feature['first_coordinate'] = feature['last_coordinate']
feature['last_coordinate'] = temp
current_feature = ordered_features[-1]
ordered_features.remove(current_feature)
while current_feature:
ordered_features.append(current_feature)
if current_feature in remaining_features:
remaining_features.remove(current_feature)
next_feature = None
for feature in remaining_features:
if abs(feature['first_coordinate'][0] - current_feature['last_coordinate'][0]) < 0.001 and abs(feature['first_coordinate'][1] - current_feature['last_coordinate'][1]) < 0.001:
next_feature = feature
break
current_feature = next_feature
if len(remaining_features) != 0:
for feature in remaining_features:
temp = feature['first_coordinate']
feature['first_coordinate'] = feature['last_coordinate']
feature['last_coordinate'] = temp
current_feature = ordered_features[-1]
ordered_features.remove(current_feature)
while current_feature:
ordered_features.append(current_feature)
if current_feature in remaining_features:
remaining_features.remove(current_feature)
next_feature = None
for feature in remaining_features:
if abs(feature['first_coordinate'][0] - current_feature['last_coordinate'][0]) < 0.003 and abs(feature['first_coordinate'][1] - current_feature['last_coordinate'][1]) < 0.003:
next_feature = feature
break
current_feature = next_feature
if len(remaining_features) != 0:
for feature in remaining_features:
temp = feature['first_coordinate']
feature['first_coordinate'] = feature['last_coordinate']
feature['last_coordinate'] = temp
current_feature = ordered_features[-1]
ordered_features.remove(current_feature)
while current_feature:
ordered_features.append(current_feature)
if current_feature in remaining_features:
remaining_features.remove(current_feature)
next_feature = None
for feature in remaining_features:
print(feature['id'], feature['first_coordinate'][0], current_feature['last_coordinate'][0])
if abs(feature['first_coordinate'][0] - current_feature['last_coordinate'][0]) < 0.003 and abs(feature['first_coordinate'][1] - current_feature['last_coordinate'][1]) < 0.003:
next_feature = feature
break
current_feature = next_feature
return ordered_features
def extract_feature_coordinates(feature):
coords = feature['geometry']['coordinates']
return {
'first_coordinate': coords[0],
'last_coordinate': coords[-1],
'id': feature['properties']['id']
}
def are_coordinates_equal(coord1, coord2, tolerance=0.001):
return (abs(coord1[0] - coord2[0]) < tolerance and
abs(coord1[1] - coord2[1]) < tolerance)
def find_matching_feature(target_coords, target_id, features, used_indices):
for idx, feature in enumerate(features):
if idx in used_indices:
continue
if feature['properties']['id'] != target_id:
continue
coords = feature['geometry']['coordinates']
start_coord = coords[0]
end_coord = coords[-1]
if (are_coordinates_equal(start_coord, target_coords) or
are_coordinates_equal(end_coord, target_coords)):
return idx
return None
def reorder_features(geojson_data):
features = geojson_data['features']
coordinate_features = [extract_feature_coordinates(f) for f in features]
ordered_segments = find_and_rearrange(coordinate_features)
reordered_features = []
used_indices = set()
for segment in ordered_segments:
first_coord = segment['first_coordinate']
last_coord = segment['last_coordinate']
segment_id = segment['id']
idx = find_matching_feature(first_coord, segment_id, features, used_indices)
if idx is not None:
feature = features[idx]
coords = feature['geometry']['coordinates']
if are_coordinates_equal(coords[-1], first_coord):
feature = feature.copy()
feature['geometry'] = feature['geometry'].copy()
feature['geometry']['coordinates'] = list(reversed(coords))
reordered_features.append(feature)
used_indices.add(idx)
new_geojson = geojson_data.copy()
new_geojson['features'] = reordered_features
return new_geojson
def process_file(input_path, output_path):
"""Process a single GeoJSON file."""
try:
data = load_data(input_path)
processed_data = reorder_features(data)
save_data(processed_data, output_path)
print(f"Successfully processed: {input_path.name}")
original_count = len(data['features'])
processed_count = len(processed_data['features'])
print(f"Original features: {original_count}")
print(f"Features in output: {processed_count}")
print(f"Features filtered out: {original_count - processed_count}")
except Exception as e:
print(f"Error processing {input_path.name}: {str(e)}")
def main():
input_folder = './data'
output_folder = './transformed_data'
Path(output_folder).mkdir(parents=True, exist_ok=True)
for input_path in Path(input_folder).glob('*.json'):
output_path = Path(output_folder) / input_path.name
process_file(input_path, output_path)
if __name__ == "__main__":
main()