python-osw-validation 0.2.4__py3-none-any.whl → 0.2.6__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.
@@ -1,10 +1,11 @@
1
1
  import os
2
+ import gc
2
3
  import json
3
4
  import jsonschema
4
5
  from typing import Dict, Any, Optional, List
5
6
  import geopandas as gpd
6
7
  from .zipfile_handler import ZipFileHandler
7
- from .extracted_data_validator import ExtractedDataValidator, OSW_dataset_files
8
+ from .extracted_data_validator import ExtractedDataValidator, OSW_DATASET_FILES
8
9
  from .version import __version__
9
10
 
10
11
  SCHEMA_PATH = os.path.join(os.path.dirname(__file__), 'schema')
@@ -29,7 +30,7 @@ class OSWValidation:
29
30
  self.schema_file_path = schema_file_path
30
31
 
31
32
  def load_osw_schema(self, schema_path: str) -> Dict[str, Any]:
32
- '''Load OSW Schema'''
33
+ """Load OSW Schema"""
33
34
  try:
34
35
  with open(schema_path, 'r') as file:
35
36
  return json.load(file)
@@ -40,12 +41,12 @@ class OSWValidation:
40
41
  def are_ids_unique(self, gdf):
41
42
  """Check for duplicate values in the _id field"""
42
43
  duplicates = gdf[gdf.duplicated('_id', keep=False)]['_id'].unique()
43
-
44
44
  is_valid = len(duplicates) == 0
45
-
46
45
  return is_valid, list(duplicates)
47
46
 
48
47
  def validate(self, max_errors=20) -> ValidationResult:
48
+ zip_handler = None
49
+ OSW_DATASET = {}
49
50
  try:
50
51
  # Extract the zipfile
51
52
  zip_handler = ZipFileHandler(self.zipfile_path)
@@ -60,43 +61,42 @@ class OSWValidation:
60
61
  if not validator.is_valid():
61
62
  self.errors.append(validator.error)
62
63
  return ValidationResult(False, self.errors)
64
+
63
65
  for file in validator.files:
64
66
  file_path = os.path.join(file)
65
- if not self.validate_osw_errors(file_path, max_errors):
67
+ if not self.validate_osw_errors(file_path=str(file_path), max_errors=max_errors):
66
68
  break
67
69
 
68
70
  if self.errors:
69
- zip_handler.remove_extracted_files()
70
71
  return ValidationResult(False, self.errors)
71
72
 
72
73
  # Validate data integrity
73
- OSW_dataset = {}
74
74
  for file in validator.files:
75
75
  file_path = os.path.join(file)
76
- osw_file = next((osw_file_any for osw_file_any in OSW_dataset_files.keys() if osw_file_any in file_path), '')
77
- OSW_dataset[osw_file] = gpd.read_file(file_path)
76
+ osw_file = next((osw_file_any for osw_file_any in OSW_DATASET_FILES.keys() if osw_file_any in file_path), '')
77
+ OSW_DATASET[osw_file] = gpd.read_file(file_path)
78
78
 
79
79
  # Are all id's unique in each file? No need to check uniqueness across files yet since we do not have a global OSW ID format yet
80
- for osw_file in OSW_dataset:
81
- is_valid, duplicates = self.are_ids_unique(OSW_dataset[osw_file])
80
+ for osw_file in OSW_DATASET:
81
+ is_valid, duplicates = self.are_ids_unique(OSW_DATASET[osw_file])
82
82
  if not is_valid:
83
83
  self.errors.append(f"Duplicate _id's found in {osw_file} : {duplicates}")
84
84
 
85
85
  # Create sets of node id's and foreign keys to be used in validation
86
- if "nodes" in OSW_dataset:
87
- node_ids = set(OSW_dataset['nodes']['_id'])
86
+ if 'nodes' in OSW_DATASET:
87
+ node_ids = set(OSW_DATASET['nodes']['_id'])
88
88
  else:
89
89
  node_ids = set()
90
90
 
91
- if "edges" in OSW_dataset:
92
- node_ids_edges_u = set(OSW_dataset['edges']['_u_id'])
93
- node_ids_edges_v = set(OSW_dataset['edges']['_v_id'])
91
+ if 'edges' in OSW_DATASET:
92
+ node_ids_edges_u = set(OSW_DATASET['edges']['_u_id'])
93
+ node_ids_edges_v = set(OSW_DATASET['edges']['_v_id'])
94
94
  else:
95
95
  node_ids_edges_u = set()
96
96
  node_ids_edges_v = set()
97
97
 
98
- if "zones" in OSW_dataset:
99
- node_ids_zones_w = set([item for sublist in OSW_dataset['zones']['_w_id'] for item in sublist])
98
+ if 'zones' in OSW_DATASET:
99
+ node_ids_zones_w = set([item for sublist in OSW_DATASET['zones']['_w_id'] for item in sublist])
100
100
  else:
101
101
  node_ids_zones_w = set()
102
102
 
@@ -119,8 +119,8 @@ class OSWValidation:
119
119
  self.errors.append(f"All _w_id's in zones should be part of _id's mentioned in nodes, _w_id's not in nodes are: {unmatched}")
120
120
 
121
121
  # Geometry validation: check geometry type in each file and test if coordinates make a shape that is reasonable geometric shape according to the Simple Feature Access standard
122
- for osw_file in OSW_dataset:
123
- invalid_geojson = OSW_dataset[osw_file][(OSW_dataset[osw_file].geometry.type != OSW_dataset_files[osw_file]['geometry']) | (OSW_dataset[osw_file].is_valid == False)]
122
+ for osw_file in OSW_DATASET:
123
+ invalid_geojson = OSW_DATASET[osw_file][(OSW_DATASET[osw_file].geometry.type != OSW_DATASET_FILES[osw_file]['geometry']) | (OSW_DATASET[osw_file].is_valid == False)]
124
124
  is_valid = len(invalid_geojson) == 0
125
125
  if not is_valid:
126
126
  self.errors.append(f"Invalid {osw_file} geometries found, id's of invalid geometries: {set(invalid_geojson['_id'])}")
@@ -135,30 +135,31 @@ class OSWValidation:
135
135
  self.errors.append(f"Invalid geometries found in extension file {file}, list of invalid geometries: {invalid_geojson.to_json()}")
136
136
 
137
137
  if self.errors:
138
- zip_handler.remove_extracted_files()
139
138
  return ValidationResult(False, self.errors)
140
139
  else:
141
140
  return ValidationResult(True)
142
141
  except Exception as e:
143
142
  self.errors.append(f'Unable to validate: {e}')
144
143
  return ValidationResult(False, self.errors)
144
+ finally:
145
+ del OSW_DATASET
146
+ if zip_handler:
147
+ zip_handler.remove_extracted_files()
148
+ gc.collect()
145
149
 
146
150
  def load_osw_file(self, graph_geojson_path: str) -> Dict[str, Any]:
147
- '''Load OSW Data'''
151
+ """Load OSW Data"""
148
152
  with open(graph_geojson_path, 'r') as file:
149
153
  return json.load(file)
150
154
 
151
155
  def validate_osw_errors(self, file_path: str, max_errors: int) -> bool:
152
- '''Validate OSW Data against the schema and process all errors'''
156
+ """Validate OSW Data against the schema and process all errors"""
153
157
  geojson_data = self.load_osw_file(file_path)
154
158
  validator = jsonschema.Draft7Validator(self.load_osw_schema(self.schema_file_path))
155
159
 
156
160
  for error in validator.iter_errors(geojson_data):
157
161
  self.errors.append(f'Validation error: {error.message}')
158
- if len(self.errors) == max_errors:
159
- break
160
-
161
- if len(self.errors) >= max_errors:
162
- return False
162
+ if len(self.errors) >= max_errors:
163
+ return False
163
164
 
164
- return True
165
+ return len(self.errors) < max_errors
@@ -1,32 +1,32 @@
1
1
  import os
2
2
  import glob
3
3
 
4
-
5
- OSW_dataset_files = {"edges": {
6
- "required": False,
7
- "geometry": "LineString"
8
- },
9
- "nodes": {
10
- "required": False,
11
- "geometry": "Point"
12
- },
13
- "points": {
14
- "required": False,
15
- "geometry": "Point"
16
- },
17
- "lines": {
18
- "required": False,
19
- "geometry": "LineString"
20
- },
21
- "zones": {
22
- "required": False,
23
- "geometry": "Polygon"
24
- },
25
- "polygons": {
26
- "required": False,
27
- "geometry": "Polygon"
28
- }
29
- }
4
+ OSW_DATASET_FILES = {
5
+ "edges": {
6
+ "required": False,
7
+ "geometry": "LineString"
8
+ },
9
+ "nodes": {
10
+ "required": False,
11
+ "geometry": "Point"
12
+ },
13
+ "points": {
14
+ "required": False,
15
+ "geometry": "Point"
16
+ },
17
+ "lines": {
18
+ "required": False,
19
+ "geometry": "LineString"
20
+ },
21
+ "zones": {
22
+ "required": False,
23
+ "geometry": "Polygon"
24
+ },
25
+ "polygons": {
26
+ "required": False,
27
+ "geometry": "Polygon"
28
+ }
29
+ }
30
30
 
31
31
 
32
32
  class ExtractedDataValidator:
@@ -53,8 +53,8 @@ class ExtractedDataValidator:
53
53
  self.error = 'No .geojson files found in the specified directory or its subdirectories.'
54
54
  return False
55
55
 
56
- required_files = [key for key, value in OSW_dataset_files.items() if value['required']]
57
- optional_files = [key for key, value in OSW_dataset_files.items() if not value['required']]
56
+ required_files = [key for key, value in OSW_DATASET_FILES.items() if value['required']]
57
+ optional_files = [key for key, value in OSW_DATASET_FILES.items() if not value['required']]
58
58
  missing_files = []
