2009-01-16 03:35:04 +08:00
|
|
|
"""
|
2014-08-12 20:08:40 +08:00
|
|
|
This module holds simple classes to convert geospatial values from the
|
|
|
|
database.
|
2009-01-16 03:35:04 +08:00
|
|
|
"""
|
2009-03-31 01:15:49 +08:00
|
|
|
|
2014-08-12 20:08:40 +08:00
|
|
|
from django.contrib.gis.geometry.backend import Geometry
|
|
|
|
from django.contrib.gis.measure import Area, Distance
|
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2009-01-16 03:35:04 +08:00
|
|
|
class BaseField(object):
|
2009-12-22 23:18:51 +08:00
|
|
|
empty_strings_allowed = True
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2009-01-16 03:35:04 +08:00
|
|
|
class AreaField(BaseField):
|
|
|
|
"Wrapper for Area values."
|
|
|
|
def __init__(self, area_att):
|
|
|
|
self.area_att = area_att
|
|
|
|
|
2014-08-12 20:08:40 +08:00
|
|
|
def from_db_value(self, value, connection):
|
|
|
|
if value is not None:
|
|
|
|
value = Area(**{self.area_att: value})
|
|
|
|
return value
|
|
|
|
|
|
|
|
def get_internal_type(self):
|
|
|
|
return 'AreaField'
|
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2009-01-16 03:35:04 +08:00
|
|
|
class DistanceField(BaseField):
|
|
|
|
"Wrapper for Distance values."
|
|
|
|
def __init__(self, distance_att):
|
|
|
|
self.distance_att = distance_att
|
|
|
|
|
2014-08-12 20:08:40 +08:00
|
|
|
def from_db_value(self, value, connection):
|
|
|
|
if value is not None:
|
|
|
|
value = Distance(**{self.distance_att: value})
|
|
|
|
return value
|
|
|
|
|
|
|
|
def get_internal_type(self):
|
|
|
|
return 'DistanceField'
|
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2009-01-16 03:35:04 +08:00
|
|
|
class GeomField(BaseField):
|
|
|
|
"""
|
2013-10-11 04:42:30 +08:00
|
|
|
Wrapper for Geometry values. It is a lightweight alternative to
|
2013-09-06 06:23:48 +08:00
|
|
|
using GeometryField (which requires an SQL query upon instantiation).
|
2009-01-16 03:35:04 +08:00
|
|
|
"""
|
2014-08-12 20:08:40 +08:00
|
|
|
def from_db_value(self, value, connection):
|
|
|
|
if value is not None:
|
|
|
|
value = Geometry(value)
|
|
|
|
return value
|
|
|
|
|
|
|
|
def get_internal_type(self):
|
|
|
|
return 'GeometryField'
|