2010-03-27 04:14:53 +08:00
|
|
|
===================
|
|
|
|
Measurement Objects
|
|
|
|
===================
|
|
|
|
|
|
|
|
.. module:: django.contrib.gis.measure
|
2016-02-19 15:31:25 +08:00
|
|
|
:synopsis: GeoDjango's distance and area measurement objects.
|
2010-03-27 04:14:53 +08:00
|
|
|
|
2010-08-20 03:27:44 +08:00
|
|
|
The :mod:`django.contrib.gis.measure` module contains objects that allow
|
|
|
|
for convenient representation of distance and area units of measure. [#]_
|
|
|
|
Specifically, it implements two objects, :class:`Distance` and
|
|
|
|
:class:`Area` -- both of which may be accessed via the
|
2010-03-27 04:14:53 +08:00
|
|
|
:class:`D` and :class:`A` convenience aliases, respectively.
|
|
|
|
|
|
|
|
Example
|
|
|
|
=======
|
|
|
|
|
2010-08-20 03:27:44 +08:00
|
|
|
:class:`Distance` objects may be instantiated using a keyword argument indicating the
|
|
|
|
context of the units. In the example below, two different distance objects are
|
2010-03-27 04:14:53 +08:00
|
|
|
instantiated in units of kilometers (``km``) and miles (``mi``)::
|
|
|
|
|
2018-05-13 01:37:42 +08:00
|
|
|
>>> from django.contrib.gis.measure import D, Distance
|
2010-03-27 04:14:53 +08:00
|
|
|
>>> d1 = Distance(km=5)
|
2012-04-29 00:02:01 +08:00
|
|
|
>>> print(d1)
|
2010-03-27 04:14:53 +08:00
|
|
|
5.0 km
|
|
|
|
>>> d2 = D(mi=5) # `D` is an alias for `Distance`
|
2012-04-29 00:02:01 +08:00
|
|
|
>>> print(d2)
|
2010-03-27 04:14:53 +08:00
|
|
|
5.0 mi
|
|
|
|
|
|
|
|
Conversions are easy, just access the preferred unit attribute to get a
|
|
|
|
converted distance quantity::
|
|
|
|
|
2012-04-29 00:02:01 +08:00
|
|
|
>>> print(d1.mi) # Converting 5 kilometers to miles
|
2010-03-27 04:14:53 +08:00
|
|
|
3.10685596119
|
2012-04-29 00:02:01 +08:00
|
|
|
>>> print(d2.km) # Converting 5 miles to kilometers
|
2010-03-27 04:14:53 +08:00
|
|
|
8.04672
|
|
|
|
|
|
|
|
Moreover, arithmetic operations may be performed between the distance
|
|
|
|
objects::
|
|
|
|
|
2012-04-29 00:02:01 +08:00
|
|
|
>>> print(d1 + d2) # Adding 5 miles to 5 kilometers
|
2010-08-20 03:27:44 +08:00
|
|
|
13.04672 km
|
2012-04-29 00:02:01 +08:00
|
|
|
>>> print(d2 - d1) # Subtracting 5 kilometers from 5 miles
|
2010-03-27 04:14:53 +08:00
|
|
|
1.89314403881 mi
|
|
|
|
|
|
|
|
Two :class:`Distance` objects multiplied together will yield an :class:`Area`
|
|
|
|
object, which uses squared units of measure::
|
|
|
|
|
|
|
|
>>> a = d1 * d2 # Returns an Area object.
|
2012-04-29 00:02:01 +08:00
|
|
|
>>> print(a)
|
2010-03-27 04:14:53 +08:00
|
|
|
40.2336 sq_km
|
|
|
|
|
|
|
|
To determine what the attribute abbreviation of a unit is, the ``unit_attname``
|
|
|
|
class method may be used::
|
|
|
|
|
2012-04-29 00:02:01 +08:00
|
|
|
>>> print(Distance.unit_attname('US Survey Foot'))
|
2010-03-27 04:14:53 +08:00
|
|
|
survey_ft
|
2012-04-29 00:02:01 +08:00
|
|
|
>>> print(Distance.unit_attname('centimeter'))
|
2010-03-27 04:14:53 +08:00
|
|
|
cm
|
|
|
|
|
|
|
|
.. _supported_units:
|
|
|
|
|
|
|
|
Supported units
|
|
|
|
===============
|
|
|
|
|
|
|
|
================================= ========================================
|
|
|
|
Unit Attribute Full name or alias(es)
|
|
|
|
================================= ========================================
|
2010-08-20 03:27:44 +08:00
|
|
|
``km`` Kilometre, Kilometer
|
2010-03-27 04:14:53 +08:00
|
|
|
``mi`` Mile
|
|
|
|
``m`` Meter, Metre
|
|
|
|
``yd`` Yard
|
|
|
|
``ft`` Foot, Foot (International)
|
|
|
|
``survey_ft`` U.S. Foot, US survey foot
|
|
|
|
``inch`` Inches
|
|
|
|
``cm`` Centimeter
|
|
|
|
``mm`` Millimetre, Millimeter
|
|
|
|
``um`` Micrometer, Micrometre
|
|
|
|
``british_ft`` British foot (Sears 1922)
|
|
|
|
``british_yd`` British yard (Sears 1922)
|
|
|
|
``british_chain_sears`` British chain (Sears 1922)
|
|
|
|
``indian_yd`` Indian yard, Yard (Indian)
|
|
|
|
``sears_yd`` Yard (Sears)
|
|
|
|
``clarke_ft`` Clarke's Foot
|
|
|
|
``chain`` Chain
|
|
|
|
``chain_benoit`` Chain (Benoit)
|
|
|
|
``chain_sears`` Chain (Sears)
|
|
|
|
``british_chain_benoit`` British chain (Benoit 1895 B)
|
|
|
|
``british_chain_sears_truncated`` British chain (Sears 1922 truncated)
|
|
|
|
``gold_coast_ft`` Gold Coast foot
|
|
|
|
``link`` Link
|
|
|
|
``link_benoit`` Link (Benoit)
|
|
|
|
``link_sears`` Link (Sears)
|
|
|
|
``clarke_link`` Clarke's link
|
|
|
|
``fathom`` Fathom
|
|
|
|
``rod`` Rod
|
2019-04-09 02:17:05 +08:00
|
|
|
``furlong`` Furlong, Furrow Long
|
2010-03-27 04:14:53 +08:00
|
|
|
``nm`` Nautical Mile
|
|
|
|
``nm_uk`` Nautical Mile (UK)
|
|
|
|
``german_m`` German legal metre
|
|
|
|
================================= ========================================
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
:class:`Area` attributes are the same as :class:`Distance` attributes,
|
|
|
|
except they are prefixed with ``sq_`` (area units are square in nature).
|
|
|
|
For example, ``Area(sq_m=2)`` creates an :class:`Area` object
|
|
|
|
representing two square meters.
|
|
|
|
|
|
|
|
Measurement API
|
|
|
|
===============
|
|
|
|
|
|
|
|
``Distance``
|
|
|
|
------------
|
|
|
|
|
|
|
|
.. class:: Distance(**kwargs)
|
|
|
|
|
2016-02-19 15:31:25 +08:00
|
|
|
To initialize a distance object, pass in a keyword corresponding to the
|
|
|
|
desired :ref:`unit attribute name <supported_units>` set with desired
|
|
|
|
value. For example, the following creates a distance object representing 5
|
|
|
|
miles::
|
2010-03-27 04:14:53 +08:00
|
|
|
|
2016-02-19 15:31:25 +08:00
|
|
|
>>> dist = Distance(mi=5)
|
2010-03-27 04:14:53 +08:00
|
|
|
|
2016-02-19 15:31:25 +08:00
|
|
|
.. method:: __getattr__(unit_att)
|
2010-03-27 04:14:53 +08:00
|
|
|
|
2016-02-19 15:31:25 +08:00
|
|
|
Returns the distance value in units corresponding to the given unit
|
|
|
|
attribute. For example::
|
2010-03-27 04:14:53 +08:00
|
|
|
|
2016-02-19 15:31:25 +08:00
|
|
|
>>> print(dist.km)
|
|
|
|
8.04672
|
2010-03-27 04:14:53 +08:00
|
|
|
|
2016-02-19 15:31:25 +08:00
|
|
|
.. classmethod:: unit_attname(unit_name)
|
2010-03-27 04:14:53 +08:00
|
|
|
|
2016-02-19 15:31:25 +08:00
|
|
|
Returns the distance unit attribute name for the given full unit name. For
|
|
|
|
example::
|
2010-03-27 04:14:53 +08:00
|
|
|
|
2016-02-19 15:31:25 +08:00
|
|
|
>>> Distance.unit_attname('Mile')
|
|
|
|
'mi'
|
2010-03-27 04:14:53 +08:00
|
|
|
|
|
|
|
.. class:: D
|
|
|
|
|
2016-02-19 15:31:25 +08:00
|
|
|
Alias for :class:`Distance` class.
|
2010-03-27 04:14:53 +08:00
|
|
|
|
|
|
|
``Area``
|
|
|
|
--------
|
|
|
|
|
|
|
|
.. class:: Area(**kwargs)
|
|
|
|
|
2016-02-19 15:31:25 +08:00
|
|
|
To initialize an area object, pass in a keyword corresponding to the
|
|
|
|
desired :ref:`unit attribute name <supported_units>` set with desired
|
|
|
|
value. For example, the following creates an area object representing 5
|
|
|
|
square miles::
|
2010-03-27 04:14:53 +08:00
|
|
|
|
2016-02-19 15:31:25 +08:00
|
|
|
>>> a = Area(sq_mi=5)
|
2010-03-27 04:14:53 +08:00
|
|
|
|
2016-02-19 15:31:25 +08:00
|
|
|
.. method:: __getattr__(unit_att)
|
2010-03-27 04:14:53 +08:00
|
|
|
|
2016-02-19 15:31:25 +08:00
|
|
|
Returns the area value in units corresponding to the given unit attribute.
|
|
|
|
For example::
|
2010-03-27 04:14:53 +08:00
|
|
|
|
2016-02-19 15:31:25 +08:00
|
|
|
>>> print(a.sq_km)
|
|
|
|
12.949940551680001
|
2010-03-27 04:14:53 +08:00
|
|
|
|
2016-02-19 15:31:25 +08:00
|
|
|
.. classmethod:: unit_attname(unit_name)
|
2010-08-20 03:27:44 +08:00
|
|
|
|
2016-02-19 15:31:25 +08:00
|
|
|
Returns the area unit attribute name for the given full unit name. For
|
|
|
|
example::
|
2010-03-27 04:14:53 +08:00
|
|
|
|
|
|
|
>>> Area.unit_attname('Kilometer')
|
|
|
|
'sq_km'
|
|
|
|
|
|
|
|
.. class:: A
|
|
|
|
|
2016-02-19 15:31:25 +08:00
|
|
|
Alias for :class:`Area` class.
|
2010-03-27 04:14:53 +08:00
|
|
|
|
|
|
|
.. rubric:: Footnotes
|
2013-12-09 01:39:26 +08:00
|
|
|
.. [#] `Robert Coup <https://koordinates.com/>`_ is the initial author of the measure objects,
|
2015-08-08 19:56:37 +08:00
|
|
|
and was inspired by Brian Beck's work in `geopy <https://github.com/geopy/geopy/>`_
|
2010-03-27 04:14:53 +08:00
|
|
|
and Geoff Biggs' PhD work on dimensioned units for robotics.
|