Skip to content

Commit e0e0c2b

Browse files
committed
Prototype it with simple functions
1 parent 157e509 commit e0e0c2b

5 files changed

Lines changed: 81 additions & 21 deletions

File tree

validate.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
_here = os.path.abspath(__file__)
55
sys.path.insert(0, _here)
66

7+
from validator import Validator
8+
79

810
config = {
911
'geo_path': 'tmp/PM-MAR-MS-Arsinoes_01/vector/PM-MAR-MS-Arsinoes_01.gpkg',
1012
'geopackage_tables': ['units', 'contacts']
1113
}
1214

13-
from validator import Validator
1415

1516
def run(config, log_path):
1617
validator = Validator(config, log_path)

validator/validator.py

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,54 @@
22

33
from tasks import *
44

5+
6+
def check_exist(filename):
7+
import os
8+
return os.path.exists(filename)
9+
10+
def check_is_gpkg(filename):
11+
import geopandas
12+
try:
13+
geopandas.read_file(filename, driver='GPKG')
14+
except:
15+
return False
16+
return True
17+
18+
def check_layer(pkg, layer):
19+
out = layer in pkg
20+
return out
21+
22+
def check_gpkg_layers(filename, layers=[]):
23+
pkg = load_gpkg(filename)
24+
for layer in layers:
25+
out += check_layer(pkg, layer)
26+
return out
27+
28+
def check_layer_columns(gdf, columns):
29+
out = []
30+
for column in columns:
31+
out += column in gdf
32+
return out
33+
34+
def check_layers_columns(filename, layers={}):
35+
pkg = load_gpkg(filename)
36+
gdf = pkg[layer]
37+
for layer, columns in layers.items():
38+
out += check_layer_columns(gdf, columns)
39+
return out
40+
41+
def check_column_content(gdf, column, rules):
42+
for rule in rules:
43+
out += gdf[column].apply(rule)
44+
return out
45+
46+
def check_columns_content(filename, layer, columns={}):
47+
pkg = load_gpkg(filename)
48+
gdf = pkg[layer]
49+
for column, rules in columns.items():
50+
out += check_column_content(gdf, column, rules)
51+
return out
52+
553
class Validator(object):
654
tasks = None
755

@@ -18,7 +66,12 @@ def __init__(self, config, log_path):
1866

1967
self.config = config
2068

21-
tasks = [TaskVector, TaskVectorTables]
69+
# tasks = [TaskVector, TaskVectorTables]
70+
tasks = [check_exist(filename),
71+
check_is_gpkg(filename),
72+
check_gpkg_layers(filename),
73+
check_layers_columns(filename),
74+
check_columns_content(filename)]
2275
self.tasks = self.set_tasks(tasks)
2376

2477
def set_tasks(self, tasks):
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
_component = __file__.split('.')[0]
3+
_schemafile = '.'.join([_component, 'schema', 'json'])
4+
5+
with open(_schemafile) as f:
6+
import json
7+
schema = json.load(f)
8+
9+
def validate(gpkg_path):
10+
import fiona
11+
data = fiona.listlayers(gpkg_path)
12+
13+
import jsonschema
14+
res = jsonschema.validate(data, schema)
15+
16+
return res
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
{
2-
"$schema": "http://json-schema.org/draft-07/schema#",
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
4+
"title": "Match Geopackage layers list",
5+
6+
"$comment": "Works for, e.g., ['units','contacts','bla'], but not for ['bla','units','contacts']",
7+
"$example": ["units","contacts","another-column"],
38

49
"definitions": {
5-
"columns": {
10+
"layers": {
611
"type": "string",
712
"enum": ["units", "contacts"]
813
}
@@ -11,13 +16,11 @@
1116
"type": "array",
1217
"items": [
1318
{
14-
"$ref": "#/definitions/columns"
15-
},
16-
{
17-
"$ref": "#/definitions/columns"
19+
"$ref": "#/definitions/layers"
1820
}
1921
],
2022
"minItems": 2,
23+
2124
"uniqueItems": true,
2225
"additionalItems": { "type": "string" }
2326
}

validator/vector/geopackage_layers.validator.py

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)