Browse Source

Add a function to help other programs validate stand-alone geojson

Gu1 11 years ago
parent
commit
f2e4ebcc85
1 changed files with 24 additions and 3 deletions
  1. 24 3
      ispformat/validator/schemavalidator.py

+ 24 - 3
ispformat/validator/schemavalidator.py

@@ -15,7 +15,7 @@ class MyRefResolver(RefResolver):
 
 
 geojson_allowed_types=('Polygon', 'MultiPolygon')
-def validate_geojson(d):
+def validate_geojson_type(d):
     """
     Make sure a geojson dict only contains allowed geometry types
     """
@@ -25,6 +25,27 @@ def validate_geojson(d):
     return True
 
 
+def validate_geojson(geodict):
+    """
+    Convenience function to validate a geojson dict
+    """
+    _version = 0.1
+    schema = _schema.load_schema(_version, 'geojson/geojson')
+    v = Draft4Validator(
+        schema,
+        resolver=MyRefResolver.from_schema(schema, store=_schema.deps_for_version(_version)),
+        format_checker=draft4_format_checker,
+    )
+
+    for err in v.iter_errors(geodict):
+        return False
+
+    if not validate_geojson_type(geodict):
+        return False
+
+    return True
+
+
 def validate_isp(jdict):
     """
     Validate a json-object against the isp json-schema
@@ -79,7 +100,7 @@ def validate_isp(jdict):
 
     for i, ca in enumerate(jdict.get('coveredAreas', [])):
         area=ca.get('area')
-        if area and validate_geojson(area):
+        if area and validate_geojson_type(area):
             continue
         elif not area:
             continue
@@ -89,6 +110,6 @@ def validate_isp(jdict):
             instance=ca, schema=schema[u'definitions'][u'coveredArea'][u'properties'][u'area'],
             path=['coveredAreas', i, 'area'],
             schema_path=[u'properties', u'coveredAreas', u'items', u'properties', u'area'],
-            validator=u'validate_geojson', validator_value=ca
+            validator=u'validate_geojson_type', validator_value=ca
         )