-
Notifications
You must be signed in to change notification settings - Fork 21
/
bj_traj.py
71 lines (55 loc) · 1.98 KB
/
bj_traj.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# link: Please contact the author!
import pickle
import csv
import os
import json
from util import int_to_isoformat, ensure_dir
def convert(x):
if x <= 10:
return "0" + str(x)
return str(x)
month = 6 # ONLY CHANGE HERE IF U WANT OTHER MONTHS' DATA
DATA_NAME = "bj_traj_2015" + convert(month)
geo_cnt = 0
output_dir = os.path.join("output", DATA_NAME)
ensure_dir(output_dir)
dyna_cnt = 0
dyna_file = open(os.path.join(output_dir, DATA_NAME + ".dyna"), "w", newline='')
dyna_writer = csv.writer(dyna_file)
dyna_writer.writerow(["dyna_id", "type", "time", "entity_id", "coordinates"])
usr_file = open(os.path.join(output_dir, DATA_NAME + ".usr"), "w", newline='')
usr_writer = csv.writer(usr_file)
usr_writer.writerow(["usr_id"])
entity_id = 0
def dumpconfig(data_name):
config = dict()
config['usr'] = dict()
config['usr']['properties'] = dict()
config['dyna'] = dict()
config['dyna']['including_types'] = ['trajectory']
config['dyna']['trajectory'] = {'entity_id': 'usr_id',
'coordinates': 'coordinate'}
json.dump(config, open(os.path.join(data_name, 'config.json'),
'w', encoding='utf-8'), ensure_ascii=False)
def get_dyna(f):
global entity_id, dyna_cnt
x = pickle.load(f)
for path in x:
usr_writer.writerow([entity_id])
for time, coords in path:
cur_time = int_to_isoformat(time)
coords = [coords[1], coords[0]]
dyna_col = [dyna_cnt, "trajectory", cur_time,
entity_id, coords]
dyna_writer.writerow(dyna_col)
dyna_cnt += 1
entity_id += 1
for day in range(1, 32):
time = "2015" + convert(month) + convert(day)
file_name = "gps_" + time
input_dirname = os.path.join("input", DATA_NAME)
input_filename = os.path.join(input_dirname, file_name)
if os.path.exists(input_filename):
file = open(input_filename, "rb")
get_dyna(file)
dumpconfig(output_dir)