-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMapConfig.py
305 lines (257 loc) · 9.66 KB
/
MapConfig.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
###############################################################################
# MapConfig.py
#
# The map document model.
#
# -----------------------------------------------------------------------------
# gpsmap - A GPSD simulator based on map positions
# (C) 2014 Gerardo García Peña <[email protected]>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
###############################################################################
import pygtk
pygtk.require('2.0')
import cairo
import gtk
import json
import logging
import math
import GPS
import Vect
class MapConfig(object):
# config
filename = None
imgpath = None
A = None
H = None
V = None
route = None
points = None
walk_speed = None
# calculated properties
to = None
raca = None
dflo = None
dfla = None
bg_surface = None
bg_w = None
bg_h = None
curr_pos = None
curr_xy = None
def __init__(self):
self.reset()
def reset(self):
self.route = [ ]
self.points = { }
self.filename = None
self.imgpath = None
self.A = None
self.H = None
self.V = None
self.to = None
self.raca = None
self.dflo = None
self.dfla = None
self.bg_surface = None
self.bg_w = None
self.bg_h = None
self.walk_speed = 4000 # humans with laptop walk at 4km/h
def json_read(self, config):
self.reset()
j = json.loads(config)
# load img config
if j["imgpath"] is not None:
self.map_load(j["imgpath"])
else:
self.map_unset()
# walk speed
if "walk_speed" in j:
self.walk_speed = j["walk_speed"]
# load reference points
if j["A"] is not None:
la, lo = GPS.sex2dec(j["A"]["coord"])
self.set_ref("A", j["A"]["xy"][0], j["A"]["xy"][1], la, lo)
else:
self.unset_ref("A")
if j["H"] is not None:
la, lo = GPS.sex2dec(j["H"]["coord"])
self.set_ref("H", j["H"]["xy"][0], j["H"]["xy"][1], la, lo)
else:
self.unset_ref("H")
if j["V"] is not None:
la, lo = GPS.sex2dec(j["V"]["coord"])
self.set_ref("V", j["V"]["xy"][0], j["V"]["xy"][1], la, lo)
else:
self.unset_ref("V")
# load last route
self.route = j["route"]
def json_write(self):
return json.dumps({
"imgpath": self.imgpath,
"A": { "coord": GPS.dec2sex(self.A[1][1], self.A[1][0]), "xy": self.A[0] }
if self.A is not None else None,
"H": { "coord": GPS.dec2sex(self.H[1][1], self.H[1][0]), "xy": self.H[0] }
if self.H is not None else None,
"V": { "coord": GPS.dec2sex(self.V[1][1], self.V[1][0]), "xy": self.V[0] }
if self.V is not None else None,
"route": self.route,
"walk_speed": self.walk_speed,
}, indent = 4)
def map_load(self, mapfile):
logging.info("Loading map image '%s'." % mapfile)
self.imgpath = mapfile
# load image using gtk.gdk
pixbuf = gtk.gdk.pixbuf_new_from_file(mapfile)
# convert image to a standard cairo surface
# (to avoid dependency on pixbuf)
self.bg_w = pixbuf.get_width()
self.bg_h = pixbuf.get_height()
self.bg_surface = cairo.ImageSurface(
cairo.FORMAT_ARGB32,
self.bg_w,
self.bg_h)
cr = cairo.Context(self.bg_surface)
gdkcr = gtk.gdk.CairoContext(cr)
gdkcr.set_source_pixbuf(pixbuf, 0, 0)
gdkcr.paint()
def map_unset(self):
logging.info("Unset map image.")
self.imgpath = None
if self.pixbuf is not None:
self.pixbuf.destroy()
self.pixbuf = None
def point_add(self, name, x, y):
self.points[name] = [ x, y ]
def unset_ref(self, pn):
if pn == "A": self.A = None
elif pn == "H": self.H = None
elif pn == "V": self.V = None
else:
logging.error("Bad reference point '%s'.", pn)
return
self.to = None
self.raca = None
self.dflo = None
self.dfla = None
def set_ref(self, pn, x, y, la, lo):
if pn == "A": self.A = [ Vect.vect(x, y), Vect.vect(lo, la) ]
elif pn == "H": self.H = [ Vect.vect(x, y), Vect.vect(lo, la) ]
elif pn == "V": self.V = [ Vect.vect(x, y), Vect.vect(lo, la) ]
else:
logging.error("Bad reference point '%s'.", pn)
return
# check we have everything that we need before starting calculations
if self.A is None or self.H is None or self.V is None:
return
logging.info("ref(A) = %f, %f" % (self.A[1][0], self.A[1][1]))
logging.info("ref(H) = %f, %f" % (self.H[1][0], self.H[1][1]))
logging.info("ref(V) = %f, %f" % (self.V[1][0], self.V[1][1]))
# do calculus (we have all needed data)
AH = Vect.vsub(self.H[1], self.A[1])
AV = Vect.vsub(self.V[1], self.A[1])
logging.info("vect(AH) = %f, %f" % (AH[0], AH[1]))
logging.info("vect(AV) = %f, %f" % (AV[0], AV[1]))
# 1) Calculate angle between an horizontal vector and 'AH' vector
self.to = Vect.vangle2(AH, Vect.vect(1, 0))
logging.info("angle(AH, (1,0)) = %f rad (%fº)" % (self.to, math.degrees(self.to)))
# 2) Calculate 'raca' (angle between AH ^ AV)
self.raca = Vect.vangle(AH, AV)
logging.info("angle(AH, AV) = %f" % math.degrees(self.raca))
# 3) calculate 'dflo' and 'dfla' factors (pixels to dec degrees)
logging.info("|ha|º = %f" % Vect.vmod(AH))
logging.info("|ha|px = %f" % Vect.vmod(Vect.vsub(self.H[0], self.A[0])))
logging.info("|va|º = %f" % Vect.vmod(AV))
logging.info("|va|px = %f" % Vect.vmod(Vect.vsub(self.V[0], self.A[0])))
self.dflo = Vect.vmod(AH) \
/ Vect.vmod(Vect.vsub(self.H[0], self.A[0]))
self.dfla = Vect.vmod(AV) \
/ Vect.vmod(Vect.vsub(self.V[0], self.A[0]))
# 4) following printf should be always 90º
cri = Vect.vangle(Vect.vsub(self.V[0], self.A[0]), Vect.vsub(self.H[0], self.A[0]))
logging.info("angle(ex, ey) = %f" % math.degrees(cri))
self.dflo = self.dflo * math.sin(self.raca)
self.dfla = self.dfla * math.sin(self.raca)
logging.info("dflo = %f" % self.dflo)
logging.info("dfla = %f" % self.dfla)
def pixel2coords(self, x, y):
if x < 0 or x > self.pixbuf.get_width() \
or y < 0 or y > self.pixbuf.get_height():
return None
if self.A is None or self.H is None or self.V is None:
return None
sin = math.sin(self.to)
cos = math.cos(self.to)
x1 = (x - self.A[0][0]) * self.dflo
y1 = (y - self.A[0][1]) * self.dfla
x, y = x1, y1
x1 = x / math.sin(self.raca) + y * math.cos(self.raca)
y1 = y
x, y = x1, y1
x1 = (y * sin - x * cos) / (sin*sin + cos*cos)
y1 = (y * cos + x * sin) / (sin*sin + cos*cos)
x, y = x1, y1
v = Vect.vsum(Vect.vect(x1, y1), self.A[1])
return [v[1], v[0]]
def route_point_xy(self, n):
return (self.route[n][0], self.route[n][1])
def route_point_coords(self, n):
return self.pixel2coords(self.route[n][0], self.route[n][1])
def set_curr_pos(self, x, y):
self.curr_xy = [x, y]
self.curr_pos = self.pixel2coords(x, y)
return self.curr_pos
def unset_curr_pos(self):
self.curr_xy = None
self.curr_pos = None
def route_add(self, x, y):
self.route.append([x, y])
def load(self, configfile):
self.filename = configfile
f = open(configfile, "r")
self.json_read(f.read())
f.close()
def save(self, configfile):
self.filename = configfile
f = open(configfile, "w")
f.write(self.json_write())
f.close()
def route_unset(self):
self.route = []
def route_save_kml(self, routefile):
if routefile is None or routefile == "":
self.route_file = \
"%s.kml" % (self.configfile if self.configfile is not None else "route")
f = open(routefile, "w")
f.write(
"""<?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Placemark><name>route</name>
<description>gpsmap route</description>
<Style><LineStyle><color>7fff0000</color><width>3</width></LineStyle></Style>
<LineString>
<coordinates>
""")
for p in self.route:
c = self.pixel2coords(p[0], p[1])
f.write("%f,%f\n" % (c[1], c[0]))
f.write(
"""</coordinates>
</LineString>
</Placemark></Document>
</kml>
""")