コード例 #1
0
    def test_calculate_road_length(self):
        """
        The total distance of the segmented road should be similar to the length before segmentation, within
        a margin given by the variable "margin"
        :return: Nothing
        """
        margin = 3
        for road in self.road_net:
            road = convert(road)

            length_before = calculate_road_length_simple(
                road["the_geom"]["coordinates"])

            road_segmented = split_segment(road, self.max_segment_distance, [],
                                           self.min_coordinates_length)

            length_after = 0
            for segment in road_segmented:
                length_after += calculate_road_length_simple(
                    segment["the_geom"]["coordinates"])

            self.assertLess(
                abs(length_after - length_before), margin,
                "The difference between the original "
                "length and the segmented length is "
                "too large")
コード例 #2
0
 def test_over_and_undersegmenting(self):
     """
     The segmenter should only run on segments that are over the limit in length, it should never segment something
     shorter than that. In other words the segmented road should still be only one segment
     :return: Nothing
     """
     i = 0
     for road in self.road_net:
         i += 1
         converted_road = convert(road)
         road_coords_length = len(converted_road["the_geom"]["coordinates"])
         road_distance = calculate_road_length_simple(
             converted_road["the_geom"]["coordinates"])
         road_segmented = segment_network([filter_road(road)],
                                          self.max_segment_distance,
                                          self.min_coordinates_length)
         road_segmented_length = len(road_segmented)
         if road_distance < self.max_segment_distance:
             self.assertTrue(
                 road_segmented_length == 1,
                 "This road was segmented, but should not have been.")
         elif road_coords_length >= 2 * self.min_coordinates_length and road_distance > self.max_segment_distance:
             self.assertTrue(
                 road_segmented_length > 1,
                 ("This road should have been segmented, but was not. "
                  "Stretchdistance:", road_distance, "Coordinates:",
                  converted_road["the_geom"]["coordinates"], i))
コード例 #3
0
 def test_correct_length(self):
     """
     The calculate_road_length function should return 103 meters, where the road exceeds 100 meters, while the
     calculate_road_length_simple function should return the total length of the road, which is 111
     :return: None
     """
     self.assertEqual(
         calculate_road_length(self.coordinates, self.max_length)[1], 103)
     self.assertEqual(calculate_road_length_simple(self.coordinates), 111)
コード例 #4
0
 def test_int(self):
     """
     The distance calculator should always return an int
     :return: None
     """
     self.assertIsInstance(calculate_road_length_simple(self.coordinates),
                           int)
     self.assertIsInstance(
         calculate_road_length(self.coordinates, self.max_length)[1], int)
コード例 #5
0
ファイル: road_segmenter.py プロジェクト: IT2901-24-2018/vapi
def split_segment(road_segment, max_distance, segmented_road_network, min_gps):
    """
    Recursive function that splits the road segments into two new ones based on input criteria
    :param road_segment: A dict containing the road segment
    :param max_distance: Int. Max distance of a road segment
    :param segmented_road_network: List of new road segments after segmentation
    :param min_gps: Minimum amount of GPS points in a segment
    :return: Final compiled list of all segmented_road_network after being passed down recursively
    """
    coordinates = road_segment["the_geom"]["coordinates"]
    index, meter = (calculate_road_length(coordinates, max_distance))

    segment_before_split = copy.deepcopy(road_segment)
    segment_before_split["the_geom"]["coordinates"] = segment_before_split[
        "the_geom"]["coordinates"][:index]
    segment_before_split["stretchdistance"] = calculate_road_length_simple(
        segment_before_split["the_geom"]["coordinates"])

    segment_after_split = copy.deepcopy(road_segment)
    segment_after_split["the_geom"]["coordinates"] = segment_after_split[
        "the_geom"]["coordinates"][index - 1:]
    segment_after_split["stretchdistance"] = calculate_road_length_simple(
        segment_after_split["the_geom"]["coordinates"])

    if len(segment_before_split["the_geom"]["coordinates"]) >= min_gps:
        segmented_road_network.append(segment_before_split)
    else:
        segmented_road_network.append(road_segment)
        return segmented_road_network

    if check_split(segment_after_split, meter):
        if len(segment_after_split["the_geom"]["coordinates"]) <= min_gps:
            segmented_road_network.append(road_segment)
            return segmented_road_network
        segmented_road_network = split_segment(segment_after_split, meter,
                                               segmented_road_network, min_gps)
    else:
        if len(segment_after_split["the_geom"]["coordinates"]) >= min_gps:
            segmented_road_network.append(segment_after_split)
    return segmented_road_network
コード例 #6
0
ファイル: road_segmenter.py プロジェクト: IT2901-24-2018/vapi
def check_split(road_segment, max_distance):
    """
    Checks if road_segment is longer than meter, meaning it should be split
    :param road_segment: The road segment you want to check
    :param max_distance: Max length of a road segment
    :return: True or False, True meaning the road segment should be split
    """

    if (calculate_road_length_simple(
            road_segment["the_geom"]["coordinates"])) > max_distance:
        return True
    else:
        return False
コード例 #7
0
ファイル: road_segmenter.py プロジェクト: IT2901-24-2018/vapi
def segment_network(road_network, max_distance, min_gps):
    """
    :param road_network: A dict containing the road network. Specified in the wiki
    :param max_distance: Max distance for each road segment
    :param min_gps: Minimum number of gps points for each road segment
    :return: The segmented road network
    """
    segmented_road_network = []
    for road_segment in road_network:
        road_segment["the_geom"] = geometry_to_list(road_segment["the_geom"])

        if road_segment["stretchdistance"] < 1:
            print("Found an invalid stretchdistance:",
                  road_segment["stretchdistance"], ", roadsectionid:",
                  road_segment["roadsectionid"])
            new_distance = calculate_road_length_simple(
                road_segment["the_geom"]["coordinates"])
            road_segment["stretchdistance"] = new_distance
            print("setting stretchdistance to:", new_distance)

        if len(road_segment["the_geom"]["coordinates"]
               ) > min_gps and check_split(road_segment, max_distance):
            split_roads = split_segment(road_segment, max_distance, [],
                                        min_gps)

            if split_roads:
                for new_road in split_roads:
                    segmented_road_network.append(new_road)
        else:
            segmented_road_network.append(road_segment)

    for road_segment in segmented_road_network:
        road_segment["the_geom"] = list_to_geometry(
            road_segment["the_geom"]["coordinates"],
            road_segment["the_geom"]["srid"])
    return segmented_road_network