-
Notifications
You must be signed in to change notification settings - Fork 0
/
compass.py
244 lines (198 loc) · 6.29 KB
/
compass.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import copy, uuid, os
import slippytiles, tileencoding
class GisObj(object):
def __init__(self):
self.positions = []
self.tags = {}
self.children = []
self.uuid = None
class TileBranch(object):
def __init__(self, x, y, zoom):
self.tileId = (x, y, zoom)
def Commit(self, mapObj, commitUuid):
print "Committing to tile {0}, {1}, {2}".format(*self.tileId)
#Filter objects to find what is in this tile
objsWithin = mapObj.GetObjsWithinTile(*self.tileId)
print "Num objects in tile", len(objsWithin)
print map(id, objsWithin)
#Identify shared nodes
countRefs = {}
for obj in objsWithin:
for pos in obj.positions:
pid = id(pos)
if pid not in countRefs:
countRefs[pid] = 0
countRefs[pid] += 1
print countRefs
#Save to file
pth = "tiles"
if not os.path.exists(pth):
os.mkdir(pth)
pth += "/{0}".format(self.tileId[0])
if not os.path.exists(pth):
os.mkdir(pth)
pth += "/{0}".format(self.tileId[1])
if not os.path.exists(pth):
os.mkdir(pth)
fina = pth + "/tile.dat"
fi = open(fina, "wb")
tileencoding.WriteTile(objsWithin, fi)
fi.close()
def Fetch(self, mapData):
print "Fetching tile {0}, {1}, {2}".format(*self.tileId)
pth = "tiles"
pth += "/{0}".format(self.tileId[0])
pth += "/{0}".format(self.tileId[1])
fina = pth + "/tile.dat"
fi = open(fina, "rb")
tileObjs = tileencoding.ReadTile(fi)
print len(tileObjs)
#Merge tile objects into map data
mapData._objs.extend(tileObjs)
class Map(object):
def __init__(self):
self._objs = []
self.nativeZoom = 12
def Copy(self):
return copy.deepcopy(self)
def AddGisObj(self, gisObj):
self._objs.append(gisObj)
def WithinTiles(self):
out = set()
for obj in self._objs:
for pos in obj.positions:
tilex, tiley = slippytiles.deg2num(pos[0], pos[1], self.nativeZoom)
out.add((tilex, tiley, self.nativeZoom))
return out
def GetObjsWithinTile(self, x, y, zoom):
nwCorner = slippytiles.num2deg(x, y, zoom)
seCorner = slippytiles.num2deg(x+1, y+1, zoom)
print nwCorner, seCorner
outFiltered = []
count = 0
for obj in self._objs:
within = False
for pos in obj.positions:
if pos[0] < seCorner[0] or pos[0] >= nwCorner[0]: continue
if pos[1] < nwCorner[1] or pos[1] >= seCorner[1]: continue
within = True
break
if within:
outFiltered.append(obj)
count += 1
return outFiltered
def AssignUuids(self):
#Count how many tiles object crosses
objToTileMap = {}
for obj in self._objs:
objInTiles = set()
for pos in obj.positions:
tilex, tiley = slippytiles.deg2num(pos[0], pos[1], self.nativeZoom)
objInTiles.add((tilex, tiley))
objToTileMap[obj] = objInTiles
#Assign uuid to any cross tile object that has none already
for obj in objToTileMap:
objInTiles = objToTileMap[obj]
count = len(objInTiles)
if count <= 1: continue
#Object is indeed in multiple tiles
if obj.uuid is not None:
continue #Already has a uuid
obj.uuid = uuid.uuid4()
#print "Assigning uuid", obj.uuid
class TileManager(object):
def __init__(self):
pass
def Commit(self, mapObj):
#Check which tiles are referenced
withinTiles = mapObj.WithinTiles()
print withinTiles
commitUuid = uuid.uuid4()
#Assign uuids to new objects that cross tiles
mapObj.AssignUuids()
#Commit to tile branches
for tilex, tiley, zoom in withinTiles:
tile = TileBranch(tilex, tiley, zoom)
tile.Commit(mapObj, commitUuid)
def GetTiles(self, tileIds):
mapData = Map()
for tilex, tiley in tileIds:
tile = TileBranch(tilex, tiley, 12)
tile.Fetch(mapData)
return mapData
def TestEdit1():
sharedPoint = (51.24762292031704, -0.590133572441356)
currentMap = Map()
longWay = [(51.25625276724307, -0.5569565354674175),
(51.25266853326553, -0.5610435318131136),
(51.2498167331032, -0.5661505613209978),
(51.248907880503666, -0.5702562124940027),
(51.249628696108054, -0.5753632420018869),
(51.249503337684125, -0.5798193755920997),
(51.248782520114766, -0.5829737173469692),
(51.24771694301155, -0.5870292996032302),
sharedPoint,
(51.24734085108016, -0.5939094804215219),
(51.2458991368506, -0.5961125127582564),
(51.24289019635384, -0.5968134775926718),
(51.241448342606276, -0.5990165099294061),
(51.23856449951962, -0.600518577431725),
(51.23467729452127, -0.603222298935899),
(51.233047078549895, -0.6067271231079763),
(51.23129139675417, -0.6114336012819089),
(51.23066435130686, -0.6145378741200347),
(51.22834420885645, -0.6185433874595516),
(51.22326456973149, -0.6319618571469335),
(51.21956423276796, -0.6372691623217938),
(51.21761986871632, -0.6417753648287504),
(51.21416998846001, -0.6473830835040743)]
longWayObj = GisObj()
longWayObj.positions = longWay
longWayObj.tags = {"type": "long way"}
currentMap.AddGisObj(longWayObj)
shortway = [sharedPoint,
(51.247168669637766, -0.5884420676103962),
(51.247201912030505, -0.5871144149645342),
(51.24804404463198, -0.5849016605547642),
(51.24798864164526, -0.5836625180852929),
(51.247712739373924, -0.5822479185608349),
(51.247712739373924, -0.5797945416403808),
(51.24821419054396, -0.5758390972176076),
(51.247963465642414, -0.5703315163757717)]
shortWayObj = GisObj()
shortWayObj.positions = shortway
shortWayObj.tags = {"type": "short way"}
currentMap.AddGisObj(shortWayObj)
sharedPoint2 = (51.24373784556225, -0.5957410012035282)
closeWay = [sharedPoint2,
(51.24163228801223, -0.5960242337679789),
(51.2414278761271, -0.5875932007620944),
(51.24061272024824, -0.5861819021533217),
(51.24092302587304, -0.5837744253554917),
(51.243582702505805, -0.5843054864138366),
(51.24555519668222, -0.5882353382455883),
(51.2453335723415, -0.5918819575128893),
sharedPoint2]
closeWayObj = GisObj()
closeWayObj.positions = closeWay
closeWayObj.tags = {"type": "closed way"}
currentMap.AddGisObj(closeWayObj)
point = (51.24109091450995, -0.5899589570782465)
pointObj = GisObj()
pointObj.positions = [point]
pointObj.tags = {"type": "single point"}
currentMap.AddGisObj(pointObj)
tileManager = TileManager()
tileManager.Commit(currentMap)
def ReadEdit1():
tileManager = TileManager()
mapData = tileManager.GetTiles([(2041, 1366)])
if 0:
for obj in mapData._objs:
print "tags", obj.tags
print "positions"
for pos in obj.positions:
print id(pos), pos
if __name__ == "__main__":
TestEdit1()
ReadEdit1()