File: utils.cc

package info (click to toggle)
qdmr 0.13.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,420 kB
  • sloc: cpp: 95,929; xml: 10,749; python: 1,108; makefile: 78; sh: 9
file content (598 lines) | stat: -rw-r--r-- 17,528 bytes parent folder | download
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
#include "utils.hh"
#include <QRegularExpression>
#include <QVector>
#include <QHash>
#include <cmath>
#include <QRegularExpression>
#include <yaml-cpp/yaml.h>

// Maps APRS icon number to code-char
static QVector<char> aprsIconCodeTable{
  '!','"','#','$','%','&','\'','(',')','*','+',',','-','.','/','0',
  '1','2','3','4','5','6', '7','8','9',':',';','<','=','>','?','@',
  'A','B','C','D','E','F', 'G','H','I','J','K','L','M','N','O','P',
  'Q','R','S','T','U','V', 'W','X','Y','Z','[','/',']','^','_','`',
  'a','b','c','d','e','f', 'g','h','i','j','k','l','m','n','o','p',
  'q','r','s','t','u','v','w','x','y','z','{','|','}','~'};

static QHash<unsigned, QString> aprsIconNameTable{
  {(unsigned)APRSSystem::Icon::None, ""},
  {(unsigned)APRSSystem::Icon::PoliceStation, "Police station"},
  {(unsigned)APRSSystem::Icon::Digipeater, "Digipeater"},
  {(unsigned)APRSSystem::Icon::Phone, "Phone"},
  {(unsigned)APRSSystem::Icon::DXCluster, "DX cluster"},
  {(unsigned)APRSSystem::Icon::HFGateway, "HF gateway"},
  {(unsigned)APRSSystem::Icon::SmallPlane, "Small plane"},
  {(unsigned)APRSSystem::Icon::MobileSatelliteStation, "Mobile Satellite station"},
  {(unsigned)APRSSystem::Icon::WheelChair, "Wheel chair"},
  {(unsigned)APRSSystem::Icon::Snowmobile, "Snowmobile"},
  {(unsigned)APRSSystem::Icon::RedCross, "Red cross"},
  {(unsigned)APRSSystem::Icon::BoyScout, "Boy scout"},
  {(unsigned)APRSSystem::Icon::Home,  "Home"},
  {(unsigned)APRSSystem::Icon::X, "X"},
  {(unsigned)APRSSystem::Icon::RedDot, "Red dot"},
  {(unsigned)APRSSystem::Icon::Circle0, "Circle 0"},
  {(unsigned)APRSSystem::Icon::Circle1, "Circle 1"},
  {(unsigned)APRSSystem::Icon::Circle2, "Circle 2"},
  {(unsigned)APRSSystem::Icon::Circle3, "Circle 3"},
  {(unsigned)APRSSystem::Icon::Circle4, "Circle 4"},
  {(unsigned)APRSSystem::Icon::Circle5, "Circle 5"},
  {(unsigned)APRSSystem::Icon::Circle6, "Circle 6"},
  {(unsigned)APRSSystem::Icon::Circle7, "Circle 7"},
  {(unsigned)APRSSystem::Icon::Circle8, "Circle 8"},
  {(unsigned)APRSSystem::Icon::Circle9, "Circle 9"},
  {(unsigned)APRSSystem::Icon::Fire, "Fire"},
  {(unsigned)APRSSystem::Icon::Campground, "Campground"},
  {(unsigned)APRSSystem::Icon::Motorcycle, "Motorcycle"},
  {(unsigned)APRSSystem::Icon::RailEngine, "Rail engine"},
  {(unsigned)APRSSystem::Icon::Car, "Car"},
  {(unsigned)APRSSystem::Icon::FileServer, "File server"},
  {(unsigned)APRSSystem::Icon::HCFuture, "HC future"},
  {(unsigned)APRSSystem::Icon::AidStation, "Aid station"},
  {(unsigned)APRSSystem::Icon::BBS, "BBS"},
  {(unsigned)APRSSystem::Icon::Canoe, "Canoe"},
  {(unsigned)APRSSystem::Icon::Eyeball, "Eyeball"},
  {(unsigned)APRSSystem::Icon::Tractor, "Tractor"},
  {(unsigned)APRSSystem::Icon::GridSquare, "Grid square"},
  {(unsigned)APRSSystem::Icon::Hotel, "Hotel"},
  {(unsigned)APRSSystem::Icon::TCPIP, "TCP/IP"},
  {(unsigned)APRSSystem::Icon::School, "School"},
  {(unsigned)APRSSystem::Icon::Logon, "Logon"},
  {(unsigned)APRSSystem::Icon::MacOS, "MacOS"},
  {(unsigned)APRSSystem::Icon::NTSStation, "NTS station"},
  {(unsigned)APRSSystem::Icon::Balloon, "Balloon"},
  {(unsigned)APRSSystem::Icon::PoliceCar, "Police car"},
  {(unsigned)APRSSystem::Icon::TBD, "TBD"},
  {(unsigned)APRSSystem::Icon::RV, "RV"},
  {(unsigned)APRSSystem::Icon::Shuttle, "Shuttle"},
  {(unsigned)APRSSystem::Icon::SSTV, "SSTV"},
  {(unsigned)APRSSystem::Icon::Bus, "Bus"},
  {(unsigned)APRSSystem::Icon::ATV, "ATV"},
  {(unsigned)APRSSystem::Icon::WXService, "WX service"},
  {(unsigned)APRSSystem::Icon::Helo, "Helo"},
  {(unsigned)APRSSystem::Icon::Yacht, "Yacht"},
  {(unsigned)APRSSystem::Icon::Windows, "Windows"},
  {(unsigned)APRSSystem::Icon::Jogger, "Jogger"},
  {(unsigned)APRSSystem::Icon::Triangle, "Triangle"},
  {(unsigned)APRSSystem::Icon::PBBS, "PBBS"},
  {(unsigned)APRSSystem::Icon::LargePlane, "Large plane"},
  {(unsigned)APRSSystem::Icon::WXStation, "WX station"},
  {(unsigned)APRSSystem::Icon::DishAntenna, "Dish antenna"},
  {(unsigned)APRSSystem::Icon::Ambulance, "Ambulance"},
  {(unsigned)APRSSystem::Icon::Bike, "Bike"},
  {(unsigned)APRSSystem::Icon::ICP, "ICP"},
  {(unsigned)APRSSystem::Icon::FireStation, "Fire station"},
  {(unsigned)APRSSystem::Icon::Horse, "Horse"},
  {(unsigned)APRSSystem::Icon::FireTruck, "Fire truck"},
  {(unsigned)APRSSystem::Icon::Glider, "Glider"},
  {(unsigned)APRSSystem::Icon::Hospital, "Hospital"},
  {(unsigned)APRSSystem::Icon::IOTA, "IOTA"},
  {(unsigned)APRSSystem::Icon::Jeep, "Jeep"},
  {(unsigned)APRSSystem::Icon::SmallTruck, "Small truck"},
  {(unsigned)APRSSystem::Icon::Laptop, "Laptop"},
  {(unsigned)APRSSystem::Icon::MicE, "Mic-E"},
  {(unsigned)APRSSystem::Icon::Node, "Node"},
  {(unsigned)APRSSystem::Icon::EOC, "EOC"},
  {(unsigned)APRSSystem::Icon::Rover, "Rover"},
  {(unsigned)APRSSystem::Icon::Grid, "Grid"},
  {(unsigned)APRSSystem::Icon::Antenna, "Antenna"},
  {(unsigned)APRSSystem::Icon::PowerBoat, "Power boat"},
  {(unsigned)APRSSystem::Icon::TruckStop, "Truck stop"},
  {(unsigned)APRSSystem::Icon::TruckLarge, "Truck large"},
  {(unsigned)APRSSystem::Icon::Van, "Van"},
  {(unsigned)APRSSystem::Icon::Water, "Water"},
  {(unsigned)APRSSystem::Icon::XAPRS, "XAPRS"},
  {(unsigned)APRSSystem::Icon::Yagi, "Yagi"},
  {(unsigned)APRSSystem::Icon::Shelter, "Shelter"}};

