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-08-12 20:08:40 +08:00
|
|
|
from django.contrib.gis.measure import Area, Distance
|
2017-03-20 15:10:29 +08:00
|
|
|
from django.db import models
|
2014-08-12 20:08:40 +08:00
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2017-03-20 15:10:29 +08:00
|
|
|
class AreaField(models.FloatField):
|
2009-01-16 03:35:04 +08:00
|
|
|
"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
|
|
|
|
|
2017-03-20 15:10:29 +08:00
|
|
|
def get_prep_value(self, value):
|
|
|
|
if not isinstance(value, Area):
|
|
|
|
raise ValueError('AreaField only accepts Area measurement objects.')
|
|
|
|
return value
|
|
|
|
|
|
|
|
def get_db_prep_value(self, value, connection, prepared=False):
|
|
|
|
if value is None or not self.area_att:
|
|
|
|
return value
|
|
|
|
return getattr(value, self.area_att)
|
|
|
|
|
2017-07-07 01:18:05 +08:00
|
|
|
def from_db_value(self, value, expression, connection):
|
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
|
|
|
|
2017-04-07 07:27:45 +08:00
|
|
|
class DistanceField(models.FloatField):
|
2009-01-16 03:35:04 +08:00
|
|
|
"Wrapper for Distance values."
|
2017-04-07 07:27:45 +08:00
|
|
|
def __init__(self, distance_att=None):
|
2009-01-16 03:35:04 +08:00
|
|
|
self.distance_att = distance_att
|
|
|
|
|
2017-04-07 07:27:45 +08:00
|
|
|
def get_prep_value(self, value):
|
|
|
|
if isinstance(value, Distance):
|
|
|
|
return value
|
|
|
|
return super().get_prep_value(value)
|
|
|
|
|
|
|
|
def get_db_prep_value(self, value, connection, prepared=False):
|
|
|
|
if not isinstance(value, Distance):
|
|
|
|
return value
|
|
|
|
if not self.distance_att:
|
|
|
|
raise ValueError('Distance measure is supplied, but units are unknown for result.')
|
|
|
|
return getattr(value, self.distance_att)
|
|
|
|
|
2017-07-07 01:18:05 +08:00
|
|
|
def from_db_value(self, value, expression, connection):
|
2017-04-07 07:27:45 +08:00
|
|
|
if value is None or not self.distance_att:
|
|
|
|
return value
|
|
|
|
return Distance(**{self.distance_att: value})
|
2014-08-12 20:08:40 +08:00
|
|
|
|
|
|
|
def get_internal_type(self):
|
|
|
|
return 'DistanceField'
|