Exemplo n.º 1
0
def route_matching_2(lst1,lst2,step,radius,min_score):
    # input 2 lists of tracking points, each tracking points is geojson format
    # the two lists must have at least two tracking points
    if len(lst1)<2 or len(lst2)<2:
        return False
    start_pnt1=lst1[0]
    end_pnt1=lst1[-1]
    start_pnt2=lst2[0]
    end_pnt2=lst2[-1]
    # Case 1, lst2 is part of lst1:
    lst1_extended=[]
    for i in range(len(lst1)-1):
        dis=ec.calDistance(lst1[i]['track_location']['coordinates'],lst1[i+1]['track_location']['coordinates'])
        num_inter=int(round(old_div(dis,step)))
        if num_inter==0:
            lst1_extended.append(lst1[i]['track_location']['coordinates'])
        else:
            lon_list=np.linspace(lst1[i]['track_location']['coordinates'][0],lst1[i+1]['track_location']['coordinates'][0],num_inter,False)
            lat_list=np.linspace(lst1[i]['track_location']['coordinates'][1],lst1[i+1]['track_location']['coordinates'][1],num_inter,False)
            for j in range(len(lon_list)):
                lst1_extended.append([lon_list[j],lat_list[j]])
    lst1_extended.append(end_pnt1['track_location']['coordinates'])
    lst2_extended=[]
    for i in range(len(lst2)-1):
        dis=ec.calDistance(lst2[i]['track_location']['coordinates'],lst2[i+1]['track_location']['coordinates'])
        num_inter=int(round(old_div(dis,step)))
        if num_inter==0:
            lst2_extended.append(lst2[i]['track_location']['coordinates'])
        else:
            lon_list=np.linspace(lst2[i]['track_location']['coordinates'][0],lst2[i+1]['track_location']['coordinates'][0],num_inter,False)
            lat_list=np.linspace(lst2[i]['track_location']['coordinates'][1],lst2[i+1]['track_location']['coordinates'][1],num_inter,False)
            for j in range(len(lon_list)):
                lst2_extended.append([lon_list[j],lat_list[j]])
    lst2_extended.append(end_pnt2['track_location']['coordinates'])

    # print(len(lst1_extended))
    # print(len(lst2_extended))
    best_score=[]
    score_2_in_1=0
    for point2 in lst2:
        if ec.Include_place_2(lst1_extended,point2['track_location']['coordinates'],radius):
            score_2_in_1+=1
    best_score.append(old_div(score_2_in_1,len(lst2)))
    score_1_in_2=0
    for point1 in lst1:
        if ec.Include_place_2(lst2_extended,point1['track_location']['coordinates'],radius):
            score_1_in_2+=1
    best_score.append(old_div(score_1_in_2,len(lst1)))
    print(best_score)
    if max(best_score)>min_score:
        return True
    else:
        return False
    def compare_approx_result(self, result, expect, distance_fuzz, time_fuzz):
        # This is basically a bunch of asserts to ensure that the timeline is as
        # expected. We are not using a recursive diff because things like the IDs
        # will change from run to run. Instead, I pick out a bunch of important
        # things that are highly user visible
        # Since this is deterministic, we can also include things that are not that user visible :)

        for rt, et in zip(result, expect):
            logging.debug("Comparing %s -> %s with %s -> %s" %
                          (rt.properties.start_fmt_time, rt.properties.end_fmt_time,
                           et.properties.start_fmt_time, et.properties.end_fmt_time))
        self.assertEqual(len(result), len(expect))
        for rt, et in zip(result, expect):
            logging.debug("======= Comparing trip =========")
            logging.debug(json.dumps(rt.properties, indent=4, default=bju.default))
            logging.debug(json.dumps(et.properties, indent=4, default=bju.default))
            # Highly user visible
            self.assertAlmostEqual(rt.properties.start_ts, et.properties.start_ts, delta=time_fuzz)
            self.assertAlmostEqual(rt.properties.end_ts, et.properties.end_ts, delta=time_fuzz)
            self.assertLessEqual(ecc.calDistance(rt.properties.start_loc.coordinates, et.properties.start_loc.coordinates), distance_fuzz)
            self.assertLessEqual(ecc.calDistance(rt.properties.end_loc.coordinates, et.properties.end_loc.coordinates), distance_fuzz)
            self.assertAlmostEqual(rt.properties.distance, et.properties.distance, delta=distance_fuzz)
            self.assertEqual(len(rt.features), len(et.features))

            for rs, es in zip(rt.features, et.features):
                logging.debug("------- Comparing trip feature ---------")
                logging.debug(json.dumps(rs, indent=4, default=bju.default))
                logging.debug(json.dumps(es, indent=4, default=bju.default))
                self.assertEqual(rs.type, es.type)
                if rs.type == "Feature":
                    # The first place will not have an enter time, so we can't check it
                    if 'enter_fmt_time' not in rs.properties:
                        self.assertNotIn("enter_fmt_time", es.properties)
                    else:
                        self.assertAlmostEqual(rs.properties.enter_ts, es.properties.enter_ts, delta=time_fuzz)

                    # Similarly, the last place will not have an exit time, so we can't check it
                    if 'exit_fmt_time' not in rs.properties:
                        self.assertNotIn("exit_fmt_time", es.properties)
                    else:
                        self.assertAlmostEqual(rs.properties.exit_ts, es.properties.exit_ts, delta=time_fuzz)
                    self.assertEqual(rs.properties.feature_type, es.properties.feature_type)
                else:
                    self.assertEqual(rs.type, "FeatureCollection")
                    self.assertAlmostEqual(rs.features[0].properties.start_ts, es.features[0].properties.start_ts, delta=time_fuzz)
                    self.assertAlmostEqual(rs.features[0].properties.end_ts, es.features[0].properties.end_ts, delta=time_fuzz)
                    self.assertEqual(rs.features[0].properties.sensed_mode, es.features[0].properties.sensed_mode)
                    # Fuzz for resampled data as well
                    # https://github.com/e-mission/e-mission-server/issues/288#issuecomment-242450106
                    self.assertAlmostEqual(len(rs.features[0].properties.speeds), len(es.features[0].properties.speeds), delta=2)
                    self.assertAlmostEqual(len(rs.features[0].geometry.coordinates), len(es.features[0].geometry.coordinates), delta=2)
                logging.debug(20 * "-")
            logging.debug(20 * "=")
    def compare_approx_result(self, result, expect, distance_fuzz, time_fuzz):
        # This is basically a bunch of asserts to ensure that the timeline is as
        # expected. We are not using a recursive diff because things like the IDs
        # will change from run to run. Instead, I pick out a bunch of important
        # things that are highly user visible
        # Since this is deterministic, we can also include things that are not that user visible :)

        for rt, et in zip(result, expect):
            logging.debug("Comparing %s -> %s with %s -> %s" %
                          (rt.properties.start_fmt_time, rt.properties.end_fmt_time,
                           et.properties.start_fmt_time, et.properties.end_fmt_time))
        self.assertEqual(len(result), len(expect))
        for rt, et in zip(result, expect):
            logging.debug("======= Comparing trip =========")
            logging.debug(json.dumps(rt.properties, indent=4, default=bju.default))
            logging.debug(json.dumps(et.properties, indent=4, default=bju.default))
            # Highly user visible
            self.assertAlmostEqual(rt.properties.start_ts, et.properties.start_ts, delta=time_fuzz)
            self.assertAlmostEqual(rt.properties.end_ts, et.properties.end_ts, delta=time_fuzz)
            self.assertLessEqual(ecc.calDistance(rt.properties.start_loc.coordinates, et.properties.start_loc.coordinates), distance_fuzz)
            self.assertLessEqual(ecc.calDistance(rt.properties.end_loc.coordinates, et.properties.end_loc.coordinates), distance_fuzz)
            self.assertAlmostEqual(rt.properties.distance, et.properties.distance, delta=distance_fuzz)
            self.assertEqual(len(rt.features), len(et.features))

            for rs, es in zip(rt.features, et.features):
                logging.debug("------- Comparing trip feature ---------")
                logging.debug(json.dumps(rs, indent=4, default=bju.default))
                logging.debug(json.dumps(es, indent=4, default=bju.default))
                self.assertEqual(rs.type, es.type)
                if rs.type == "Feature":
                    # The first place will not have an enter time, so we can't check it
                    if 'enter_fmt_time' not in rs.properties:
                        self.assertNotIn("enter_fmt_time", es.properties)
                    else:
                        self.assertAlmostEqual(rs.properties.enter_ts, es.properties.enter_ts, delta=time_fuzz)

                    # Similarly, the last place will not have an exit time, so we can't check it
                    if 'exit_fmt_time' not in rs.properties:
                        self.assertNotIn("exit_fmt_time", es.properties)
                    else:
                        self.assertAlmostEqual(rs.properties.exit_ts, es.properties.exit_ts, delta=time_fuzz)
                    self.assertEqual(rs.properties.feature_type, es.properties.feature_type)
                else:
                    self.assertEqual(rs.type, "FeatureCollection")
                    self.assertAlmostEqual(rs.features[0].properties.start_ts, es.features[0].properties.start_ts, delta=time_fuzz)
                    self.assertAlmostEqual(rs.features[0].properties.end_ts, es.features[0].properties.end_ts, delta=time_fuzz)
                    self.assertEqual(rs.features[0].properties.sensed_mode, es.features[0].properties.sensed_mode)
                    # Fuzz for resampled data as well
                    # https://github.com/e-mission/e-mission-server/issues/288#issuecomment-242450106
                    self.assertAlmostEqual(len(rs.features[0].properties.speeds), len(es.features[0].properties.speeds), delta=2)
                    self.assertAlmostEqual(len(rs.features[0].geometry.coordinates), len(es.features[0].geometry.coordinates), delta=2)
                logging.debug(20 * "-")
            logging.debug(20 * "=")