QString
decode_unicode(const uint16_t *data, size_t size, uint16_t fill) {
  QString res; res.reserve(size);
  for (size_t i=0; (i<size) && (fill!=data[i]); i++) {
    res.append(QChar(data[i]));
  }
  return res;
}

void
encode_unicode(uint16_t *data, const QString &text, size_t size, uint16_t fill) {
  for (size_t i=0; (i<size); i++) {
    if (i<size_t(text.size()))
      data[i] = text.at(i).unicode();
    else
      data[i] = fill;
  }
}

QString
decode_ascii(const uint8_t *data, size_t size, uint16_t fill) {
  QString res; res.reserve(size);
  for (size_t i=0; (i<size) && (0!=data[i]) && (fill!=data[i]); i++)
    res.append(QChar(data[i]));
  return res;
}

void
encode_ascii(uint8_t *data, const QString &text, size_t size, uint16_t fill) {
  for (size_t i=0; (i<size); i++) {
    if (i<size_t(text.size()))
      data[i] = text.at(i).toLatin1();
    else
      data[i] = fill;
  }
}

QString
decode_utf8(const uint8_t *data, size_t size, uint16_t fill) {
  Q_UNUSED(fill)
  return QString::fromUtf8((const char *)data, size);
}

void
encode_utf8(uint8_t *data, const QString &text, size_t size, uint16_t fill) {
  QByteArray buffer = text.toUtf8();
  memset(data, fill, size);
  memcpy(data, buffer.data(), std::min(size_t(buffer.size()), size));
}

