This is one of the functions in the code that I am using to study the trajectory of ships.
I literally ran this code yesterday and this never happened. Overnight (without seemingly changing anything) this error started to appear. Here is the code:
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()
That error popped up in this line: gdf_trajetoria = gpd.GeoDataFrame(df_navio, geometry=gpd.points_from_xy(df_navio['Longitude'], df_navio['Latitude']))
Error: Exception has occurred: IndexError only integers, slices (
:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices
Once again, this seemingly happened overnight and I don't recollect changing anything in this specific part of the code. Could it be that one of the packages that I am using was updated? If so, here are all the packages I am using for the entire code:
- import numpy as np
- import pandas as pd
- import scipy.io
- import geopandas as gpd
- import matplotlib.pyplot as plt
- from datetime import datetime, timedelta
- import tkinter as tk
- from shapely.geometry import Polygon
- from tkinter import simpledialog
- import os
- import time
This function should theoretically create the plot based on the information provided by the user. It used to work but now it doesn't and I don't recollect changing anything in this part of the code.