/usr/lib/python3/dist-packages/geojson/validation.py is in python3-geojson 1.3.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | import geojson
def is_valid(obj):
""" IsValid provides validation for GeoJSON objects
All of error messages obtained from the offical site
http://geojson.org/geojson-spec.html
Args:
obj(geoJSON object): check validation
Returns:
dict of two paremeters 'valid' and 'message'.
In the case if geoJSON object is valid, returns
{valid: 'yes', message: ''}
If json objects is not valid, returns
{valid: 'no', message:'explanation, why this object is not valid'}
Example:
>> point = Point((10,20), (30,40))
>> IsValid(point)
>> {valid: 'yes', message: ''}
"""
if not isinstance(obj, geojson.GeoJSON):
return output('this is not GeoJSON object')
if isinstance(obj, geojson.Point):
if len(obj['coordinates']) != 2:
return output('the "coordinates" member must be a single position')
if isinstance(obj, geojson.MultiPoint):
if checkListOfObjects(obj['coordinates'], lambda x: len(x) == 2):
return output(
'the "coordinates" member must be an array of positions'
)
if isinstance(obj, geojson.LineString):
if len(obj['coordinates']) < 2:
return output('the "coordinates" member must be an array '
'of two or more positions')
if isinstance(obj, geojson.MultiLineString):
coord = obj['coordinates']
if checkListOfObjects(coord, lambda x: len(x) >= 2):
return output('the "coordinates" member must be an array '
'of LineString coordinate arrays')
if isinstance(obj, geojson.Polygon):
coord = obj['coordinates']
lengths = all([len(elem) >= 4 for elem in coord])
if lengths is False:
return output('LinearRing must contain with 4 or more positions')
isring = all([elem[0] == elem[-1] for elem in coord])
if isring is False:
return output('The first and last positions in LinearRing'
'must be equivalent')
if isinstance(obj, geojson.MultiPolygon):
if checkListOfObjects(obj['coordinates'], lambda x: is_polygon(x)):
return output('the "coordinates" member must be an array'
'of Polygon coordinate arrays')
return output('')
def is_polygon(coords):
lengths = all(len(elem) >= 4 for elem in coords)
isring = all(elem[0] == elem[-1] for elem in coords)
return lengths and isring
def checkListOfObjects(coord, pred):
""" This method provides checking list of geojson objects such Multipoint or
MultiLineString that each element of the list is valid geojson object.
This is helpful method for IsValid.
Args:
coord(list): List of coordinates
pred(function): Predicate to check validation of each
member in the coord
Returns:
True if list contains valid objects, False otherwise
"""
return not isinstance(coord, list) or not all([pred(ls) for ls in coord])
def output(message):
""" Output result for IsValid
Args:
message - If message is not empty,
object is not valid
"""
result = {'valid': 'no', 'message': ''}
if message != '':
result['message'] = message
return result
result['valid'] = 'yes'
return result
|