来自地理信息系统Stack Exchange的

This question was migrated,因为可以在Stack Overflow上进行回答。
                            Migrated 6年前。
                        
                    
                
                            
                    
我正在尝试在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

最佳答案

好的,这是一个清理后的版本,上面有所有注释,加上逻辑上的小改动和一些变量清理。
请注意,这不是球体上的真实方位角。

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

08-24 16:08