def test_polyline_round_trip(self): test_polyline = ("gcneIpgxzRcDnBoBlEHzKjBbHlG`@`IkDxIi" "KhKoMaLwTwHeIqHuAyGXeB~Ew@fFjAtIzExF") points = convert.decode_polyline(test_polyline) actual_polyline = convert.encode_polyline(points) self.assertEqual(test_polyline, actual_polyline)
def get_path_from_h3(h3id, color="0xFF0000AA", weight=1, fillcolor="0xFFB6C1BB"): boundry = h3.h3_to_geo_boundary(h3id) poly_lines = list(boundry) poly_lines.append(boundry[0]) polyline_code = convert.encode_polyline(poly_lines) return MAP_PATH.format( color=color, weight=weight, fcolor=fillcolor, poly_code=polyline_code )
def generate_polyline(points): return encode_polyline(points.values())
def divide_polyline(polyline_string_or_list=[""], biggest_polyline_length=None, include_interm_endpoints=True, accumulate_distances=True): """ Divide up an encoded polyline or lists of polylines into lists of polylines with specified length :param polyline_string_or_list: encoded polyline string or list of strings :type polyline_string_or_list: str or List(str) :param biggest_polyline_length: cut-off length for polylines :type biggest_polyline_length: numbers.Number :param include_interm_endpoints: whether to include the first point in the next :type include_interm_endpoints: bool :param accumulate_distances: whether to accumulate distances along the lines :type accumulate_distances: bool :return: list of encoded polylines with specified length (except last) :rtype: list of str """ import numpy as np if isinstance(polyline_string_or_list, str): polyline_string_or_list = [polyline_string_or_list] if biggest_polyline_length is None: biggest_polyline_length = np.inf # generator over all coordinates in list def next_coordinate(list_of_polyline_strings): for _poly_string in list_of_polyline_strings: coordinate_obj_list = con.decode_polyline(_poly_string) for coord in coordinate_obj_list: yield coord all_coordinates = next_coordinate(polyline_string_or_list) minimum_coord_distance = 0.5 result_polyline_lists = [] result_coordinate_distances = [] accumulated_distance = 0 previous_polylines_length = 0 current_distance_list = [accumulated_distance] previous_coordinate = next(all_coordinates) current_coordinate_list = [previous_coordinate] for coordinate in all_coordinates: reached_end_of_leg = False while not reached_end_of_leg: # ignore elevation next_leg_length = distance_between_coordinates( previous_coordinate, coordinate) if next_leg_length < minimum_coord_distance: # ignore points too close reached_end_of_leg = True # go to next coordinate elif accumulated_distance + next_leg_length < biggest_polyline_length: # can still add to polyline current_coordinate_list.append(coordinate) accumulated_distance += next_leg_length current_distance_list.append(accumulated_distance) previous_coordinate = coordinate reached_end_of_leg = True else: # split leg into two parts and start new polyline required_leg_length = biggest_polyline_length - accumulated_distance fraction_of_road_dist = required_leg_length / next_leg_length # Note: this is an approximation for small distances. At 100km distance, # this formula might give a result that is ~270 m off. inbetween_coord_lat = ( previous_coordinate["lat"] + fraction_of_road_dist * (coordinate["lat"] - previous_coordinate["lat"])) inbetween_coord_lng = ( previous_coordinate["lng"] + fraction_of_road_dist * (coordinate["lng"] - previous_coordinate["lng"])) inbetween_coord = dict([("lat", inbetween_coord_lat), ("lng", inbetween_coord_lng)]) accumulated_distance += fraction_of_road_dist * next_leg_length if include_interm_endpoints: current_coordinate_list.append(inbetween_coord) # should be == n * biggest_polyline_length current_distance_list.append(accumulated_distance) # add finished polyline to lists result_polyline_lists.append( con.encode_polyline(current_coordinate_list)) if accumulate_distances: result_coordinate_distances.append( list( np.array(current_distance_list) + previous_polylines_length)) previous_polylines_length += accumulated_distance else: result_coordinate_distances.append(current_distance_list) current_coordinate_list = [inbetween_coord] previous_coordinate = inbetween_coord accumulated_distance = 0 current_distance_list = [accumulated_distance] # add last lists result_polyline_lists.append(con.encode_polyline(current_coordinate_list)) # previous_polylines_length only increases if accumulate_distances is set to True result_coordinate_distances.append( list(np.array(current_distance_list) + previous_polylines_length)) return result_polyline_lists, result_coordinate_distances