def validate(request): """ POST /validate Validate GeoJSON data in POST body """ testing = request.GET.get('testing') try: test_geojson = json.loads(request.raw_post_data) if not isinstance(test_geojson, dict): return _geojson_error('POSTed data was not a JSON object.', testing) except: return _geojson_error('POSTed data was not JSON serializeable.', testing) if not 'type' in test_geojson: return _geojson_error('The "type" member is requried and was not found.', testing) try: validate_geojson(test_geojson) except GeoJSONValidationException as e: return _geojson_error(str(e), testing) # Everything checked out. Return 'ok'. if not testing: track_validate() resp = { 'status': 'ok', } return HttpResponse(json.dumps(resp), mimetype='application/json')
def test_invalid_geojson(self): s = samples invalids = [ s.point_with_strings, s.featurecollection_bad_geom, s.bad_type, ] valids = 0 for invalid in invalids: try: validate_geojson(invalid) valids += 1 except GeoJSONValidationException: pass self.assertEqual(valids, 0)
def test_invalid_geojson(self): s = samples invalids = [ s.point_with_strings, s.featurecollection_bad_geom, s.bad_type, s.polygon_non_coincident_first_last, s.featurecollection_missing_feature_types ] valids = 0 for invalid in invalids: try: validate_geojson(invalid) valids += 1 except GeoJSONValidationException: pass self.assertEqual(valids, 0)
def validate(request): """ POST /validate Validate GeoJSON data in POST body """ testing = request.GET.get('testing') if request.method == 'POST': stringy_json = request.raw_post_data else: # GET try: remote_url = request.GET['url'] stringy_json = get_remote_json(remote_url) except KeyError: # The "url" URL parameter was missing return _geojson_error( 'When validating via GET, a "url" URL parameter is required.', status=400) except NonFetchableURLException: return _geojson_error('The URL passed could not be fetched.') try: test_geojson = json.loads(stringy_json) if not isinstance(test_geojson, dict): return _geojson_error('Data was not a JSON object.', testing) except: return _geojson_error('Data was not JSON serializeable.', testing) if not 'type' in test_geojson: return _geojson_error( 'The "type" member is required and was not found.', testing) try: validate_geojson(test_geojson) except GeoJSONValidationException as e: return _geojson_error(str(e), testing) # Everything checked out. Return 'ok'. track_validate() resp = { 'status': 'ok', } return HttpResponse(json.dumps(resp), mimetype='application/json')
def validate(request): """ POST /validate Validate GeoJSON data in POST body """ testing = request.GET.get('testing') if request.method == 'POST': stringy_json = request.raw_post_data else: # GET try: remote_url = request.GET['url'] stringy_json = get_remote_json(remote_url) except KeyError: # The "url" URL parameter was missing return _geojson_error('When validating via GET, a "url" URL parameter is required.', status=400) except NonFetchableURLException: return _geojson_error('The URL passed could not be fetched.') try: test_geojson = json.loads(stringy_json) if not isinstance(test_geojson, dict): return _geojson_error('Data was not a JSON object.', testing) except: return _geojson_error('Data was not JSON serializeable.', testing) if not 'type' in test_geojson: return _geojson_error('The "type" member is required and was not found.', testing) try: validate_geojson(test_geojson) except GeoJSONValidationException as e: return _geojson_error(str(e), testing) # Everything checked out. Return 'ok'. resp = { 'status': 'ok', } return HttpResponse(json.dumps(resp), mimetype='application/json')
class UnitTestValidGeoJSON(unittest.TestCase): s = samples valids = [ s.point, s.point_three, s.multipoint, s.linestring, s.multilinestring, s.polygon, s.multipolygon, s.feature, s.featurecollection, s.geometrycollection, ] for valid in valids: # A GeoJSONValidationException will be raised if something is invalid, so just looping is fine. validate_geojson(valid)