-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
327 lines (264 loc) · 9.13 KB
/
main.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
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
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <boost/filesystem.hpp>
#include "gdal/ogrsf_frmts.h"
#include "Tile.h"
using namespace std;
using namespace boost::filesystem;
/** @brief Transform OGREnvelope to full blown OGRPolyon
*
* transform by converting to text form and back again
*/
OGRPolygon* transformEnvToGeom(OGREnvelope* env)
{
stringstream wkt;
wkt << "POLYGON (( ";
wkt << env->MinX << " " << env-> MaxY << ", ";
wkt << env->MinX << " " << env-> MinY << ", ";
wkt << env->MaxX << " " << env-> MinY << ", ";
wkt << env->MaxX << " " << env-> MaxY << ", ";
wkt << env->MinX << " " << env-> MaxY <<" ))";
OGRPolygon* result = new OGRPolygon();
char* copy = (char*) wkt.str().c_str();
OGRGeometryFactory::createFromWkt(©, NULL, (OGRGeometry**)&result);
if (result != NULL)
return result;
else
exit(1);
}
/** @brief load tile infrmation from areas.list
*
* read in areas.list file and return list of tiles
* (with name and coordinates)
*/
void loadAreas(vector<Tile*>* tiles_p)
{
string line;
stringstream valid_data;
cout<<current_path()<<endl;
if (exists("areas.list"))
{
ifstream areas_list ("areas.list");
if (areas_list.is_open())
{
int num; //to hold tile number between lines
while(areas_list.good())
{
getline(areas_list, line);
if(line.find_first_not_of(" \n\t\r\0") != string::npos
&& line.find_first_of("0123456789") != string::npos
&& line.find("Generated") == string::npos) // skip empty lines and header
{
if (line[2] != ' ') // the first line with the tile number
{
num = atoi(line.substr(0,line.find_first_of(':')).c_str());
// cout << num << endl;
}
else //second line with coords
{
unsigned int begin = line.find_first_of(':')+2;
unsigned int end = line.find(" to");
string pair1 = line.substr(begin,end-begin);
begin = end +4;
string pair2 = line.substr(begin);
float s = atof(pair1.substr(0, pair1.find_first_of(',')).c_str());
float e = atof(pair1.substr(pair1.find_first_of(',')+1).c_str());
float n = atof(pair2.substr(0, pair2.find_first_of(',')).c_str());
float w = atof(pair2.substr(pair2.find_first_of(',')+1).c_str());
Tile* t = new Tile(num, s, e, n , w);
tiles_p->push_back(t);
// cout.precision(8);
// cout << line << endl;
// cout << pair1 << "####"<< pair2 << endl;
// cout << s << "##" << e << "##" <<endl;
// cout << n << "##" << w << "##" <<endl;
}
}
}
}
}
else
{
cout << "file not found " << endl;
}
cout << "Loaded " << tiles_p->size() << " tiles." << endl << endl;
}
/** @brief Get list of shapes from file, optional search by name
*
* read in shape file, extract shapes return as list
* optional string argument returns only matching shapes (by string.find)
* (!case sensitive!)
*/
void getAvailShapes(vector<OGRFeature*>* availShapes_p, const string selected = "")
{
OGRRegisterAll();
OGRDataSource * poDS;
string path = "../admin_level_6/shape_al6/admin_level_6.shp";
if (exists(path))
{
poDS = OGRSFDriverRegistrar::Open(path.c_str());
if (poDS == NULL)
{
cout << "Data read error" << endl;
exit (1);
}
}
else
{
cout << "File not found" << endl;
exit(1);
}
// cout << "Found " << poDS->GetLayerCount() << "Layers in File" << endl;
OGRLayer *poLayer;
OGRFeature *poFeature;
poLayer = poDS->GetLayer(0);
poLayer->ResetReading();
while ( (poFeature = poLayer->GetNextFeature()) != NULL)
{
if (selected.empty())
{
availShapes_p->push_back(poFeature);
}
else
{
if (string(poFeature->GetFieldAsString(1)).find(selected) != string::npos)
availShapes_p->push_back(poFeature);
else
OGRFeature::DestroyFeature(poFeature);
}
}
OGRDataSource::DestroyDataSource(poDS);
}
/** @brief Extract bounding box from shape
*
* get bounding box as OGREnvelope from polygon shape in OGRFeature
*/
OGREnvelope* getBBoxOfShape(OGRFeature* poFeature)
{
OGRGeometry *poGeometry;
poGeometry = poFeature->GetGeometryRef();
cout <<"extracting shape of " << poFeature->GetFieldAsString(1) << endl;
if (poGeometry != NULL
&& wkbFlatten(poGeometry->getGeometryType()) == wkbPolygon)
{
OGRPolygon *poPoly = (OGRPolygon *) poGeometry;
OGREnvelope *poEnv = new OGREnvelope();
poPoly->getEnvelope(poEnv);
return poEnv;
// ofstream outfile ("outfile.kml");
// double s = poEnv->MaxY;
// double n = poEnv->MinY;
// double e = poEnv->MaxX;
// double w = poEnv->MinX;
//
// cout<< s << ", " << n << " to " << e << ", " << w << endl;
// outfile.close();
// cout << poPoint->getX() <<", "<<poPoint->getY()<<endl;
}
else
{
cout << "No point Geometry"<<endl;
return NULL;
}
}
/** @brief Check which tiles are part of this bbox
*
* checks inclusion and intersection with supplied tiles list
*/
vector<Tile*> tilesInBBox(const vector<Tile*>* tiles, OGREnvelope* bbox)
{
cout << "BBox is " << bbox->MinY <<", "<<bbox->MinX<<" to "<<bbox->MaxY<<", "<<bbox->MaxX<<endl;
cout << "searching in " << tiles->size() << " tiles"<<endl;
vector<Tile*> result;
for(unsigned int i = 0; i < tiles->size(); i++)
{
const OGREnvelope* currentTile = (OGREnvelope*)tiles->at(i);
//cout << "Comparing " << currentTile->MinY <<", "<<currentTile->MinX<<" to "<<currentTile->MaxY<<", "<<currentTile->MaxX<<endl;
if (bbox->Contains(*currentTile) || bbox->Intersects(*currentTile))
{
result.push_back(tiles->at(i));
}
}
cout << "Box enclosed " << result.size() << " tiles." << endl;
return result;
}
/** @brief Check which tiles are part of this shape
*
* checks inclusion and intersection with supplied tiles list
*/
vector<Tile*> tilesInShape(vector<Tile*>* tiles, OGRFeature* shape)
{
cout << "searching in " << tiles->size() << " tiles"<<endl;
vector<Tile*> result;
for(unsigned int i = 0; i < tiles->size(); i++)
{
OGRPolygon* currentTile = transformEnvToGeom((OGREnvelope*)tiles->at(i));
//cout << "Comparing " << currentTile->MinY <<", "<<currentTile->MinX<<" to "<<currentTile->MaxY<<", "<<currentTile->MaxX<<endl;
if (shape->GetGeometryRef()->Contains(currentTile) || shape->GetGeometryRef()->Intersects(currentTile))
{
result.push_back(tiles->at(i));
}
}
cout << "Shape enclosed " << result.size() << " tiles." << endl;
return result;
}
int main()
{
vector<Tile*> tiles;
vector<OGRFeature*> availShapes;
loadAreas(&tiles);
OGREnvelope* bbox;
// for (unsigned int i = 0; i < tiles.size(); i++)
// {
// cout << tiles[i].num <<": " << tiles[i].coords[0]<< endl;
// }
string selected = "";
bool found = false;
while (!found)
{
selected.clear();
cout << "Select a shape: ";
cin >> selected;
getAvailShapes(&availShapes, selected);
if (availShapes.size() >1)
{
//found = true;
cout << "Found more than one match: " << endl;
for (unsigned int i = 0; i < availShapes.size(); i++)
{
cout << "\t" << availShapes[i]->GetFieldAsString(1) << endl;
}
cout << "(Please be more specific.)" << endl;
// cleanup
for (unsigned int i = 0; i < availShapes.size(); i++)
{
OGRFeature::DestroyFeature(availShapes[i]);
}
availShapes.clear();
}
else if (availShapes.size() == 1)
{
found = true;
cout << "Found match: " << availShapes[0]->GetFieldAsString(1) << endl;
}
else
cout << "No match found, try again." << endl;
}
bbox = getBBoxOfShape(availShapes[0]);
vector<Tile*> filteredTiles, shapeTiles;
filteredTiles = tilesInBBox(&tiles, bbox);
shapeTiles = tilesInShape(&filteredTiles, availShapes[0]);
// cleanup
for (unsigned int i = 0; i < availShapes.size(); i++)
{
OGRFeature::DestroyFeature(availShapes[i]);
}
return 0;
}
//todo:
// get boundary rectangle of selected shape
// pre- sort tiles
// check remaining tiles for inclusion in shape