我一直在尝试实现这段代码,但是由于我对numpy非常陌生,所以我对这个错误知之甚少。
import numpy as np
from sympy import *
import matplotlib.pyplot as plt
def asSpherical(coord):
x=coord[0]
y=coord[1]
z=coord[2]
azimuth = np.arctan2(y,x)
elevation = np.arctan2(z,np.sqrt(x**2 + y**2))
r = np.sqrt(x**2 + y**2 + z**2)
return [azimuth, elevation, r]
def ecefToEnu(lamb, phi, coord_set):
# @param1 is lattitude
# @param2 is longitude
# @param3 is coordinate set in form of a list
# R matrix of size 3x3
# This transforms the ECEF coordinates to ENU
trans_matrix = [
[-np.sin(lamb), np.cos(lamb), 0],
[-(np.cos(lamb))*np.sin(phi), -
np.sin(lamb)*np.sin(phi), np.cos(phi)],
[np.cos(lamb)*np.cos(phi), np.sin(lamb)*np.cos(phi), sin(phi)]
]
# Performs the matrix multiplication
enu_coords = np.matmul(trans_matrix, coord_set)
# Returns a list of Cartesian Coordinates
sph_coords = asSpherical(enu_coords)
return sph_coords
coord_list = []
print("Enter the latitude")
latitude = float(input())
print("Enter the coordinate list:")
print("Enter X:")
coord_list.append(float(input()))
print("Enter Y:")
coord_list.append(float(input()))
print("Enter Z:")
coord_list.append(float(input()))
phiDegSet = np.arange(-180, 180, 1)
Nset = len(phiDegSet)
t0 = np.linspace(0, 24, Nset)
t0 = list(t0)
res = []
for i in range(0, 360):
temp_list = ecefToEnu(latitude, phiDegSet[i], coord_list)
res.append(temp_list)
elevDegSet = []
for i in res:
elevDegSet.append(i[1])
这给了我一个错误:
elevation = np.arctan2(z,np.sqrt(x**2 + y**2))
AttributeError: 'Float' object has no attribute 'arctan2'
我已经将该值作为float传递给该函数。即使是进口的numpy也能正常工作。
发布于 2022-02-11 08:51:59
我在print
之前添加了一个arctan
语句
def asSpherical(coord):
x=coord[0]
y=coord[1]
z=coord[2]
print(x,y,z)
azimuth = np.arctan2(y,x)
...
得到了这个结果:
....
ter the latitude
45
Enter the coordinate list:
Enter X:
45
Enter Y:
45
Enter Z:
45
-14.65116910723749 -76.54620448997258 -37.062720709188 - 45.0*sin(180)
Traceback (most recent call last):
File "stack71078990.py", line 52, in <module>
temp_list = ecefToEnu(latitude, phiDegSet[i], coord_list)
File "stack71078990.py", line 31, in ecefToEnu
sph_coords = asSpherical(enu_coords)
File "stack71078990.py", line 11, in asSpherical
elevation = np.arctan2(z,np.sqrt(x**2 + y**2))
AttributeError: 'Add' object has no attribute 'arctan2'
z
是-37.062720709188 - 45.0*sin(180)
,其中sin(180)
是sympy
表达式。
我在sin(phi)
表达式中看到了trans_matrix
。由于from sympy import *
,sin
(而不是np.sin
)是sympy
函数。
删除该导入(正如我评论的那样)会引发:
Traceback (most recent call last):
File "stack71078990.py", line 52, in <module>
temp_list = ecefToEnu(latitude, phiDegSet[i], coord_list)
File "stack71078990.py", line 26, in ecefToEnu
[np.cos(lamb)*np.cos(phi), np.sin(lamb)*np.cos(phi), sin(phi)]
NameError: name 'sin' is not defined
将其更改为np.sin
(与定义中的其他函数一样),可以让脚本运行而不会出错。
https://stackoverflow.com/questions/71078990
复制相似问题