def _ios_fill_fake_data(locs_df):
    diff_ts = locs_df.ts.diff()
    fill_ends = diff_ts[diff_ts > 60].index.tolist()

    if len(fill_ends) == 0:
        logging.debug("No large gaps found, no gaps to fill")
        return locs_df
    else:
        logging.debug("Found %s large gaps, filling them all" % len(fill_ends))

    filled_df = locs_df

    for end in fill_ends:
        logging.debug("Found large gap ending at %s, filling it" % end)
        assert end > 0
        start = end - 1
        start_point = locs_df.iloc[start]["loc"]["coordinates"]
        end_point = locs_df.iloc[end]["loc"]["coordinates"]
        if ecc.calDistance(start_point, end_point) > DEFAULT_SAME_POINT_DISTANCE:
            logging.debug("Distance between %s and %s = %s, adding noise is not enough, skipping..." %
                          (start_point, end_point, ecc.calDistance(start_point, end_point)))
            continue

        # else
        # Design from https://github.com/e-mission/e-mission-server/issues/391#issuecomment-247246781
        logging.debug("start = %s, end = %s, generating entries between %s and %s" %
            (start, end, locs_df.ts[start], locs_df.ts[end]))
        ts_fill = np.arange(locs_df.ts[start] + 60, locs_df.ts[end], 60)
        # We only pick entries that are *greater than* 60 apart
        assert len(ts_fill) > 0
        dist_fill = np.random.uniform(low=0, high=100, size=len(ts_fill))
        angle_fill = np.random.uniform(low=0, high=2 * np.pi, size=len(ts_fill))
        # Formula from http://gis.stackexchange.com/questions/5821/calculating-latitude-longitude-x-miles-from-point
        lat_fill = locs_df.latitude[end] + np.multiply(dist_fill, np.sin(
            angle_fill) / 111111)
        cl = np.cos(locs_df.latitude[end])
        lng_fill = locs_df.longitude[end] + np.multiply(dist_fill, np.cos(
            angle_fill) / cl / 111111)
        logging.debug("Fill lengths are: dist %s, angle %s, lat %s, lng %s" %
                      (len(dist_fill), len(angle_fill), len(lat_fill), len(lng_fill)))

        # Unsure if this is needed, but lets put it in just in case
        loc_fill = [gj.Point(l) for l in zip(lng_fill, lat_fill)]
        fill_df = pd.DataFrame(
            {"ts": ts_fill, "longitude": lng_fill, "latitude": lat_fill,
             "loc": loc_fill})
        filled_df = pd.concat([filled_df, fill_df])

    sorted_filled_df = filled_df.sort("ts").reset_index()
    logging.debug("after filling, returning head = %s, tail = %s" %
                  (sorted_filled_df[["fmt_time", "ts", "latitude", "longitude", "metadata_write_ts"]].head(),
                   sorted_filled_df[["fmt_time", "ts", "latitude", "longitude", "metadata_write_ts"]].tail()))
    return sorted_filled_df
Exemplo n.º 5
0
def stitch_together(ending_section_entry, stop_entry, starting_section_entry):
    ending_section = ending_section_entry.data
    stop = stop_entry.data
    starting_section = starting_section_entry.data

    ending_section.end_stop = stop_entry.get_id()

    stop.enter_ts = ending_section.end_ts
    stop.enter_local_dt = ending_section.end_local_dt
    stop.enter_fmt_time = ending_section.end_fmt_time
    stop.ending_section = ending_section_entry.get_id()

    stop.enter_loc = ending_section.end_loc
    stop.exit_loc = starting_section.start_loc
    stop.duration = starting_section.start_ts - ending_section.end_ts
    stop.distance = ecc.calDistance(stop.enter_loc.coordinates,
                                    stop.exit_loc.coordinates)
    stop.source = "SmoothedHighConfidenceMotion"

    stop.exit_ts = starting_section.start_ts
    stop.exit_local_dt = starting_section.start_local_dt
    stop.exit_fmt_time = starting_section.start_fmt_time
    stop.starting_section = starting_section_entry.get_id()

    starting_section.start_stop = stop_entry.get_id()

    ending_section_entry["data"] = ending_section
    stop_entry["data"] = stop
    starting_section_entry["data"] = starting_section
Exemplo n.º 6
0
def find_near(lst,pnt,radius):
    near=[]
    for i in range(len(lst)):
        # print(ec.calDistance(lst[i],pnt))
        if ec.calDistance(lst[i],pnt)<radius:
            near.append(i)
    return near
def stitch_together(ending_section_entry, stop_entry, starting_section_entry):
    ending_section = ending_section_entry.data
    stop = stop_entry.data
    starting_section = starting_section_entry.data

    ending_section.end_stop = stop_entry.get_id()

    stop.enter_ts = ending_section.end_ts
    stop.enter_local_dt = ending_section.end_local_dt
    stop.enter_fmt_time = ending_section.end_fmt_time
    stop.ending_section = ending_section_entry.get_id()

    stop.enter_loc = ending_section.end_loc
    stop.exit_loc = starting_section.start_loc
    stop.duration = starting_section.start_ts - ending_section.end_ts
    stop.distance = ecc.calDistance(stop.enter_loc.coordinates,
                                    stop.exit_loc.coordinates)
    stop.source = "SmoothedHighConfidenceMotion"

    stop.exit_ts = starting_section.start_ts
    stop.exit_local_dt = starting_section.start_local_dt
    stop.exit_fmt_time = starting_section.start_fmt_time
    stop.starting_section = starting_section_entry.get_id()

    starting_section.start_stop = stop_entry.get_id()

    ending_section_entry["data"] = ending_section
    stop_entry["data"] = stop
    starting_section_entry["data"] = starting_section
