python-osw-validation 0.2.15__py3-none-any.whl → 0.3.0__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.
@@ -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
- default_schema_file_path = os.path.join(SCHEMA_PATH, 'opensidewalks.schema.json')
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 = os.path.join(SCHEMA_PATH, 'Point_schema.json')
40
- default_line_schema = os.path.join(SCHEMA_PATH, 'Linestring_schema.json')
41
- default_polygon_schema = os.path.join(SCHEMA_PATH, 'Polygon_schema.json')
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.default_point_schema
62
- self.line_schema_path = line_schema_path or self.default_line_schema
63
- self.polygon_schema_path = polygon_schema_path or self.default_polygon_schema
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,45 @@ 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]) -> bool:
128
+ """Detect Tree coverage or Custom Point/Line/Polygon in legacy 0.2 datasets."""
129
+ for feat in geojson_data.get("features", []):
130
+ props = feat.get("properties") or {}
131
+ val = props.get("natural")
132
+ if isinstance(val, str) and val.strip().lower() in {"tree", "wood"}:
133
+ return True
134
+ if any(k in props for k in ("leaf_cycle", "leaf_type")):
135
+ return True
136
+ for k, v in props.items():
137
+ target = ""
138
+ if isinstance(v, str):
139
+ target = v.lower()
140
+ elif isinstance(k, str):
141
+ target = k.lower()
142
+ if any(tok in target for tok in ["custom point", "custom_point", "custompoint",
143
+ "custom line", "custom_line", "customline",
144
+ "custom polygon", "custom_polygon", "custompolygon"]):
145
+ return True
146
+ return False
147
+
95
148
  # ----------------------------
96
149
  # Schema selection
97
150
  # ----------------------------
@@ -118,25 +171,12 @@ class OSWValidation:
118
171
  def pick_schema_for_file(self, file_path: str, geojson_data: Dict[str, Any]) -> str:
119
172
  if self.schema_file_path:
120
173
  return self.schema_file_path
121
- try:
122
- features = geojson_data.get('features', [])
123
- if features:
124
- gtype = (features[0].get('geometry') or {}).get('type')
125
- if gtype == 'Point':
126
- return self.point_schema_path
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
174
+
175
+ basename = os.path.basename(file_path)
176
+ schema_key = self._schema_key_from_text(basename)
177
+ if schema_key and schema_key in self.dataset_schema_paths:
178
+ return self.dataset_schema_paths[schema_key]
179
+
140
180
  return self.line_schema_path
141
181
 
142
182
  # ----------------------------
@@ -432,6 +472,17 @@ class OSWValidation:
432
472
  return False
433
473
  except OSError:
434
474
  return False
475
+
476
+ schema_url = geojson_data.get('$schema')
477
+ if isinstance(schema_url, str) and '0.2/schema.json' in schema_url:
478
+ if self._contains_disallowed_features_for_02(geojson_data):
479
+ self.log_errors(
480
+ message="0.2 schema does not support Tree coverage, Custom Point, Custom Line, and Custom Polygon",
481
+ filename=os.path.basename(file_path),
482
+ feature_index=None,
483
+ )
484
+ return False
485
+
435
486
  schema_path = self.pick_schema_for_file(file_path, geojson_data)
436
487
  schema = self.load_osw_schema(schema_path)
437
488
  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
- # If not found at the root, check inside folders
50
- if not geojson_files:
51
- geojson_files = glob.glob(os.path.join(self.extracted_dir, '*', '*.geojson'))
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