This repository has been archived by the owner on Oct 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimgPlotter.py
67 lines (55 loc) · 1.74 KB
/
imgPlotter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import plotly.graph_objects as go
import io
from PIL import Image
def create_geo_image(flight_path, scale_factor=2, margin=0.5):
# Create the figure
fig = go.Figure()
all_lats = []
all_lons = []
# Add lines
lats = [point["lat"] for point in flight_path]
lons = [point["lon"] for point in flight_path]
all_lats.extend(lats)
all_lons.extend(lons)
fig.add_trace(
go.Scattergeo(
lat=lats,
lon=lons,
mode="lines",
line=dict(width=2, color="blue"),
# disable the legend
showlegend=False,
)
)
min_lat, max_lat = min(all_lats), max(all_lats)
min_lon, max_lon = min(all_lons), max(all_lons)
# calculate the top margin + based on the lat and lon, cause at the top we have the arced pahs
top_margin = (max_lat - min_lat) * 0.1
# Add some margins for better visibility
min_lat -= margin
max_lat += margin + top_margin
min_lon -= margin
max_lon += margin
# Layout properties
fig.update_geos(
projection_type="equirectangular",
lataxis_range=[min_lat, max_lat],
lonaxis_range=[min_lon, max_lon],
showland=True,
landcolor="rgb(243, 243, 243)",
showocean=True,
oceancolor="rgb(127,205,255)",
showcountries=True,
countrycolor="Black",
countrywidth=0.5,
showlakes=True,
lakecolor="Blue",
)
fig.update_layout(autosize=True, margin=dict(l=0, r=0, b=0, t=0, pad=0))
# Convert the figure to a PNG image
img_bytes = fig.to_image(format="png", scale=scale_factor)
# Create a BytesIO object and save the PNG image data to it
buf = io.BytesIO()
buf.write(img_bytes)
buf.seek(0)
return buf