python-osw-validation 0.2.15__py3-none-any.whl → 0.3.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- python_osw_validation/__init__.py +102 -26
- python_osw_validation/extracted_data_validator.py +49 -5
- python_osw_validation/schema/opensidewalks.edges.schema-0.3.json +1937 -0
- python_osw_validation/schema/opensidewalks.lines.schema-0.3.json +328 -0
- python_osw_validation/schema/opensidewalks.nodes.schema-0.3.json +437 -0
- python_osw_validation/schema/opensidewalks.points.schema-0.3.json +474 -0
- python_osw_validation/schema/opensidewalks.polygons.schema-0.3.json +525 -0
- python_osw_validation/schema/opensidewalks.schema-0.3.json +6153 -0
- python_osw_validation/schema/{Polygon_schema.json → opensidewalks.zones.schema-0.3.json} +142 -161
- python_osw_validation/version.py +1 -1
- {python_osw_validation-0.2.15.dist-info → python_osw_validation-0.3.1.dist-info}/METADATA +16 -5
- python_osw_validation-0.3.1.dist-info/RECORD +17 -0
- {python_osw_validation-0.2.15.dist-info → python_osw_validation-0.3.1.dist-info}/WHEEL +1 -1
- python_osw_validation/schema/Linestring_schema.json +0 -2334
- python_osw_validation/schema/Point_schema.json +0 -256
- python_osw_validation/schema/opensidewalks.schema.json +0 -5464
- python_osw_validation-0.2.15.dist-info/RECORD +0 -14
- {python_osw_validation-0.2.15.dist-info → python_osw_validation-0.3.1.dist-info/licenses}/LICENSE +0 -0
- {python_osw_validation-0.2.15.dist-info → python_osw_validation-0.3.1.dist-info}/top_level.txt +0 -0
|
@@ -12,6 +12,14 @@ from .version import __version__
|
|
|
12
12
|
from .helpers import _feature_index_from_error, _pretty_message, _rank_for
|
|
13
13
|
|
|
14
14
|
SCHEMA_PATH = os.path.join(os.path.dirname(__file__), 'schema')
|
|
15
|
+
DEFAULT_DATASET_SCHEMAS = {
|
|
16
|
+
"edges": os.path.join(SCHEMA_PATH, 'opensidewalks.edges.schema-0.3.json'),
|
|
17
|
+
"lines": os.path.join(SCHEMA_PATH, 'opensidewalks.lines.schema-0.3.json'),
|
|
18
|
+
"nodes": os.path.join(SCHEMA_PATH, 'opensidewalks.nodes.schema-0.3.json'),
|
|
19
|
+
"points": os.path.join(SCHEMA_PATH, 'opensidewalks.points.schema-0.3.json'),
|
|
20
|
+
"polygons": os.path.join(SCHEMA_PATH, 'opensidewalks.polygons.schema-0.3.json'),
|
|
21
|
+
"zones": os.path.join(SCHEMA_PATH, 'opensidewalks.zones.schema-0.3.json'),
|
|
22
|
+
}
|
|
15
23
|
|
|
16
24
|
|
|
17
25
|
class ValidationResult:
|
|
@@ -33,17 +41,18 @@ class ValidationResult:
|
|
|
33
41
|
|
|
34
42
|
|
|
35
43
|
class OSWValidation:
|
|
36
|
-
|
|
44
|
+
default_schema_file_path_03 = os.path.join(SCHEMA_PATH, 'opensidewalks.schema-0.3.json')
|
|
37
45
|
|
|
38
46
|
# per-geometry defaults
|
|
39
|
-
default_point_schema =
|
|
40
|
-
default_line_schema =
|
|
41
|
-
default_polygon_schema =
|
|
47
|
+
default_point_schema = DEFAULT_DATASET_SCHEMAS['points']
|
|
48
|
+
default_line_schema = DEFAULT_DATASET_SCHEMAS['edges']
|
|
49
|
+
default_polygon_schema = DEFAULT_DATASET_SCHEMAS['zones']
|
|
42
50
|
|
|
43
51
|
def __init__(
|
|
44
52
|
self,
|
|
45
53
|
zipfile_path: str,
|
|
46
54
|
schema_file_path=None,
|
|
55
|
+
schema_paths: Optional[Dict[str, str]] = None,
|
|
47
56
|
point_schema_path: Optional[str] = None,
|
|
48
57
|
line_schema_path: Optional[str] = None,
|
|
49
58
|
polygon_schema_path: Optional[str] = None,
|
|
@@ -57,10 +66,15 @@ class OSWValidation:
|
|
|
57
66
|
# Legacy single schema (if set, used for all)
|
|
58
67
|
self.schema_file_path = schema_file_path # may be None
|
|
59
68
|
|
|
69
|
+
# Dataset-specific schemas (override via schema_paths)
|
|
70
|
+
self.dataset_schema_paths = {**DEFAULT_DATASET_SCHEMAS}
|
|
71
|
+
if schema_paths:
|
|
72
|
+
self.dataset_schema_paths.update({k: v for k, v in schema_paths.items() if v})
|
|
73
|
+
|
|
60
74
|
# Per-geometry schemas (with defaults)
|
|
61
|
-
self.point_schema_path = point_schema_path or self.
|
|
62
|
-
self.line_schema_path = line_schema_path or self.
|
|
63
|
-
self.polygon_schema_path = polygon_schema_path or self.
|
|
75
|
+
self.point_schema_path = point_schema_path or self.dataset_schema_paths['points']
|
|
76
|
+
self.line_schema_path = line_schema_path or self.dataset_schema_paths['edges']
|
|
77
|
+
self.polygon_schema_path = polygon_schema_path or self.dataset_schema_paths['zones']
|
|
64
78
|
|
|
65
79
|
# ----------------------------
|
|
66
80
|
# Utilities & helpers
|
|
@@ -92,6 +106,54 @@ class OSWValidation:
|
|
|
92
106
|
self.log_errors(f"Could not create set for column '{col}' in {filekey}.", filekey, None)
|
|
93
107
|
return set()
|
|
94
108
|
|
|
109
|
+
def _schema_key_from_text(self, text: Optional[str]) -> Optional[str]:
|
|
110
|
+
"""Return dataset key (edges/nodes/points/lines/polygons/zones) if mentioned in text."""
|
|
111
|
+
if not text:
|
|
112
|
+
return None
|
|
113
|
+
lower = text.lower()
|
|
114
|
+
aliases = {
|
|
115
|
+
"edges": ("edge", "edges"),
|
|
116
|
+
"lines": ("line", "lines", "linestring"),
|
|
117
|
+
"nodes": ("node", "nodes"),
|
|
118
|
+
"points": ("point", "points"),
|
|
119
|
+
"polygons": ("polygon", "polygons", "area"),
|
|
120
|
+
"zones": ("zone", "zones"),
|
|
121
|
+
}
|
|
122
|
+
for key, variants in aliases.items():
|
|
123
|
+
if any(alias in lower for alias in variants):
|
|
124
|
+
return key
|
|
125
|
+
return None
|
|
126
|
+
|
|
127
|
+
def _contains_disallowed_features_for_02(self, geojson_data: Dict[str, Any]) -> set:
|
|
128
|
+
"""Detect Tree coverage or Custom content in legacy 0.2 datasets.
|
|
129
|
+
|
|
130
|
+
Returns a set of reason tags, e.g. {"tree", "custom_ext", "custom_token"}.
|
|
131
|
+
Empty set means no 0.2-only violations detected.
|
|
132
|
+
"""
|
|
133
|
+
reasons = set()
|
|
134
|
+
for feat in geojson_data.get("features", []):
|
|
135
|
+
props = feat.get("properties") or {}
|
|
136
|
+
geom = feat.get("geometry") or {}
|
|
137
|
+
geom_type = geom.get("type") if isinstance(geom, dict) else None
|
|
138
|
+
is_point = isinstance(geom_type, str) and geom_type.lower() == "point"
|
|
139
|
+
|
|
140
|
+
val = props.get("natural")
|
|
141
|
+
if isinstance(val, str) and val.strip().lower() in {"tree", "wood"}:
|
|
142
|
+
reasons.add("tree")
|
|
143
|
+
if any(k in props for k in ("leaf_cycle", "leaf_type")):
|
|
144
|
+
reasons.add("tree")
|
|
145
|
+
for k, v in props.items():
|
|
146
|
+
target = ""
|
|
147
|
+
if isinstance(v, str):
|
|
148
|
+
target = v.lower()
|
|
149
|
+
elif isinstance(k, str):
|
|
150
|
+
target = k.lower()
|
|
151
|
+
if any(tok in target for tok in ["custom point", "custom_point", "custompoint",
|
|
152
|
+
"custom line", "custom_line", "customline",
|
|
153
|
+
"custom polygon", "custom_polygon", "custompolygon"]):
|
|
154
|
+
reasons.add("custom_token")
|
|
155
|
+
return reasons
|
|
156
|
+
|
|
95
157
|
# ----------------------------
|
|
96
158
|
# Schema selection
|
|
97
159
|
# ----------------------------
|
|
@@ -118,25 +180,12 @@ class OSWValidation:
|
|
|
118
180
|
def pick_schema_for_file(self, file_path: str, geojson_data: Dict[str, Any]) -> str:
|
|
119
181
|
if self.schema_file_path:
|
|
120
182
|
return self.schema_file_path
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
if gtype == 'LineString':
|
|
128
|
-
return self.line_schema_path
|
|
129
|
-
if gtype == 'Polygon':
|
|
130
|
-
return self.polygon_schema_path
|
|
131
|
-
except Exception:
|
|
132
|
-
pass
|
|
133
|
-
lower = os.path.basename(file_path).lower()
|
|
134
|
-
if 'node' in lower or 'point' in lower:
|
|
135
|
-
return self.point_schema_path
|
|
136
|
-
if 'edge' in lower or 'line' in lower:
|
|
137
|
-
return self.line_schema_path
|
|
138
|
-
if 'zone' in lower or 'polygon' in lower or 'area' in lower:
|
|
139
|
-
return self.polygon_schema_path
|
|
183
|
+
|
|
184
|
+
basename = os.path.basename(file_path)
|
|
185
|
+
schema_key = self._schema_key_from_text(basename)
|
|
186
|
+
if schema_key and schema_key in self.dataset_schema_paths:
|
|
187
|
+
return self.dataset_schema_paths[schema_key]
|
|
188
|
+
|
|
140
189
|
return self.line_schema_path
|
|
141
190
|
|
|
142
191
|
# ----------------------------
|
|
@@ -432,6 +481,33 @@ class OSWValidation:
|
|
|
432
481
|
return False
|
|
433
482
|
except OSError:
|
|
434
483
|
return False
|
|
484
|
+
|
|
485
|
+
schema_url = geojson_data.get('$schema')
|
|
486
|
+
if isinstance(schema_url, str) and '0.2/schema.json' in schema_url:
|
|
487
|
+
reasons = self._contains_disallowed_features_for_02(geojson_data)
|
|
488
|
+
if reasons:
|
|
489
|
+
dataset_key = self._schema_key_from_text(file_path) or "data"
|
|
490
|
+
custom_label_map = {
|
|
491
|
+
"edges": "Custom Edge",
|
|
492
|
+
"lines": "Custom Line",
|
|
493
|
+
"polygons": "Custom Polygon",
|
|
494
|
+
"zones": "Custom Polygon/Zone",
|
|
495
|
+
"points": "Custom Point",
|
|
496
|
+
"nodes": "Custom Node",
|
|
497
|
+
}
|
|
498
|
+
parts = []
|
|
499
|
+
if "tree" in reasons:
|
|
500
|
+
parts.append("Tree coverage")
|
|
501
|
+
if "custom_ext" in reasons or "custom_token" in reasons:
|
|
502
|
+
parts.append(custom_label_map.get(dataset_key, "Custom content"))
|
|
503
|
+
msg = f"0.2 schema does not support " + " and ".join(parts)
|
|
504
|
+
self.log_errors(
|
|
505
|
+
message=msg,
|
|
506
|
+
filename=os.path.basename(file_path),
|
|
507
|
+
feature_index=None,
|
|
508
|
+
)
|
|
509
|
+
return False
|
|
510
|
+
|
|
435
511
|
schema_path = self.pick_schema_for_file(file_path, geojson_data)
|
|
436
512
|
schema = self.load_osw_schema(schema_path)
|
|
437
513
|
validator = jsonschema_rs.Draft7Validator(schema)
|
|
@@ -29,6 +29,24 @@ OSW_DATASET_FILES = {
|
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
ALLOWED_OSW_03_FILENAMES = (
|
|
33
|
+
"opensidewalks.edges.geojson",
|
|
34
|
+
"opensidewalks.lines.geojson",
|
|
35
|
+
"opensidewalks.nodes.geojson",
|
|
36
|
+
"opensidewalks.points.geojson",
|
|
37
|
+
"opensidewalks.polygons.geojson",
|
|
38
|
+
"opensidewalks.zones.geojson",
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
_FILENAME_TO_KEY = {
|
|
42
|
+
"opensidewalks.edges.geojson": "edges",
|
|
43
|
+
"opensidewalks.lines.geojson": "lines",
|
|
44
|
+
"opensidewalks.nodes.geojson": "nodes",
|
|
45
|
+
"opensidewalks.points.geojson": "points",
|
|
46
|
+
"opensidewalks.polygons.geojson": "polygons",
|
|
47
|
+
"opensidewalks.zones.geojson": "zones",
|
|
48
|
+
}
|
|
49
|
+
|
|
32
50
|
|
|
33
51
|
class ExtractedDataValidator:
|
|
34
52
|
def __init__(self, extracted_dir: str):
|
|
@@ -45,15 +63,41 @@ class ExtractedDataValidator:
|
|
|
45
63
|
|
|
46
64
|
# Look for required files at the root level
|
|
47
65
|
geojson_files = glob.glob(os.path.join(self.extracted_dir, '*.geojson'))
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
66
|
+
nested_files = glob.glob(os.path.join(self.extracted_dir, '*', '*.geojson'))
|
|
67
|
+
for f in nested_files:
|
|
68
|
+
if f not in geojson_files:
|
|
69
|
+
geojson_files.append(f)
|
|
52
70
|
|
|
53
71
|
if not geojson_files:
|
|
54
72
|
self.error = 'No .geojson files found in the specified directory or its subdirectories.'
|
|
55
73
|
return False
|
|
56
74
|
|
|
75
|
+
basenames = [os.path.basename(f) for f in geojson_files]
|
|
76
|
+
is_osw_03 = any(name.startswith("opensidewalks.") for name in basenames)
|
|
77
|
+
|
|
78
|
+
if is_osw_03:
|
|
79
|
+
invalid_basenames = [bn for bn in basenames if bn not in ALLOWED_OSW_03_FILENAMES]
|
|
80
|
+
if invalid_basenames:
|
|
81
|
+
allowed_fmt = ", ".join(ALLOWED_OSW_03_FILENAMES)
|
|
82
|
+
self.error = f'Dataset contains non-standard file names. The only allowed file names are {{{allowed_fmt}}}'
|
|
83
|
+
return False
|
|
84
|
+
|
|
85
|
+
duplicate_keys = []
|
|
86
|
+
for filename in ALLOWED_OSW_03_FILENAMES:
|
|
87
|
+
occurrences = [f for f in geojson_files if os.path.basename(f) == filename]
|
|
88
|
+
if len(occurrences) > 1:
|
|
89
|
+
duplicate_keys.append(_FILENAME_TO_KEY.get(filename, filename))
|
|
90
|
+
elif len(occurrences) == 1:
|
|
91
|
+
self.files.append(occurrences[0])
|
|
92
|
+
|
|
93
|
+
if duplicate_keys:
|
|
94
|
+
self.error = f'Multiple .geojson files of the same type found: {", ".join(duplicate_keys)}.'
|
|
95
|
+
return False
|
|
96
|
+
|
|
97
|
+
self.externalExtensions.extend([item for item in geojson_files if item not in self.files])
|
|
98
|
+
gc.collect()
|
|
99
|
+
return True
|
|
100
|
+
|
|
57
101
|
required_files = [key for key, value in OSW_DATASET_FILES.items() if value['required']]
|
|
58
102
|
optional_files = [key for key, value in OSW_DATASET_FILES.items() if not value['required']]
|
|
59
103
|
missing_files = []
|
|
@@ -106,7 +150,7 @@ class ExtractedDataValidator:
|
|
|
106
150
|
|
|
107
151
|
finally:
|
|
108
152
|
# Cleanup large lists and call garbage collector
|
|
109
|
-
del geojson_files, required_files, optional_files, missing_files, duplicate_files
|
|
153
|
+
del geojson_files, basenames, required_files, optional_files, missing_files, duplicate_files
|
|
110
154
|
gc.collect()
|
|
111
155
|
|
|
112
156
|
return True
|