Skip to content

Commit

Permalink
#15 Work in progress:
Browse files Browse the repository at this point in the history
- Fix plot issues with negative latitudes or longitudes
- Add new test files

[ci skip]
  • Loading branch information
FABallemand committed Jul 9, 2024
1 parent ad854f9 commit 2142c82
Show file tree
Hide file tree
Showing 3 changed files with 2,668 additions and 39 deletions.
90 changes: 51 additions & 39 deletions ezgpx/gpx/gpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,25 +672,26 @@ def to_dataframe(
Dataframe containing data from GPX.
"""
# Disable time related values if no time data available
time_related_values = ["time", "speed", "pace", "ascent_speed"]
if not self._time_data:
if any([v in ["time", "speed", "pace", "ascent_speed"] for v in values]):
if any([v in time_related_values for v in values]):
warnings.warn(f"Trying to create dataframe from GPX file {self.file_path} which does not contain time data"
"Time related values (time, speed, pace, ascent speed) will not be present in the dataframe.",
UserWarning)
values.remove("time")
values.remove("speed")
values.remove("pace")
values.remove("ascent_speed")
for v in time_related_values:
if v in values:
values.remove(v)

# Disable elevation related values if no elevation data available
elevation_related_values = ["ele", "ascent_rate", "ascent_speed"]
if not self._ele_data:
if any([v in ["ele", "ascent_rate", "ascent_speed"] for v in values]):
if any([v in elevation_related_values for v in values]):
warnings.warn(f"Trying to create dataframe from GPX file {self.file_path} which does not contain elevation data"
"Time related values (elevation, ascent rate, ascent speed) will not be present in the dataframe.",
UserWarning)
values.remove("ele")
values.remove("ascent_rate")
values.remove("ascent_speed")
for v in elevation_related_values:
if v in values:
values.remove(v)

return self.gpx.to_dataframe(values)

Expand Down Expand Up @@ -907,33 +908,38 @@ def matplotlib_plot(
title_fontsize: int = 20,
watermark: bool = False,
file_path: str = None):
dynamic_colors = ["ele", "speed", "pace", "vertical_drop", "ascent_rate", "ascent_speed"]

# Create dataframe containing data from the GPX file
self._dataframe = self.to_dataframe(elevation=True,
time=True,
speed=True,
pace=True,
ascent_rate=True,
ascent_speed=True,
distance_from_start=True)
values = ["lat", "lon"]
if color in dynamic_colors:
values.append(color)
self._dataframe = self.to_dataframe(values)

# Create figure
fig = plt.figure(figsize=figsize)

# Compute track boundaries
min_lat, min_lon, max_lat, max_lon = self.bounds()

# Add default offset
delta_max = max(max_lat - min_lat, max_lon - min_lon)
# Compute default offset
delta_lat = abs(max_lat - min_lat)
delta_lon = abs(max_lon - min_lon)
delta_max = max(delta_lat, delta_lon)
offset = delta_max * offset_percentage
min_lat, min_lon = max(0, min_lat - offset), max(0, min_lon - offset)
max_lat, max_lon = min(max_lat + offset, 90), min(max_lon + offset, 180)

# Add default offset
min_lat = max(-90, min_lat - offset)
max_lat = min(max_lat + offset, 90)
min_lon = max(-180, min_lon - offset)
max_lon = min(max_lon + offset, 180)

# Some sort of magic to achieve the correct map aspect ratio
# CREATE FUNCTION (also used in animation??)
lat_offset = 1e-5
lon_offset = 1e-5
delta_lat = max_lat - min_lat
delta_lon = max_lon - min_lon
delta_lat = abs(max_lat - min_lat)
delta_lon = abs(max_lon - min_lon)
r = delta_lon / delta_lat # Current map aspect ratio
r_ref = figsize[0] / figsize[1] # Target map aspect ratio, Adapt in function of the shape of the subplot...

Expand All @@ -943,11 +949,11 @@ def matplotlib_plot(
# not isclose(delta_lat % pos.height, 0.0, abs_tol=tolerance)):
while not isclose(r, r_ref, abs_tol=tolerance):
if r > r_ref:
min_lat = max(0, min_lat - lat_offset)
min_lat = max(-90, min_lat - lat_offset)
max_lat = min(max_lat + lat_offset, 90)
delta_lat = max_lat - min_lat
if r < r_ref:
min_lon = max(0, min_lon - lon_offset)
min_lon = max(-180, min_lon - lon_offset)
max_lon = min(max_lon + lon_offset, 180)
delta_lon = max_lon - min_lon
r = delta_lon / delta_lat
Expand Down Expand Up @@ -980,7 +986,7 @@ def matplotlib_plot(
verbose=True)

# Scatter track points
if color in ["ele", "speed", "pace", "vertical_drop", "ascent_rate", "ascent_speed"]:
if color in dynamic_colors:
im = map.scatter(self._dataframe["lon"],
self._dataframe["lat"],
s=size,
Expand All @@ -994,13 +1000,13 @@ def matplotlib_plot(

# Scatter start point with different color
if start_point_color:
map.scatter(x[0], y[0], marker="^",
color=start_point_color)
map.scatter(self._dataframe["lon"][0], self._dataframe["lat"][0],
marker="^", color=start_point_color)

# Scatter stop point with different color
if stop_point_color:
map.scatter(x[-1], y[-1], marker="h",
color=stop_point_color)
map.scatter(self._dataframe["lon"].iloc[-1], self._dataframe["lat"].iloc[-1],
marker="h", color=stop_point_color)

# Scatter way points with different color
if way_points_color:
Expand Down Expand Up @@ -1095,18 +1101,24 @@ def expert_map(
# Compute track boundaries
min_lat, min_lon, max_lat, max_lon = self.bounds()

# Add default offset
delta_max = max(max_lat - min_lat, max_lon - min_lon)
# Compute default offset
delta_lat = abs(max_lat - min_lat)
delta_lon = abs(max_lon - min_lon)
delta_max = max(delta_lat, delta_lon)
offset = delta_max * offset_percentage
min_lat, min_lon = max(0, min_lat - offset), max(0, min_lon - offset)
max_lat, max_lon = min(max_lat + offset, 90), min(max_lon + offset, 180)

# Add default offset
min_lat = max(-90, min_lat - offset)
max_lat = min(max_lat + offset, 90)
min_lon = max(-180, min_lon - offset)
max_lon = min(max_lon + offset, 180)

# Some sort of magic to achieve the correct map aspect ratio
# CREATE FUNCTION (also used in anmation??)
lat_offset = 1e-5
lon_offset = 1e-5
delta_lat = max_lat - min_lat
delta_lon = max_lon - min_lon
delta_lat = abs(max_lat - min_lat)
delta_lon = abs(max_lon - min_lon)
r = delta_lon / delta_lat # Current map aspect ratio
pos = axes.get_position() # Axes bounding box
print(f"pos.width = {pos.width}")
Expand Down Expand Up @@ -1182,13 +1194,13 @@ def expert_map(

# Scatter start point with different color
if start_point_color:
map.scatter(x[0], y[0], marker="^",
color=start_point_color)
map.scatter(self._dataframe["lon"][0], self._dataframe["lat"][0],
marker="^", color=start_point_color)

# Scatter stop point with different color
if stop_point_color:
map.scatter(x[-1], y[-1], marker="h",
color=stop_point_color)
map.scatter(self._dataframe["lon"].iloc[-1], self._dataframe["lat"].iloc[-1],
marker="h", color=stop_point_color)

# Scatter way points with different color
if way_points_color:
Expand Down
Loading

0 comments on commit 2142c82

Please sign in to comment.