Measurement Objects

The django.contrib.gis.measure module contains objects that allow for convenient representation of distance and area units of measure. [1] Specifically, it implements two objects, Distance and Area – both of which may be accessed via the D and A convenience aliases, respectively.

Example

Distance objects may be instantiated using a keyword argument indicating the context of the units. In the example below, two different distance objects are instantiated in units of kilometers (km) and miles (mi):

  1. >>> from django.contrib.gis.measure import D, Distance
  2. >>> d1 = Distance(km=5)
  3. >>> print(d1)
  4. 5.0 km
  5. >>> d2 = D(mi=5) # `D` is an alias for `Distance`
  6. >>> print(d2)
  7. 5.0 mi

For conversions, access the preferred unit attribute to get a converted distance quantity:

  1. >>> print(d1.mi) # Converting 5 kilometers to miles
  2. 3.10685596119
  3. >>> print(d2.km) # Converting 5 miles to kilometers
  4. 8.04672

Moreover, arithmetic operations may be performed between the distance objects:

  1. >>> print(d1 + d2) # Adding 5 miles to 5 kilometers
  2. 13.04672 km
  3. >>> print(d2 - d1) # Subtracting 5 kilometers from 5 miles
  4. 1.89314403881 mi

Two Distance objects multiplied together will yield an Area object, which uses squared units of measure:

  1. >>> a = d1 * d2 # Returns an Area object.
  2. >>> print(a)
  3. 40.2336 sq_km

To determine what the attribute abbreviation of a unit is, the unit_attname class method may be used:

  1. >>> print(Distance.unit_attname("US Survey Foot"))
  2. survey_ft
  3. >>> print(Distance.unit_attname("centimeter"))
  4. cm

Supported units

Unit AttributeFull name or alias(es)
kmKilometre, Kilometer
miMile
mMeter, Metre
ydYard
ftFoot, Foot (International)
survey_ftU.S. Foot, US survey foot
inchInches
cmCentimeter
mmMillimetre, Millimeter
umMicrometer, Micrometre
british_ftBritish foot (Sears 1922)
british_ydBritish yard (Sears 1922)
british_chain_searsBritish chain (Sears 1922)
indian_ydIndian yard, Yard (Indian)
sears_ydYard (Sears)
clarke_ftClarke’s Foot
chainChain
chain_benoitChain (Benoit)
chain_searsChain (Sears)
british_chain_benoitBritish chain (Benoit 1895 B)
british_chain_sears_truncatedBritish chain (Sears 1922 truncated)
gold_coast_ftGold Coast foot
linkLink
link_benoitLink (Benoit)
link_searsLink (Sears)
clarke_linkClarke’s link
fathomFathom
rodRod
furlongFurlong, Furrow Long
nmNautical Mile
nm_ukNautical Mile (UK)
german_mGerman legal metre

Note

Area attributes are the same as Distance attributes, except they are prefixed with sq_ (area units are square in nature). For example, Area(sq_m=2) creates an Area object representing two square meters.

Measurement API

Distance

class Distance(**kwargs)[source]

To initialize a distance object, pass in a keyword corresponding to the desired unit attribute name set with desired value. For example, the following creates a distance object representing 5 miles:

  1. >>> dist = Distance(mi=5)
  • __getattr__(unit_att)

Returns the distance value in units corresponding to the given unit attribute. For example:

  1. >>> print(dist.km)
  2. 8.04672
  • classmethod unit_attname(unit_name)

Returns the distance unit attribute name for the given full unit name. For example:

  1. >>> Distance.unit_attname("Mile")
  2. 'mi'

class D

Alias for Distance class.

Area

class Area(**kwargs)[source]

To initialize an area object, pass in a keyword corresponding to the desired unit attribute name set with desired value. For example, the following creates an area object representing 5 square miles:

  1. >>> a = Area(sq_mi=5)
  • __getattr__(unit_att)

Returns the area value in units corresponding to the given unit attribute. For example:

  1. >>> print(a.sq_km)
  2. 12.949940551680001
  • classmethod unit_attname(unit_name)

Returns the area unit attribute name for the given full unit name. For example:

  1. >>> Area.unit_attname("Kilometer")
  2. 'sq_km'

class A

Alias for Area class.

Footnotes

[1]Robert Coup is the initial author of the measure objects, and was inspired by Brian Beck’s work in geopy and Geoff Biggs’ PhD work on dimensioned units for robotics.