text: # a tutorial example based on T-Drive dataset
  from common.road_network import load_rn_shp
  from common.trajectory import Trajectory, store_traj_file, parse_traj_file
  from common.trajectory import STPoint
  from noise_filtering import STFilter, HeuristicFilter
  from segmentation import TimeIntervalSegmentation, StayPointSegmentation
  from map_matching.hmm.hmm_map_matcher import TIHMMMapMatcher
  from common.mbr import MBR
  from datetime import datetime
  import os
  from tqdm import tqdm
  import argparse
  from statistics import statistics
  import logging
  
  min_lat = 200.0
  max_lat = 0.0
  min_lng = 200.0
  max_lng = 0.0
  
  
  def parse_tdrive(filename, tdrive_root_dir):
      global max_lat, max_lng, min_lng, min_lat
      logging.basicConfig(level=logging.DEBUG)
      oid = filename.replace('.csv', '')
      with open(os.path.join(tdrive_root_dir, filename), 'r') as f:
          pt_list = []
          for line in f.readlines():
              attrs = line
              attrs = attrs.replace('\'', '')
              attrs = attrs.replace(']', '')
              attrs = attrs.replace('[', '')
              # print(attrs)
              attrs = attrs.strip('\n').split(',')
              if attrs[3] == "Latitude":
                  continue
              lat = float(attrs[3])
              lng = float(attrs[2])
              if lat > max_lat:
                  max_lat = lat
              if lat < min_lat:
                  min_lat = lat
              if lng > max_lng:
                  max_lng = lng
              if lng < min_lng:
                  min_lng = lng
              tmp = attrs[1].strip(' ')
              tmp = tmp.split('.')
              time = datetime.strptime(tmp[0], '%Y-%m-%d %H:%M:%S')
              pt_list.append(STPoint(lat, lng, time))
      # logging.debug("current_lng:%f current_lat:%f", lng, lat)
      logging.debug("min_lng:%f  max_lng:%f min_lat:%f max_lat:%f", min_lng, max_lng, min_lat, max_lat)
      if len(pt_list) > 1:
          return Trajectory(oid, 0, pt_list)
      else:
          return None
  
  
  def do_clean(raw_traj, filters, segmentations):
      logging.basicConfig(level=logging.DEBUG)
      clean_traj = raw_traj
      for filter in filters:
          clean_traj = filter.filter(clean_traj)
          if clean_traj is None:
              return []
      clean_traj_list = [clean_traj]
      for seg in segmentations:
          tmp_clean_traj_list = []
          for clean_traj in clean_traj_list:
              segment_trajs = seg.segment(clean_traj)
              tmp_clean_traj_list.extend(segment_trajs)
          clean_traj_list = tmp_clean_traj_list
      logging.debug("group after segmentations:%d\n", len(clean_traj_list))
      return clean_traj_list
  
  
  def clean_tdrive(tdrive_root_dir, clean_traj_dir):
      start_time = datetime(2020, 5, 18)
      end_time = datetime(2020, 5, 19)
      mbr = MBR(36.0, 117.0, 37.0, 118.0)
      st_filter = STFilter(mbr, start_time, end_time)
      heuristic_filter = HeuristicFilter(max_speed=100)
      filters = [st_filter, heuristic_filter]
      ti_seg = TimeIntervalSegmentation(max_time_interval_min=6)
      sp_seg = StayPointSegmentation(dist_thresh_meter=100, max_stay_time_min=15)
      segs = [ti_seg, sp_seg]
      for filename in tqdm(os.listdir(tdrive_root_dir)):
          raw_traj = parse_tdrive(filename, tdrive_root_dir)
          print(raw_traj)
          if raw_traj is None:
              return
          clean_trajs = do_clean(raw_traj, filters, segs)
          if len(clean_trajs) > 0:
              print((len(clean_trajs)))
              store_traj_file(clean_trajs, os.path.join(clean_traj_dir, filename))
  
  
  def mm_tdrive(clean_traj_dir, mm_traj_dir, rn_path):
      rn = load_rn_shp(rn_path, is_directed=True)
      map_matcher = TIHMMMapMatcher(rn)
      for filename in tqdm(os.listdir(clean_traj_dir)):
          clean_trajs = parse_traj_file(os.path.join(clean_traj_dir, filename))
          mm_trajs = [map_matcher.match(clean_traj) for clean_traj in clean_trajs]
          store_traj_file(mm_trajs, os.path.join(mm_traj_dir, filename), traj_type='mm')
  
  
  if __name__ == '__main__':
      # parser = argparse.ArgumentParser()
      # parser.add_argument('--tdrive_root_dir', help='the directory of the TDrive dataset')
      # parser.add_argument('--clean_traj_dir', help='the directory of the cleaned trajectories')
      # parser.add_argument('--rn_path', help='the road network data path generated by osm2rn')
      # parser.add_argument('--mm_traj_dir', help='the directory of the map-matched trajectories')
      # parser.add_argument('--phase', help='the preprocessing phase [clean,mm,stat]')
      #
      # opt = parser.parse_args()
      # print(opt)
      #
      # if opt.phase == 'clean':
      #     clean_tdrive(opt.tdrive_root_dir, opt.clean_traj_dir)
      # elif opt.phase == 'mm':
      #     mm_tdrive(opt.clean_traj_dir, opt.mm_traj_dir, opt.rn_path)
      # elif opt.phase == 'stat':
      #     statistics(opt.clean_traj_dir)
      # else:
      #     raise Exception('unknown phase')
      #clean_tdrive("/home/zhangtaiyan/tmp/pycharm_project_570/data1/", "/home/zhangtaiyan/tmp/pycharm_project_570/clean_data/")
      mm_tdrive("/home/zhangtaiyan/tmp/pycharm_project_570/clean_data/", "/home/zhangtaiyan/tmp/pycharm_project_570/mm_data", "/home/zhangtaiyan/tmp/osm2rn/interest_region")