double
decode_frequency(uint32_t bcd) {
  double freq =
      1e2  * ((bcd >> 28) & 0xf) +
      1e1  * ((bcd >> 24) & 0xf) +
      1.0  * ((bcd >> 20) & 0xf) +
      1e-1 * ((bcd >> 16) & 0xf) +
      1e-2 * ((bcd >> 12) & 0xf) +
      1e-3 * ((bcd >>  8) & 0xf) +
      1e-4 * ((bcd >>  4) & 0xf) +
      1e-5 * ((bcd >>  0) & 0xf);
  return freq;
}

uint32_t
encode_frequency(double freq) {
  uint32_t hz = std::round(freq * 1e6);
  uint32_t a  = (hz / 100000000) % 10;
  uint32_t b  = (hz /  10000000) % 10;
  uint32_t c  = (hz /   1000000) % 10;
  uint32_t d  = (hz /    100000) % 10;
  uint32_t e  = (hz /     10000) % 10;
  uint32_t f  = (hz /      1000) % 10;
  uint32_t g  = (hz /       100) % 10;
  uint32_t h  = (hz /        10) % 10;

  return (a << 28) + (b << 24) + (c << 20) + (d << 16) + (e << 12) + (f << 8) + (g << 4) + h;
}


uint32_t decode_dmr_id_bin(const uint8_t *id) {
  return ( (id[0]) | (id[1] << 8) | (id[2] << 16) );
}

void encode_dmr_id_bin(uint8_t *id, uint32_t no) {
  id[0] = no;
  id[1] = no >> 8;
  id[2] = no >> 16;
}


uint32_t decode_dmr_id_bcd(const uint8_t *id) {
  return ((id[0] >> 4) * 10000000
      + (id[0] & 15) * 1000000
      + (id[1] >> 4) * 100000
      + (id[1] & 15) * 10000
      + (id[2] >> 4) * 1000
      + (id[2] & 15) * 100
      + (id[3] >> 4) * 10
      + (id[3] & 15));
}

uint32_t decode_dmr_id_bcd_le(const uint8_t *id) {
  return ((id[3] >> 4) * 10000000
      + (id[3] & 15) * 1000000
      + (id[2] >> 4) * 100000
      + (id[2] & 15) * 10000
      + (id[1] >> 4) * 1000
      + (id[1] & 15) * 100
      + (id[0] >> 4) * 10
      + (id[0] & 15));
}

