-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathUtfgrid.py
More file actions
161 lines (125 loc) · 3.86 KB
/
Copy pathUtfgrid.py
File metadata and controls
161 lines (125 loc) · 3.86 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# This Python file uses the following encoding: utf-8
import json
import time
import numpy
class Utfgrid:
def __init__(self):
self.resultGrid = None
self.gridKeys = []
self.gridData = {}
self.keyIndex = {}
self.maxKey = -1
self.decodeCache = {}
def addLayer( self, layer ):
layer = json.loads(layer)
gridSize = len(layer['grid'])
if self.resultGrid == None:
self.resultGrid = numpy.empty(shape=(gridSize,gridSize),dtype=int)
self.resultGrid.fill(-1)
keys = layer['keys']
keyRemap = {}
remapedKeys = {}
remapMaxKey = self.maxKey
emptyKey = 0
i = 0
for k in keys:
if k == "":
emptyKey = i
elif k in self.keyIndex:
remapMaxKey += 1
keyRemap[str(k)] = str(remapMaxKey)
i += 1
emptyKey = self.encodeId(emptyKey)
addedKeys = {}
chunksCount = 32
chunkSize = gridSize/chunksCount
t = time.clock();
try:
emptyChunk = numpy.empty(gridSize/chunksCount,dtype=str)
emptyChunk.fill(emptyKey)
grid = numpy.array(layer['grid'])
except UnicodeEncodeError:
emptyChunk = numpy.empty(gridSize/chunksCount,dtype=unicode)
emptyChunk.fill(emptyKey)
grid = numpy.array(layer['grid'],dtype=unicode)
keyPairRemap = {}
for y in xrange(gridSize):
line = grid[y]
for chunk in xrange(0,chunksCount):
if line[chunk*chunkSize:(chunk+1)*chunkSize] == emptyChunk:
continue;
for x in xrange(chunk*chunkSize,(chunk+1)*chunkSize):
if line[x] == emptyKey:
continue
idNo = self.decodeId(line[x])
key = str(keys[idNo])
if str(keys[idNo]) in keyRemap:
key = keyRemap[str(keys[idNo])]
if self.resultGrid[x][y] != -1:
idNo2 = self.resultGrid[x][y]
key2 = self.gridKeys[idNo2]
if (key,key2) in keyPairRemap:
key = keyPairRemap[(key,key2)]
else:
remapMaxKey += 1
keyPairRemap[(key,key2)] = str(remapMaxKey)
key = str(remapMaxKey)
if not key in addedKeys:
self.keyIndex[key] = len(self.gridKeys)
self.gridKeys.append(key)
if self.maxKey < int(key):
self.maxKey = int(key)
addedKeys[key] = True;
if self.resultGrid[x][y] != -1:
self.gridData[key] = self.gridData[key2][:]
self.gridData[key].append(layer['data'][str(keys[idNo])])
else:
self.gridData[key] = [layer['data'][str(keys[idNo])]]
newId = self.keyIndex[key]
self.resultGrid[x][y] = newId
def writeResult( self, crop = None ):
gridSize = len(self.resultGrid)
finalKeys = []
finalData = {}
finalGrid = []
for y in xrange(crop[1],crop[3]):
finalGrid.append("")
finalIdCounter = 1
idToFinalId = {}
if not crop:
crop = [0,0,gridSize,gridSize]
finalKeys.append("");
idToFinalId[-1] = 0
for y in xrange(crop[1],crop[3]):
for x in xrange(crop[0],crop[2]):
id = self.resultGrid[x][y]
if not id in idToFinalId and id != -1:
idToFinalId[id] = finalIdCounter
finalIdCounter = finalIdCounter + 1
finalKeys.append(self.gridKeys[id])
finalData[self.gridKeys[id]] = self.gridData[self.gridKeys[id]]
finalId = idToFinalId[id]
finalGrid[y-crop[1]] = finalGrid[y-crop[1]] + self.encodeId(finalId)
result = {}
result['keys'] = finalKeys
result['data'] = finalData
result['grid'] = finalGrid
result = json.dumps(result,ensure_ascii = False).encode('utf-8')
return result;
def encodeId ( self, id ):
id += 32
if id >= 34:
id = id + 1
if id >= 92:
id = id + 1
return unichr(id)
def decodeId( self, id ):
if id in self.decodeCache:
return self.decodeCache[id]
ret = ord(id)
if ret >= 93:
ret -= 1
if ret >= 35:
ret -= 1
self.decodeCache[id] = ret - 32;
return ret - 32