def CombinePlaces(places, time_resolution):
  """Try to combine consecutive viewpoints into a meta-viewpoints by
  locality, state, and then country. Combination can only occur if the
  time delta between the viewpoints is <= time_resolution. Returns two
  lists: one for locality and one for country.
  """
  sorted_places = sorted([{'locality': GetPlace(p['placemark']),
                           'timestamp': p['timestamp'],
                           'opacity': 1.0,
                           'num_combined': 1,
                           'total_count': p['total_count']} for p in places], key=itemgetter('timestamp'))
  place_list = []
  def _GetAvgTimestamp(p):
    return p['timestamp'] / p['num_combined']

  def _MaybeCombinePlace(p):
    if place_list:
      last_place = place_list[-1]
      if p['locality'] == last_place['locality'] and \
            abs(p['timestamp'] - _GetAvgTimestamp(last_place)) < time_resolution:
        for attr in ['timestamp', 'total_count', 'num_combined']:
          last_place[attr] += p[attr]
        return
    place_list.append(p)

  for p in sorted_places:
    _MaybeCombinePlace(p)

  for p in place_list:
    p['timestamp'] = _GetAvgTimestamp(p)
  return place_list
dataset/ETHPy150Open viewfinderco/viewfinder/clients/experimental/viewfinder.py/CombinePlaces/original