void encode_dmr_id_bcd(uint8_t *id, uint32_t no) {
  id[0] = ((no / 10000000) << 4)    | ((no / 1000000) % 10);
  id[1] = ((no / 100000 % 10) << 4) | ((no / 10000) % 10);
  id[2] = ((no / 1000 % 10) << 4)   | ((no / 100) % 10);
  id[3] = ((no / 10 % 10) << 4)     | (no % 10);
}

void encode_dmr_id_bcd_le(uint8_t *id, uint32_t no) {
  id[3] = ((no / 10000000) << 4)    | ((no / 1000000) % 10);
  id[2] = ((no / 100000 % 10) << 4) | ((no / 10000) % 10);
  id[1] = ((no / 1000 % 10) << 4)   | ((no / 100) % 10);
  id[0] = ((no / 10 % 10) << 4)     | (no % 10);
}

QVector<char> bin_dtmf_tab = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','*','#'};

QString
decode_dtmf_bin(const uint8_t *num, int size, uint8_t fill) {
  Q_UNUSED(fill);
  QString number;
  for (int i=0; (i<size)&&(num[i]<16); i++) {
    number.append(bin_dtmf_tab[num[i]]);
  }
  return number;
}

bool
encode_dtmf_bin(const QString &number, uint8_t *num, int size, uint8_t fill) {
  QString tmp = number.simplified().toUpper();
  for (int i=0; i<size; i++) {
    num[i] = fill;
    if (i>=tmp.size())
      continue;
    int idx = bin_dtmf_tab.indexOf(number.at(i).toLatin1());
    if (idx<0)
      continue;
    num[i] = idx;
  }
  return true;
}

QString
decode_dtmf_bcd_be(const uint8_t *num, int digits) {
  QString number;
  for (int i=0; i<digits; i++) {
    uint8_t d = (0 == (i%2)) ? (((num[i/2])>>4)&0xf) : ((num[i/2])&0xf);
    number.append(bin_dtmf_tab[d]);
  }
  return number;
}

bool
encode_dtmf_bcd_be(const QString &number, uint8_t *num, int size, uint8_t fill) {
  memset(num, fill, size);
  QString tmp = number.simplified().toUpper();
  for (int i=0; i<tmp.size(); i++) {
    if (0 == (i%2)) {
      num[i/2] = ((bin_dtmf_tab.indexOf(tmp[i].toLatin1()))<<4);
    } else {
      num[i/2] |= bin_dtmf_tab.indexOf(tmp[i].toLatin1());
    }
  }
  return true;
}

SelectiveCall
decode_ctcss_tone_table(uint16_t data) {
  if (data == 0xffff)
    return SelectiveCall();

  unsigned tag = data >> 14;
  unsigned a = (data >> 12) & 3;
  unsigned b = (data >> 8) & 15;
  unsigned c = (data >> 4) & 15;
  unsigned d = data & 15;

  switch (tag) {
  case 2:
    // DCS Normal
    return SelectiveCall(100*b+10*c+1*d, false);
  case 3:
    // DCS Inverted
    return SelectiveCall(100*b+10*c+1*d, true);
  default:
    break;
  }

  // CTCSS
  return SelectiveCall(100.0*a+10.0*b+1.0*c+0.1*d);
}


uint16_t
encode_ctcss_tone_table(const SelectiveCall &code)
{
  unsigned tag=0xff, a=0xf, b=0xf, c=0xf, d=0xf;

  // Disabled
  if (code.isInvalid())
    return 0xffff;

  if (code.isCTCSS()) {
    // CTCSS tone
    tag = 0;
    unsigned val = code.Hz() * 10.0 + 0.5;
    a = val / 1000;
    b = (val / 100) % 10;
    c = (val / 10) % 10;
    d = val % 10;
  } else if (code.isDCS()) {
    // DCS normal
    if (code.isInverted())
      tag = 3;
    else
      tag = 2;
    unsigned val = code.octalCode();
    a = 0;
    b = (val / 100) % 10;
    c = (val / 10) % 10;
    d = val % 10;
  }

  return (a << 12) | (b << 8) | (c << 4) | d | (tag << 14);
}