Exemplo n.º 8
0
def find_near(lst, pnt, radius):
    near = []
    for i in range(len(lst)):
        # print(ec.calDistance(lst[i],pnt))
        if ec.calDistance(lst[i], pnt) < radius:
            near.append(i)
    return near
Exemplo n.º 9
0
def Frechet(R1,R2,varargin=None):

    # get path point length
    L1=len(R1);
    L2=len(R2);

    frechet1=np.zeros((L1,L2))
    # print(frechet1)
    # calculate frechet distance matrix
    for i in range(L1):
        for j in range(L2):
            # frechet1[i,j]=math.sqrt(math.pow(R1[i][0]-R2[j][0],2)+math.pow(R1[i][1]-R2[j][1],2))
            frechet1[i,j]=ec.calDistance(R1[i],R2[j])
    fmin=frechet1.min();
    fmax=frechet1.max();
    # print(fmin)
    # print(fmax)

    # handle resolution
    if varargin==None:
        varargin=1000
    # print(frechet1<=3)
    # print(varargin)
    # compute frechet distance
    # print(np.linspace(fmin,fmax,varargin))
    for q3 in np.linspace(fmin,fmax,varargin):
        # print(q3)
        im1=np.asarray(measurements.label(frechet1<=q3))[0]
        # print(im1.shape)
        # print(im1[-1,-1])
        # get region number of beginning and end points
        if im1[0,0]!=0 and im1[0,0]==im1[-1,-1]:
            f=q3
            break
    return f
Exemplo n.º 10
0
def stitch_together_end(new_place_entry, curr_trip_entry, end_loc):
    """
    Stitch together the last place and the current trip at the start location.
    Note that we don't actually know the time that we left the start place
    because we are only invoked when we have exited the geofence. We can do
    something fancy with extraploation based on average speed, but let's keep
    the fuzz factor for now.
    """
    new_place = new_place_entry.data
    curr_trip = curr_trip_entry.data

    curr_trip.end_ts = end_loc.ts
    curr_trip.end_local_dt = end_loc.local_dt
    curr_trip.end_fmt_time = end_loc.fmt_time
    curr_trip.end_place = new_place_entry.get_id()
    curr_trip.end_loc = end_loc.loc
    curr_trip.duration = curr_trip.end_ts - curr_trip.start_ts
    curr_trip.distance = ecc.calDistance(curr_trip.end_loc.coordinates,
                                         curr_trip.start_loc.coordinates)

    new_place.enter_ts = end_loc.ts
    new_place.enter_local_dt = end_loc.local_dt
    new_place.enter_fmt_time = end_loc.fmt_time
    new_place.ending_trip = curr_trip_entry.get_id()
    new_place.location = end_loc.loc

    # The wrapper class returns a copy of the data object, so any changes to it
    # are not reflected in the original
    new_place_entry["data"] = new_place
    curr_trip_entry["data"] = curr_trip
def stitch_together_end(new_place_entry, curr_trip_entry, end_loc):
    """
    Stitch together the last place and the current trip at the start location.
    Note that we don't actually know the time that we left the start place
    because we are only invoked when we have exited the geofence. We can do
    something fancy with extraploation based on average speed, but let's keep
    the fuzz factor for now.
    """
    new_place = new_place_entry.data
    curr_trip = curr_trip_entry.data

    curr_trip.end_ts = end_loc.ts
    curr_trip.end_local_dt = end_loc.local_dt
    curr_trip.end_fmt_time = end_loc.fmt_time
    curr_trip.end_place = new_place_entry.get_id()
    curr_trip.end_loc = end_loc.loc
    curr_trip.duration = curr_trip.end_ts - curr_trip.start_ts
    curr_trip.distance = ecc.calDistance(curr_trip.end_loc.coordinates,
                                         curr_trip.start_loc.coordinates)

    new_place.enter_ts = end_loc.ts
    new_place.enter_local_dt = end_loc.local_dt
    new_place.enter_fmt_time = end_loc.fmt_time
    new_place.ending_trip = curr_trip_entry.get_id()
    new_place.location = end_loc.loc

    # The wrapper class returns a copy of the data object, so any changes to it
    # are not reflected in the original
    new_place_entry["data"] = new_place
    curr_trip_entry["data"] = curr_trip
def _get_distance_from_start_place_to_end(raw_trip_entry):
    import emission.core.common as ecc

    start_place_id = raw_trip_entry.data.start_place
    start_place = esda.get_object(esda.RAW_PLACE_KEY, start_place_id)
    dist = ecc.calDistance(start_place.location.coordinates,
                           raw_trip_entry.data.end_loc.coordinates)
    logging.debug("Distance from raw_place %s to the end of raw_trip_entry %s = %s" %
                  (start_place_id, raw_trip_entry.get_id(), dist))
    return dist
def _get_distance_from_start_place_to_end(raw_trip_entry):
    import emission.core.common as ecc

    start_place_id = raw_trip_entry.data.start_place
    start_place = esda.get_object(esda.RAW_PLACE_KEY, start_place_id)
    dist = ecc.calDistance(start_place.location.coordinates,
                           raw_trip_entry.data.end_loc.coordinates)
    logging.debug("Distance from raw_place %s to the end of raw_trip_entry %s = %s" %
                  (start_place_id, raw_trip_entry.get_id(), dist))
    return dist
Exemplo n.º 14
0
def find_nearest(lst,pnt):
    nearest=lst[0]
    dis=99999999
    for i in range(len(lst)):
        # print(ec.calDistance(lst[i],pnt))
        new_dis=ec.calDistance(lst[i],pnt)
        if new_dis<dis:
            dis=new_dis
            nearest=lst[i]
    print(dis)
    return nearest
Exemplo n.º 15
0
def calSpeed(point1, point2):
  from dateutil import parser
  distanceDelta = calDistance(point1['data']['loc']['coordinates'],
                              point2['data']['loc']['coordinates'])
  timeDelta = point2['data']['ts'] - point1['data']['ts']
  # logging.debug("while calculating speed form %s -> %s, distanceDelta = %s, timeDelta = %s" %
  #               (trackpoint1, trackpoint2, distanceDelta, timeDelta))
  if timeDelta != 0:
    return old_div(distanceDelta, timeDelta.total_seconds())
  else:
    return None
Exemplo n.º 16
0
def max_Distance(points):
    # 'track_points':[{'track_location':{'type':'Point', 'coordinates':[point["lat"],point["lon"]]}, 'time':point["time"]}for point in seg_act_note["trackPoints"]] if "trackPoints" in seg_act_note else []}
    num_pts = len(points)
    max_d = 0
    for i in range(num_pts):
        for j in range(i + 1, num_pts):
            max_d = max(
                max_d,
                calDistance(points[i]["track_location"]["coordinates"], points[j]["track_location"]["coordinates"]),
            )
    return max_d
Exemplo n.º 17
0
def max_Distance(points):
    # 'track_points':[{'track_location':{'type':'Point', 'coordinates':[point["lat"],point["lon"]]}, 'time':point["time"]}for point in seg_act_note["trackPoints"]] if "trackPoints" in seg_act_note else []}
    num_pts = len(points)
    max_d = 0
    for i in range(num_pts):
        for j in range(i + 1, num_pts):
            max_d = max(
                max_d,
                calDistance(points[i]['track_location']['coordinates'],
                            points[j]['track_location']['coordinates']))
    return max_d
