This repository has been archived by the owner on Oct 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserTrajectory.py
118 lines (94 loc) · 3.45 KB
/
UserTrajectory.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# -*- coding:GBK -*-
import os
import points
import cPickle
import sys
import test
__author__ = 'Administrator'
class UserTrajectory:
def __init__(self, uid, trajectory):
self.uid = uid
self.trajectory = trajectory
def getTrajectory_old(uid, userDir):
Trajectory = []
# global Trajectory
userDir += "\\Trajectory\\"
fileDirs = os.listdir(userDir)
if fileDirs != [] and len(fileDirs) > 0:
for dir in fileDirs:
file = open(userDir + dir)
try:
allTheText = file.read().split('\n')[6:-1]
# Trajectory = [points.Point()] * len(allTheText)
finally:
file.close()
for iterText in range(0, len(allTheText)):
Trajectory.append(points.getPointViaString(allTheText[iterText]))
return UserTrajectory(uid, Trajectory)
def getAllTheText(fileDir):
file = open(fileDir)
try:
allTheText = file.read().split('\n')[6:-1]
# Trajectory = [points.Point()] * len(allTheText)
finally:
file.close()
return allTheText
def parsePltFile(fileDir):
# print test.iii
# test.iii += 1
file = open(fileDir)
pointSet = []
try:
allTheText = file.read().split('\n')[6:-1]
# Trajectory = [points.Point()] * len(allTheText)
finally:
file.close()
for iterText in range(0, len(allTheText)):
pointSet.append(points.getPointViaString(allTheText[iterText]))
return pointSet
"""
Return a list of trajectory for a certain user.
"""
def getTrajectory(uid, userDir, DisThreh, TimeThreh):
Trajectory = []
pointSet = []
# global Trajectory
userDir += "\\Trajectory\\"
fileDirs = os.listdir(userDir)
lastFileLastLine = None
dir = 0
if fileDirs != [] and len(fileDirs) > 0:
while dir < len(fileDirs):
pointSet = parsePltFile(userDir + fileDirs[dir])
addFile = 1
while True: #and dir + addFile < len(fileDirs):# 加上时间条件
if dir + addFile >= len(fileDirs):
dir = dir + addFile
break
tmpFile = open(userDir + fileDirs[dir + addFile])
tmpPoint = points.getPointViaString(tmpFile.read().split('\n')[6])
tmpFile.close()
if points.gpsDistance(pointSet[-1], tmpPoint) > DisThreh and tmpPoint.time - pointSet[
-1].time > TimeThreh:
dir = dir + addFile
break
pointSet += parsePltFile(userDir + fileDirs[dir + addFile])
# pointSet.append(parsePltFile(userDir + fileDirs[dir + addFile]))
addFile += 1
Trajectory.append(UserTrajectory(uid, pointSet))
# if addFile == 1:
# dir += 1
return Trajectory
def getLocationHistory(DataDir, DisThreh, TimeThreh):
fileDirs = os.listdir(DataDir)
LocationHistory = []
if fileDirs != [] and len(fileDirs) > 0:
for uid in range(0, len(fileDirs)):
print DataDir + fileDirs[uid]
LocationHistory.append(UserTrajectory(uid, points.getStayPoints(
getTrajectory(uid, DataDir + fileDirs[uid], DisThreh, TimeThreh), DisThreh, TimeThreh)))
return LocationHistory
# sys.setrecursionlimit(10000000) #设置递归深度为10,000,000
# dir = 'D:\\Geolife Trajectories 1.3\\Data\\'
# a = getLocationHistory(dir, 200, 20 * 60)
# cPickle.dump(a, open("LocHistory.pkl", "wb"))