Summary
Returns the factor for converting a distance measurement to a different linear unit.
Discussion
The linear units supported by the function are the same units returned from the geoprocessing Linear Units parameter data type, with the following exceptions:
- Unknown—Only known units can be converted.
- Decimal Degrees—Decimal degrees are measured on a sphere and do not have a consistent conversion factor to the other units.
Syntax
LinearUnitConversionFactor (from_unit, to_unit)
Parameter | Explanation | Data Type |
from_unit | The original or source linear unit.
| String |
to_unit | The target or destination linear unit.
| String |
Data Type | Explanation |
Double | The conversion factor for converting values between the two linear units. |
Code sample
A simple use case for converting from international miles to kilometers.
import arcpy
# Convert 620.5 MilesInt to Kilometers
620.5 * arcpy.LinearUnitConversionFactor(from_unit="MilesInt", to_unit="Kilometers")
print(area) # This will print a value of 998.597952
Within a script tool, convert a specified value to meters for processing.
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 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