Exemplo n.º 18
0
def calSpeed(trackpoint1, trackpoint2):
  from dateutil import parser
  distanceDelta = calDistance(trackpoint1['track_location']['coordinates'],
                              trackpoint2['track_location']['coordinates'])
  timeDelta = parser.parse(trackpoint2['time']) - parser.parse(trackpoint1['time'])
  # logging.debug("while calculating speed form %s -> %s, distanceDelta = %s, timeDelta = %s" %
  #               (trackpoint1, trackpoint2, distanceDelta, timeDelta))
  if timeDelta.total_seconds() != 0:
    return distanceDelta / timeDelta.total_seconds()
  else:
    return None
Exemplo n.º 19
0
def find_nearest(lst, pnt):
    nearest = lst[0]
    dis = 99999999
    for i in range(len(lst)):
        # print(ec.calDistance(lst[i],pnt))
        new_dis = ec.calDistance(lst[i], pnt)
        if new_dis < dis:
            dis = new_dis
            nearest = lst[i]
    print(dis)
    return nearest
Exemplo n.º 20
0
def searchTrip(user, period, startpoint, endpoint, mode, option):
    user_id = UUID(user)
    user_home = detect_home(user_id)
    gmap = pygmaps.maps(user_home[1], user_home[0], 14)
    start, end = Date(period)
    sectionList = []
    startpoint = Geocoder.geocode(startpoint)[0].coordinates
    endpoint = Geocoder.geocode(endpoint)[0].coordinates

    for section in get_section_db().find({
            "$and": [{
                "mode": mode
            }, {
                "mode": {
                    "$ne": 'airplane'
                }
            }, {
                "mode": {
                    "$ne": 7
                }
            }, {
                "section_start_point": {
                    "$ne": None
                }
            }, {
                "section_end_point": {
                    "$ne": None
                }
            }]
    }):
        point_start = section['section_start_point']['coordinates']
        point_end = section['section_end_point']['coordinates']
        if calDistance(startpoint, point_start) < 100 and calDistance(
                endpoint, point_end) < 100:
            sectionList.append(section['_id'])
            gmap.addpoint(point_end[1], point_end[0], COLOR[1])
            gmap.addpoint(point_start[1], point_start[0], COLOR[1])
        drawSection(section, option, gmap)
    gmap.draw('gmap_display/' + 'SearchResult' + str(start)[:10] + '-' +
              str(end)[:10] + '_' + user + '.html')
Exemplo n.º 21
0
    def squish_stops(self, motion_changes, section_list):
        STOP_DISTANCE_THRESHOLD = max(
            eac.get_config()["section.startStopRadius"],
            eac.get_config()["section.endStopRadius"])

        for i, x in enumerate(zip(section_list, section_list[1:])):
            try:
                ((ss1, se1, st1), (ss2, se2, st2)) = x
            except ValueError as e:
                print(len(x), [len(xm) for xm in x])
                print(x[0])
            # i is the index of s1, i+1 is the index of s2
            stop_duration = ss2.ts - se1.ts
            stop_distance = ecc.calDistance(ss2.loc["loc"]["coordinates"],
                                            se1.loc["loc"]["coordinates"])
            logging.debug(
                "while squishing stop, %s (%d) -> %s (%d), duration = %d, dist = %d"
                % (se1.fmt_time, i, ss2.fmt_time, i + 1, stop_duration,
                   stop_distance))
            if stop_distance > STOP_DISTANCE_THRESHOLD:
                mcs1, mce1 = motion_changes[i]
                mcs2, mce2 = motion_changes[i + 1]
                assert mce1.ts == mcs2.ts, \
                    "curr motion change ends at %s, next motion change ends at %s" % \
                        (mce1.fmt_time, mce2.fmt_time)

                # e.g. if the motion changed at 11:33, mce1.ts = mcs2.ts = 11:33
                # if the first section's last point (se1) was at 11:31 and the
                # second section's first point (ss2) was at 11:51, then we
                # should set the second section's first point to be the first
                # section's last point (e.g. ss2 = se1)

                stop_start_gap = mce1.ts - se1.ts
                stop_end_gap = ss2.ts - mcs2.ts
                gap_ratio = max(stop_start_gap, stop_end_gap) / min(
                    stop_start_gap, stop_end_gap)

                log_msg = (
                    "need to squish, comparing start_gap = %d, end_gap = %d, ratio = %4f"
                    % (stop_start_gap, stop_end_gap, gap_ratio))
                if gap_ratio >= 1.5:
                    if stop_start_gap < stop_end_gap:
                        logging.debug(log_msg + ", setting ss2 <- se1")
                        section_list[i + 1] = (se1, se2, st2)
                    else:
                        logging.debug(log_msg + ", setting se1 <- ss2")
                        section_list[i] = (ss1, ss2, st1)
                else:
                    logging.debug(log_msg +
                                  ", no change, fixed in clean_and_resample")
Exemplo n.º 22
0
def searchTrip(user, period, startpoint, endpoint, mode, option):
    user_id = UUID(user)
    user_home = detect_home(user_id)
    gmap = pygmaps.maps(user_home[1], user_home[0], 14)
    start, end = Date(period)
    sectionList = []
    startpoint = Geocoder.geocode(startpoint)[0].coordinates
    endpoint = Geocoder.geocode(endpoint)[0].coordinates

    for section in get_section_db().find({"$and":[
        {"mode": mode},
        {"mode": {"$ne": 'airplane'}},
        {"mode": {"$ne":7}},
        {"section_start_point": {"$ne": None}},
        {"section_end_point": {"$ne": None}}]}):
        point_start = section['section_start_point']['coordinates']
        point_end = section['section_end_point']['coordinates']
        if calDistance(startpoint, point_start) < 100 and calDistance(endpoint, point_end) < 100:
            sectionList.append(section['_id'])
            gmap.addpoint(point_end[1], point_end[0], COLOR[1])
            gmap.addpoint(point_start[1], point_start[0], COLOR[1])
        drawSection(section, option, gmap)
    gmap.draw('gmap_display/' + 'SearchResult' + str(start)[:10] + '-' + str(end)[:10] + '_' + user + '.html')
def found_untracked_period(timeseries, last_place, start_loc):
    """
    Check to see whether the two places are the same.
    This is a fix for https://github.com/e-mission/e-mission-server/issues/378
    Note both last_place and start_loc are data wrappers (e.g. RawPlace and Location objects)
    NOT entries. So field access should not be preceeded by "data"

    :return: True if we should create a new start place instead of linking to
    the last_place, False otherwise
    """
    # Implementing logic from https://github.com/e-mission/e-mission-server/issues/378
    if last_place.enter_ts is None:
        logging.debug("last_place.enter_ts = %s" % (last_place.enter_ts))
        logging.debug(
            "start of a chain, unable to check for restart from previous trip end, assuming not restarted"
        )
        return False

    if _is_tracking_restarted(last_place, start_loc, timeseries):
        logging.debug("tracking has been restarted, returning True")
        return True

    transition_distance = ecc.calDistance(last_place.location.coordinates,
                                          start_loc.loc.coordinates)
    logging.debug(
        "while determining new_start_place, transition_distance = %s" %
        transition_distance)
    if transition_distance < 1000:
        logging.debug("transition_distance %s < 1000, returning False",
                      transition_distance)
        return False

    time_delta = start_loc.ts - last_place.enter_ts
    transition_speed = old_div(transition_distance, time_delta)
    logging.debug(
        "while determining new_start_place, time_delta = %s, transition_speed = %s"
        % (time_delta, transition_speed))

    # Let's use a little less than walking speed 3km/hr < 3mph (4.83 kmph)
    speed_threshold = old_div(float(3000), (60 * 60))

    if transition_speed > speed_threshold:
        logging.debug("transition_speed %s > %s, returning False" %
                      (transition_speed, speed_threshold))
        return False
    else:
        logging.debug("transition_speed %s <= %s, 'stopped', returning True" %
                      (transition_speed, speed_threshold))
        return True
