def make_sample_trajectory():
    coordinates = [(0, 80), (90, 80), (180, 80), (-90, 80), (0, 80)]

    time_strings = [
        "2000-01-01 00:00:00", "2000-01-01 02:00:00", "2000-01-01 03:00:00",
        "2000-01-01 04:00:00", "2000-01-01 06:00:00"
    ]

    trajectory = TerrestrialTrajectory()
    for (coords, time_string) in zip(coordinates, time_strings):
        point = TerrestrialTrajectoryPoint(coords)
        point.object_id = 'terrestrial_dg_test'
        point.timestamp = datetime.datetime.strptime(time_string,
                                                     '%Y-%m-%d %H:%M:%S')
        trajectory.append(point)

    return trajectory
Ejemplo n.º 2
0
def trajectory_point_generator(start_airport,
                               end_airport,
                               start_time,
                               object_id='ANON',
                               desired_speed=800,
                               seconds_between_points=60,
                               minimum_num_points=10):
    """trajectory_point_generator(start_airport: Airport,
                                  end_airport: Airport,
                                  start_time=Timestamp (datetime.datetime),
                                  object_id='ANON' (string),
                                  desired_speed=60 (float, km/h),
                                  seconds_between_points=60 (int),
                                  minimum_num_points=10 (int)) -> iterable of points

    Generate a sequence of points that go from the starting airport to
    the ending airport with the desired speed and time between points.
    """

    start_position = TerrestrialTrajectoryPoint()
    start_position[0] = start_airport.position[0]
    start_position[1] = start_airport.position[1]

    end_position = TerrestrialTrajectoryPoint()
    end_position[0] = end_airport.position[0]
    end_position[1] = end_airport.position[1]

    travel_time = time_between_positions(start_position,
                                         end_position,
                                         desired_speed=desired_speed)

    num_points = num_points_between_positions(
        start_position,
        end_position,
        desired_speed=desired_speed,
        seconds_between_points=seconds_between_points)

    if num_points < minimum_num_points:
        num_points = minimum_num_points

    start_position.object_id = object_id
    start_position.timestamp = start_time
    end_position.object_id = object_id
    end_position.timestamp = start_time + travel_time

    point_list = [start_position]
    if num_points == 2:
        point_list.append(end_position)
    else:
        interpolant_increment = 1.0 / (num_points - 1)
        for i in range(1, num_points - 1):
            interpolant = i * interpolant_increment
            point_list.append(
                geomath.interpolate(start_position, end_position, interpolant))
        point_list.append(end_position)

    return point_list
def create_trajectory():
    los_angeles = AirTrajectoryPoint( -118.25, 34.05 )
    los_angeles.object_id = 'TEST'
    los_angeles.timestamp = Timestamp.from_any('2014-01-01 00:00:00')
    los_angeles.properties['altitude'] = 0

    new_york = AirTrajectoryPoint( -74.0, 40.71 )



    source = path_point_source.TrajectoryPointSource(AirTrajectoryPoint)
    source.start_time = 
    source.end_time = Timestamp.from_any('2014-01-01 04:00:00')
    source.start_point = los_angeles
    source.end_point = new_york
    source.num_points = 240
    source.object_id = 'TEST'

    all_points = list(source.points())

    # Now we need to add altitude.  Let's say that we get to maximum
    # altitude (50,000 feet) at point 90.  We start descending at
    # point 150 and get back to zero altitude at point 240.
    max_altitude = 50000
    for i in range(240):
        all_points[i].altitude = max_altitude

    for i in range(90):
        x = float(i) / 90

        level = 1 - (x-1)*(x-1)
        altitude = level * max_altitude
        all_points[i].altitude = altitude
        all_points[-(i+1)].altitude = altitude

    trajectory = AirTrajectory.from_position_list(all_points)
    return trajectory