bool
validDMRNumber(const QString &text) {
  return QRegularExpression("^[0-9]+$").match(text).hasMatch();
}

bool
validDTMFNumber(const QString &text) {
  return QRegularExpression("^[0-9a-dA-D\\*#]+$").match(text).hasMatch();
}

QString
aprsicon2config(APRSSystem::Icon icon) {
  if ((APRSSystem::Icon::None == icon) || (! aprsIconCodeTable.contains(unsigned(icon))))
    return "-";
  return QString("\"%1\"").arg(aprsIconNameTable.value((unsigned)icon));
}

QString
aprsicon2name(APRSSystem::Icon icon) {
  if ((APRSSystem::Icon::None == icon) || (! aprsIconCodeTable.contains(unsigned(icon))))
    return "";
  return aprsIconNameTable.value((unsigned)icon);
}

APRSSystem::Icon
name2aprsicon(const QString &name) {
  if (name.isEmpty())
    return APRSSystem::Icon::None;

  APRSSystem::Icon icon = APRSSystem::Icon::None;
  int best = levDist(name, "");

  QHash<unsigned, QString>::const_iterator item=aprsIconNameTable.constBegin();
  for(; item != aprsIconNameTable.constEnd(); item++) {
    int dist = levDist(name, item.value());
    if (dist < best) {
      icon = (APRSSystem::Icon)item.key();
      best = dist;
    }
  }

  return icon;
}

char
aprsicon2iconcode(APRSSystem::Icon icon) {
 unsigned num = unsigned(APRSSystem::ICON_MASK & unsigned(icon));
 if (num >= unsigned(aprsIconCodeTable.size()))
   return '"';
 return aprsIconCodeTable[num];
}

char
aprsicon2tablecode(APRSSystem::Icon icon) {
  unsigned tab = (APRSSystem::TABLE_MASK & unsigned(icon));
  switch (tab) {
  case APRSSystem::SECONDARY_TABLE: return '\\';
  case APRSSystem::PRIMARY_TABLE: return '/';
  }
  return '/';
}

APRSSystem::Icon
code2aprsicon(char table, char icon) {
  unsigned num = (APRSSystem::ICON_MASK & unsigned(APRSSystem::Icon::None));
  if (aprsIconCodeTable.contains(icon))
    num = aprsIconCodeTable.indexOf(icon);
  if ('/' == table)
    num = num | APRSSystem::PRIMARY_TABLE;
  else if ('\\' == table)
    num = num | APRSSystem::PRIMARY_TABLE;
  return APRSSystem::Icon(num);
}

int
levDist(const QString &source, const QString &target, Qt::CaseSensitivity cs) {
  // Mostly stolen from https://qgis.org/api/2.14/qgsstringutils_8cpp_source.html

  if (0 == QString::compare(source,target, cs)) {
    return 0;
  }

  const int sourceCount = source.size();
  const int targetCount = target.size();

  if (source.isEmpty())
    return targetCount;
  if (target.isEmpty())
    return sourceCount;
  if (sourceCount > targetCount)
    return levDist(target, source, cs);


  QVector<int> column;
  column.fill(0, targetCount + 1);

  QVector<int> previousColumn;
  previousColumn.reserve(targetCount + 1);
  for (int i = 0; i < targetCount + 1; i++)
    previousColumn.append(i);

  for (int i = 0; i < sourceCount; i++) {
    column[0] = i + 1;
    for (int j = 0; j < targetCount; j++) {
      column[j + 1] = std::min(
      {
              1 + column.at(j),
              1 + previousColumn.at(1 + j),
              previousColumn.at(j) + (QString::compare(source.at(i),target.at(j), cs) ? 1 : 0)
            });
    }
    column.swap(previousColumn);
  }

  return previousColumn.at(targetCount);
}

uint32_t
align_size(uint32_t size, uint32_t block) {
  if (0 == (size % block))
    return size;
  return (size + (block - (size%block)));
}

uint32_t
align_addr(uint32_t addr, uint32_t block) {
  if (0 == (addr % block))
    return addr;
  return (addr - (addr%block));
}