Exemplo n.º 24
0
def found_untracked_period(timeseries, last_place, start_loc):
    """
    Check to see whether the two places are the same.
    This is a fix for https://github.com/e-mission/e-mission-server/issues/378
    Note both last_place and start_loc are data wrappers (e.g. RawPlace and Location objects)
    NOT entries. So field access should not be preceeded by "data"

    :return: True if we should create a new start place instead of linking to
    the last_place, False otherwise
    """
    # Implementing logic from https://github.com/e-mission/e-mission-server/issues/378
    if last_place.enter_ts is None:
        logging.debug("last_place.enter_ts = %s" % (last_place.enter_ts))
        logging.debug("start of a chain, unable to check for restart from previous trip end, assuming not restarted")
        return False

    if _is_tracking_restarted(last_place, start_loc, timeseries):
        logging.debug("tracking has been restarted, returning True")
        return True

    transition_distance = ecc.calDistance(last_place.location.coordinates,
                       start_loc.loc.coordinates)
    logging.debug("while determining new_start_place, transition_distance = %s" % transition_distance)
    if transition_distance < 1000:
        logging.debug("transition_distance %s < 1000, returning False", transition_distance)
        return False

    time_delta = start_loc.ts - last_place.enter_ts
    transition_speed = old_div(transition_distance, time_delta)
    logging.debug("while determining new_start_place, time_delta = %s, transition_speed = %s"
                  % (time_delta, transition_speed))

    # Let's use a little less than walking speed 3km/hr < 3mph (4.83 kmph)
    speed_threshold = old_div(float(3000), (60*60))

    if transition_speed > speed_threshold:
        logging.debug("transition_speed %s > %s, returning False" %
                      (transition_speed, speed_threshold))
        return False
    else:
        logging.debug("transition_speed %s <= %s, 'stopped', returning True" %
                        (transition_speed, speed_threshold))
        return True
Exemplo n.º 25
0
def refineRoute(lst1,step):
    if lst1 ==[]:
        return lst1

    # print(len(lst1))
    lst1_extended=[]
    for i in range(len(lst1)-1):
        dis=ec.calDistance(lst1[i],lst1[i+1])
        num_inter=int(round(old_div(dis,step)))
        if num_inter==0:
            lst1_extended.append(lst1[i])
        else:
            lon_list=np.linspace(lst1[i][0],lst1[i+1][0],num_inter,False)
            lat_list=np.linspace(lst1[i][1],lst1[i+1][1],num_inter,False)
            for j in range(len(lon_list)):
                lst1_extended.append([lon_list[j],lat_list[j]])
    lst1_extended.append(lst1[-1])
    # print(len(lst1))
    # print(len(lst1_extended))
    return lst1_extended
Exemplo n.º 26
0
def refineRoute(lst1, step):
    if lst1 == []:
        return lst1

    # print(len(lst1))
    lst1_extended = []
    for i in range(len(lst1) - 1):
        dis = ec.calDistance(lst1[i], lst1[i + 1])
        num_inter = int(round(old_div(dis, step)))
        if num_inter == 0:
            lst1_extended.append(lst1[i])
        else:
            lon_list = np.linspace(lst1[i][0], lst1[i + 1][0], num_inter,
                                   False)
            lat_list = np.linspace(lst1[i][1], lst1[i + 1][1], num_inter,
                                   False)
            for j in range(len(lon_list)):
                lst1_extended.append([lon_list[j], lat_list[j]])
    lst1_extended.append(lst1[-1])
    # print(len(lst1))
    # print(len(lst1_extended))
    return lst1_extended
 def get_section_speed(self, loc_points, with_speed_loc_points,
                       points_before, points_after):
     if len(loc_points) > 1:
         curr_median_speed = with_speed_loc_points.speed.median()
         logging.debug("Median calculation from speeds = %s" %
                       curr_median_speed)
     elif (len(points_before) > 0) and (len(points_after) > 0):
         # We don't have any points of our own. Let's use the last point
         # from before and the first point from after
         last_prev_point = points_before.iloc[-1]
         first_next_point = points_after.iloc[0]
         dist = ecc.calDistance(last_prev_point["loc"]['coordinates'],
                                first_next_point["loc"]['coordinates'])
         time = first_next_point.ts - last_prev_point.ts
         logging.debug(
             "Backup calculation from %s -> %s. dist = %d, time = %4f, speed = %4f"
             % (last_prev_point.fmt_time, first_next_point.fmt_time, dist,
                time, dist / time))
         curr_median_speed = dist / time
     else:
         curr_median_speed = 0
     return curr_median_speed
Exemplo n.º 28
0
def _ios_fill_fake_data(locs_df):
    diff_ts = locs_df.ts.diff()
    fill_ends = diff_ts[diff_ts > 60].index.tolist()

    if len(fill_ends) == 0:
        logging.debug("No large gaps found, no gaps to fill")
        return locs_df
    else:
        logging.debug("Found %s large gaps, filling them all" % len(fill_ends))

    filled_df = locs_df

    for end in fill_ends:
        logging.debug("Found large gap ending at %s, filling it" % end)
        assert end > 0
        start = end - 1
        start_point = locs_df.iloc[start]["loc"]["coordinates"]
        end_point = locs_df.iloc[end]["loc"]["coordinates"]
        if ecc.calDistance(start_point,
                           end_point) > DEFAULT_SAME_POINT_DISTANCE:
            logging.debug(
                "Distance between %s and %s = %s, adding noise is not enough, skipping..."
                % (start_point, end_point,
                   ecc.calDistance(start_point, end_point)))
            continue

        # else
        # Design from https://github.com/e-mission/e-mission-server/issues/391#issuecomment-247246781
        logging.debug(
            "start = %s, end = %s, generating entries between %s and %s" %
            (start, end, locs_df.ts[start], locs_df.ts[end]))
        ts_fill = np.arange(locs_df.ts[start] + 60, locs_df.ts[end], 60)
        # We only pick entries that are *greater than* 60 apart
        assert len(ts_fill) > 0
        dist_fill = np.random.uniform(low=0, high=100, size=len(ts_fill))
        angle_fill = np.random.uniform(low=0,
                                       high=2 * np.pi,
                                       size=len(ts_fill))
        # Formula from http://gis.stackexchange.com/questions/5821/calculating-latitude-longitude-x-miles-from-point
        lat_fill = locs_df.latitude[end] + np.multiply(
            dist_fill, old_div(np.sin(angle_fill), 111111))
        cl = np.cos(locs_df.latitude[end])
        lng_fill = locs_df.longitude[end] + np.multiply(
            dist_fill,
            np.cos(angle_fill) / cl / 111111)
        logging.debug(
            "Fill lengths are: dist %s, angle %s, lat %s, lng %s" %
            (len(dist_fill), len(angle_fill), len(lat_fill), len(lng_fill)))

        # Unsure if this is needed, but lets put it in just in case
        loc_fill = [gj.Point(l) for l in zip(lng_fill, lat_fill)]
        fill_df = pd.DataFrame({
            "ts": ts_fill,
            "longitude": lng_fill,
            "latitude": lat_fill,
            "loc": loc_fill
        })
        filled_df = pd.concat([filled_df, fill_df])

    sorted_filled_df = filled_df.sort_values(by="ts").reset_index()
    logging.debug(
        "after filling, returning head = %s, tail = %s" % (sorted_filled_df[[
            "fmt_time", "ts", "latitude", "longitude", "metadata_write_ts"
        ]].head(), sorted_filled_df[[
            "fmt_time", "ts", "latitude", "longitude", "metadata_write_ts"
        ]].tail()))
    return sorted_filled_df
