This is the validator I use for my geometry fields
import re
from pydal.validators import Validator, ValidationError
class IS_GEOLOCATION(Validator):
"""
Validate that the input is a location within latitude and longitude
constraints.
"""
regex_geopoint = re.compile(r"""POINT *\((?P<lat>-?[\d\.]+)
(?P<lng>-?[\d\.]+)\)""")
def __init__(self, minlat=-90, maxlat=90,
minlng=-180, maxlng=180,
error_message='Invalid coordinates'):
self.minlat = minlat
self.maxlat = maxlat
self.minlng = minlng
self.maxlng = maxlng
self.error_message = error_message
@staticmethod
def parse_geopoint(value):
""" Returns a tuple (lat, lng) from a POINT (lat lng) """
match = IS_GEOLOCATION.regex_geopoint.search(value)
return float(match.group('lat')), float(match.group('lng'))
def __call__(self, value):
try:
lat, lng = self.parse_geopoint(value)
if (self.minlat <= lat <= self.maxlat) and (self.minlng <= lng
<= self.maxlng):
return (value, None)
else:
raise ValidationError(self.translator(self.error_message))
except:
raise ValidationError(self.translator(self.error_message))
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.