From 23fbd3ff48a90b7e5d373b6fe50b65d1c48dabed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20No=C3=A9?= Date: Tue, 19 Apr 2016 11:35:50 +0200 Subject: [PATCH] Fixed #26512 -- Added tests for SpatialRefSysMixin.get_units(). --- django/contrib/gis/db/backends/base/models.py | 7 ++--- tests/gis_tests/test_spatialrefsys.py | 28 ++++++++++++++++++- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/django/contrib/gis/db/backends/base/models.py b/django/contrib/gis/db/backends/base/models.py index 6bebfa9f7e..85f4478534 100644 --- a/django/contrib/gis/db/backends/base/models.py +++ b/django/contrib/gis/db/backends/base/models.py @@ -164,15 +164,14 @@ class SpatialRefSysMixin(object): @classmethod def get_units(cls, wkt): """ - Class method used by GeometryField on initialization to - retrieve the units on the given WKT, without having to use - any of the database fields. + Return a tuple of (unit_value, unit_name) for the given WKT without + using any of the database fields. """ if gdal.HAS_GDAL: return gdal.SpatialReference(wkt).units else: m = cls.units_regex.match(wkt) - return m.group('unit'), m.group('unit_name') + return float(m.group('unit')), m.group('unit_name') @classmethod def get_spheroid(cls, wkt, string=True): diff --git a/tests/gis_tests/test_spatialrefsys.py b/tests/gis_tests/test_spatialrefsys.py index e0c593ff4a..c31b6280c3 100644 --- a/tests/gis_tests/test_spatialrefsys.py +++ b/tests/gis_tests/test_spatialrefsys.py @@ -1,8 +1,9 @@ +import re import unittest from django.contrib.gis.gdal import HAS_GDAL from django.db import connection -from django.test import skipUnlessDBFeature +from django.test import mock, skipUnlessDBFeature from django.utils import six from .utils import SpatialRefSys, oracle, postgis, spatialite @@ -20,6 +21,18 @@ test_srs = ({ # From proj's "cs2cs -le" and Wikipedia (semi-minor only) 'ellipsoid': (6378137.0, 6356752.3, 298.257223563), 'eprec': (1, 1, 9), + 'wkt': re.sub('[\s+]', '', """ + GEOGCS["WGS 84", + DATUM["WGS_1984", + SPHEROID["WGS 84",6378137,298.257223563, + AUTHORITY["EPSG","7030"]], + AUTHORITY["EPSG","6326"]], + PRIMEM["Greenwich",0, + AUTHORITY["EPSG","8901"]], + UNIT["degree",0.01745329251994328, + AUTHORITY["EPSG","9122"]], + AUTHORITY["EPSG","4326"]] + """) }, { 'srid': 32140, 'auth_name': ('EPSG', False), @@ -43,6 +56,19 @@ test_srs = ({ @skipUnlessDBFeature("has_spatialrefsys_table") class SpatialRefSysTest(unittest.TestCase): + def test_get_units(self): + epsg_4326 = next(f for f in test_srs if f['srid'] == 4326) + unit, unit_name = SpatialRefSys().get_units(epsg_4326['wkt']) + self.assertEqual(unit_name, 'degree') + self.assertAlmostEqual(unit, 0.01745329251994328) + + @mock.patch('django.contrib.gis.gdal.HAS_GDAL', False) + def test_get_units_without_gdal(self): + epsg_4326 = next(f for f in test_srs if f['srid'] == 4326) + unit, unit_name = SpatialRefSys().get_units(epsg_4326['wkt']) + self.assertEqual(unit_name, 'degree') + self.assertAlmostEqual(unit, 0.01745329251994328) + def test_retrieve(self): """ Test retrieval of SpatialRefSys model objects.