Объекты измерения¶
Модуль django.contrib.gis.measure содержит объекты, которые позволяют удобно представлять единицы измерения расстояния и площади. [1] В частности, он реализует два объекта: Distance и Area — к обоим из которых можно получить доступ через удобные псевдонимы D и A соответственно.
Пример¶
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):
>>> from django.contrib.gis.measure import D, Distance
>>> d1 = Distance(km=5)
>>> print(d1)
5.0 km
>>> d2 = D(mi=5) # `D` is an alias for `Distance`
>>> print(d2)
5.0 mi
For conversions, access the preferred unit attribute to get a converted distance quantity:
>>> print(d1.mi) # Converting 5 kilometers to miles
3.10685596119
>>> print(d2.km) # Converting 5 miles to kilometers
8.04672
Moreover, arithmetic operations may be performed between the distance objects:
>>> print(d1 + d2) # Adding 5 miles to 5 kilometers
13.04672 km
>>> print(d2 - d1) # Subtracting 5 kilometers from 5 miles
1.89314403881 mi
Two Distance objects multiplied together will yield an Area
object, which uses squared units of measure:
>>> a = d1 * d2 # Returns an Area object.
>>> print(a)
40.2336 sq_km
To determine what the attribute abbreviation of a unit is, the unit_attname
class method may be used:
>>> print(Distance.unit_attname('US Survey Foot'))
survey_ft
>>> print(Distance.unit_attname('centimeter'))
cm
Поддерживаемые единицы¶
Атрибут единицы |
Полное имя или псевдоним(а) |
|---|---|
|
Километр, Километр |
|
Миля |
|
Метр, Метр |
|
Площадка |
|
Фут, Фут (международный) |
|
Фут США, геодезический фут США |
|
Дюймы |
|
Сантиметр |
|
Миллиметр, Миллиметр |
`` хм`` |
Микрометр, Микрометр |
|
Британская нога (Сирс, 1922) |
|
Британский двор (Сирс, 1922) |
|
Британская сеть магазинов (Sears, 1922 г.) |
|
Индийский двор, Двор (Индийский) |
|
Ярд (Сирс) |
|
Нога Кларка |
|
Цепь |
|
Цепочка (Бенуа) |
|
Цепочка (Сирс) |
|
Британская цепь (Бенуа 1895 Б) |
|
Британская сеть (Sears, 1922 г., усеченная) |
|
Нога Золотого Берега |
|
Связь |
|
Линк (Бенуа) |
|
Линк (Сирс) |
|
ссылка Кларка |
|
глубина |
|
Стержень |
|
Фарлонг, Борозда длинная |
|
Морская миля |
|
Морская миля (Великобритания) |
|
Немецкий юридический счетчик |
API измерений¶
Расстояние¶
- class Distance(**kwargs)¶
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:
>>> dist = Distance(mi=5)
- __getattr__(unit_att)¶
Returns the distance value in units corresponding to the given unit attribute. For example:
>>> print(dist.km) 8.04672
- classmethod unit_attname(unit_name)¶
Returns the distance unit attribute name for the given full unit name. For example:
>>> Distance.unit_attname('Mile') 'mi'
Площадь¶
- class Area(**kwargs)¶
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:
>>> a = Area(sq_mi=5)
- __getattr__(unit_att)¶
Returns the area value in units corresponding to the given unit attribute. For example:
>>> print(a.sq_km) 12.949940551680001
- classmethod unit_attname(unit_name)¶
Returns the area unit attribute name for the given full unit name. For example:
>>> Area.unit_attname('Kilometer') 'sq_km'
Сноски