forked from hcc226/UrbanMotionAnalysis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenGridSubProp.py
More file actions
123 lines (97 loc) · 2.64 KB
/
genGridSubProp.py
File metadata and controls
123 lines (97 loc) · 2.64 KB
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
117
118
119
120
121
122
123
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Output Format:
# [grid-at]
# JSON
#
# 生成 grid JSON 数据,适合存入 mongoDB
import os
import logging
import getopt
import sys
import time
import json
from multiprocessing import Process
from util.dbopts import connectMongo
from util.GridPropSup import GridPropSup
from util.preprocess import getCityLocs
from util.preprocess import parseFormatGID
def chunks(l, n):
"""Yield successive n-sized chunks from l."""
for i in xrange(0, len(l), n):
yield l[i:i + n]
def mergeGrids(basepath, jobsNum):
# 加载所有网格
locs = getCityLocs('beijing')
SPLIT = 0.0005
LATNUM = int((locs['north'] - locs['south']) / SPLIT + 1)
LNGNUM = int((locs['east'] - locs['west']) / SPLIT + 1)
totalLen = LATNUM * LNGNUM
result = [parseFormatGID(locs, i) for i in xrange(0, totalLen)]
# 根据每个文件对网格进行更新
for i in xrange(0, jobsNum):
with open(os.path.join(basepath, "BJ-MID-SQL", "grid-j%d" % i), 'rb') as f:
for each in f:
each = each.strip().split(',')
nid = int(each[0])
pid = each[1]
if result[nid]['pid'] == -1:
result[nid]['pid'] = pid
f.close()
# 结果写入文件
result = [i for i in result if i['pid'] != -1]
with open(os.path.join(basepath, 'BJ-MID-SQL', 'grid-at'), 'ab') as res:
res.write(json.dumps(result).encode('utf-8'))
res.close()
def processTask(INDEX, city, basepath, pidList):
task = GridPropSup({
'INDEX': INDEX,
'city': city,
'basepath': basepath,
'pidList': pidList
})
task.run()
def usage():
pass
def main(argv):
try:
opts, args = getopt.getopt(argv, "hc:d:", ["help", "city=", 'directory='])
except getopt.GetoptError as err:
print str(err)
usage()
sys.exit(2)
city, directory = 'beijing', '/home/tao.jiang/datasets/JingJinJi/records'
for opt, arg in opts:
if opt == '-h':
usage()
sys.exit()
elif opt in ("-c", "--city"):
city = arg
elif opt in ("-d", "--directory"):
directory = arg
STARTTIME = time.time()
print "Start approach at %s" % STARTTIME
conn, db = connectMongo('stvis')
plist = list(db['pois'].find({}, {
'pid': 1,
'coordinates': 1
}))
pois = list(chunks(plist, 347))
conn.close()
# @多进程运行程序 START
jobs = []
for x in xrange(0, 20):
tProcess = Process(target=processTask, args=(x, city, directory, pois[x]))
jobs.append(tProcess)
jobs[x].start()
for job in jobs:
job.join()
# 处理剩余数据进文件
# 合并操作
mergeGrids(directory, 20)
# @多进程运行程序 END
print "END TIME: %s" % time.time()
if __name__ == '__main__':
logging.basicConfig(filename='logger-gengridsubprop.log', level=logging.DEBUG)
main(sys.argv[1:])