diff --git a/django/contrib/gis/measure.py b/django/contrib/gis/measure.py index 590a80293a..707c061a29 100644 --- a/django/contrib/gis/measure.py +++ b/django/contrib/gis/measure.py @@ -347,8 +347,13 @@ class Distance(MeasureBase): class Area(MeasureBase): STANDARD_UNIT = AREA_PREFIX + Distance.STANDARD_UNIT # Getting the square units values and the alias dictionary. - UNITS = {"%s%s" % (AREA_PREFIX, k): v**2 for k, v in Distance.UNITS.items()} - ALIAS = {k: "%s%s" % (AREA_PREFIX, v) for k, v in Distance.ALIAS.items()} + UNITS = {"%s%s" % (AREA_PREFIX, k): v**2 for k, v in Distance.UNITS.items()} | { + "ha": 10000, + } + ALIAS = {k: "%s%s" % (AREA_PREFIX, v) for k, v in Distance.ALIAS.items()} | { + "hectare": "ha", + } + LALIAS = {k.lower(): v for k, v in ALIAS.items()} def __truediv__(self, other): diff --git a/docs/ref/contrib/gis/measure.txt b/docs/ref/contrib/gis/measure.txt index ad02db87fa..cee4211220 100644 --- a/docs/ref/contrib/gis/measure.txt +++ b/docs/ref/contrib/gis/measure.txt @@ -116,6 +116,19 @@ Unit Attribute Full name or alias(es) For example, ``Area(sq_m=2)`` creates an :class:`Area` object representing two square meters. +In addition to unit with the ``sq_`` prefix, the following units are also +supported on :class:`Area`: + +================================= ======================================== +Unit Attribute Full name or alias(es) +================================= ======================================== +``ha`` Hectare +================================= ======================================== + +.. versionchanged:: 5.1 + + Support for the ``ha`` unit was added. + Measurement API =============== diff --git a/docs/releases/5.1.txt b/docs/releases/5.1.txt index 30317eaa19..1f7d26d7b8 100644 --- a/docs/releases/5.1.txt +++ b/docs/releases/5.1.txt @@ -70,6 +70,8 @@ Minor features ``metro_code`` and ``region_code``, but the previous keys are also retained for backward compatibility. +* :class:`~django.contrib.gis.measure.Area` now supports the ``ha`` unit. + :mod:`django.contrib.messages` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/tests/gis_tests/test_measure.py b/tests/gis_tests/test_measure.py index 2a922abf9b..19644c1f98 100644 --- a/tests/gis_tests/test_measure.py +++ b/tests/gis_tests/test_measure.py @@ -38,6 +38,10 @@ class DistanceTest(SimpleTestCase): with self.assertRaises(AttributeError): D(banana=100) + def test_init_invalid_area_only_units(self): + with self.assertRaises(AttributeError): + D(ha=100) + def test_access(self): "Testing access in different units" d = D(m=100) @@ -294,6 +298,13 @@ class AreaTest(unittest.TestCase): self.assertEqual(repr(a1), "Area(sq_m=100.0)") self.assertEqual(repr(a2), "Area(sq_km=3.5)") + def test_hectare(self): + a = A(sq_m=10000) + self.assertEqual(a.ha, 1) + + def test_hectare_unit_att_name(self): + self.assertEqual(A.unit_attname("Hectare"), "ha") + def test_hash(self): a1 = A(sq_m=100) a2 = A(sq_m=1000000)