measure.txt 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 Distance, D
  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. Conversions are easy, just access the preferred unit attribute to get a
  24. converted 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. ``nm`` Nautical Mile
  81. ``nm_uk`` Nautical Mile (UK)
  82. ``german_m`` German legal metre
  83. ================================= ========================================
  84. .. note::
  85. :class:`Area` attributes are the same as :class:`Distance` attributes,
  86. except they are prefixed with ``sq_`` (area units are square in nature).
  87. For example, ``Area(sq_m=2)`` creates an :class:`Area` object
  88. representing two square meters.
  89. Measurement API
  90. ===============
  91. ``Distance``
  92. ------------
  93. .. class:: Distance(**kwargs)
  94. To initialize a distance object, pass in a keyword corresponding to
  95. the desired :ref:`unit attribute name <supported_units>` set with
  96. desired value. For example, the following creates a distance
  97. object representing 5 miles::
  98. >>> dist = Distance(mi=5)
  99. .. method:: __getattr__(unit_att)
  100. Returns the distance value in units corresponding to the given unit
  101. attribute. For example::
  102. >>> print(dist.km)
  103. 8.04672
  104. .. classmethod:: unit_attname(unit_name)
  105. Returns the distance unit attribute name for the given full unit name.
  106. For example::
  107. >>> Distance.unit_attname('Mile')
  108. 'mi'
  109. .. class:: D
  110. Alias for :class:`Distance` class.
  111. ``Area``
  112. --------
  113. .. class:: Area(**kwargs)
  114. To initialize an area object, pass in a keyword corresponding to
  115. the desired :ref:`unit attribute name <supported_units>` set with
  116. desired value. For example, the following creates an area
  117. object representing 5 square miles::
  118. >>> a = Area(sq_mi=5)
  119. .. method:: __getattr__(unit_att)
  120. Returns the area value in units corresponding to the given unit
  121. attribute. For example::
  122. >>> print(a.sq_km)
  123. 12.949940551680001
  124. .. classmethod:: unit_attname(unit_name)
  125. Returns the area unit attribute name for the given full unit name.
  126. For example::
  127. >>> Area.unit_attname('Kilometer')
  128. 'sq_km'
  129. .. class:: A
  130. Alias for :class:`Area` class.
  131. .. rubric:: Footnotes
  132. .. [#] `Robert Coup <https://koordinates.com/>`_ is the initial author of the measure objects,
  133. and was inspired by Brian Beck's work in `geopy <http://code.google.com/p/geopy/>`_
  134. and Geoff Biggs' PhD work on dimensioned units for robotics.