Exemplo n.º 29
0
def route_matching(lst1,lst2,step,radius,len_match,min_score):
    # input 2 lists of tracking points, each tracking points is geojson format
    # the two lists must have at least two tracking points
    if len(lst1)<2 or len(lst2)<2:
        return False
    start_pnt1=lst1[0]
    end_pnt1=lst1[-1]
    start_pnt2=lst2[0]
    end_pnt2=lst2[-1]
    # Case 1, lst2 is part of lst1:
    lst1_extended=[]
    for i in range(len(lst1)-1):
        dis=ec.calDistance(lst1[i]['track_location']['coordinates'],lst1[i+1]['track_location']['coordinates'])
        num_inter=int(round(old_div(dis,step)))
        if num_inter==0:
            lst1_extended.append(lst1[i]['track_location']['coordinates'])
        else:
            lon_list=np.linspace(lst1[i]['track_location']['coordinates'][0],lst1[i+1]['track_location']['coordinates'][0],num_inter,False)
            lat_list=np.linspace(lst1[i]['track_location']['coordinates'][1],lst1[i+1]['track_location']['coordinates'][1],num_inter,False)
            for j in range(len(lon_list)):
                lst1_extended.append([lon_list[j],lat_list[j]])
    lst1_extended.append(end_pnt1['track_location']['coordinates'])
    lst2_extended=[]
    for i in range(len(lst2)-1):
        dis=ec.calDistance(lst2[i]['track_location']['coordinates'],lst2[i+1]['track_location']['coordinates'])
        num_inter=int(round(old_div(dis,step)))
        if num_inter==0:
            lst2_extended.append(lst2[i]['track_location']['coordinates'])
        else:
            lon_list=np.linspace(lst2[i]['track_location']['coordinates'][0],lst2[i+1]['track_location']['coordinates'][0],num_inter,False)
            lat_list=np.linspace(lst2[i]['track_location']['coordinates'][1],lst2[i+1]['track_location']['coordinates'][1],num_inter,False)
            for j in range(len(lon_list)):
                lst2_extended.append([lon_list[j],lat_list[j]])
    lst2_extended.append(end_pnt2['track_location']['coordinates'])

    # print(len(lst1_extended))
    # print(len(lst2_extended))
    near_start2=find_near(lst1_extended,start_pnt2['track_location']['coordinates'],radius)
    near_end2=find_near(lst1_extended,end_pnt2['track_location']['coordinates'],radius)

    near_start1=find_near(lst2_extended,start_pnt1['track_location']['coordinates'],radius)
    near_end1=find_near(lst2_extended,end_pnt1['track_location']['coordinates'],radius)

    # print(near_start2)
    # print(near_end2)
    # print(near_start1)
    # print(near_end1)
    best_score=[]

    if len(near_start2)>0 and len(near_end2)>0:
        print("start of case 1")
        for near_s in near_start2:
            for near_e in near_end2:
                if old_div(min(abs(near_e-near_s)+1,len(lst2_extended)),max(abs(near_e-near_s)+1,len(lst2_extended)))>=len_match:
                    print("possible near_s is %s" % near_s)
                    print("possible near_e is %s" % near_e)

                    if near_e>near_s:
                        print("start index is %d" % near_s)
                        print("end index is %d" % near_e)
                        route1=lst1_extended[near_s:near_e+1:1]

                        route2=lst2_extended
                        print("route1 is %s" % route1)
                        print("route2 is %s" % route2)
                    else:
                        print("start index is %d" % near_s)
                        print("end index is %d" % near_e)
                        route1=lst1_extended[near_e:near_s+1:1][::-1]
                        route2=lst2_extended
                        print("route1 is %s" % route1)
                        print("route2 is %s" % route2)
                    best_score.append(cal_matching_score(route1,route2,radius))

    if len(near_start1)>0 and len(near_end1)>0:
        print("start of case 2")
        for near_s in near_start1:
            for near_e in near_end1:
                if old_div(min(abs(near_e-near_s)+1,len(lst1_extended)),max(abs(near_e-near_s)+1,len(lst1_extended)))>=len_match:
                    if near_e>near_s:
                        print("start index is %d" % near_s)
                        print("end index is %d" % near_e)
                        route1=lst1_extended
                        route2=lst2_extended[near_s:near_e+1:1]
                        print("route1 is %s" % route1)
                        print("route2 is %s" % route2)
                    else:
                        route1=lst1_extended
                        route2=lst2_extended[near_e:near_s+1:1][::-1]
                    best_score.append(cal_matching_score(route1,route2,radius))
    print(best_score)
    if len(best_score)>0 and max(best_score)>min_score:
        return True
    else:
        return False
Exemplo n.º 30
0
 def get_distance(self):
     return cm.calDistance(self.trip_start_location, self.trip_end_location, True)
Exemplo n.º 31
0
def calDistance(point1, point2):
    return ec.calDistance([point1.longitude, point1.latitude],
                          [point2.longitude, point2.latitude])
Exemplo n.º 32
0
def _check_zip_validity(user_home, user):
    if user_home != "N/A" and detect_home_from_db(
            user) != "N/A" and calDistance(
                user_home, detect_home_from_db(user)) < TOLERANCE:
        return True
    return False
Exemplo n.º 33
0
def _check_zip_validity(user_home, user):
    if user_home != "N/A" and detect_home_from_db(user) != "N/A" and calDistance(user_home, detect_home_from_db(user)) < TOLERANCE:
        return True
    return False
