1

我正在尝试在 arcgis 10 中使用 Python 计算方位角,但出现语法错误。这是我的代码:

def CalculaAzimuth(Linea):
    Xorigen = linea.firstPoint.Y
    Yorigen = linea.firstPoint.X
    Xfinal = linea.lastPoint.X
    Yfinal = linea.lastPoint.Y
    DeltaX = Xfinal - Xorigen
    DeltaY = Yfinal - Xorigen
    PI = math.pi()
    Azimuth = 4 * PI
    if DeltaX = 0:
        if DeltaY >=0:
            Azimuth = 0
        else:
            Azimuth = 180
    elif DeltaX >0:
        Azimuth = 90 - math.atan( DeltaY / DeltaX ) * 180 / PI
    elif DeltaX <0:
        Azimuth = 270 - math.atan( DeltaY / DeltaX )* 180 / PI

return Azimuth
4

1 回答 1

4

好的,这是一个经过清理的版本,上面包含所有注释以及逻辑上的小改动和一些变量清理。请注意,这不是椭球体上的真正方位角。

def CalculaAzimuth(linea):
    if (hasattr(linea,'type') and linea.type == 'polyline'):
        xf = linea.firstPoint.X
        yf = linea.firstPoint.Y
        xl = linea.lastPoint.X
        yl = linea.lastPoint.Y
        dX = xl - xf
        dY = yl - yf
        PI = math.pi
        Azimuth = 0 #Default case, dX = 0 and dY >= 0
        if dX > 0:
            Azimuth = 90 - math.atan( dY / dX ) * 180 / PI
        elif dX < 0:
            Azimuth = 270 - math.atan( dY / dX )* 180 / PI
        elif dY < 0:
            Azimuth = 180
        return Azimuth
    else:
        return False
于 2013-03-18T14:52:43.320 回答