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."
|
2017-09-11 23:56:39 +08:00
|
|
|
def __init__(self, geo_field):
|
2017-07-07 20:54:08 +08:00
|
|
|
super().__init__()
|
2017-09-11 23:56:39 +08:00
|
|
|
self.geo_field = geo_field
|
2009-01-16 03:35:04 +08:00
|
|
|
|
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):
|
2017-09-11 23:56:39 +08:00
|
|
|
if value is None:
|
|
|
|
return
|
|
|
|
area_att = connection.ops.get_area_att_for_field(self.geo_field)
|
|
|
|
return getattr(value, area_att) if area_att else value
|
2017-03-20 15:10:29 +08:00
|
|
|
|
2017-07-07 01:18:05 +08:00
|
|
|
def from_db_value(self, value, expression, connection):
|
2017-09-11 23:56:39 +08:00
|
|
|
if value is None:
|
|
|
|
return
|
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.
|
2017-09-11 23:56:39 +08:00
|
|
|
area_att = connection.ops.get_area_att_for_field(self.geo_field)
|
|
|
|
return Area(**{area_att: value}) if area_att else value
|
2014-08-12 20:08:40 +08:00
|
|
|
|
|
|
|
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-09-11 23:56:39 +08:00
|
|
|
def __init__(self, geo_field):
|
2017-07-07 20:54:08 +08:00
|
|
|
super().__init__()
|
2017-09-11 23:56:39 +08:00
|
|
|
self.geo_field = geo_field
|
2009-01-16 03:35:04 +08:00
|
|
|
|
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
|
2017-09-11 23:56:39 +08:00
|
|
|
distance_att = connection.ops.get_distance_att_for_field(self.geo_field)
|
|
|
|
if not distance_att:
|
2017-04-07 07:27:45 +08:00
|
|
|
raise ValueError('Distance measure is supplied, but units are unknown for result.')
|
2017-09-11 23:56:39 +08:00
|
|
|
return getattr(value, distance_att)
|
2017-04-07 07:27:45 +08:00
|
|
|
|
2017-07-07 01:18:05 +08:00
|
|
|
def from_db_value(self, value, expression, connection):
|
2017-09-11 23:56:39 +08:00
|
|
|
if value is None:
|
|
|
|
return
|
|
|
|
distance_att = connection.ops.get_distance_att_for_field(self.geo_field)
|
|
|
|
return Distance(**{distance_att: value}) if distance_att else value
|
2014-08-12 20:08:40 +08:00
|
|
|
|
|
|
|
def get_internal_type(self):
|
|
|
|
return 'DistanceField'
|