|
| 1 | +import gpt |
| 2 | + |
| 3 | +def check_layer_names(pkg): |
| 4 | + """ |
| 5 | + Some layers are mandatory, others are commonly used, but not necessarily. |
| 6 | + """ |
| 7 | + out = [] |
| 8 | + _mandatory = ['geologic_units', |
| 9 | + 'layer_styles'] |
| 10 | + _common = ['geologic_contacts', |
| 11 | + 'linear_features', |
| 12 | + 'surface_features'] |
| 13 | + |
| 14 | + # check for the mandatory one |
| 15 | + if not all(l in pkg for l in _mandatory): |
| 16 | + out.append("Mandatory layers {!s} not found.".format(_mandatory)) |
| 17 | + |
| 18 | + # check for the commonly used. |
| 19 | + # Extra layers -- not in _mandatory neither in _common -- can be used, |
| 20 | + # which makes the checking for optional layers a bit will defined. |
| 21 | + # What we can do here is to check for if there are many layers and *warn* |
| 22 | + # the user if these _common ones are not there. |
| 23 | + if len(pkg) >= len(_mandatory + _common): |
| 24 | + if not all(l in pkg for l in _common): |
| 25 | + # The cool thing to do here is to have their geometry inspected |
| 26 | + # to have a clue on layer/content match |
| 27 | + msg = "Expected to see layers {!s}".format(_common) |
| 28 | + out.append(msg) |
| 29 | + |
| 30 | + return out |
| 31 | + |
| 32 | +LAYER_DEFS = dict( |
| 33 | + geologic_units = {'columns': ['name','rgb','geo_type','geo_code'], |
| 34 | + 'geometry': 'Polygon'}, |
| 35 | + geologic_contacts = {'columns': ['geo_type'], |
| 36 | + 'geometry': 'Linestring'}, |
| 37 | + surface_features = {'columns': ['geo_type'], |
| 38 | + 'geometry': 'Polygon'}, |
| 39 | + linear_features = {'columns': ['geo_type'], |
| 40 | + 'geometry': 'Linestring'}, |
| 41 | + layer_styles = {'columns': ['styleQML','styleSLD']} |
| 42 | +) |
| 43 | + |
| 44 | +def check_field_names(pkg, case_insensitive=False): |
| 45 | + out = [] |
| 46 | + for _layer,_defs in LAYER_DEFS.items(): |
| 47 | + if _layer not in pkg: |
| 48 | + msg = "Layer {} not found in pkg".format(_layer) |
| 49 | + out.append(msg) |
| 50 | + continue |
| 51 | + |
| 52 | + if case_insensitive: |
| 53 | + _check_columns = lambda c: c not in pkg[_layer].lower() |
| 54 | + else: |
| 55 | + _check_columns = lambda c: c not in pkg[_layer] |
| 56 | + not_found = list(filter(_check_columns, _defs['columns'])) |
| 57 | + if len(not_found): |
| 58 | + msg = "Columns {} not found in layer {}".format(not_found,_layer) |
| 59 | + out.append(msg) |
| 60 | + |
| 61 | + return out |
| 62 | + |
| 63 | +def check_geometry(pkg): |
| 64 | + """ |
| 65 | + Check if geometry has nulls |
| 66 | + """ |
| 67 | + out = [] |
| 68 | + for lname, df in pkg.layers(): |
| 69 | + geometry_bool = df['geometry'].isnull() |
| 70 | + if all(geometry_bool): |
| 71 | + continue |
| 72 | + if any(geometry_bool): |
| 73 | + msg = ("Found null geometries in layer {}\n{}" |
| 74 | + .format(lname,df[geometry_bool])) |
| 75 | + out.append(msg) |
| 76 | + |
| 77 | + return out |
| 78 | + |
| 79 | +def check_crs(pkg): |
| 80 | + out = [] |
| 81 | + CRSs = dict() |
| 82 | + for lname, df in pkg.layers(): |
| 83 | + crs = df.crs |
| 84 | + # - 'layer_styles' (by QGIS) has no 'geometry' |
| 85 | + # - accumulate crs(s) in a hash to check later for heterogeneity (multiple crs's) |
| 86 | + if lname != 'layer_styles': |
| 87 | + lcrs = CRSs.get(crs, []) |
| 88 | + lcrs.append(lname) |
| 89 | + CRSs[crs] = lcrs |
| 90 | + |
| 91 | + # - if more than one crs was found, say it |
| 92 | + if len(CRSs) > 1: |
| 93 | + out.append('Multiple CRSs found') |
| 94 | + |
| 95 | + # For each crs (hopefully, one), print it (WKT) |
| 96 | + for crs,lrs in CRSs.items(): |
| 97 | + out.append(crs.to_string()) |
| 98 | + |
| 99 | + return out |
| 100 | + |
| 101 | +def geopackage(gpkg): |
| 102 | + """ |
| 103 | + Checks to be done: |
| 104 | + - check layer names |
| 105 | + - check column names |
| 106 | + - check if any geometry entry is null, unless all of them are null |
| 107 | + - check if all CRS are equal (for table layers with geometry) |
| 108 | + - check if there are multiple "shape" and "id" columns |
| 109 | + """ |
| 110 | + pkg = gpt.read_file(gpkg) |
| 111 | + # # make it "case insensitive" (lower the keys) |
| 112 | + # for lname in list(pkg.keys()): |
| 113 | + # pkg[lname.lower()] = pkg[lname] |
| 114 | + # del pkg[lname] |
| 115 | + |
| 116 | + # Check layer names |
| 117 | + res = check_layer_names(pkg) |
| 118 | + res += check_field_names(pkg) |
| 119 | + res += check_geometry(pkg) |
| 120 | + res += check_crs(pkg) |
| 121 | + for r in res: |
| 122 | + print(r) |
0 commit comments