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
|
|
|
"""
|
2016-01-26 06:50:46 +08:00
|
|
|
from decimal import Decimal
|
|
|
|
|
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
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class BaseField:
|
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."
|
2016-01-22 19:03:05 +08:00
|
|
|
def __init__(self, area_att=None):
|
2009-01-16 03:35:04 +08:00
|
|
|
self.area_att = area_att
|
|
|
|
|
2015-02-20 18:53:59 +08:00
|
|
|
def from_db_value(self, value, expression, connection, context):
|
2015-09-20 18:41:25 +08:00
|
|
|
if connection.features.interprets_empty_strings_as_nulls and value == '':
|
|
|
|
value = None
|
2016-01-26 06:50:46 +08:00
|
|
|
# If the database returns a Decimal, convert it to a float as expected
|
|
|
|
# by the Python geometric objects.
|
|
|
|
if isinstance(value, Decimal):
|
|
|
|
value = float(value)
|
2016-01-22 19:03:05 +08:00
|
|
|
# If the units are known, convert value into area measure.
|
|
|
|
if value is not None and self.area_att:
|
2014-08-12 20:08:40 +08:00
|
|
|
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
|