Exemplo n.º 34
0
    def filter(self, with_speeds_df):
        self.inlier_mask_ = [True] * with_speeds_df.shape[0]

        quality_segments = []
        curr_segment = []
        prev_pt = None

        for (i, pt) in enumerate(with_speeds_df.to_dict('records')):
            pt = ad.AttrDict(pt)
            if prev_pt is None:
                # Don't have enough data yet, so don't make any decisions
                prev_pt = pt
            else:
                currSpeed = pf.calSpeed(prev_pt, pt)
                print("while considering point %s, speed = %s" % (i, currSpeed))
                # Should make this configurable
                if currSpeed > self.maxSpeed:
                    print("currSpeed > %d, starting new quality segment at index %s " % (self.maxSpeed, i))
                    quality_segments.append(curr_segment)
                    curr_segment = []
                else:
                    print("currSpeed < %d, retaining index %s in existing quality segment " % (self.maxSpeed, i))
                prev_pt = pt
                curr_segment.append(i)
        # Append the last segment once we are at the end
        quality_segments.append(curr_segment)

        print("Number of quality segments is %d" % len(quality_segments))

        last_segment = quality_segments[0]
        for curr_segment in quality_segments[1:]:
            print("Considering segments %s and %s" % (last_segment, curr_segment))

            if len(last_segment) == 0:
                # If the last segment has no points, we can't compare last and
                # current, but should reset last, otherwise, we will be stuck
                # forever
                logging.info("len(last_segment) = %d, len(curr_segment) = %d, skipping" %
                    (len(last_segment), len(curr_segment)))
                last_segment = curr_segment
                continue

            if len(curr_segment) == 0:
                # If the current segment has no points, we can't compare last and
                # current, but can just continue since the for loop will reset current
                logging.info("len(last_segment) = %d, len(curr_segment) = %d, skipping" %
                    (len(last_segment), len(curr_segment)))
                continue
            get_coords = lambda i: [with_speeds_df.iloc[i]["mLongitude"], with_speeds_df.iloc[i]["mLatitude"]]
            get_ts = lambda i: with_speeds_df.iloc[i]["mTime"]
            # I don't know why they would use time instead of distance, but
            # this is what the existing POSDAP code does.
            print("About to compare curr_segment duration %s with last segment duration %s" %
                            (get_ts(curr_segment[-1]) - get_ts(curr_segment[0]),
                             get_ts(last_segment[-1]) - get_ts(last_segment[0])))
            if (get_ts(curr_segment[-1]) - get_ts(curr_segment[0]) <=
                get_ts(last_segment[-1]) - get_ts(last_segment[0])):
                print("curr segment %s is shorter, cut it" % curr_segment)
                ref_idx = last_segment[-1]
                for curr_idx in curr_segment:
                    print("Comparing distance %s with speed %s * time %s = %s" %
                        (math.fabs(ec.calDistance(get_coords(ref_idx), get_coords(curr_idx))),
                         old_div(self.maxSpeed, 100), abs(get_ts(ref_idx) - get_ts(curr_idx)),
                         self.maxSpeed / 100 * abs(get_ts(ref_idx) - get_ts(curr_idx))))

                    if (math.fabs(ec.calDistance(get_coords(ref_idx), get_coords(curr_idx))) >
                        (self.maxSpeed / 1000 * abs(get_ts(ref_idx) - get_ts(curr_idx)))):
                        print("Distance is greater than max speed * time, deleting %s" % curr_idx)
                        self.inlier_mask_[curr_idx] = False
            else:
                print("prev segment %s is shorter, cut it" % last_segment)
                ref_idx = curr_segment[-1]
                for curr_idx in reversed(last_segment):
                    print("Comparing distance %s with speed %s * time %s = %s" %
                        (math.fabs(ec.calDistance(get_coords(ref_idx), get_coords(curr_idx))),
                         old_div(self.maxSpeed, 1000) , abs(get_ts(ref_idx) - get_ts(curr_idx)),
                         self.maxSpeed / 1000 * abs(get_ts(ref_idx) - get_ts(curr_idx))))
                    if (abs(ec.calDistance(get_coords(ref_idx), get_coords(curr_idx))) >
                        (self.maxSpeed / 1000 *  abs(get_ts(ref_idx) - get_ts(curr_idx)))):
                        print("Distance is greater than max speed * time, deleting %s" % curr_idx)
                        self.inlier_mask_[curr_idx] = False
            last_segment = curr_segment
        logging.info("Filtering complete, removed indices = %s" % np.nonzero(self.inlier_mask_))
Exemplo n.º 35
0
 def distance(self, other):
     ## Returns distance between 2 coordinates in meters
     return cm.calDistance(self, other, True)
Exemplo n.º 36
0
def route_matching_2(lst1, lst2, step, radius, min_score):
    # input 2 lists of tracking points, each tracking points is geojson format
    # the two lists must have at least two tracking points
    if len(lst1) < 2 or len(lst2) < 2:
        return False
    start_pnt1 = lst1[0]
    end_pnt1 = lst1[-1]
    start_pnt2 = lst2[0]
    end_pnt2 = lst2[-1]
    # Case 1, lst2 is part of lst1:
    lst1_extended = []
    for i in range(len(lst1) - 1):
        dis = ec.calDistance(lst1[i]['track_location']['coordinates'],
                             lst1[i + 1]['track_location']['coordinates'])
        num_inter = int(round(old_div(dis, step)))
        if num_inter == 0:
            lst1_extended.append(lst1[i]['track_location']['coordinates'])
        else:
            lon_list = np.linspace(
                lst1[i]['track_location']['coordinates'][0],
                lst1[i + 1]['track_location']['coordinates'][0], num_inter,
                False)
            lat_list = np.linspace(
                lst1[i]['track_location']['coordinates'][1],
                lst1[i + 1]['track_location']['coordinates'][1], num_inter,
                False)
            for j in range(len(lon_list)):
                lst1_extended.append([lon_list[j], lat_list[j]])
    lst1_extended.append(end_pnt1['track_location']['coordinates'])
    lst2_extended = []
    for i in range(len(lst2) - 1):
        dis = ec.calDistance(lst2[i]['track_location']['coordinates'],
                             lst2[i + 1]['track_location']['coordinates'])
        num_inter = int(round(old_div(dis, step)))
        if num_inter == 0:
            lst2_extended.append(lst2[i]['track_location']['coordinates'])
        else:
            lon_list = np.linspace(
                lst2[i]['track_location']['coordinates'][0],
                lst2[i + 1]['track_location']['coordinates'][0], num_inter,
                False)
            lat_list = np.linspace(
                lst2[i]['track_location']['coordinates'][1],
                lst2[i + 1]['track_location']['coordinates'][1], num_inter,
                False)
            for j in range(len(lon_list)):
                lst2_extended.append([lon_list[j], lat_list[j]])
    lst2_extended.append(end_pnt2['track_location']['coordinates'])

    # print(len(lst1_extended))
    # print(len(lst2_extended))
    best_score = []
    score_2_in_1 = 0
    for point2 in lst2:
        if ec.Include_place_2(lst1_extended,
                              point2['track_location']['coordinates'], radius):
            score_2_in_1 += 1
    best_score.append(old_div(score_2_in_1, len(lst2)))
    score_1_in_2 = 0
    for point1 in lst1:
        if ec.Include_place_2(lst2_extended,
                              point1['track_location']['coordinates'], radius):
            score_1_in_2 += 1
    best_score.append(old_div(score_1_in_2, len(lst1)))
    print(best_score)
    if max(best_score) > min_score:
        return True
    else:
        return False
