Certainly! I've reviewed your code and made several optimizations to enhance its performance, readability, and maintainability. Below is the optimized version of your script with explanations of the key improvements:
-
Vectorization of Operations: Replaced loop-based operations with vectorized pandas operations for improved performance, especially in assigning PathIDs.
-
Optimized Geometry Creation: Used geopandas' points_from_xy for more efficient geometry creation instead of list comprehensions.
-
Enhanced Logging: Switched from print statements to Python's built-in logging module for better control over logging levels and outputs.
-
Modular Structure: Organized the code into clear, reusable functions to improve readability and maintainability.
-
Reduced Redundant Code: Eliminated unnecessary try-except blocks and streamlined error handling.
-
Type Annotations: Added type hints for better code clarity and to assist with static type checking.
-
Improved User Prompts: Made user prompts more consistent and informative.
Here's the optimized code:
import geopandas as gpd
import pandas as pd
from shapely.geometry import LineString
import os
import glob
import numpy as np
import logging
from typing import Tuple
def setup_logging():
"""Configure the logging settings."""
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s [%(levelname)s] %(message)s',
handlers=[logging.StreamHandler()]
)
def get_input(prompt: str, default: str = None, cast_type: type = str) -> str:
"""
Prompts the user for input with an optional default value and casts it to the specified type.
"""
while True:
try:
if default is not None:
user_input = input(f"{prompt} [{default}]: ").strip()
if not user_input:
user_input = default
else:
user_input = input(f"{prompt}: ").strip()
return cast_type(user_input)
except ValueError:
logging.error(f"Input non valido. Inserisci un valore di tipo {cast_type.__name__}.")
def merge_files(input_folder: str, file_extension: str = 'txt') -> Tuple[pd.DataFrame, int]:
"""
Merges multiple CSV or TXT files from a specified folder into a single DataFrame.
Returns:
merged_df (DataFrame): The merged DataFrame containing all columns.
num_files (int): The number of files merged.
"""
file_pattern = os.path.join(input_folder, f'*.{file_extension}')
file_list = glob.glob(file_pattern)
if not file_list:
raise FileNotFoundError(f"Nessun file con estensione '{file_extension}' trovato in '{input_folder}'.")
df_list = []
for file in file_list:
try:
df = pd.read_csv(file) if file_extension == 'csv' else pd.read_csv(file, delimiter='\t')
df['Source_File'] = os.path.basename(file)
df_list.append(df)
except Exception as e:
logging.warning(f"Errore nella lettura di {file}: {e}")
merged_df = pd.concat(df_list, ignore_index=True).drop_duplicates()
num_files = len(file_list)
return merged_df, num_files
def dataframe_to_geodataframe(df: pd.DataFrame, lon_column: str, lat_column: str, crs: str = 'EPSG:4326') -> gpd.GeoDataFrame:
"""
Converts a pandas DataFrame into a GeoDataFrame with Point geometries.
"""
missing_cols = {lon_column, lat_column} - set(df.columns)
if missing_cols:
raise KeyError(f"Colonne mancanti per le coordinate: {missing_cols}")
geometry = gpd.points_from_xy(df[lon_column], df[lat_column])
gdf = gpd.GeoDataFrame(df, geometry=geometry, crs=crs)
return gdf
def export_to_shapefile(gdf: gpd.GeoDataFrame, output_path: str):
"""
Exports a GeoDataFrame to a shapefile.
"""
os.makedirs(os.path.dirname(output_path), exist_ok=True)
gdf.to_file(output_path, driver='ESRI Shapefile')
logging.info(f"Fatto: Shapefile salvato in '{output_path}'.")
def standardize_column_names(df: pd.DataFrame) -> pd.DataFrame:
"""
Removes whitespace and standardizes column names to Title Case,
keeping 'geometry' column unchanged.
"""
df.columns = [col.strip() for col in df.columns]
df.rename(columns=lambda x: 'geometry' if x.lower() == 'geometry' else x.title(), inplace=True)
return df
def categorize_gap(gap: float, median_gap: float, tolerance: float = 0.2) -> str:
"""
Categorize the gap based on its relation to the median_gap.
"""
if gap > median_gap * (1 + tolerance):
return "Significantly Large Gap"
elif gap < median_gap * (1 - tolerance):
return "Significantly Small Gap"
else:
return "Normal"
def main():
setup_logging()
while True:
logging.info("=== Conversione Punti in Percorsi ===\n")
# Section 1: Creating Shapefile from Multiple Files
create_shapefile = get_input(
"Vuoi creare uno shapefile da più file CSV/TXT? (s/n)",
default='s',
cast_type=str
).lower()
if create_shapefile == 's':
input_folder = get_input(
"Inserisci il percorso della cartella contenente i file CSV/TXT da unire: ",
cast_type=str
).strip('"').strip("'")
if not os.path.isdir(input_folder):
logging.error("Errore: La cartella specificata non esiste.\n")
continue
file_extension = get_input(
"Inserisci l'estensione dei file da unire ('csv' o 'txt')",
default='txt',
cast_type=str
).lower()
if file_extension not in ['csv', 'txt']:
logging.error("Errore: Estensione dei file non valida. Usa 'csv' o 'txt'.\n")
continue
try:
merged_df, num_files = merge_files(input_folder, file_extension)
logging.info(f"I file sono stati fusi correttamente: {num_files} file uniti.")
logging.info(f"Numero totale di punti/immagini: {len(merged_df)}.\n")
except Exception as e:
logging.error(f"Errore nell'unire i file: {e}\n")
continue
found_columns = merged_df.columns.tolist()
logging.info(f"Colonne trovate nei file uniti: {', '.join(found_columns)}\n")
expected_columns_input = get_input(
"Inserisci i nomi delle colonne richieste separati da virgola:",
default='Time,Image,X,Y,Z,Roll,Pitch,Heading',
cast_type=str
)
expected_columns = [col.strip() for col in expected_columns_input.split(',')]
missing_columns = [col for col in expected_columns if col not in merged_df.columns]
if missing_columns:
logging.error(f"Errore: Le seguenti colonne richieste non sono presenti nei file: {', '.join(missing_columns)}\n")
continue
merged_df = merged_df[expected_columns]
lon_column = get_input(
"Inserisci il nome della colonna per X (longitudine)",
default='X',
cast_type=str
)
lat_column = get_input(
"Inserisci il nome della colonna per Y (latitudine)",
default='Y',
cast_type=str
)
if lon_column not in merged_df.columns or lat_column not in merged_df.columns:
logging.error("Errore: Le colonne X o Y specificate non esistono nei dati.\n")
continue
# Data Cleaning - Remove Non-Finite Coordinate Values
initial_count = len(merged_df)
merged_df.replace([np.inf, -np.inf], np.nan, inplace=True)
merged_df.dropna(subset=[lon_column, lat_column], inplace=True)
final_count = len(merged_df)
if final_count < initial_count:
removed = initial_count - final_count
logging.warning(f"Avviso: Rimosse {removed} righe con valori di coordinate non validi.\n")
if merged_df.empty:
logging.error("Errore: Dopo la pulizia, nessun dato rimane. Controlla i tuoi file di input.\n")
continue
crs_input = get_input(
"Inserisci il CRS (es. 'EPSG:4326')",
default='EPSG:4326',
cast_type=str
)
# Save Merged DataFrame as TXT Before Conversion
try:
folder_name = os.path.basename(os.path.normpath(input_folder))
merged_output_default = f"{folder_name}_merged.txt"
merged_output = get_input(
"Inserisci il nome per il file merged (senza estensione)",
default=merged_output_default,
cast_type=str
)
merged_output_path = os.path.abspath(merged_output + ".txt")
merged_df.to_csv(merged_output_path, sep='\t', index=False)
logging.info(f"Fatto: File merged salvato in '{merged_output_path}'.\n")
except Exception as e:
logging.error(f"Errore nel salvare il file merged: {e}\n")
continue
# Convert to GeoDataFrame
try:
gdf = dataframe_to_geodataframe(
merged_df,
lon_column=lon_column,
lat_column=lat_column,
crs=crs_input
)
logging.info(f"GeoDataFrame convertito correttamente dai file '{file_extension}'. Pronto per essere convertito in Shapefile.\n")
except Exception as e:
logging.error(f"Errore nella conversione in GeoDataFrame: {e}\n")
continue
# Export to Shapefile
output_base_default = f"{folder_name}_elaborato"
output_base = get_input(
"Inserisci il nome per il nuovo shapefile (senza estensione)",
default=output_base_default,
cast_type=str
)
output_shp = os.path.abspath(output_base + ".shp")
output_kml = os.path.abspath(output_base + ".kml")
try:
export_to_shapefile(gdf, output_shp)
except Exception as e:
logging.error(f"Errore nell'esportazione dello Shapefile: {e}\n")
continue
else:
# If not creating shapefile, prompt for existing shapefile
input_shapefile = get_input(
"Inserisci il percorso dello shapefile di input (.shp)",
cast_type=str
).strip('"').strip("'")
if not os.path.isfile(input_shapefile):
logging.error("Errore: Lo shapefile specificato non esiste.\n")
continue
try:
gdf = gpd.read_file(input_shapefile)
gdf = standardize_column_names(gdf)
logging.info("Shapefile caricato e colonne standardizzate correttamente.\n")
except Exception as e:
logging.error(f"Errore nel caricamento dello shapefile: {e}\n")
continue
# Ensure 'Time' column exists and is numeric
try:
if 'Time' not in gdf.columns:
logging.error("Errore: La colonna 'Time' non è presente nello shapefile.\n")
continue
if not pd.api.types.is_numeric_dtype(gdf['Time']):
logging.error(f"Errore: La colonna 'Time' non è numerica (tipo attuale: {gdf['Time'].dtype}).\n")
continue
logging.info("Colonna 'Time' verificata come numerica.\n")
except Exception as e:
logging.error(f"Errore nella verifica della colonna 'Time': {e}\n")
continue
# Set gap threshold
try:
gap_threshold = get_input(
"Inserisci la soglia del divario, ovvero il tempo massimo consentito (in secondi) tra due punti per considerarli nello stesso percorso",
default=3.0,
cast_type=float
)
except Exception as e:
logging.error(f"Errore nell'impostazione della soglia del divario: {e}\n")
continue
# Set group field name
group_field = get_input(
"Inserisci il nome per il campo del nuovo gruppo [default: PathID]: ",
default="PathID",
cast_type=str
)
if group_field not in gdf.columns:
gdf[group_field] = 0
# Sort by 'Time'
gdf_sorted = gdf.sort_values(by='Time').reset_index(drop=True)
# Assign PathID using vectorized operations
try:
gdf_sorted['Gap'] = gdf_sorted['Time'].diff().fillna(0)
gdf_sorted[group_field] = (gdf_sorted['Gap'] > gap_threshold).cumsum() + 1
gaps_df = gdf_sorted[gdf_sorted['Gap'] > 0][['index', 'Time', 'Gap']].copy()
gaps_df.rename(columns={'index': 'Entry_Index', 'Time': 'Current_Time'}, inplace=True)
except Exception as e:
logging.error(f"Errore nell'assegnazione dei PathID e nel calcolo dei gap: {e}\n")
continue
# Check if Shapefile was Created
if 'output_shp' not in locals() and create_shapefile == 's':
logging.error("Errore: Variabile 'output_shp' non definita. Assicurati di aver creato uno shapefile prima.")
continue
# Generate connected paths and export
try:
# Filter groups with at least two points
valid_groups = gdf_sorted[gdf_sorted[group_field].duplicated(keep=False)]
if valid_groups.empty:
logging.error("Errore: Nessun gruppo contiene più di un punto. Impossibile creare LineStrings.\n")
continue
# Group by PathID and create LineStrings
paths = valid_groups.groupby(group_field)['geometry'].apply(lambda x: LineString(x.tolist())).reset_index()
paths_gdf = gpd.GeoDataFrame(paths, geometry='geometry', crs=gdf_sorted.crs)
# Calculate lengths in kilometers
if not gdf_sorted.crs.is_projected:
logging.warning("Avviso: Il CRS di input non è proiettato. Riproiezione a EPSG:3857 per i calcoli delle distanze.")
paths_gdf = paths_gdf.to_crs(epsg=3857)
paths_gdf['Length_km'] = paths_gdf.length / 1000 # Convert meters to kilometers
# Calculate total distance
total_distance_km = paths_gdf['Length_km'].sum()
logging.info(f"Distanza totale di tutti i percorsi: {total_distance_km:.3f} chilometri.\n")
# Save paths as KML
try:
paths_gdf.to_file(output_kml, driver='KML')
logging.info(f"Fatto: I percorsi sono stati salvati in '{output_kml}'.\n")
except Exception as e:
logging.error(f"Errore nel salvare i percorsi come KML: {e}\n")
continue
# Generate summary report
try:
# Calculate Start_Time and End_Time
start_times = valid_groups.groupby(group_field)['Time'].min()
end_times = valid_groups.groupby(group_field)['Time'].max()
# Merge Start_Time and End_Time with paths_gdf
paths_gdf = paths_gdf.merge(start_times.rename('Start_Time'), on=group_field, how='left')
paths_gdf = paths_gdf.merge(end_times.rename('End_Time'), on=group_field, how='left')
# Create summary
summary = paths_gdf[['PathID', 'Length_km', 'Start_Time', 'End_Time']].drop_duplicates()
# Save summary as CSV
summary_output = os.path.splitext(output_shp)[0] + "_summary.csv"
summary.to_csv(summary_output, index=False)
logging.info(f"Fatto: Il report di sintesi dei percorsi è stato salvato in '{summary_output}'.\n")
except Exception as e:
logging.error(f"Errore nel generare il report di sintesi dei percorsi: {e}\n")
continue
except Exception as e:
logging.error(f"Errore durante la generazione dei percorsi connessi e l'esportazione: {e}\n")
continue
# Prepare final report
try:
report_lines = [
"=== Report Finale ===\n",
f"Shapefile di input: {os.path.abspath(input_shapefile) if 'input_shapefile' in locals() else 'Multipli file CSV/TXT fusi.'}",
f"Shapefile di output: {output_shp}",
f"KML di output: {output_kml}",
f"Campo PathID: {group_field}",
f"Campo Time: Time",
f"Soglia del divario: {gap_threshold}",
f"Distanza totale di tutti i percorsi: {total_distance_km:.3f} chilometri.\n"
]
if not gaps_df.empty:
median_gap = gaps_df['Gap'].median()
mean_gap = gaps_df['Gap'].mean()
std_gap = gaps_df['Gap'].std()
report_lines += [
"=== Statistiche Gap ===\n",
f"Gap Mediano: {median_gap:.3f} unità",
f"Gap Medio: {mean_gap:.3f} unità",
f"Deviazione Standard dei Gap: {std_gap:.3f} unità\n"
]
gaps_df['Observation'] = gaps_df['Gap'].apply(lambda x: categorize_gap(x, median_gap))
category_counts = gaps_df['Observation'].value_counts()
report_lines += ["=== Sintesi delle Categorie dei Gap ===\n"] + [
f"{category}: {count}" for category, count in category_counts.items()
] + ["\n"]
# Prepare gap table (limit to 100 entries)
gaps_table = gaps_df.copy()
gaps_table['Entry'] = gaps_table.index + 1
gaps_table = gaps_table.rename(columns={
'Entry_Index': 'Entry',
'Time': 'Previous_Time',
'Current_Time': 'Next_Time',
'Gap': 'Gap (Seconds)'
})
if len(gaps_table) > 100:
gaps_table = gaps_table.head(100)
gaps_table.at[99, 'Entry'] = '100+'
report_lines += [
"=== Analisi dei Gap ===\n",
gaps_table[['Entry', 'Previous_Time', 'Next_Time', 'Gap (Seconds)', 'Observation']].to_string(index=False, float_format='{:,.3f}'.format),
"\n"
]
else:
report_lines += ["Nessun gap calcolato.\n"]
# Add path summary
report_lines += [
"=== Sintesi dei Percorsi ===\n",
summary.to_string(index=False, float_format='{:,.3f}'.format),
"\n",
f"Numero totale di PathID: {summary['PathID'].nunique()}\n"
]
# Save the final report
final_report_path = os.path.splitext(output_shp)[0] + "_final_report.txt"
with open(final_report_path, 'w', encoding='utf-8') as f:
f.write('\n'.join(report_lines))
logging.info(f"Fatto: Il report finale è stato salvato in '{final_report_path}'.\n")
except Exception as e:
logging.error(f"Errore nella preparazione del report finale: {e}\n")
continue
# Console summary
try:
unique_path_ids = summary['PathID'].nunique()
logging.info(f"Numero totale di PathID creati: {unique_path_ids}")
if not gaps_df.empty:
logging.info(f"Gap mediano: {median_gap:.3f} unità")
else:
logging.info("Nessun gap calcolato.")
logging.info(f"Distanza totale di tutti i percorsi: {total_distance_km:.3f} chilometri.\n")
except Exception as e:
logging.error(f"Errore nella stampatura del riepilogo console: {e}\n")
continue
# Continue or Exit
cont = get_input("Vuoi continuare? (s/n)", default='s', cast_type=str).strip().lower()
if cont != 's':
logging.info("Script terminato.\n")
break
if __name__ == "__main__":
main()
Key Optimizations and Enhancements
-
Vectorized PathID Assignment:
- Before: Loop-based approach to assign
PathIDs and calculate gaps.
- After: Utilized pandas'
diff() and cumsum() for efficient, vectorized PathID assignment.
gdf_sorted['Gap'] = gdf_sorted['Time'].diff().fillna(0)
gdf_sorted[group_field] = (gdf_sorted['Gap'] > gap_threshold).cumsum() + 1
-
Efficient Geometry Creation:
- Before: Used list comprehensions to create
Point geometries.
- After: Utilized
geopandas.points_from_xy for optimized geometry creation.
geometry = gpd.points_from_xy(df[lon_column], df[lat_column])
-
Improved Logging:
- Replaced
print statements with the logging module to provide better control over log levels and formatting.
- Added informative log messages to track the script's progress and issues.
-
Modular Function Design:
- Organized the code into clear, reusable functions (
merge_files, dataframe_to_geodataframe, export_to_shapefile, etc.) to enhance readability and maintainability.
-
Error Handling:
- Streamlined
try-except blocks to handle exceptions more gracefully.
- Provided meaningful error messages to assist in debugging.
-
Report Generation:
- Enhanced the final report with gap statistics and summaries.
- Limited the gap analysis table to the first 100 entries to maintain readability.
-
Type Annotations:
- Added type hints for function parameters and return types to improve code clarity and facilitate static type checking.
-
Consistent User Prompts:
- Made user prompts more consistent and clear, ensuring that users are guided effectively through the input process.
Additional Recommendations
-
CRS Validation: Before performing spatial operations, validate the provided CRS to ensure it's supported and correctly formatted.
-
Performance Monitoring: For very large datasets, consider monitoring performance and optimizing further by processing data in chunks or leveraging parallel processing where appropriate.
-
User Interface: Depending on the end-users, you might consider developing a graphical user interface (GUI) to make the tool more user-friendly.
-
Documentation: Enhance the script with comprehensive docstrings and comments to assist future maintainers or users in understanding the workflow.
I hope these optimizations help improve your script's efficiency and usability! If you have any specific areas you'd like further assistance with or additional features you'd like to implement, feel free to ask.