import pyproj import numpy as np 定义地理右手坐标系 projection = pyproj.Proj(proj='cart',…
- User
import pyproj import numpy as np
定义地理右手坐标系
projection = pyproj.Proj(proj='cart', ellps='WGS84', datum='WGS84')
定义数学笛卡尔坐标系
cartesian_coords = np.array([[5.051966,1.606852,0.951975]])
[x2, y2, z2],
…
[xn, yn, zn]])
cartesian_coords = [5.051966,1.606852,0.951975]
转换坐标系
lon, lat, _ = projection(cartesian_coords[:,0], cartesian_coords[:,1], cartesian_coords[:,2])
输出地理右手坐标系坐标
for i in range(len(lon)): print("经度:{}".format(lon[i])) print("纬度:{}".format(lat[i])) print("---------") Traceback (most recent call last): File "e:\桌面\新建文件夹\1.py", line 15, in lon, lat, _ = projection(cartesian_coords[:,0], cartesian_coords[:,1], cartesian_coords[:,2]) TypeError: list indices must be integers or slices, not tuple
- Assistant
The error in the code is due to the fact that
cartesian_coordsis a list, not a NumPy array, and thus cannot be indexed using[:,0],[:,1],[:,2].To fix the error, you can either use a NumPy array instead of a list for
cartesian_coords, or modify the code to directly access the elements of the list without using indexing.Here's the modified code using a list:
# 转换坐标系 lon, lat, _ = projection(cartesian_coords[0], cartesian_coords[1], cartesian_coords[2]) # 输出地理右手坐标系坐标 print("经度:{}".format(lon)) print("纬度:{}".format(lat))Or, here's the modified code using a NumPy array:
# 转换坐标系 lon, lat, _ = projection(cartesian_coords[:,0], cartesian_coords[:,1], cartesian_coords[:,2]) # 输出地理右手坐标系坐标 for i in range(len(lon)): print("经度:{}".format(lon[i])) print("纬度:{}".format(lat[i])) print("---------")Make sure to choose the method that matches the format of your input coordinates.