Exemplo n.º 37
0
    def filter(self, with_speeds_df):
        self.inlier_mask_ = [True] * with_speeds_df.shape[0]

        quality_segments = []
        curr_segment = []
        prev_pt = None

        for (i, pt) in enumerate(with_speeds_df.to_dict('records')):
            pt = ad.AttrDict(pt)
            if prev_pt is None:
                # Don't have enough data yet, so don't make any decisions
                prev_pt = pt
            else:
                currSpeed = pf.calSpeed(prev_pt, pt)
                print("while considering point %s, speed = %s" %
                      (i, currSpeed))
                # Should make this configurable
                if currSpeed > self.maxSpeed:
                    print(
                        "currSpeed > %d, starting new quality segment at index %s "
                        % (self.maxSpeed, i))
                    quality_segments.append(curr_segment)
                    curr_segment = []
                else:
                    print(
                        "currSpeed < %d, retaining index %s in existing quality segment "
                        % (self.maxSpeed, i))
                prev_pt = pt
                curr_segment.append(i)
        # Append the last segment once we are at the end
        quality_segments.append(curr_segment)

        print("Number of quality segments is %d" % len(quality_segments))

        last_segment = quality_segments[0]
        for curr_segment in quality_segments[1:]:
            print("Considering segments %s and %s" %
                  (last_segment, curr_segment))

            if len(last_segment) == 0:
                # If the last segment has no points, we can't compare last and
                # current, but should reset last, otherwise, we will be stuck
                # forever
                logging.info(
                    "len(last_segment) = %d, len(curr_segment) = %d, skipping"
                    % (len(last_segment), len(curr_segment)))
                last_segment = curr_segment
                continue

            if len(curr_segment) == 0:
                # If the current segment has no points, we can't compare last and
                # current, but can just continue since the for loop will reset current
                logging.info(
                    "len(last_segment) = %d, len(curr_segment) = %d, skipping"
                    % (len(last_segment), len(curr_segment)))
                continue
            get_coords = lambda i: [
                with_speeds_df.iloc[i]["mLongitude"], with_speeds_df.iloc[i][
                    "mLatitude"]
            ]
            get_ts = lambda i: with_speeds_df.iloc[i]["mTime"]
            # I don't know why they would use time instead of distance, but
            # this is what the existing POSDAP code does.
            print(
                "About to compare curr_segment duration %s with last segment duration %s"
                % (get_ts(curr_segment[-1]) - get_ts(curr_segment[0]),
                   get_ts(last_segment[-1]) - get_ts(last_segment[0])))
            if (get_ts(curr_segment[-1]) - get_ts(curr_segment[0]) <=
                    get_ts(last_segment[-1]) - get_ts(last_segment[0])):
                print("curr segment %s is shorter, cut it" % curr_segment)
                ref_idx = last_segment[-1]
                for curr_idx in curr_segment:
                    print(
                        "Comparing distance %s with speed %s * time %s = %s" %
                        (math.fabs(
                            ec.calDistance(get_coords(ref_idx),
                                           get_coords(curr_idx))),
                         old_div(self.maxSpeed, 100),
                         abs(get_ts(ref_idx) - get_ts(curr_idx)), self.maxSpeed
                         / 100 * abs(get_ts(ref_idx) - get_ts(curr_idx))))

                    if (math.fabs(
                            ec.calDistance(get_coords(ref_idx),
                                           get_coords(curr_idx))) >
                        (self.maxSpeed / 1000 *
                         abs(get_ts(ref_idx) - get_ts(curr_idx)))):
                        print(
                            "Distance is greater than max speed * time, deleting %s"
                            % curr_idx)
                        self.inlier_mask_[curr_idx] = False
            else:
                print("prev segment %s is shorter, cut it" % last_segment)
                ref_idx = curr_segment[-1]
                for curr_idx in reversed(last_segment):
                    print(
                        "Comparing distance %s with speed %s * time %s = %s" %
                        (math.fabs(
                            ec.calDistance(get_coords(ref_idx),
                                           get_coords(curr_idx))),
                         old_div(self.maxSpeed, 1000),
                         abs(get_ts(ref_idx) - get_ts(curr_idx)), self.maxSpeed
                         / 1000 * abs(get_ts(ref_idx) - get_ts(curr_idx))))
                    if (abs(
                            ec.calDistance(get_coords(ref_idx),
                                           get_coords(curr_idx))) >
                        (self.maxSpeed / 1000 *
                         abs(get_ts(ref_idx) - get_ts(curr_idx)))):
                        print(
                            "Distance is greater than max speed * time, deleting %s"
                            % curr_idx)
                        self.inlier_mask_[curr_idx] = False
            last_segment = curr_segment
        logging.info("Filtering complete, removed indices = %s" %
                     np.nonzero(self.inlier_mask_))
Exemplo n.º 38
0
def route_matching(lst1, lst2, step, radius, len_match, min_score):
    # input 2 lists of tracking points, each tracking points is geojson format
    # the two lists must have at least two tracking points
    if len(lst1) < 2 or len(lst2) < 2:
        return False
    start_pnt1 = lst1[0]
    end_pnt1 = lst1[-1]
    start_pnt2 = lst2[0]
    end_pnt2 = lst2[-1]
    # Case 1, lst2 is part of lst1:
    lst1_extended = []
    for i in range(len(lst1) - 1):
        dis = ec.calDistance(lst1[i]['track_location']['coordinates'],
                             lst1[i + 1]['track_location']['coordinates'])
        num_inter = int(round(old_div(dis, step)))
        if num_inter == 0:
            lst1_extended.append(lst1[i]['track_location']['coordinates'])
        else:
            lon_list = np.linspace(
                lst1[i]['track_location']['coordinates'][0],
                lst1[i + 1]['track_location']['coordinates'][0], num_inter,
                False)
            lat_list = np.linspace(
                lst1[i]['track_location']['coordinates'][1],
                lst1[i + 1]['track_location']['coordinates'][1], num_inter,
                False)
            for j in range(len(lon_list)):
                lst1_extended.append([lon_list[j], lat_list[j]])
    lst1_extended.append(end_pnt1['track_location']['coordinates'])
    lst2_extended = []
    for i in range(len(lst2) - 1):
        dis = ec.calDistance(lst2[i]['track_location']['coordinates'],
                             lst2[i + 1]['track_location']['coordinates'])
        num_inter = int(round(old_div(dis, step)))
        if num_inter == 0:
            lst2_extended.append(lst2[i]['track_location']['coordinates'])
        else:
            lon_list = np.linspace(
                lst2[i]['track_location']['coordinates'][0],
                lst2[i + 1]['track_location']['coordinates'][0], num_inter,
                False)
            lat_list = np.linspace(
                lst2[i]['track_location']['coordinates'][1],
                lst2[i + 1]['track_location']['coordinates'][1], num_inter,
                False)
            for j in range(len(lon_list)):
                lst2_extended.append([lon_list[j], lat_list[j]])
    lst2_extended.append(end_pnt2['track_location']['coordinates'])

    # print(len(lst1_extended))
    # print(len(lst2_extended))
    near_start2 = find_near(lst1_extended,
                            start_pnt2['track_location']['coordinates'],
                            radius)
    near_end2 = find_near(lst1_extended,
                          end_pnt2['track_location']['coordinates'], radius)

    near_start1 = find_near(lst2_extended,
                            start_pnt1['track_location']['coordinates'],
                            radius)
    near_end1 = find_near(lst2_extended,
                          end_pnt1['track_location']['coordinates'], radius)

    # print(near_start2)
    # print(near_end2)
    # print(near_start1)
    # print(near_end1)
    best_score = []

    if len(near_start2) > 0 and len(near_end2) > 0:
        print("start of case 1")
        for near_s in near_start2:
            for near_e in near_end2:
                if old_div(min(abs(near_e - near_s) + 1, len(lst2_extended)),
                           max(abs(near_e - near_s) + 1,
                               len(lst2_extended))) >= len_match:
                    print("possible near_s is %s" % near_s)
                    print("possible near_e is %s" % near_e)

                    if near_e > near_s:
                        print("start index is %d" % near_s)
                        print("end index is %d" % near_e)
                        route1 = lst1_extended[near_s:near_e + 1:1]

                        route2 = lst2_extended
                        print("route1 is %s" % route1)
                        print("route2 is %s" % route2)
                    else:
                        print("start index is %d" % near_s)
                        print("end index is %d" % near_e)
                        route1 = lst1_extended[near_e:near_s + 1:1][::-1]
                        route2 = lst2_extended
                        print("route1 is %s" % route1)
                        print("route2 is %s" % route2)
                    best_score.append(
                        cal_matching_score(route1, route2, radius))

    if len(near_start1) > 0 and len(near_end1) > 0:
        print("start of case 2")
        for near_s in near_start1:
            for near_e in near_end1:
                if old_div(min(abs(near_e - near_s) + 1, len(lst1_extended)),
                           max(abs(near_e - near_s) + 1,
                               len(lst1_extended))) >= len_match:
                    if near_e > near_s:
                        print("start index is %d" % near_s)
                        print("end index is %d" % near_e)
                        route1 = lst1_extended
                        route2 = lst2_extended[near_s:near_e + 1:1]
                        print("route1 is %s" % route1)
                        print("route2 is %s" % route2)
                    else:
                        route1 = lst1_extended
                        route2 = lst2_extended[near_e:near_s + 1:1][::-1]
                    best_score.append(
                        cal_matching_score(route1, route2, radius))
    print(best_score)
    if len(best_score) > 0 and max(best_score) > min_score:
        return True
    else:
        return False
Exemplo n.º 39
0
 def distance(self, other):
     ## Returns distance between 2 coordinates in meters
     return cm.calDistance(self, other, True)
Exemplo n.º 40
0
def calDistance(point1, point2):
    return ec.calDistance([point1.longitude, point1.latitude], [point2.longitude, point2.latitude])
Exemplo n.º 41
0
 def get_distance(self):
     return cm.calDistance(self.trip_start_location, self.trip_end_location,
                           True)