Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
hishizuka committed Oct 13, 2023
1 parent f01f567 commit 7e8c024
Showing 1 changed file with 176 additions and 174 deletions.
350 changes: 176 additions & 174 deletions modules/course.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ def downsample(self):

diff_dist_max = int(np.max(dist_diff)) * 2 / 1000 # [m->km]
if diff_dist_max > self.config.G_GPS_SEARCH_RANGE: # [km]
app_logger.info(
app_logger.debug(
f"G_GPS_SEARCH_RANGE[km]: {self.config.G_GPS_SEARCH_RANGE} -> {diff_dist_max}"
)
self.config.G_GPS_SEARCH_RANGE = diff_dist_max
Expand Down Expand Up @@ -634,190 +634,192 @@ def calc_slope_smoothing(self):
self.colored_altitude = np.array(self.config.G_SLOPE_COLOR)[slope_smoothing_cat]

def modify_course_points(self):
if self.config.G_COURSE_INDEXING:
course_points = self.course_points

len_pnt_lat = len(course_points.latitude)
len_pnt_dist = len(course_points.distance)
len_pnt_alt = len(course_points.altitude)
len_dist = len(self.distance)
len_alt = len(self.altitude)

# calculate course point distance/altitude if not both already set
# If one is already set, it's not going to be overwritten
# But if both are already set there's no need to recalculate anything
if not len_pnt_dist or not len_pnt_alt:
if not len_pnt_dist and len_dist:
course_points.distance = np.empty(len_pnt_lat)
if not len_pnt_alt and len_alt:
course_points.altitude = np.zeros(len_pnt_lat)

min_index = 0

for i in range(len_pnt_lat):
b_a_x = self.points_diff[0][min_index:]
b_a_y = self.points_diff[1][min_index:]
lon_diff = course_points.longitude[i] - self.longitude[min_index:]
lat_diff = course_points.latitude[i] - self.latitude[min_index:]
p_a_x = lon_diff[:-1]
p_a_y = lat_diff[:-1]
inner_p = (
b_a_x * p_a_x + b_a_y * p_a_y
) / self.points_diff_sum_of_squares[min_index:]
inner_p_check = np.where(
(0.0 <= inner_p) & (inner_p <= 1.0), True, False
)
if not self.config.G_COURSE_INDEXING:
return

course_points = self.course_points

min_j = None
min_dist_diff_h = np.inf
min_dist_delta = 0
min_alt_delta = 0

for j in list(*np.where(inner_p_check == True)):
h_lon = (
self.longitude[min_index + j]
+ (
self.longitude[min_index + j + 1]
- self.longitude[min_index + j]
)
* inner_p[j]
)
h_lat = (
self.latitude[min_index + j]
+ (
self.latitude[min_index + j + 1]
- self.latitude[min_index + j]
)
* inner_p[j]
len_pnt_lat = len(course_points.latitude)
len_pnt_dist = len(course_points.distance)
len_pnt_alt = len(course_points.altitude)
len_dist = len(self.distance)
len_alt = len(self.altitude)

# calculate course point distance/altitude if not both already set
# If one is already set, it's not going to be overwritten
# But if both are already set there's no need to recalculate anything
if not len_pnt_dist or not len_pnt_alt:
if not len_pnt_dist and len_dist:
course_points.distance = np.empty(len_pnt_lat)
if not len_pnt_alt and len_alt:
course_points.altitude = np.zeros(len_pnt_lat)

min_index = 0

for i in range(len_pnt_lat):
b_a_x = self.points_diff[0][min_index:]
b_a_y = self.points_diff[1][min_index:]
lon_diff = course_points.longitude[i] - self.longitude[min_index:]
lat_diff = course_points.latitude[i] - self.latitude[min_index:]
p_a_x = lon_diff[:-1]
p_a_y = lat_diff[:-1]
inner_p = (
b_a_x * p_a_x + b_a_y * p_a_y
) / self.points_diff_sum_of_squares[min_index:]
inner_p_check = np.where(
(0.0 <= inner_p) & (inner_p <= 1.0), True, False
)

min_j = None
min_dist_diff_h = np.inf
min_dist_delta = 0
min_alt_delta = 0

for j in list(*np.where(inner_p_check == True)):
h_lon = (
self.longitude[min_index + j]
+ (
self.longitude[min_index + j + 1]
- self.longitude[min_index + j]
)
dist_diff_h = get_dist_on_earth(
h_lon,
h_lat,
course_points.longitude[i],
course_points.latitude[i],
* inner_p[j]
)
h_lat = (
self.latitude[min_index + j]
+ (
self.latitude[min_index + j + 1]
- self.latitude[min_index + j]
)
* inner_p[j]
)
dist_diff_h = get_dist_on_earth(
h_lon,
h_lat,
course_points.longitude[i],
course_points.latitude[i],
)

if (
dist_diff_h < self.config.G_GPS_ON_ROUTE_CUTOFF
and dist_diff_h < min_dist_diff_h
):
if min_j is not None and j - min_j > 2:
continue

min_j = j
min_dist_diff_h = dist_diff_h
min_dist_delta = (
get_dist_on_earth(
self.longitude[min_index + j],
self.latitude[min_index + j],
h_lon,
h_lat,
)
/ 1000
if (
dist_diff_h < self.config.G_GPS_ON_ROUTE_CUTOFF
and dist_diff_h < min_dist_diff_h
):
if min_j is not None and j - min_j > 2:
continue

min_j = j
min_dist_diff_h = dist_diff_h
min_dist_delta = (
get_dist_on_earth(
self.longitude[min_index + j],
self.latitude[min_index + j],
h_lon,
h_lat,
)
if len_alt:
min_alt_delta = (
(
self.altitude[min_index + j + 1]
- self.altitude[min_index + j]
)
/ (
self.distance[min_index + j + 1]
- self.distance[min_index + j]
)
* min_dist_delta
/ 1000
)
if len_alt:
min_alt_delta = (
(
self.altitude[min_index + j + 1]
- self.altitude[min_index + j]
)
/ (
self.distance[min_index + j + 1]
- self.distance[min_index + j]
)
* min_dist_delta
)

if min_j is None:
min_j = 0

min_index = min_index + min_j
if min_j is None:
min_j = 0

if not len_pnt_dist and len_dist:
course_points.distance[i] = (
self.distance[min_index] + min_dist_delta
)
if not len_pnt_alt and len_alt:
course_points.altitude[i] = (
self.altitude[min_index] + min_alt_delta
)
min_index = min_index + min_j

# add climb tops
# if len(self.climb_segment):
# min_index = 0
# for i in range(len(self.climb_segment)):
# diff_dist = np.abs(course_points.distance - self.climb_segment[i]['course_point_distance'])
# min_index = np.where(diff_dist == np.min(diff_dist))[0][0]+1
# course_points.name.insert(min_index, "Top of Climb")
# course_points.latitude = np.insert(course_points._latitude, min_index, self.climb_segment[i]['course_point_latitude'])
# course_points.longitude = np.insert(course_points.longitude, min_index, self.climb_segment[i]['course_point_longitude'])
# course_points.type.insert(min_index, "Summit")
# course_points.distance = np.insert(course_points.distance, min_index, self.climb_segment[i]['course_point_distance'])
# course_points.altitude = np.insert(course_points.altitude, min_index, self.climb_segment[i]['course_point_altitude'])

len_pnt_dist = len(course_points.distance)
len_pnt_alt = len(course_points.latitude)

# add start course point
if (
len_pnt_lat
and len_pnt_dist
and len_dist
# TODO do not use float
and course_points.distance[0] != 0.0
):
app_logger.info(
f"Missing start of the course point, first value is {course_points.distance[0]}, inserting"
)
course_points.name = np.insert(course_points.name, 0, "Start")
course_points.latitude = np.insert(
course_points.latitude, 0, self.latitude[0]
)
course_points.longitude = np.insert(
course_points.longitude, 0, self.longitude[0]
)
course_points.type = np.insert(course_points.type, 0, "")
course_points.notes = np.insert(course_points.notes, 0, "")
if len_pnt_dist and len_dist:
course_points.distance = np.insert(course_points.distance, 0, 0.0)
if len_pnt_alt and len_alt:
course_points.altitude = np.insert(
course_points.altitude, 0, self.altitude[0]
if not len_pnt_dist and len_dist:
course_points.distance[i] = (
self.distance[min_index] + min_dist_delta
)
if not len_pnt_alt and len_alt:
course_points.altitude[i] = (
self.altitude[min_index] + min_alt_delta
)

# add end course point
end_distance = None
if len(self.latitude) and len(course_points.longitude):
end_distance = get_dist_on_earth_array(
self.longitude[-1],
self.latitude[-1],
course_points.longitude[-1],
course_points.latitude[-1],
)
if (
len_pnt_lat
and len_pnt_dist
and len_dist
and end_distance is not None
and end_distance > 5
):
app_logger.info(
f"Missing end of the course point last distance is {end_distance}, inserting"
# add climb tops
# if len(self.climb_segment):
# min_index = 0
# for i in range(len(self.climb_segment)):
# diff_dist = np.abs(course_points.distance - self.climb_segment[i]['course_point_distance'])
# min_index = np.where(diff_dist == np.min(diff_dist))[0][0]+1
# course_points.name.insert(min_index, "Top of Climb")
# course_points.latitude = np.insert(course_points._latitude, min_index, self.climb_segment[i]['course_point_latitude'])
# course_points.longitude = np.insert(course_points.longitude, min_index, self.climb_segment[i]['course_point_longitude'])
# course_points.type.insert(min_index, "Summit")
# course_points.distance = np.insert(course_points.distance, min_index, self.climb_segment[i]['course_point_distance'])
# course_points.altitude = np.insert(course_points.altitude, min_index, self.climb_segment[i]['course_point_altitude'])

len_pnt_dist = len(course_points.distance)
len_pnt_alt = len(course_points.latitude)

# add start course point
if (
len_pnt_lat
and len_pnt_dist
and len_dist
# TODO do not use float
and course_points.distance[0] != 0.0
):
app_logger.info(
f"Missing start of the course point, first value is {course_points.distance[0]}, inserting"
)
course_points.name = np.insert(course_points.name, 0, "Start")
course_points.latitude = np.insert(
course_points.latitude, 0, self.latitude[0]
)
course_points.longitude = np.insert(
course_points.longitude, 0, self.longitude[0]
)
course_points.type = np.insert(course_points.type, 0, "")
course_points.notes = np.insert(course_points.notes, 0, "")
if len_pnt_dist and len_dist:
course_points.distance = np.insert(course_points.distance, 0, 0.0)
if len_pnt_alt and len_alt:
course_points.altitude = np.insert(
course_points.altitude, 0, self.altitude[0]
)
course_points.name = np.append(course_points.name, "End")
course_points.latitude = np.append(
course_points.latitude, self.latitude[-1]

# add end course point
end_distance = None
if len(self.latitude) and len(course_points.longitude):
end_distance = get_dist_on_earth_array(
self.longitude[-1],
self.latitude[-1],
course_points.longitude[-1],
course_points.latitude[-1],
)
if (
len_pnt_lat
and len_pnt_dist
and len_dist
and end_distance is not None
and end_distance > 5
):
app_logger.info(
f"Missing end of the course point last distance is {end_distance}, inserting"
)
course_points.name = np.append(course_points.name, "End")
course_points.latitude = np.append(
course_points.latitude, self.latitude[-1]
)
course_points.longitude = np.append(
course_points.longitude, self.longitude[-1]
)
course_points.type = np.append(course_points.type, "")
course_points.notes = np.append(course_points.notes, "")
if len_pnt_dist and len_dist:
course_points.distance = np.append(
course_points.distance, self.distance[-1]
)
course_points.longitude = np.append(
course_points.longitude, self.longitude[-1]
if len_pnt_alt and len_alt:
course_points.altitude = np.append(
course_points.altitude, self.altitude[-1]
)
course_points.type = np.append(course_points.type, "")
course_points.notes = np.append(course_points.notes, "")
if len_pnt_dist and len_dist:
course_points.distance = np.append(
course_points.distance, self.distance[-1]
)
if len_pnt_alt and len_alt:
course_points.altitude = np.append(
course_points.altitude, self.altitude[-1]
)

0 comments on commit 7e8c024

Please sign in to comment.