measure.txt 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. ===================
  2. Measurement Objects
  3. ===================
  4. .. module:: django.contrib.gis.measure
  5. :synopsis: GeoDjango's distance and area measurement objects.
  6. The :mod:`django.contrib.gis.measure` module contains objects that allow
  7. for convenient representation of distance and area units of measure. [#]_
  8. Specifically, it implements two objects, :class:`Distance` and
  9. :class:`Area` -- both of which may be accessed via the
  10. :class:`D` and :class:`A` convenience aliases, respectively.
  11. Example
  12. =======
  13. :class:`Distance` objects may be instantiated using a keyword argument indicating the
  14. context of the units. In the example below, two different distance objects are
  15. instantiated in units of kilometers (``km``) and miles (``mi``)::
  16. >>> from django.contrib.gis.measure import D, Distance
  17. >>> d1 = Distance(km=5)
  18. >>> print(d1)
  19. 5.0 km
  20. >>> d2 = D(mi=5) # `D` is an alias for `Distance`
  21. >>> print(d2)
  22. 5.0 mi
  23. For conversions, access the preferred unit attribute to get a converted
  24. distance quantity::
  25. >>> print(d1.mi) # Converting 5 kilometers to miles
  26. 3.10685596119
  27. >>> print(d2.km) # Converting 5 miles to kilometers
  28. 8.04672
  29. Moreover, arithmetic operations may be performed between the distance
  30. objects::
  31. >>> print(d1 + d2) # Adding 5 miles to 5 kilometers
  32. 13.04672 km
  33. >>> print(d2 - d1) # Subtracting 5 kilometers from 5 miles
  34. 1.89314403881 mi
  35. Two :class:`Distance` objects multiplied together will yield an :class:`Area`
  36. object, which uses squared units of measure::
  37. >>> a = d1 * d2 # Returns an Area object.
  38. >>> print(a)
  39. 40.2336 sq_km
  40. To determine what the attribute abbreviation of a unit is, the ``unit_attname``
  41. class method may be used::
  42. >>> print(Distance.unit_attname('US Survey Foot'))
  43. survey_ft
  44. >>> print(Distance.unit_attname('centimeter'))
  45. cm
  46. .. _supported_units:
  47. Supported units
  48. ===============
  49. ================================= ========================================
  50. Unit Attribute Full name or alias(es)
  51. ================================= ========================================
  52. ``km`` Kilometre, Kilometer
  53. ``mi`` Mile
  54. ``m`` Meter, Metre
  55. ``yd`` Yard
  56. ``ft`` Foot, Foot (International)
  57. ``survey_ft`` U.S. Foot, US survey foot
  58. ``inch`` Inches
  59. ``cm`` Centimeter
  60. ``mm`` Millimetre, Millimeter
  61. ``um`` Micrometer, Micrometre
  62. ``british_ft`` British foot (Sears 1922)
  63. ``british_yd`` British yard (Sears 1922)
  64. ``british_chain_sears`` British chain (Sears 1922)
  65. ``indian_yd`` Indian yard, Yard (Indian)
  66. ``sears_yd`` Yard (Sears)
  67. ``clarke_ft`` Clarke's Foot
  68. ``chain`` Chain
  69. ``chain_benoit`` Chain (Benoit)
  70. ``chain_sears`` Chain (Sears)
  71. ``british_chain_benoit`` British chain (Benoit 1895 B)
  72. ``british_chain_sears_truncated`` British chain (Sears 1922 truncated)
  73. ``gold_coast_ft`` Gold Coast foot
  74. ``link`` Link
  75. ``link_benoit`` Link (Benoit)
  76. ``link_sears`` Link (Sears)
  77. ``clarke_link`` Clarke's link
  78. ``fathom`` Fathom
  79. ``rod`` Rod
  80. ``furlong`` Furlong, Furrow Long
  81. ``nm`` Nautical Mile
  82. ``nm_uk`` Nautical Mile (UK)
  83. ``german_m`` German legal metre
  84. ================================= ========================================
  85. .. note::
  86. :class:`Area` attributes are the same as :class:`Distance` attributes,
  87. except they are prefixed with ``sq_`` (area units are square in nature).
  88. For example, ``Area(sq_m=2)`` creates an :class:`Area` object
  89. representing two square meters.
  90. Measurement API
  91. ===============
  92. ``Distance``
  93. ------------
  94. .. class:: Distance(**kwargs)
  95. To initialize a distance object, pass in a keyword corresponding to the
  96. desired :ref:`unit attribute name <supported_units>` set with desired
  97. value. For example, the following creates a distance object representing 5
  98. miles::
  99. >>> dist = Distance(mi=5)
  100. .. method:: __getattr__(unit_att)
  101. Returns the distance value in units corresponding to the given unit
  102. attribute. For example::
  103. >>> print(dist.km)
  104. 8.04672
  105. .. classmethod:: unit_attname(unit_name)
  106. Returns the distance unit attribute name for the given full unit name. For
  107. example::
  108. >>> Distance.unit_attname('Mile')
  109. 'mi'
  110. .. class:: D
  111. Alias for :class:`Distance` class.
  112. ``Area``
  113. --------
  114. .. class:: Area(**kwargs)
  115. To initialize an area object, pass in a keyword corresponding to the
  116. desired :ref:`unit attribute name <supported_units>` set with desired
  117. value. For example, the following creates an area object representing 5
  118. square miles::
  119. >>> a = Area(sq_mi=5)
  120. .. method:: __getattr__(unit_att)
  121. Returns the area value in units corresponding to the given unit attribute.
  122. For example::
  123. >>> print(a.sq_km)
  124. 12.949940551680001
  125. .. classmethod:: unit_attname(unit_name)
  126. Returns the area unit attribute name for the given full unit name. For
  127. example::
  128. >>> Area.unit_attname('Kilometer')
  129. 'sq_km'
  130. .. class:: A
  131. Alias for :class:`Area` class.
  132. .. rubric:: Footnotes
  133. .. [#] `Robert Coup <https://koordinates.com/>`_ is the initial author of the measure objects,
  134. and was inspired by Brian Beck's work in `geopy <https://github.com/geopy/geopy/>`_
  135. and Geoff Biggs' PhD work on dimensioned units for robotics.