-
Notifications
You must be signed in to change notification settings - Fork 0
/
geodataprocessor.cpp
297 lines (245 loc) · 5.68 KB
/
geodataprocessor.cpp
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
#include "geodataprocessor.h"
#include <QDebug>
#include <QPointF>
#include <QStack>
double getPsa(const bc::barvector &matr)
{
std::unordered_map<uint, bool> map;
int minX = 10000, maxX = 0, minY = 1000, maxY = 0;
for (const auto &pm : matr)
{
map[pm.index] = true;
int xa = pm.getX();
int ya = pm.getY();
if (xa > maxX)
maxX = xa;
if (xa < minX)
minX = xa;
if (ya > maxY)
maxY = ya;
if (ya < minY)
minY = ya;
}
double entr = 0;
bool foundPrev = map.find(bc::barvalue::getStatInd(minX, minY)) != map.end();
for (int x = minX; x < maxX; ++x)
{
double locEntr = 1.0;
for (int y = minY; y < maxY; ++y)
{
bool foundCur = map.find(bc::barvalue::getStatInd(x, y)) != map.end();
locEntr *= 0.5;
if (foundPrev != foundCur)
{
entr += locEntr;
locEntr = 1.0;
}
}
if (locEntr != 1.0)
entr += locEntr;
}
for (int y = minY; y < maxY; ++y)
{
double locEntr = 1.0;
for (int x = minX; x < maxX; ++x)
{
bool foundCur = map.find(bc::barvalue::getStatInd(x, y)) != map.end();
locEntr *= 0.5;
if (foundPrev != foundCur)
{
entr += locEntr;
locEntr = 1.0;
}
}
if (locEntr != 1.0)
entr += locEntr;
}
return entr;
}
#define DICT_EXISTS(DICT, X, Y) (DICT.find(bc::barvalue::getStatInd(X, Y)) != DICT.end())
class MapCountur
{
int x = 0, y = 0;
int stIndex = 0;
mcountor &contur;
std::unordered_map<uint, bool> points;
enum StartPos : char { LeftMid = 0, LeftTop = 1, TopMid = 2, RigthTop = 3, RigthMid = 4, RigthBottom = 5, BottomMid = 6, LeftBottom = 7 };
StartPos dirct = RigthMid;
QStack<StartPos> dirs;
QStack<uint> pointsStack;
public:
MapCountur(mcountor &contur) : contur(contur) {}
void run(const bool aproxim = false)
{
static int poss[16][2] = {{-1, 0}, {-1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}};
dirct = RigthMid;
while (true)
{
char start = (dirct + 6) % 8; // Safe minus 2
char end = start + 5;
// Check
// 1 2 3
// 0 X 4
int prevS = getIndex();
for (; start < end; ++start)
{
int *off = poss[(int) start];
if (tryVal(off[0], off[1]))
{
break;
}
}
if (start != end)
{
int s = getIndex();
StartPos old = dirct;
dirs.push(dirct);
dirct = (StartPos)(start % 8);
// Check new disk with the old one
if (dirct != old || !aproxim)
{
contur.push_back(prevS);
// contur.push_back(s);
}
if (s == stIndex)
{
break;
}
}
else
{
unset();
if (pointsStack.size() < 1)
break;
// if (addIndex < 1)
// break;
StartPos old = dirct;
dirct = dirs.pop();
if (dirct != old || !aproxim)
contur.pop_back();
auto p = bc::barvalue::getStatPoint(pointsStack.pop());
//contur[addIndex]);
x = p.x;
y = p.y;
}
}
}
void set(const bc::barvalue &p) { points[p.getIndex()] = true; }
void setStart(int x, int y)
{
this->x = x;
this->y = y;
stIndex = bc::barvalue::getStatInd(x, y);
}
private:
void unset() { points[bc::barvalue::getStatInd(x, y)] = false; }
bool exists(int xl, int yl)
{
auto ds = points.find(bc::barvalue::getStatInd(xl, yl));
if (ds == points.end())
return false;
return ds->second;
}
bool tryVal(int oX, int oY)
{
if (exists(x + oX, y + oY))
{
pointsStack.push(getIndex());
x += oX;
y += oY;
return true;
}
return false;
}
uint getIndex()
{
return bc::barvalue::getStatInd(x, y);
}
};
void getCountour(const bc::barvector &points, mcountor &contur, bool aproximate)
{
contur.clear();
int rect[4]{99999999, 99999999, 0, 0};
int stY;
MapCountur dictPoints(contur);
for (auto &p : points)
{
dictPoints.set(p);
int x = p.getX();
int y = p.getY();
if (x < rect[0])
{
rect[0] = x;
stY = y;
}
if (x > rect[2])
{
rect[2] = x;
}
if (y < rect[1])
{
rect[1] = y;
}
if (y > rect[3])
{
rect[3] = y;
}
}
int wid = rect[2] - rect[0];
int hei = rect[3] - rect[1];
if (wid < 5 || hei < 5)// || (wid > 1000 && hei > 1000))
return;
dictPoints.setStart(rect[0], stY);
dictPoints.run(aproximate);
}
void saveAllJsons(bc::barlinevector *geojson, QString path)
{
QPointF start(55.578100, 42.052772);
double coof = 0.00001;
saveAsGeojson(geojson[1], path + "2skat.geojson", start, coof);
saveAsGeojson(geojson[2], path + "1skat.geojson", start, coof);
}
void saveAsGeojson(const bc::barlinevector &lines, QString savePath, QPointF startOffset, double coof)
{
QString json = "{\"type\":\"FeatureCollection\","
"\"name\":\"Roofs\","
"\"crs\": { \"type\": \"name\", \"properties\":{\"name\": \"urn:ogc:def:crs:EPSG::3857\" } }," //3857
"\"features\":[ ";
json += "";
for (int i = 0, total = lines.size(); i < total; ++i)
{
QString safsd = "{ \"type\": \"Feature\",";
safsd += QString("\"properties\":{\"id\": %1 },").arg(i + 1);
safsd += "\"geometry\": {\"type\":\"MultiPolygon\", \"coordinates\":[[[ ";
auto &allPoints = lines[i]->getMatrix();
mcountor cont;
getCountour(allPoints, cont, true);
int totalc = cont.size();
for (int j = 0; j < totalc; ++j)
{
const auto &p = bc::barvalue::getStatPoint(cont[j]);
double x = startOffset.x() - p.y * coof;
double y = startOffset.y() + p.x * coof;
safsd += QString("[%1, %2],").arg(y, 0, 'g', 8).arg(x, 0, 'g', 8);
}
if (totalc > 0)
{
safsd[safsd.length() - 1] = ']';
safsd += "]]}},";
}
else
{
safsd += "]]]}},";
}
json += safsd;
}
json[json.length() - 1] = ']';
json += "}";
QFile file(savePath);
if (file.open(QIODevice::WriteOnly)) {
file.write(json.toUtf8());
file.close();
}
// widget->importedMakrers->release();
// // Size2 size = imgsrch.getTileSize();
}