|
| 1 | +#--------------------------------------------------------------------------------- |
| 2 | +# derived from https://github.com/cityjson/specs/blob/main/misc/schema-minifier.py |
| 3 | +#--------------------------------------------------------------------------------- |
| 4 | + |
| 5 | +import os |
| 6 | +import json |
| 7 | +import jsonref |
| 8 | + |
| 9 | +#------------------------------------------------------------------------------------- |
| 10 | +# functions to remove $defs (all values have been resolved) and nested $id and $schema |
| 11 | +#------------------------------------------------------------------------------------- |
| 12 | + |
| 13 | +def remove_key(input, key): |
| 14 | + if isinstance(input, dict): |
| 15 | + return {k: remove_key(v, key) for k, v in input.items() if k != key} |
| 16 | + elif isinstance(input, list): |
| 17 | + return [remove_key(element, key) for element in input] |
| 18 | + else: |
| 19 | + return input |
| 20 | + |
| 21 | +def remove_nested_key(input, key): |
| 22 | + if isinstance(input, dict): |
| 23 | + return {k: remove_key(v, key) for k, v in input.items()} |
| 24 | + elif isinstance(input, list): |
| 25 | + return [remove_key(element, key) for element in input] |
| 26 | + else: |
| 27 | + return input |
| 28 | + |
| 29 | +def remove_keys(js): |
| 30 | + js1 = remove_key(js, "$defs") |
| 31 | + js1 = remove_nested_key(js1, "$id") |
| 32 | + js1 = remove_nested_key(js1, "$schema") |
| 33 | + js1 = remove_nested_key(js1, "title") |
| 34 | + js1 = remove_nested_key(js1, "description") |
| 35 | + # js1 = {k: remove_key(v, "$id") if isinstance(v, dict) else v for k, v in js1.items()} |
| 36 | + # js1 = {k: remove_key(v, "$schema") if isinstance(v, dict) else v for k, v in js1.items()} |
| 37 | + # js1 = {k: remove_key(v, "title") if isinstance(v, dict) else v for k, v in js1.items()} |
| 38 | + # js1 = {k: remove_key(v, "description") if isinstance(v, dict) else v for k, v in js1.items()} |
| 39 | + return js1 |
| 40 | + |
| 41 | +#------------------------------------------------------------------------------------- |
| 42 | +# function to resolve and inline $ref values |
| 43 | +#------------------------------------------------------------------------------------- |
| 44 | + |
| 45 | +def resolve_refs(root_schema_path): |
| 46 | + root_schema = os.path.abspath(root_schema_path) |
| 47 | + fins = open(root_schema) |
| 48 | + |
| 49 | + abs_path = os.path.abspath(os.path.dirname(root_schema)) |
| 50 | + base_uri = 'file://{}/'.format(abs_path) |
| 51 | + |
| 52 | + js = jsonref.loads(fins.read(), jsonschema=False, base_uri=base_uri) |
| 53 | + |
| 54 | + # -- remove $defs and nested $id and $schema |
| 55 | + js = remove_keys(js) |
| 56 | + js['$id'] = js['$id'].replace('.json', '.min.json') |
| 57 | + |
| 58 | + json_str = json.dumps(js, separators=(',',':')) |
| 59 | + f = open(root_schema_path.replace('.json', '.min.json'), 'w') |
| 60 | + f.write(json_str) |
| 61 | + |
| 62 | +#----------------- |
| 63 | +# feature.min.json |
| 64 | +#----------------- |
| 65 | +resolve_refs('feature.json') |
| 66 | + |
| 67 | +#--------------------------- |
| 68 | +# featurecollection.min.json |
| 69 | +#--------------------------- |
| 70 | +resolve_refs('featurecollection.json') |
| 71 | + |
| 72 | +#--------------------------- |
| 73 | +# jsonfg-object.min.json |
| 74 | +#--------------------------- |
| 75 | +resolve_refs('jsonfg-object.json') |
| 76 | + |
| 77 | +#--------------------------- |
| 78 | +# jsonfg-root-object.min.json |
| 79 | +#--------------------------- |
| 80 | +resolve_refs('jsonfg-root-object.json') |
0 commit comments