Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
Update Version 0.1.0
  • Loading branch information
ot2i7ba authored Jun 19, 2024
1 parent 62972cf commit 90dace2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 50 deletions.
101 changes: 52 additions & 49 deletions plotlyimex.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ def clear_screen():

def print_header():
# Print the header
print(" plotlyimex v0.2 by ot2i7ba ")
print("============================")
print(" plotlyimex v0.1.0 by ot2i7ba ")
print("==============================")
print("")

def get_csv_filename():
# Clear the screen and print the header
clear_screen()
print_header()

def print_csv_format_info():
# Provide information about the required CSV file format
print("Please provide a CSV file with the following columns: latitude, longitude, id, userId, lastSeenAt, speed, direction, source.")
print("Please provide a CSV file with the following columns:")
print("latitude, longitude, id, userId, lastSeenAt, speed, direction, source.")
print("The file should ideally be comma-separated.")

print("")

def get_csv_filename():
# Prompt the user to input the CSV filename
print()
csv_file = input("Input csv filename (enter for 'import.csv'): ")

# Set default filename if none provided
Expand All @@ -51,11 +51,8 @@ def validate_csv_file(csv_file):
return csv_file

def get_delimiter():
# Clear the screen and print the header
clear_screen()
print_header()

# Prompt the user to input the delimiter used in the CSV file
print()
delimiter = input("Enter the delimiter used in the CSV file (',' for comma, ';' for semicolon, default is ','): ")

# Set default delimiter to comma if none provided
Expand All @@ -65,63 +62,66 @@ def get_delimiter():

def load_csv(csv_file, delimiter):
# Load the CSV file using pandas with the specified delimiter
return pd.read_csv(csv_file, delimiter=delimiter, usecols=["latitude", "longitude", "id", "userId", "lastSeenAt", "speed", "direction", "source"])
try:
df = pd.read_csv(csv_file, delimiter=delimiter, usecols=["latitude", "longitude", "id", "userId", "lastSeenAt", "speed", "direction", "source"])
return df
except Exception as e:
raise ValueError(f"Error loading CSV file: {e}")

def choose_plot_type():
# Clear the screen and print the header
clear_screen()
print_header()

# Define available plot types
# Print the plot type options
print()
print("Choose a plot type:")
plot_types = {
"1": "Scatter Plot",
"2": "Density Plot",
"3": "Lines Plot",
"A": "All"
}

# Print the plot type options
print("Choose a plot type:")
for key, value in plot_types.items():
print(f"{key}. {value}")

# Prompt the user to select a plot type
print()
choice = input("Enter the number of the plot type (default is 1): ")
return plot_types.get(choice, "Scatter Plot")

def create_map(df, plot_type):
# Create the appropriate plot based on the plot type selected
if plot_type == "Scatter Plot":
fig = px.scatter_mapbox(df, lat="latitude", lon="longitude", hover_name="id",
hover_data=["userId", "lastSeenAt", "speed", "direction", "source"],
zoom=3)
elif plot_type == "Density Plot":
fig = px.density_mapbox(df, lat="latitude", lon="longitude", hover_name="id",
hover_data=["userId", "lastSeenAt", "speed", "direction", "source"],
zoom=3)
elif plot_type == "Lines Plot":
fig = px.line_geo(df, lat="latitude", lon="longitude", hover_name="id",
hover_data=["userId", "lastSeenAt", "speed", "direction", "source"],
projection="orthographic")
else:
raise ValueError(f"Unknown plot type: {plot_type}")

# Update the layout of the map
fig.update_layout(mapbox_style="open-street-map", height=1080, width=1920)
return fig
try:
if plot_type == "Scatter Plot":
fig = px.scatter_mapbox(df, lat="latitude", lon="longitude", hover_name="id",
hover_data=["userId", "lastSeenAt", "speed", "direction", "source"],
zoom=3)
elif plot_type == "Density Plot":
fig = px.density_mapbox(df, lat="latitude", lon="longitude", hover_name="id",
hover_data=["userId", "lastSeenAt", "speed", "direction", "source"],
zoom=3)
elif plot_type == "Lines Plot":
fig = px.line_geo(df, lat="latitude", lon="longitude", hover_name="id",
hover_data=["userId", "lastSeenAt", "speed", "direction", "source"],
projection="orthographic")
else:
raise ValueError(f"Unknown plot type: {plot_type}")

# Update the layout of the map
fig.update_layout(mapbox_style="open-street-map", height=1080, width=1920)
return fig
except Exception as e:
raise ValueError(f"Error creating map: {e}")

def export_plot(fig, plot_name):
# Export the plot to an HTML file
html_file = f"export_{plot_name}.html"
pio.write_html(fig, file=html_file)
print(f"Plot saved as {html_file}")
try:
html_file = f"export_{plot_name}.html"
pio.write_html(fig, file=html_file)
print(f"Plot saved as {html_file}")
except Exception as e:
raise ValueError(f"Error exporting plot: {e}")

def get_html_filename(default_name):
# Clear the screen and print the header
clear_screen()
print_header()

# Prompt the user to input the output HTML filename
print()
html_file = input(f"Output html filename (enter for '{default_name}'): ")

# Set default filename if none provided
Expand All @@ -133,8 +133,8 @@ def get_html_filename(default_name):

def export_all_plots(df):
# Export all plot types using parallel processing
plot_types = ["Scatter Plot", "Density Plot", "Lines Plot"]
with ThreadPoolExecutor() as executor:
plot_types = ["Scatter Plot", "Density Plot", "Lines Plot"]
futures = []
for plot_type in plot_types:
futures.append(executor.submit(export_plot, create_map(df, plot_type), plot_type.lower().replace(" ", "_")))
Expand All @@ -145,6 +145,9 @@ def main():
# Clear the screen and print the header
clear_screen()
print_header()

# Provide information about the required CSV file format
print_csv_format_info()

# Define argument parser for command-line options
parser = argparse.ArgumentParser(description='Generate an interactive map from a CSV file.')
Expand All @@ -155,7 +158,7 @@ def main():

try:
# Get and validate the input CSV filename
csv_file = args.input
csv_file = get_csv_filename()
validate_csv_file(csv_file)

# Get the delimiter used in the CSV file
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pandas==1.5.3
plotly==5.10.0
argparse==1.4.0

0 comments on commit 90dace2

Please sign in to comment.