LinearUnitConversionFactor
汇总
返回将距离测量值转换为其他线性单位的因子。
讨论
该函数支持的线性单位与从地理处理 线性单位 参数数据类型返回的单位相同,例外情况如下:
Unknown- 只能转换已知单位。Decimal Degrees- 十进制度在球体上测量并且与其他单位的转换因子不一致。
语法
LinearUnitConversionFactor(from_unit, to_unit)
| 参数 | 说明 | 数据类型 |
|---|---|---|
|
from_unit |
Specifies the original or source linear unit.
|
String |
|
to_unit |
Specifies the target or destination linear unit.
|
String |
返回值
| 数据类型 | 说明 |
|---|---|
|
Double |
用于在两个线性单位之间转换值的转换因子。 |
代码示例
从国际英里转换为千米的简单用例。
import arcpy
# Convert 620.5 MilesInt to Kilometers
area = 620.5 * arcpy.LinearUnitConversionFactor(from_unit="MilesInt", to_unit="Kilometers")
print(area) # This will print a value of 998.597952
在脚本工具中,将指定值转换为米以进行处理。
import arcpy
import locale
import sys
dist = arcpy.GetParameter(n) # Update n to the parameter index
dist_value, dist_unit = dist.split(" ") # For example, "10 Kilometers"
# Convert the distance into "Meters" as needed later in this script
try:
conv_factor = arcpy.LinearUnitConversionFactor(dist_unit, "Meters")
except ValueError as e:
# If it fails, the likely distance unit is "Unknown" or "Decimal Degrees".
# Add code to either deal with it or produce an appropriate error as shown below.
arcpy.AddError('Invalid linear unit type.')
sys.exit()
# Apply the conv_factor to the supplied value
# locale.atof is required for locales that don't use a period as the separator
dist_m = locale.atof(dist_value) * conv_factor