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-12-01 15:28:01 +08:00
|
|
|
from django.contrib.gis.db.models.fields import GeoSelectFormatMixin
|
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
|
|
|
|
2014-12-01 15:28:01 +08:00
|
|
|
def get_db_converters(self, connection):
|
|
|
|
return [self.from_db_value]
|
|
|
|
|
|
|
|
def select_format(self, compiler, sql, params):
|
|
|
|
return sql, params
|
|
|
|
|
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
|
|
|
|
|
2015-02-20 18:53:59 +08:00
|
|
|
def from_db_value(self, value, expression, connection, context):
|
2014-08-12 20:08:40 +08:00
|
|
|
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
|
|
|
|
|
2015-02-20 18:53:59 +08:00
|
|
|
def from_db_value(self, value, expression, connection, context):
|
2014-08-12 20:08:40 +08:00
|
|
|
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
|
|
|
|
2014-12-01 15:28:01 +08:00
|
|
|
class GeomField(GeoSelectFormatMixin, BaseField):
|
2009-01-16 03:35:04 +08:00
|
|
|
"""
|
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-12-01 15:28:01 +08:00
|
|
|
# Hacky marker for get_db_converters()
|
|
|
|
geom_type = None
|
|
|
|
|
2015-02-20 18:53:59 +08:00
|
|
|
def from_db_value(self, value, expression, connection, context):
|
2014-08-12 20:08:40 +08:00
|
|
|
if value is not None:
|
|
|
|
value = Geometry(value)
|
|
|
|
return value
|
|
|
|
|
|
|
|
def get_internal_type(self):
|
|
|
|
return 'GeometryField'
|
2014-09-21 15:51:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
class GMLField(BaseField):
|
|
|
|
"""
|
|
|
|
Wrapper for GML to be used by Oracle to ensure Database.LOB conversion.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def get_internal_type(self):
|
|
|
|
return 'GMLField'
|
|
|
|
|
2015-02-20 18:53:59 +08:00
|
|
|
def from_db_value(self, value, expression, connection, context):
|
2014-09-21 15:51:59 +08:00
|
|
|
return value
|