QGeoCoordinate
loc2deg(const QString &loc) {
  double lon = 0, lat = 0, dlon = 20, dlat = 10;
  if (2 > loc.size())
    return QGeoCoordinate();
  QChar l = loc[0].toUpper();
  QChar c = loc[1].toUpper();
  lon += double(int(l.toLatin1())-'A')*dlon;
  lat += double(int(c.toLatin1())-'A')*dlat;

  if (4 > loc.size()) {
    lon = lon - 180;
    lat = lat - 90;
    // Offset places coordinate in the middle of the square
    return QGeoCoordinate(lat+dlat/2, lon+dlon/2);
  }

  dlon /= 10; dlat /= 10;
  l = loc[2].toUpper();
  c = loc[3].toUpper();
  lon += double(int(l.toLatin1())-'0')*dlon;
  lat += double(int(c.toLatin1())-'0')*dlat;

  if (6 > loc.size()){
    lon = lon - 180;
    lat = lat - 90;
    // Offset places coordinate in the middle of the square
    return QGeoCoordinate(lat+dlat/2, lon+dlon/2);
  }

  dlon /= 24; dlat /= 24;
  l = loc[4].toUpper();
  c = loc[5].toUpper();
  lon += double(int(l.toLatin1())-'A')*dlon;
  lat += double(int(c.toLatin1())-'A')*dlat;

  if (8 > loc.size()) {
    lon = lon - 180;
    lat = lat - 90;
    // Offset places coordinate in the middle of the square
    return QGeoCoordinate(lat+dlat/2, lon+dlon/2);
  }

  dlon /= 10; dlat /= 10;
  l = loc[6].toUpper();
  c = loc[7].toUpper();
  lon += double(int(l.toLatin1())-'0')*dlon;
  lat += double(int(c.toLatin1())-'0')*dlat;

  if (10 > loc.size()) {
    lon = lon - 180;
    lat = lat - 90;
    // Offset places coordinate in the middle of the square
    return QGeoCoordinate(lat+dlat/2, lon+dlon/2);
  }

  dlon /= 24; dlat /= 24;
  l = loc[8].toUpper();
  c = loc[9].toUpper();
  lon += double(int(l.toLatin1())-'A')*dlon;
  lat += double(int(c.toLatin1())-'A')*dlat;

  lon = lon - 180;
  lat = lat - 90;
  // Offset places coordinate in the middle of the square
  return QGeoCoordinate(lat+dlat/2, lon+dlon/2);
}


QString
deg2loc(const QGeoCoordinate &coor, unsigned int size) {
  QString loc;
  double lon = (coor.longitude()+180)/360;
  double lat = (coor.latitude()+90)/180;

  size += (size % 2);
  if (2 > size)
    return loc;

  lon *= 18; lat *= 18;
  char l = lon; lon -= l;
  char c = lat; lat -= c;
  loc.append(QChar::fromLatin1(l+'A')); loc.append(QChar::fromLatin1(c+'A'));

  if (4 > size)
    return loc;

  lon *= 10; lat *= 10;
  l = lon; lon -= l;
  c = lat; lat -= c;
  loc.append(QChar::fromLatin1(l+'0')); loc.append(QChar::fromLatin1(c+'0'));

  if (6 > size)
    return loc;

  lon *= 24; lat *= 24;
  l = lon; lon -= l;
  c = lat; lat -= c;
  loc.append(QChar::fromLatin1(l+'a')); loc.append(QChar::fromLatin1(c+'a'));

  if (8 > size)
    return loc;

  lon *= 10; lat *= 10;
  l = lon; lon -= l;
  c = lat; lat -= c;
  loc.append(QChar::fromLatin1(l+'0')); loc.append(QChar::fromLatin1(c+'0'));

  if (10 > size)
    return loc;

  lon *= 24; lat *= 24;
  l = lon; //lon -= l;
  c = lat; //lat -= c;
  loc.append(QChar::fromLatin1(l+'a')); loc.append(QChar::fromLatin1(c+'a'));

  return loc;
}