def test_compute_bounding_box_after_pickle():
    error_count = 0

    albuquerque = TerrestrialTrajectoryPoint(-106.6504, 35.0844)
    albuquerque.timestamp = datetime.datetime(year=2020,
                                              month=1,
                                              day=1,
                                              hour=12)
    san_francisco = TerrestrialTrajectoryPoint(-122.4194, 37.7749)
    san_francisco.timestamp = albuquerque.timestamp + datetime.timedelta(
        hours=3)
    tokyo = TerrestrialTrajectoryPoint(-221.6917, 35.6895)
    tokyo.timestamp = albuquerque.timestamp + datetime.timedelta(hours=12)

    trajectory_generator = TrajectoryPointSource()
    trajectory_generator.start_point = albuquerque
    trajectory_generator.end_point = tokyo
    trajectory_generator.num_points = 20

    print("DEBUG: TerrestrialTrajectory: {}".format(TerrestrialTrajectory))
    albuquerque_to_tokyo = TerrestrialTrajectory.from_position_list(
        list(trajectory_generator.points()))

    expected_min_corner = tracktable.domain.domain_class_for_object(
        albuquerque, 'BasePoint')()
    expected_max_corner = tracktable.domain.domain_class_for_object(
        albuquerque, 'BasePoint')()

    expected_min_corner[0] = min(albuquerque[0], tokyo[0])
    expected_min_corner[1] = min(albuquerque[1], tokyo[1])
    expected_max_corner[0] = max(albuquerque[0], tokyo[0])
    expected_max_corner[1] = max(albuquerque[1], tokyo[1])

    bbox_before_pickling = geomath.compute_bounding_box(albuquerque_to_tokyo)

    store = io.BytesIO()
    pickle.dump(albuquerque_to_tokyo, store)
    store.seek(0)
    restored_trajectory = pickle.load(store)
    bbox_after_pickling = geomath.compute_bounding_box(restored_trajectory)

    print("Bounding box before pickling: ({} {}) - ({} {})".format(
        bbox_before_pickling.min_corner[0], bbox_before_pickling.min_corner[1],
        bbox_before_pickling.max_corner[0],
        bbox_before_pickling.max_corner[1]))
    print("Bounding box after pickling: ({} {}) - ({} {})".format(
        bbox_after_pickling.min_corner[0], bbox_after_pickling.min_corner[1],
        bbox_after_pickling.max_corner[0], bbox_after_pickling.max_corner[1]))

    bbox_min_delta = (bbox_after_pickling.min_corner[0] -
                      bbox_before_pickling.min_corner[0],
                      bbox_after_pickling.min_corner[1] -
                      bbox_before_pickling.min_corner[1])
    bbox_max_delta = (bbox_after_pickling.max_corner[0] -
                      bbox_before_pickling.max_corner[0],
                      bbox_after_pickling.max_corner[1] -
                      bbox_before_pickling.max_corner[1])

    if (math.fabs(bbox_min_delta[0]) > 0.01
            or math.fabs(bbox_min_delta[1]) > 0.01
            or math.fabs(bbox_max_delta[0]) > 0.01
            or math.fabs(bbox_max_delta[1]) > 0.01):
        print(
            ("ERROR: Expected delta between bounding box before and after "
             "pickling to be zero.  Delta for minimum corner is {}.  "
             "Delta for maximum corner is {}.").format(bbox_min_delta,
                                                       bbox_max_delta))
        error_count += 1

    return error_count
