measure.txt 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. .. _ref-measure:
  2. ===================
  3. Measurement Objects
  4. ===================
  5. .. module:: django.contrib.gis.measure
  6. :synopsis: GeoDjango's distance and area measurment objects.
  7. The :mod:`django.contrib.gis.measure` module contains objects that allow
  8. for convenient representation of distance and area units of measure. [#]_
  9. Specifically, it implements two objects, :class:`Distance` and
  10. :class:`Area` -- both of which may be accessed via the
  11. :class:`D` and :class:`A` convenience aliases, respectively.
  12. Example
  13. =======
  14. :class:`Distance` objects may be instantiated using a keyword argument indicating the
  15. context of the units. In the example below, two different distance objects are
  16. instantiated in units of kilometers (``km``) and miles (``mi``)::
  17. >>> from django.contrib.gis.measure import Distance, D
  18. >>> d1 = Distance(km=5)
  19. >>> print(d1)
  20. 5.0 km
  21. >>> d2 = D(mi=5) # `D` is an alias for `Distance`
  22. >>> print(d2)
  23. 5.0 mi
  24. Conversions are easy, just access the preferred unit attribute to get a
  25. converted distance quantity::
  26. >>> print(d1.mi) # Converting 5 kilometers to miles
  27. 3.10685596119
  28. >>> print(d2.km) # Converting 5 miles to kilometers
  29. 8.04672
  30. Moreover, arithmetic operations may be performed between the distance
  31. objects::
  32. >>> print(d1 + d2) # Adding 5 miles to 5 kilometers
  33. 13.04672 km
  34. >>> print(d2 - d1) # Subtracting 5 kilometers from 5 miles
  35. 1.89314403881 mi
  36. Two :class:`Distance` objects multiplied together will yield an :class:`Area`
  37. object, which uses squared units of measure::
  38. >>> a = d1 * d2 # Returns an Area object.
  39. >>> print(a)
  40. 40.2336 sq_km
  41. To determine what the attribute abbreviation of a unit is, the ``unit_attname``
  42. class method may be used::
  43. >>> print(Distance.unit_attname('US Survey Foot'))
  44. survey_ft
  45. >>> print(Distance.unit_attname('centimeter'))
  46. cm
  47. .. _supported_units:
  48. Supported units
  49. ===============
  50. ================================= ========================================
  51. Unit Attribute Full name or alias(es)
  52. ================================= ========================================
  53. ``km`` Kilometre, Kilometer
  54. ``mi`` Mile
  55. ``m`` Meter, Metre
  56. ``yd`` Yard
  57. ``ft`` Foot, Foot (International)
  58. ``survey_ft`` U.S. Foot, US survey foot
  59. ``inch`` Inches
  60. ``cm`` Centimeter
  61. ``mm`` Millimetre, Millimeter
  62. ``um`` Micrometer, Micrometre
  63. ``british_ft`` British foot (Sears 1922)
  64. ``british_yd`` British yard (Sears 1922)
  65. ``british_chain_sears`` British chain (Sears 1922)
  66. ``indian_yd`` Indian yard, Yard (Indian)
  67. ``sears_yd`` Yard (Sears)
  68. ``clarke_ft`` Clarke's Foot
  69. ``chain`` Chain
  70. ``chain_benoit`` Chain (Benoit)
  71. ``chain_sears`` Chain (Sears)
  72. ``british_chain_benoit`` British chain (Benoit 1895 B)
  73. ``british_chain_sears_truncated`` British chain (Sears 1922 truncated)
  74. ``gold_coast_ft`` Gold Coast foot
  75. ``link`` Link
  76. ``link_benoit`` Link (Benoit)
  77. ``link_sears`` Link (Sears)
  78. ``clarke_link`` Clarke's link
  79. ``fathom`` Fathom
  80. ``rod`` Rod
  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
  96. the desired :ref:`unit attribute name <supported_units>` set with
  97. desired value. For example, the following creates a distance
  98. object representing 5 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.
  107. For 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
  116. the desired :ref:`unit attribute name <supported_units>` set with
  117. desired value. For example, the following creates an area
  118. object representing 5 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
  122. attribute. 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.
  127. For example::
  128. >>> Area.unit_attname('Kilometer')
  129. 'sq_km'
  130. .. class:: A
  131. Alias for :class:`Area` class.
  132. .. rubric:: Footnotes
  133. .. [#] `Robert Coup <http://koordinates.com/>`_ is the initial author of the measure objects,
  134. and was inspired by Brian Beck's work in `geopy <http://code.google.com/p/geopy/>`_
  135. and Geoff Biggs' PhD work on dimensioned units for robotics.