59
59
  duplicate_files = []
60
60
  for required_file in required_files:
@@ -89,11 +89,11 @@ class ExtractedDataValidator:
89
89
  if missing_files:
90
90
  self.error = f'Missing required .geojson files: {", ".join(missing_files)}.'
91
91
  return False
92
-
92
+
93
93
  if duplicate_files:
94
94
  self.error = f'Multiple .geojson files of the same type found: {", ".join(duplicate_files)}.'
95
95
  return False
96
-
96
+
97
97
  # Add OSW external extensions, GeoJSON files we know nothing about
98
98
  self.externalExtensions.extend([item for item in geojson_files if item not in self.files])
99
99
 
@@ -1 +1 @@
1
- __version__ = '0.2.4'
1
+ __version__ = '0.2.6'
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-osw-validation
3
- Version: 0.2.4
3
+ Version: 0.2.6
4
4
  Summary: Python library for OSW validation
5
5
  Home-page: https://github.com/TaskarCenterAtUW/TDEI-python-lib-osw-validation
6
6
  Author: Sujata Misra
@@ -14,10 +14,9 @@ Classifier: Operating System :: OS Independent
14
14
  Requires-Python: >=3.10
15
15
  Description-Content-Type: text/markdown
16
16
  License-File: LICENSE
17
- Requires-Dist: jsonschema==4.19.1
18
- Requires-Dist: zipfile36==0.1.3
19
- Requires-Dist: coverage~=7.5.1
20
- Requires-Dist: geopandas==0.13.2
17
+ Requires-Dist: jsonschema
18
+ Requires-Dist: zipfile36
19
+ Requires-Dist: geopandas
21
20
 
22
21
  # TDEI python lib OSW validation package
23
22
 
@@ -0,0 +1,10 @@
1
+ python_osw_validation/__init__.py,sha256=CToPu6up1oroUOG7sl15uNclhwTznBOGBRlpeN9q1Eo,7351
2
+ python_osw_validation/extracted_data_validator.py,sha256=1QFVVJRpkx8HfAWJmtJ2PxPBsD-UVsNk5I2VFzVJ5Ps,3333
3
+ python_osw_validation/version.py,sha256=G3YzB5xTyZTVDHgjxzwfUZHPQbqRjfJO_4NfpO-O5Lg,21
4
+ python_osw_validation/zipfile_handler.py,sha256=lCRraGcysxelVKswQw1r-i_9kGQGvRQk7hJkWUwiAPk,2692
5
+ python_osw_validation/schema/opensidewalks.schema.json,sha256=i0iJwb_4Tq20BVUpkpgyM_7fm4SW_d_HNXKw0xul7OA,240987
6
+ python_osw_validation-0.2.6.dist-info/LICENSE,sha256=2bm9uFabQZ3Ykb_SaSU_uUbAj2-htc6WJQmS_65qD00,1073
7
+ python_osw_validation-0.2.6.dist-info/METADATA,sha256=d7VByKt099S5QEjzVuT4K0Rveb1ZLNFi3Tn8MJXMsoc,6114
8
+ python_osw_validation-0.2.6.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
9
+ python_osw_validation-0.2.6.dist-info/top_level.txt,sha256=GE81UPcDHEffJCxYu3M_iPz3Dt-O3axB73R5SLm_Veo,22
10
+ python_osw_validation-0.2.6.dist-info/RECORD,,
@@ -1,10 +0,0 @@
1
- python_osw_validation/__init__.py,sha256=8G-4lF_FjKhC1vdRZ5l0ak_kw4OCt5EBzTPvzsslolk,7301
2
- python_osw_validation/extracted_data_validator.py,sha256=Tdo3I5xz9bmzlZh9OH7Fp8WOcM88VZe4kCrzR2WwVWY,3894
3
- python_osw_validation/version.py,sha256=qfsPKGSX4YbTkJ_mvg4kfAF60Sx0Ol77KSinJilpkVw,21
4
- python_osw_validation/zipfile_handler.py,sha256=lCRraGcysxelVKswQw1r-i_9kGQGvRQk7hJkWUwiAPk,2692
5
- python_osw_validation/schema/opensidewalks.schema.json,sha256=i0iJwb_4Tq20BVUpkpgyM_7fm4SW_d_HNXKw0xul7OA,240987
6
- python_osw_validation-0.2.4.dist-info/LICENSE,sha256=2bm9uFabQZ3Ykb_SaSU_uUbAj2-htc6WJQmS_65qD00,1073
7
- python_osw_validation-0.2.4.dist-info/METADATA,sha256=lqZ-tuBgDCdlBf_0u8rAGrSIlSzBHyaO1rzDYyRCwt4,6168
8
- python_osw_validation-0.2.4.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
9
- python_osw_validation-0.2.4.dist-info/top_level.txt,sha256=GE81UPcDHEffJCxYu3M_iPz3Dt-O3axB73R5SLm_Veo,22
10
- python_osw_validation-0.2.4.dist-info/RECORD,,