Ejemplo n.º 5
0
def test_trajectory():
    print("Testing Trajectory class.")
    error_count = 0

    right_now = datetime.datetime.now(pytz.utc)

    boston = TerrestrialTrajectoryPoint(-71.0636, 42.3581)
    boston.timestamp = right_now
    boston.object_id = 'ContinentalExpress'
    boston.set_property('favorite_food', 'baked_beans')
    boston.set_property('name', 'Boston')

    miami = TerrestrialTrajectoryPoint(-80.2241, 25.7877)
    miami.timestamp = right_now + datetime.timedelta(hours=4)
    miami.object_id = 'ContinentalExpress'
    miami.set_property('favorite_food', 'cuban_sandwich')
    miami.set_property('name', 'Miami')

    san_francisco = TerrestrialTrajectoryPoint(-122.4167, 37.7833)
    san_francisco.timestamp = right_now + datetime.timedelta(hours=8)
    san_francisco.object_id = 'ContinentalExpress'
    san_francisco.set_property('favorite_food', 'Ghirardelli chocolate')
    san_francisco.set_property('name', 'San Francisco')

    seattle = TerrestrialTrajectoryPoint(-122.3331, 47.6097)
    seattle.timestamp = right_now + datetime.timedelta(hours=12)
    seattle.object_id = 'ContinentalExpress'
    seattle.set_property('favorite_food', 'seafood')
    seattle.set_property('name', 'Seattle')

    boston_return = TerrestrialTrajectoryPoint(-71.0636, 42.3581)
    boston_return.timestamp = right_now + datetime.timedelta(hours=16)
    boston_return.object_id = 'ContinentalExpress'
    boston_return.set_property('favorite_food', 'baked_beans')
    boston_return.set_property('name', 'Boston')

    round_trip = [boston, miami, san_francisco, seattle, boston_return]

    my_trajectory = TerrestrialTrajectory.from_position_list(round_trip)

    print("Testing from_position_list")
    if len(my_trajectory) != 5:
        sys.stderr.write(
            'ERROR: Expected length of trajectory to be 5 points but it was {}\n'
            .format(len(my_trajectory)))
        error_count += 1

    print("Sanity-checking first and last points")
    restored_point = my_trajectory[0]
    if restored_point != boston:
        sys.stderr.write(
            'ERROR: Expected first point in trajectory to be Boston.  Instead it claims to be {}.  Dumps of original and restored points follow.\n'
            .format(restored_point.property('name')))
        sys.stderr.write(str(boston))
        sys.stderr.write('\n')
        sys.stderr.write(str(my_trajectory[0]))
        sys.stderr.write('\n')

        error_count += 1

    if my_trajectory[-1] != boston_return:
        sys.stderr.write(
            'ERROR: Expected last point in trajectory to be Boston.  Instead it claims to be {}.\n'
            .format(my_trajectory[-1].property('name')))
        error_count += 1

    print("Testing duration")
    duration = my_trajectory.duration
    if (duration != datetime.timedelta(hours=16)):
        sys.stderr.write(
            'ERROR: Expected duration to be 16 hours.  Instead it claims to be {}.\n'
            .format(duration))
        error_count += 1

    print("Testing time_at_fraction, 0.25")
    first_quarter_time = geomath.time_at_fraction(my_trajectory, 0.25)
    delta = my_trajectory[-1].timestamp - right_now
    expected_first_quarter_time = right_now + (delta // 4)
    error_count += verify_time(expected_first_quarter_time, first_quarter_time,
                               "Time at fraction 0.25")

    print("Testing time_at_fraction, 0.75")
    last_quarter_time = geomath.time_at_fraction(my_trajectory, 0.75)
    delta = my_trajectory[-1].timestamp - right_now
    expected_last_quarter_time = right_now + ((3 * delta) // 4)
    error_count += verify_time(expected_last_quarter_time, last_quarter_time,
                               "Time at fraction 0.75")

    print("Testing time_at_fraction, 0.5")
    midpoint_time = geomath.time_at_fraction(my_trajectory, 0.5)
    expected_midpoint_time = (right_now +
                              (my_trajectory[-1].timestamp - right_now) // 2)
    error_count += verify_time(expected_midpoint_time, midpoint_time,
                               "Time at fraction 0.5")

    print("Testing time_at_fraction, 0.0")
    start_time = geomath.time_at_fraction(my_trajectory, 0.0)
    expected_start_time = right_now
    error_count += verify_time(expected_start_time, start_time,
                               "Time at fraction 0.0")

    print("Testing time_at_fraction, 1.0")
    end_time = geomath.time_at_fraction(my_trajectory, 1.0)
    expected_end_time = my_trajectory[-1].timestamp
    error_count += verify_time(expected_end_time, end_time,
                               "Time at fraction 1.0")

    print("Testing time_at_fraction, -0.5")
    before_time = geomath.time_at_fraction(my_trajectory, -0.5)
    expected_before_time = right_now
    error_count += verify_time(expected_before_time, before_time,
                               "Time at fraction -0.5")

    print("Testing time_at_fraction, 1.5")
    after_time = geomath.time_at_fraction(my_trajectory, 1.5)
    expected_after_time = my_trajectory[-1].timestamp
    error_count += verify_time(expected_after_time, after_time,
                               "Time at fraction 1.5")

    print("Testing time_at_fraction, 0.33")
    first_third_time = geomath.time_at_fraction(my_trajectory, 1.0 / 3.0)
    expected_third_quarter_time = (
        right_now + (my_trajectory[-1].timestamp - right_now) // 3)
    error_count += verify_time(expected_third_quarter_time, first_third_time,
                               "Time at fraction 0.33")

    print("Testing time_at_fraction, No Points")
    empty_trajectory = TerrestrialTrajectory()
    empty_time = geomath.time_at_fraction(empty_trajectory, 0.5)
    error_count += verify_time(
        datetime.datetime(1900, 1, 1, 0, 0, 0, 0, pytz.utc), empty_time,
        "Time at fraction (no points)")

    print("Testing point_at_fraction, 0.25")
    first_quarter_point = geomath.point_at_fraction(my_trajectory, 0.25)
    expected_first_quarter_point = TerrestrialTrajectoryPoint(
        -80.2241, 25.7877)
    expected_first_quarter_point.timestamp = right_now + datetime.timedelta(
        hours=4)
    error_count += verify_point(expected_first_quarter_point,
                                first_quarter_point, "Point at fraction 0.25")

    print("Testing point_at_fraction, 0.75")
    third_quarter_point = geomath.point_at_fraction(my_trajectory, 0.75)
    expected_third_quarter_point = TerrestrialTrajectoryPoint(
        -122.3331, 47.6097)
    expected_third_quarter_point.timestamp = right_now + datetime.timedelta(
        hours=12)
    error_count += verify_point(expected_third_quarter_point,
                                third_quarter_point, "Point at fraction 0.75")

    print("Testing point_at_fraction, 0.5")
    mid_point = geomath.point_at_fraction(my_trajectory, 0.5)
    expected_mid_point = TerrestrialTrajectoryPoint(-122.4167, 37.7833)
    expected_mid_point.timestamp = right_now + datetime.timedelta(hours=8)
    error_count += verify_point(expected_mid_point, mid_point,
                                "Point at fraction 0.5")

    print("Testing point_at_fraction, 0.0")
    start_point = geomath.point_at_fraction(my_trajectory, 0.0)
    expected_start_point = TerrestrialTrajectoryPoint(-71.0636, 42.3581)
    expected_start_point.timestamp = right_now
    error_count += verify_point(expected_start_point, start_point,
                                "Point at fraction 0.0")

    print("Testing point_at_fraction, 1.0")
    end_point = geomath.point_at_fraction(my_trajectory, 1.0)
    expected_end_point = TerrestrialTrajectoryPoint(-71.0636, 42.3581)
    expected_end_point.timestamp = right_now + datetime.timedelta(hours=16)
    error_count += verify_point(expected_end_point, end_point,
                                "Point at fraction 1.0")

    print("Testing point_at_fraction, -0.5")
    before_point = geomath.point_at_fraction(my_trajectory, -0.5)
    expected_before_point = TerrestrialTrajectoryPoint(-71.0636, 42.3581)
    expected_before_point.timestamp = right_now
    error_count += verify_point(expected_before_point, before_point,
                                "Point at fraction -0.5")

    print("Testing point_at_fraction, 1.5")
    after_point = geomath.point_at_fraction(my_trajectory, 1.5)
    expected_after_point = TerrestrialTrajectoryPoint(-71.0636, 42.3581)
    expected_after_point.timestamp = right_now + datetime.timedelta(hours=16)
    error_count += verify_point(expected_after_point, after_point,
                                "Point at fraction 1.5")

    print("Testing point_at_fraction, 0.33")
    first_third_point = geomath.point_at_fraction(my_trajectory, 1.0 / 3.0)
    expected_first_third_point = TerrestrialTrajectoryPoint(-92.9849, 31.3181)
    expected_first_third_point.timestamp = (
        right_now + (my_trajectory[-1].timestamp - right_now) // 3)
    error_count += verify_point(expected_first_third_point, first_third_point,
                                "Point at fraction 0.33")

    print("Testing point_at_fraction, No Points")
    no_point = geomath.point_at_fraction(empty_trajectory, 0.5)
    empty_point = TerrestrialTrajectoryPoint.zero()
    error_count += verify_point(no_point, empty_point,
                                "Point at fraction (no points)")

    print("Testing point_at_length_fraction, 0.25")
    first_quarter_point = geomath.point_at_length_fraction(my_trajectory, 0.25)
    expected_first_quarter_point = TerrestrialTrajectoryPoint(
        -87.3824, 29.1092)
    expected_first_quarter_point.timestamp = first_quarter_point.timestamp
    error_count += verify_point(expected_first_quarter_point,
                                first_quarter_point,
                                "Point at length fraction 0.25")

    print("Testing point_at_length_fraction, 0.75")
    third_quarter_point = geomath.point_at_length_fraction(my_trajectory, 0.75)
    expected_third_quarter_point = TerrestrialTrajectoryPoint(
        -106.489, 48.5709)
    expected_third_quarter_point.timestamp = third_quarter_point.timestamp
    error_count += verify_point(expected_third_quarter_point,
                                third_quarter_point,
                                "Point at length fraction 0.75")

    print("Testing point_at_length_fraction, 0.5")
    mid_point = geomath.point_at_length_fraction(my_trajectory, 0.5)
    expected_mid_point = TerrestrialTrajectoryPoint(-116.267, 37.0967)
    expected_mid_point.timestamp = mid_point.timestamp
    error_count += verify_point(expected_mid_point, mid_point,
                                "Point at length fraction 0.5")

    print("Testing point_at_length_fraction, 0.0")
    start_point = geomath.point_at_length_fraction(my_trajectory, 0.0)
    expected_start_point = TerrestrialTrajectoryPoint(-71.0636, 42.3581)
    expected_start_point.timestamp = right_now
    error_count += verify_point(expected_start_point, start_point,
                                "Point at length fraction 0.0")

    print("Testing point_at_length_fraction, 1.0")
    end_point = geomath.point_at_length_fraction(my_trajectory, 1.0)
    expected_end_point = TerrestrialTrajectoryPoint(-71.0636, 42.3581)
    expected_end_point.timestamp = right_now + datetime.timedelta(hours=16)
    error_count += verify_point(expected_end_point, end_point,
                                "Point at length fraction 1.0")

    print("Testing point_at_length_fraction, -0.5")
    before_point = geomath.point_at_length_fraction(my_trajectory, -0.5)
    expected_before_point = TerrestrialTrajectoryPoint(-71.0636, 42.3581)
    expected_before_point.timestamp = right_now
    error_count += verify_point(expected_before_point, before_point,
                                "Point at length fraction -0.5")

    print("Testing point_at_length_fraction, 1.5")
    after_point = geomath.point_at_length_fraction(my_trajectory, 1.5)
    expected_after_point = TerrestrialTrajectoryPoint(-71.0636, 42.3581)
    expected_after_point.timestamp = right_now + datetime.timedelta(hours=16)
    error_count += verify_point(expected_after_point, after_point,
                                "Point at length fraction 1.5")

    print("Testing point_at_length_fraction, 0.33")
    first_third_point = geomath.point_at_length_fraction(
        my_trajectory, 1.0 / 3.0)
    expected_first_third_point = TerrestrialTrajectoryPoint(-96.4035, 32.5023)
    expected_first_third_point.timestamp = first_third_point.timestamp
    error_count += verify_point(expected_first_third_point, first_third_point,
                                "Point at length fraction 0.33")

    print("Testing point_at_length_fraction, No Points")
    no_point = geomath.point_at_length_fraction(empty_trajectory, 0.5)
    empty_point = TerrestrialTrajectoryPoint.zero()
    error_count += verify_point(no_point, empty_point,
                                "Point at length fraction (no points)")

    print("Testing interpolation at timestamp before trajectory")
    before_time = right_now - datetime.timedelta(hours=4)
    before_point = geomath.point_at_time(my_trajectory, before_time)
    error_count += verify_point(boston, before_point,
                                "Point at time (-4 hours)")

    print("Testing interpolation at start timestamp of trajectory")
    start_time = right_now
    start_point = geomath.point_at_time(my_trajectory, start_time)
    error_count += verify_point(boston, start_point, "Point at time (0 hours)")

    print("Testing interpolation at first third timestamp of trajectory")
    first_third_time = right_now + datetime.timedelta(hours=16.0 / 3.0)
    first_third_point = geomath.point_at_time(my_trajectory, first_third_time)
    expected_first_third_point = TerrestrialTrajectoryPoint(-92.9849, 31.3181)
    expected_first_third_point.timestamp = right_now + datetime.timedelta(
        hours=16.0 / 3.0)
    error_count += verify_point(expected_first_third_point, first_third_point,
                                "Point at time (+5.33 hours)")

    print("Testing interpolation at mid timestamp of trajectory")
    mid_time = right_now + datetime.timedelta(hours=8)
    mid_point = geomath.point_at_time(my_trajectory, mid_time)
    error_count += verify_point(san_francisco, mid_point,
                                "Point at time (+8 hours)")

    print("Testing interpolation at end timestamp of trajectory")
    end_time = right_now + datetime.timedelta(hours=16)
    end_point = geomath.point_at_time(my_trajectory, end_time)
    error_count += verify_point(boston_return, end_point,
                                "Point at time (+16 hours)")

    print("Testing interpolation at timestamp after trajectory")
    after_time = right_now + datetime.timedelta(hours=20)
    after_point = geomath.point_at_time(my_trajectory, after_time)
    error_count += verify_point(boston_return, after_point,
                                "Point at time (+20 hours)")

    print("Testing interpolation at timestamp with no points trajectory")
    no_point = geomath.point_at_time(empty_trajectory,
                                     right_now + datetime.timedelta(hours=8))
    empty_point = TerrestrialTrajectoryPoint()
    error_count += verify_point(empty_point, no_point,
                                "Point at time (no points)")

    #    print("Testing pickle support")
    #    picklebuf = StringIO()
    #    pickle.dump(my_trajectory, picklebuf)
    #    restorebuf = StringIO(picklebuf.getvalue())
    #    restored_trajectory = pickle.load(restorebuf)

    #    if my_trajectory != restored_trajectory:
    #        sys.stderr.write('ERROR: Original trajectory is not the same as the one being restored from pickle jar.\n')
    #        sys.stderr.write('Original trajectory: {}'.format(my_trajectory))
    #        sys.stderr.write('Restored trajectory: {}'.format(restored_trajectory))
    #        error_count += 1

    if error_count == 0:
        print("Surface trajectory passed all tests.")

    return error_count
def create_point(lat, lon, id):

    point = TerrestrialTrajectoryPoint(lon, lat)
    point.object_id = id

    return point
Ejemplo n.º 7
0
def test_point_type():
    print("Testing point type")

    error_count = 0

    point1 = TerrestrialBasePoint(45, 45)
    point2 = TerrestrialBasePoint(135, 45)

    print("Testing terrestrial base point interpolate, 0.5")
    result = geomath.interpolate(point1, point2, 0.5)
    expected = TerrestrialBasePoint(90, 54.7356)
    error_count += verify_base_point(expected, result,
                                     "Terrestrial Base Point Interpolate 0.5")

    print("Testing terrestrial base point interpolate, 0.3")
    result = geomath.interpolate(point1, point2, 0.3)
    expected = TerrestrialBasePoint(69.7884, 53.0018)
    error_count += verify_base_point(expected, result,
                                     "Terrestrial Base Point Interpolate 0.3")

    print("Testing terrestrial base point interpolate, 1")
    result = geomath.interpolate(point1, point2, 1)
    error_count += verify_base_point(point2, result,
                                     "Terrestrial Base Point Interpolate 1")

    print("Testing terrestrial base point extrapolate, 2")
    result = geomath.extrapolate(point1, point2, 2)
    expected = TerrestrialBasePoint(180, 0)
    error_count += verify_base_point(expected, result,
                                     "Terrestrial Base Point Extrapolate 2")

    point3 = Cartesian2DBasePoint(0, 0)
    point4 = Cartesian2DBasePoint(10, 10)

    print("Testing cartesian2d base point interpolate, 0.5")
    result = geomath.interpolate(point3, point4, 0.5)
    expected = Cartesian2DBasePoint(5, 5)
    error_count += verify_base_point(expected, result,
                                     "Cartesian2D Base Point Interpolate 0.5")

    print("Testing cartesian2d base point interpolate, 0.3")
    result = geomath.interpolate(point3, point4, 0.3)
    expected = Cartesian2DBasePoint(3, 3)
    error_count += verify_base_point(expected, result,
                                     "Cartesian2D Base Point Interpolate 0.3")

    print("Testing cartesian2d base point interpolate, 1")
    result = geomath.interpolate(point3, point4, 1)
    error_count += verify_base_point(point4, result,
                                     "Cartesian2D Base Point Interpolate 1")

    print("Testing cartesian2d base point extrapolate, 1.5")
    result = geomath.extrapolate(point3, point4, 1.5)
    expected = Cartesian2DBasePoint(15, 15)
    error_count += verify_base_point(expected, result,
                                     "Cartesian2D Base Point Extrapolate 1.5")

    point5 = Cartesian3DBasePoint(0, 0, 0)
    point6 = Cartesian3DBasePoint(10, 10, 10)

    print("Testing cartesian3d base point interpolate, 0.5")
    result = geomath.interpolate(point5, point6, 0.5)
    expected = Cartesian3DBasePoint(5, 5, 5)
    error_count += verify_base_point(expected, result,
                                     "Cartesian3D Base Point Interpolate 0.5")

    print("Testing cartesian3d base point interpolate, 0.3")
    result = geomath.interpolate(point5, point6, 0.3)
    expected = Cartesian3DBasePoint(3, 3, 3)
    error_count += verify_base_point(expected, result,
                                     "Cartesian3D Base Point Interpolate 0.3")

    print("Testing cartesian3d base point interpolate, 1")
    result = geomath.interpolate(point5, point6, 1)
    error_count += verify_base_point(point6, result,
                                     "Cartesian3D Base Point Interpolate 1")

    print("Testing cartesian3d base point extrapolate, 1.5")
    result = geomath.extrapolate(point5, point6, 1.5)
    expected = Cartesian3DBasePoint(15, 15, 15)
    error_count += verify_base_point(expected, result,
                                     "Cartesian3D Base Point Extrapolate 1.5")

    point7 = TerrestrialTrajectoryPoint(10, 30)
    point7.timestamp = datetime.strptime("2020-12-01 00:00:00",
                                         "%Y-%m-%d %H:%M:%S")
    point7.object_id = 'FOO'
    point7.set_property('speed', 100.0)
    point7.set_property('heading', 0.0)

    point8 = TerrestrialTrajectoryPoint(14.6929, 35.1023)
    point8.timestamp = datetime.strptime("2020-12-01 00:30:00",
                                         "%Y-%m-%d %H:%M:%S")
    point8.object_id = 'FOO'
    point8.set_property('speed', 150.0)
    point8.set_property('heading', 90.0)

    point9 = TerrestrialTrajectoryPoint(20, 40)
    point9.timestamp = datetime.strptime("2020-12-01 01:00:00",
                                         "%Y-%m-%d %H:%M:%S")
    point9.object_id = 'FOO'
    point9.set_property('speed', 200.0)
    point9.set_property('heading', 180.0)

    print("Testing terrestrial trajectory point interpolate, 0.5")
    result = geomath.interpolate(point7, point9, 0.5)
    error_count += verify_trajectory_point(
        point8, result, "Terrestrial Trajectory Point Interpolate 0.5")

    print("Testing terrestrial trajectory point extrapolate, 2")
    result = geomath.extrapolate(point7, point8, 2)
    error_count += verify_trajectory_point(
        point9, result, "Terrestrial Trajectory Point Extrapolate 2")

    point10 = Cartesian2DTrajectoryPoint(5, 5)
    point10.timestamp = datetime.strptime("2020-12-01 00:00:00",
                                          "%Y-%m-%d %H:%M:%S")
    point10.object_id = 'FOO'
    point10.set_property('speed', 10.0)
    point10.set_property('heading', 0.0)

    point11 = Cartesian2DTrajectoryPoint(10, 10)
    point11.timestamp = datetime.strptime("2020-12-01 00:30:00",
                                          "%Y-%m-%d %H:%M:%S")
    point11.object_id = 'FOO'
    point11.set_property('speed', 15.0)
    point11.set_property('heading', 90.0)

    point12 = Cartesian2DTrajectoryPoint(15, 15)
    point12.timestamp = datetime.strptime("2020-12-01 01:00:00",
                                          "%Y-%m-%d %H:%M:%S")
    point12.object_id = 'FOO'
    point12.set_property('speed', 20.0)
    point12.set_property('heading', 180.0)

    print("Testing cartesian2d trajectory point interpolate, 0.5")
    result = geomath.interpolate(point10, point12, 0.5)
    error_count += verify_trajectory_point(
        point11, result, "Cartesian2D Trajectory Point Interpolate 0.5")

    print("Testing cartesian2d trajectory point extrapolate, 2")
    result = geomath.extrapolate(point10, point11, 2)
    error_count += verify_trajectory_point(
        point12, result, "Cartesian2D Trajectory Point Extrapolate 2")

    point13 = Cartesian3DTrajectoryPoint(5, 5, 5)
    point13.timestamp = datetime.strptime("2020-12-01 00:00:00",
                                          "%Y-%m-%d %H:%M:%S")
    point13.object_id = 'FOO'
    point13.set_property('speed', 10.0)
    point13.set_property('heading', 0.0)

    point14 = Cartesian3DTrajectoryPoint(10, 10, 10)
    point14.timestamp = datetime.strptime("2020-12-01 00:30:00",
                                          "%Y-%m-%d %H:%M:%S")
    point14.object_id = 'FOO'
    point14.set_property('speed', 15.0)
    point14.set_property('heading', 90.0)

    point15 = Cartesian3DTrajectoryPoint(15, 15, 15)
    point15.timestamp = datetime.strptime("2020-12-01 01:00:00",
                                          "%Y-%m-%d %H:%M:%S")
    point15.object_id = 'FOO'
    point15.set_property('speed', 20.0)
    point15.set_property('heading', 180.0)

    print("Testing cartesian3d trajectory point interpolate, 0.5")
    result = geomath.interpolate(point13, point15, 0.5)
    error_count += verify_trajectory_point(
        point14, result, "Cartesian3D Trajectory Point Interpolate 0.5")

    print("Testing cartesian3d trajectory point extrapolate, 2")
    result = geomath.extrapolate(point13, point14, 2)
    error_count += verify_trajectory_point(
        point15, result, "Cartesian3D Trajectory Point Extrapolate 2")

    return error_count
Ejemplo n.º 8
0
def run_test():
    num_errors = 0

    source = TrajectoryPointSource()

    start_time = Timestamp.from_any(datetime.datetime(2010, 10, 13, 12, 00,
                                                      00))
    end_time = Timestamp.from_any(datetime.datetime(2010, 10, 13, 18, 00, 00))

    start_point = TrajectoryPoint(-100, 35.0)
    start_point.object_id = 'foo'
    start_point.timestamp = start_time

    end_point = TrajectoryPoint(-80, 45.0)
    end_point.object_id = 'foo'
    end_point.timestamp = end_time

    num_points = 100

    source.start_point = start_point
    source.end_point = end_point
    source.num_points = num_points

    all_points = list(source.points())

    if len(all_points) != num_points:
        sys.stderr.write(
            'ERROR: GreatCircleTrajectoryPointSource: Expected {} points but got {}\n'
            .format(num_points, len(all_points)))
        num_errors += 1

    traj_start_point = (all_points[0][0], all_points[0][1])
    d_lon = all_points[0][0] - start_point[0]
    d_lat = all_points[0][1] - start_point[1]
    if math.fabs(d_lon) > 1e-5 or math.fabs(d_lat) > 1e-5:
        sys.stderr.write(
            'ERROR: GreatCircleTrajectoryPointSource: Expected first point to be {} but got {} instead\n'
            .format(start_point, traj_start_point))
        num_errors += 1

    traj_end_point = (all_points[-1][0], all_points[-1][1])
    d_lon = all_points[-1][0] - end_point[0]
    d_lat = all_points[-1][1] - end_point[1]
    if math.fabs(d_lon) > 1e-5 or math.fabs(d_lat) > 1e-5:
        sys.stderr.write(
            'ERROR: GreatCircleTrajectoryPointSource: Expected last point to be {} but got {} instead\n'
            .format(end_point, traj_end_point))
        num_errors += 1

    traj_start_time = all_points[0].timestamp
    d_time = math.fabs((traj_start_time - start_time).total_seconds())
    if d_time > 0.001:
        sys.stderr.write(
            'ERROR: GreatCircleTrajectoryPointSource: Expected timestamp on first point to be {} but got {} instead\n'
            .format(start_time, traj_start_time))
        num_errors += 1

    traj_end_time = all_points[-1].timestamp
    d_time = math.fabs((traj_end_time - end_time).total_seconds())
    if d_time > 0.001:
        sys.stderr.write(
            'ERROR: GreatCircleTrajectoryPointSource: Expected timestamp on last point to be {} but got {} instead\n'
            .format(end_time, traj_end_time))
        num_errors += 1

    return num_errors
def run_test():
    # Define three sets of points: ABQ to San Diego, San Diego to
    # Seattle, Denver to NYC.  ABQ->SAN->SEA should break into two
    # trajectories because of a timestamp break in San Diego.  The
    # flight to Denver will begin right when the flight to Seattle
    # ends so we expect to break that one based on the distance
    # threshold.

    print("Beginning run_test()")

    from tracktable.domain.terrestrial import TrajectoryPoint

    albuquerque = TrajectoryPoint( -106.5, 35.25 )
    albuquerque.timestamp = Timestamp.from_string('2010-01-01 12:00:00')
    albuquerque.object_id = 'flight1'

    san_diego1 = TrajectoryPoint( -117.16, 32.67 )
    san_diego1.timestamp = Timestamp.from_string('2010-01-01 15:00:00')
    san_diego1.object_id = 'flight1'

    san_diego2 = TrajectoryPoint( -117.16, 32.67 )
    san_diego2.timestamp = Timestamp.from_string('2010-01-01 16:00:00')
    san_diego2.object_id = 'flight1'

    seattle = TrajectoryPoint( -122.31, 47.60 )
    seattle.timestamp = Timestamp.from_string('2010-01-01 19:00:00')
    seattle.object_id = 'flight1'

    denver = TrajectoryPoint( -104.98, 39.79 )
    denver.timestamp = Timestamp.from_string('2010-01-01 19:01:00')
    denver.object_id = 'flight1'

    new_york = TrajectoryPoint( -74.02, 40.71 )
    new_york.timestamp = Timestamp.from_string('2010-01-02 00:00:00')
    new_york.object_id = 'flight1'

    # Now we want sequences of points for each flight.
    abq_to_sd = TrajectoryPointSource()
    abq_to_sd.start_point = albuquerque
    abq_to_sd.end_point = san_diego1
    abq_to_sd.num_points = 180

    sd_to_sea = TrajectoryPointSource()
    sd_to_sea.start_point = san_diego2
    sd_to_sea.end_point = seattle
    sd_to_sea.num_points = 360 # flying very slowly

    denver_to_nyc = TrajectoryPointSource()
    denver_to_nyc.start_point = denver
    denver_to_nyc.end_point = new_york
    denver_to_nyc.num_points = 600 # wow, very densely sampled

    print("Done creating point sources")

    all_points = list(itertools.chain( abq_to_sd.points(),
                                       sd_to_sea.points(),
                                       denver_to_nyc.points() ))

    trajectory_assembler = AssembleTrajectoryFromPoints()
    trajectory_assembler.input = all_points
    trajectory_assembler.separation_time = timedelta(minutes=30)
    trajectory_assembler.separation_distance = 100 
    trajectory_assembler_minimum_length = 10

    print("Done instantiating assembler")

    all_trajectories = list(trajectory_assembler.trajectories())
    print("Assembler statistics: {} points, {} valid trajectories, {} invalid trajectories".format(
        trajectory_assembler.valid_trajectory_count,
        trajectory_assembler.invalid_trajectory_count,
        trajectory_assembler.points_processed_count
        ))

    print("Done creating trajectories.  Found {}.".format(len(all_trajectories)))

    test_point_proximity = geomath.sanity_check_distance_less_than(1)
    def test_timestamp_proximity(time1, time2):
        return ( (time2 - time1).total_seconds() < 1 )

    error_count = 0
    if len(all_trajectories) != 3:
        sys.stdout.write('ERROR: test_trajectory_assembly: Expected 3 trajectories but got {}\n'.format(len(all_trajectories)))
        error_count += 1

    if not test_point_proximity(all_trajectories[0][0], albuquerque):
        sys.stdout.write('ERROR: test_trajectory_assembly: Expected point 0 of first trajectory to be Albuquerque ({}) but it is instead {}\n'.format(albuquerque, str(all_trajectories[0][0])))
        error_count += 1

    if not test_point_proximity(all_trajectories[0][-1], san_diego1):
        sys.stdout.write('ERROR: test_trajectory_assembly: Expected last point of first trajectory to be San Diego ({}) but it is instead {}\n'.format(san_diego, str(all_trajectories[0][-1])))
        error_count += 1

    if not test_point_proximity(all_trajectories[1][0], san_diego2):
        sys.stdout.write('ERROR: test_trajectory_assembly: Expected point 0 of second trajectory to be San Diego ({}) but it is instead {}\n'.format(san_diego, str(all_trajectories[1][0])))
        error_count += 1

    if not test_point_proximity(all_trajectories[1][-1], seattle):
        sys.stdout.write('ERROR: test_trajectory_assembly: Expected last point of second trajectory to be Seattle ({}) but it is instead {}\n'.format(seattle, str(all_trajectories[1][-1])))
        error_count += 1

    if not test_point_proximity(all_trajectories[2][0], denver):
        sys.stdout.write('ERROR: test_trajectory_assembly: Expected first point of third trajectory to be Denver ({}) but it is instead {}\n'.format(denver, str(all_trajectories[2][0])))
        error_count += 1

    if not test_point_proximity(all_trajectories[2][-1], new_york):
        sys.stdout.write('ERROR: test_trajectory_assembly: Expected last point of third trajectory to be New York ({}) but it is instead {}\n'.format(new_york, str(all_trajectories[2][-1])))
        error_count += 1

    if not test_timestamp_proximity(all_trajectories[0][0].timestamp, albuquerque.timestamp):
        sys.stdout.write('ERROR: test_trajectory_assembly: Expected timestamp at beginning of trajectory 0 to be {} but it is instead {}\n'.format(albuquerque.timestamp, all_trajectories[0][0].timestamp))
        error_count += 1

    if not test_timestamp_proximity(all_trajectories[0][-1].timestamp, san_diego1.timestamp):
        sys.stdout.write('ERROR: test_trajectory_assembly: Expected timestamp at end of trajectory 0 to be {} but it is instead {}\n'.format(san_diego1.timestamp, all_trajectories[0][-1].timestamp))
        error_count += 1

    if not test_timestamp_proximity(all_trajectories[1][0].timestamp, san_diego2.timestamp):
        sys.stdout.write('ERROR: test_trajectory_assembly: Expected timestamp at beginning of trajectory 1 to be {} but it is instead {}\n'.format(san_diego2.timestamp, all_trajectories[1][0].timestamp))
        error_count += 1

    if not test_timestamp_proximity(all_trajectories[1][-1].timestamp, seattle.timestamp):
        sys.stdout.write('ERROR: test_trajectory_assembly: Expected end at beginning of trajectory 1 to be {} but it is instead {}\n'.format(seattle.timestamp, all_trajectories[1][-1].timestamp))
        error_count += 1

    if not test_timestamp_proximity(all_trajectories[2][0].timestamp, denver.timestamp):
        sys.stdout.write('ERROR: test_trajectory_assembly: Expected timestamp at beginning of trajectory 2 to be {} but it is instead {}\n'.format(denver.timestamp, all_trajectories[2][0].timestamp))
        error_count += 1

    if not test_timestamp_proximity(all_trajectories[2][-1].timestamp, new_york.timestamp):
        sys.stdout.write('ERROR: test_trajectory_assembly: Expected timestamp at end of trajectory 2 to be {} but it is instead {}\n'.format(new_york.timestamp, all_trajectories[2][-1].timestamp))
        error_count += 1

    print("Done checking proximities")

    return error_count