这是我用来研究船舶轨迹的代码中的函数之一。
我昨天确实运行了这段代码,但这种情况从未发生过。一夜之间(似乎没有改变任何东西)这个错误开始出现。这是代码:
def plot_trajectory(mmsi, df_navio, data_hora_inicial, data_hora_final):
# Create a GeoDataFrame with the columns of latitude and longitude of the ship in a determined space of time.
gdf_trajetoria = gpd.GeoDataFrame(df_navio, geometry=gpd.points_from_xy(df_navio['Longitude'], df_navio['Latitude']))
# Load the shapefile base map
mapa_base = gpd.read_file('D:\\Afonso\\Estágio\\Mapa.sh\\ne_50m_admin_0_countries.shp')
# Create a rectangle based on the information provided by the user
retangulo = Polygon([(longitude_1, latitude_1),
(longitude_2, latitude_1),
(longitude_2, latitude_2),
(longitude_1, latitude_2)])
# Plot the base map
ax = mapa_base.plot(figsize=(10, 10), color='white', edgecolor='black')
if escolha_aom.upper() == 'NÃO':
# Add the rectangle to the GeoDataFrame with the base map
gpd.GeoDataFrame(geometry=[retangulo]).plot(ax=ax, edgecolor='black', facecolor='None')
# Define colors for the 3 different datasets
cores = {'F_SAT': 'red', 'F': 'blue', 'F_PT': 'green'}
marcadores = {'F_SAT': 'o', 'F': 'x', 'F_PT': '^'} # Define markers for each one
# Plot the trajectory of the ship in the determined time chosen by the user
for fonte_input in fontes_input:
gdf_fonte = gdf_trajetoria[gdf_trajetoria['Fonte'] == fonte_input.upper()]
gdf_fonte.plot(ax=ax, color=cores[fonte_input], marker=marcadores[fonte_input], markersize=8, label=f'Trajetória do Navio ({fonte_input.upper()})')
# Add title and other relevant information for the plot
plt.title(f'Trajetória do Navio com MMSI: {mmsi} no Intervalo de Tempo de {data_hora_inicial} a {data_hora_final}')
plt.xlabel('Longitude')
plt.ylabel('Latitude')
plt.legend()
# Add a line connecting the dots
coords = [(x, y) for x, y in zip(gdf_trajetoria.geometry.x, gdf_trajetoria.geometry.y)]
for i in range(len(coords) - 1):
plt.plot([coords[i][0], coords[i+1][0]], [coords[i][1], coords[i+1][1]], color=cores[fontes_input[0]])
# Plot
plt.show()
该错误在这一行中弹出:gdf_trajetoria = gpd.GeoDataFrame(df_navio, Geometry=gpd.points_from_xy(df_navio['Longitude'], df_navio['Latitude']))
错误: 发生异常:IndexError 只有整数、切片 (
)、省略号 (:
)、numpy.newaxis (...
) 和整数或布尔数组是有效索引None
这似乎是在一夜之间发生的,我不记得对代码的这个特定部分进行过任何更改。难道是我正在使用的某个软件包已更新?如果是这样,这里是我在整个代码中使用的所有包:
这个函数理论上应该根据用户提供的信息创建绘图。它曾经可以工作,但现在不行,而且我不记得对这部分代码进行过任何更改。
我认为错误发生在这一行
gdf_fonte = gdf_trajetoria[gdf_trajetoria['Fonte'] == fonte_input.upper()]
您正在做
gdf_fonte = gdf_trajetoria[True]
或 gdf_fonte = gdf_trajetoria[False]
,这似乎是错误的。