GeoDjango: new measure unit suggestion, hectare

Hello,

I’d like to contribute Geodjango with a measure unit : the “hectare”, usually written as “ha”.
It is the most common measure unit in France to express land area.

It is not part of the International System of Units, but is accepted for use “With the system”, as described in this document (p 145).

[…] it is recognized that some non-SI units are widely used and are expected to continue to be used for many years. Therefore, the CIPM has accepted some non-SI units for use with the SI.

Working with a project that uses essentially this unit requires constant conversions (sq_m / 10000), and is error prone.

The particularity of this unit is that it is only for measuring areas and cannot be used for distances.

The change I’m suggesting would be something in the vein of (based on this existing code):



class Area(MeasureBase):
    STANDARD_UNIT = AREA_PREFIX + Distance.STANDARD_UNIT
    AREA_ONLY_UNITS = {
        "ha": 10000,
    }
    AREA_ONLY_ALIAS = {
        "hectare": "ha",
    }
    # Getting the square units values and the alias dictionary.
    UNITS = {"%s%s" % (AREA_PREFIX, k): v**2 for k, v in Distance.UNITS.items()} | AREA_ONLY_UNITS
    ALIAS = {k: "%s%s" % (AREA_PREFIX, v) for k, v in Distance.ALIAS.items()} | AREA_ONLY_ALIAS
    LALIAS = {k.lower(): v for k, v in ALIAS.items()}

    def __truediv__(self, other):
        if isinstance(other, NUMERIC_TYPES):
            return self.__class__(
                default_unit=self._default_unit,
                **{self.STANDARD_UNIT: (self.standard / other)},
            )
        else:
            raise TypeError(
                "%(class)s must be divided by a number" % {"class": pretty_name(self)}
            )

What do you think?

Thanks Alexis for your proposal. It has some merits for sure, and I know at least one project of mine where I could use it.
Maybe you could try transforming your suggestion in a draft pull request against Django main code (including tests)?

By the way, is there a reason you added Geojson as a title prefix here? I don’t see how it relates to your proposal.

Thank you for the feedback!

I meant to write GeoDjango, my bad.

(Fixed by your friendly local moderator.)

1 Like

Thanks a lot Alexis for pushing this into Django, much appreciated :+1: :heart: