Skip to content

Commit

Permalink
Fixed #35117 -- Added support for the hectare unit in Area.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexisig authored and felixxm committed Jan 16, 2024
1 parent 6debeac commit c7e986f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
9 changes: 7 additions & 2 deletions django/contrib/gis/measure.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,13 @@ def __mul__(self, other):
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):
Expand Down
13 changes: 13 additions & 0 deletions docs/ref/contrib/gis/measure.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
===============

Expand Down
2 changes: 2 additions & 0 deletions docs/releases/5.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
11 changes: 11 additions & 0 deletions tests/gis_tests/test_measure.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def test_init_invalid(self):
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)
Expand Down Expand Up @@ -294,6 +298,13 @@ def test_units_str(self):
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)
Expand Down

0 comments on commit c7e986f

Please sign in to comment.