-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsartopo2faks.py
More file actions
234 lines (194 loc) · 8.21 KB
/
Copy pathsartopo2faks.py
File metadata and controls
234 lines (194 loc) · 8.21 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import json
import os
import sys
import geojson
import shapely
from shapely.geometry import shape
# Sink files to prepare output
sink_files = {
"Etterretningsreflekser.geojson": {"type": "FeatureCollection", "features": []},
"Linjer.geojson": {"type": "FeatureCollection", "features": []},
"Mobilspor.geojson": {"type": "FeatureCollection", "features": []},
"Punkter.geojson": {"type": "FeatureCollection", "features": []},
"Regioner.geojson": {"type": "FeatureCollection", "features": []},
"Soeksarealer.geojson": {"type": "FeatureCollection", "features": []},
"Sperret.geojson": {"type": "FeatureCollection", "features": []},
"Statistiske_reflekser.geojson": {"type": "FeatureCollection", "features": []},
}
# Mapping folders to sink files (Folder titles help categorize features)
folder_to_sink = {
"01 Etterretning": "Etterretningsreflekser.geojson",
"02 SPOR Mannskaper": "Mobilspor.geojson",
"04 SPOR Motorisert": "Mobilspor.geojson",
"03 SPOR Hund m/Fører": "Mobilspor.geojson",
"05 SPOR Luftfartøy": "Mobilspor.geojson",
# Add more folder mappings here if needed
}
def calculate_bounding_box(coordinates):
"""
Calculate the bounding box (minLat, maxLat, minLng, maxLng) for a given Polygon's coordinates.
"""
lats = [point[1] for linear_ring in coordinates for point in linear_ring]
lngs = [point[0] for linear_ring in coordinates for point in linear_ring]
return {
"minLat": min(lats),
"maxLat": max(lats),
"minLng": min(lngs),
"maxLng": max(lngs)
}
def derive_point_category(properties):
"""
Derives the 'category' field based on the given 'marker-symbol'.
Args:
marker_symbol (str): The marker-symbol property from the source.
Returns:
str: The resulting category.
"""
title = properties.get("title", "").lower()
marker_symbol = properties.get("marker-symbol", "").lower()
if "oppmøte" in title:
return "Oppmøtested"
if "cp" in marker_symbol or "ko" in title or "kommandoplass" in title:
return "Kommandoplass"
if "bosted" in title or "bopel" in title or "bopæl" in title:
return "Bosted"
if "funn" in title:
return "Funn av spor"
# Add more conditions or mappings as needed
return "Annet"
# --- Gjenkjent ---
# Oppmøtested
# Kommandoplass
# Bosted
# Funn av spor
# Annet
# --- Ikke gjenkjent ---
# Interesse av hund
# Hindring
# Ikke søkbart
# Mobilspor
# Observasjon
# Utkikkspunkt
# Sperrepost
def enrich_features(source_data):
"""
Enrich source features by calculating bounding boxes and adding optional relationships.
"""
enriched_features = []
transformed_properties = {}
for feature in source_data["features"]:
# Extract existing geometry and properties
geometry = feature.get("geometry")
properties = feature.get("properties", {})
# Skip features with missing or invalid geometry
if not geometry:
continue
feature_type = geometry.get("type", "")
feature_class = properties.get("class", "")
# Detect geometry type and add derived information
if feature_class == "Assignment":
if feature_type == "Polygon":
transformed_properties = {
"aid": feature.get("id", ""), # Map the unique id to aid
"title": properties.get("title", ""), # Use 'title' from source
"class": feature_class,
"category": "area",
# TODO: classify mission status
"missionStatus": "empty"
}
if feature_type == "LineString":
transformed_properties = {
"aid": feature.get("id", ""), # Map the unique id to aid
"title": properties.get("title", ""), # Use 'title' from source
"class": feature_class,
"category": "path",
# TODO: classify mission status
"missionStatus": "empty"
}
else:
# Not an Assignment
if feature_type == "Point":
# Map known fields to the new structure
transformed_properties = {
"aid": feature.get("id", ""), # Map the unique id to aid
"title": properties.get("title", ""), # Use 'title' from source
"class": feature_class,
"level": "Punkt", # Assign static value
"category": derive_point_category(properties),
}
if feature_type == "Polygon":
transformed_properties = {
"aid": feature.get("id", ""), # Map the unique id to aid
"title": properties.get("title", ""), # Use 'title' from source
"class": feature_class,
"category": "area",
"missionStatus": "empty"
}
if feature_type == "LineString":
transformed_properties = {
"aid": feature.get("id", ""), # Map the unique id to aid
"title": properties.get("title", ""), # Use 'title' from source
"class": feature_class,
"category": "path",
"missionStatus": "empty"
}
# Enrich the feature with new properties
enriched_features.append(
geojson.Feature(
geometry=geometry,
properties=transformed_properties
)
)
# Return the enriched data structure
return {"type": "FeatureCollection", "features": enriched_features}
def classify_features(source_data, output_folder):
"""
Classify features from the source data into appropriate sink files
and write the output to the specified output folder.
"""
# Enrich source features before classifying them
enriched_data = enrich_features(source_data)
for feature in enriched_data["features"]:
feature_type = feature["geometry"].get("type", "")
feature_class = feature["properties"].get("class", "")
feature_title = feature["properties"].get("title", "")
feature_geometry = feature.get("geometry", None)
# Classify based on feature class
if feature_class == "Folder":
# Folders themselves aren't geometric; categorize based on folder-to-sink mapping
if feature_title in folder_to_sink:
sink_files[folder_to_sink[feature_title]]["features"].append(feature)
elif feature_geometry:
if feature_type == "Point":
# All Point features go to Punkter.geojson
sink_files["Punkter.geojson"]["features"].append(feature)
elif feature_type == "LineString": # Line features
# All LineString features go to Punkter.geojson
sink_files["Linjer.geojson"]["features"].append(feature)
elif feature_type == "Polygon": # Polygon features
sink_files["Soeksarealer.geojson"]["features"].append(feature)
# Ensure the output folder exists (create it if necessary)
os.makedirs(output_folder, exist_ok=True)
# Write output to sink files
for sink_file, content in sink_files.items():
file_path = os.path.join(output_folder, sink_file) # Write each sink file to the output folder
with open(file_path, "w", encoding="utf-8") as f:
json.dump(content, f, ensure_ascii=False, indent=2)
print(f"Features successfully classified and written to sink files in '{output_folder}'!")
if __name__ == "__main__":
# Check command-line arguments
if len(sys.argv) != 3:
print("Usage: python sartopo2faks.py <source_file> <output_folder>")
sys.exit(1)
# Get arguments from the command line
source_file = sys.argv[1]
output_folder = sys.argv[2]
# Load the source GeoJSON data
try:
with open(source_file, "r", encoding="utf-8") as f:
source_data = json.load(f)
except Exception as e:
print(f"Error reading source file '{source_file}': {e}")
sys.exit(1)
# Run feature classification
classify_features(source_data, output_folder)