Code cleanup
[GPXSee.git] / src / map / conversion.cpp
blobce177c22cdd6084373962f6fbb0adfb959a4662e
1 #include <QFile>
2 #include "common/csv.h"
3 #include "conversion.h"
5 static bool parameter(int key, double val, int units, Projection::Setup &setup)
7 switch (key) {
8 case 8801:
9 case 8811:
10 case 8821:
11 case 8832:
12 {AngularUnits au(units);
13 if (au.isNull())
14 return false;
15 setup.setLatitudeOrigin(au.toDegrees(val));}
16 return true;
17 case 8802:
18 case 8812:
19 case 8822:
20 case 8833:
21 {AngularUnits au(units);
22 if (au.isNull())
23 return false;
24 setup.setLongitudeOrigin(au.toDegrees(val));}
25 return true;
26 case 8805:
27 case 8815:
28 case 8819:
29 setup.setScale(val);
30 return true;
31 case 8806:
32 case 8816:
33 case 8826:
34 {LinearUnits lu(units);
35 if (lu.isNull())
36 return false;
37 setup.setFalseEasting(lu.toMeters(val));}
38 return true;
39 case 8807:
40 case 8817:
41 case 8827:
42 {LinearUnits lu(units);
43 if (lu.isNull())
44 return false;
45 setup.setFalseNorthing(lu.toMeters(val));}
46 return true;
47 case 8813:
48 case 8818:
49 case 8823:
50 {AngularUnits au(units);
51 if (au.isNull())
52 return false;
53 setup.setStandardParallel1(au.toDegrees(val));}
54 return true;
55 case 1036:
56 case 8814:
57 case 8824:
58 {AngularUnits au(units);
59 if (au.isNull())
60 return false;
61 setup.setStandardParallel2(au.toDegrees(val));}
62 return true;
63 default:
64 return false;
68 static int projectionSetup(const QByteArrayList &list, Projection::Setup &setup)
70 bool r1, r2, r3;
72 for (int i = 5; i < 26; i += 3) {
73 const QByteArray &ks = list.at(i);
74 if (ks.isEmpty())
75 break;
77 int key = ks.toInt(&r1);
78 double val = list.at(i+1).toDouble(&r2);
79 int un = list.at(i+2).toInt(&r3);
80 if (!r1 || !r2 || !r3)
81 return (i - 5)/3 + 1;
83 if (!parameter(key, val, un, setup))
84 return (i - 5)/3 + 1;
87 return 0;
90 QMap<int, Conversion::Entry> Conversion::_conversions = defaults();
92 QMap<int, Conversion::Entry> Conversion::defaults()
94 QMap<int, Conversion::Entry> map;
95 map.insert(3856, Entry("Popular Visualisation Pseudo-Mercator", 1024,
96 Projection::Setup(), 9001, 4400));
97 return map;
100 Conversion Conversion::conversion(int id)
102 QMap<int, Entry>::const_iterator it(_conversions.find(id));
104 if (it == _conversions.constEnd())
105 return Conversion();
106 else {
107 const Entry &e = it.value();
108 return Conversion(e.method(), e.setup(), e.units(), e.cs());
112 bool Conversion::loadList(const QString &path)
114 QFile file(path);
115 CSV csv(&file);
116 QByteArrayList entry;
117 bool res;
119 if (!file.open(QFile::ReadOnly)) {
120 qWarning("Error opening projections file: %s: %s", qPrintable(path),
121 qPrintable(file.errorString()));
122 return false;
125 while (!csv.atEnd()) {
126 if (!csv.readEntry(entry) || entry.size() < 26) {
127 qWarning("%s:%d: Parse error", qPrintable(path), csv.line());
128 return false;
131 QString name(entry.at(0));
132 int proj = entry.at(1).toInt(&res);
133 if (!res) {
134 qWarning("%s:%d: Invalid projection code", qPrintable(path),
135 csv.line());
136 continue;
138 int units = entry.at(2).toInt(&res);
139 if (!res) {
140 qWarning("%s:%d: Invalid linear units code", qPrintable(path),
141 csv.line());
142 continue;
144 int transform = entry.at(3).toInt(&res);
145 if (!res) {
146 qWarning("%s:%d: Invalid coordinate transformation code",
147 qPrintable(path), csv.line());
148 continue;
150 int cs = entry.at(4).toInt(&res);
151 if (!res) {
152 qWarning("%s:%d: Invalid coordinate system code",
153 qPrintable(path), csv.line());
154 continue;
157 if (!LinearUnits(units).isValid()) {
158 qWarning("%s:%d: Unknown linear units code", qPrintable(path),
159 csv.line());
160 continue;
162 if (!Projection::Method(transform).isValid()) {
163 qWarning("%s:%d: Unknown coordinate transformation code",
164 qPrintable(path), csv.line());
165 continue;
167 if (!CoordinateSystem(cs).isValid()) {
168 qWarning("%s:%d: Unknown coordinate system code", qPrintable(path),
169 csv.line());
170 continue;
173 Projection::Setup setup;
174 int pn = projectionSetup(entry, setup);
175 if (pn) {
176 qWarning("%s: %d: Invalid projection parameter #%d",
177 qPrintable(path), csv.line(), pn);
178 continue;
181 _conversions.insert(proj, Entry(name, transform, setup, units, cs));
184 return true;
187 QList<KV<int, QString> > Conversion::list()
189 QList<KV<int, QString> > list;
191 for (QMap<int, Entry>::const_iterator it = _conversions.constBegin();
192 it != _conversions.constEnd(); ++it)
193 list.append(KV<int, QString>(it.key(), it.value().name()));
195 return list;
198 #ifndef QT_NO_DEBUG
199 QDebug operator<<(QDebug dbg, const Conversion &conversion)
201 dbg.nospace() << "Conversion(" << conversion.method() << ", "
202 << conversion.units() << ", " << conversion.setup() << ")";
203 return dbg.space();
205 #endif // QT_NO_DEBUG