-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathexampleLayers.cxx
More file actions
451 lines (394 loc) · 14.6 KB
/
exampleLayers.cxx
File metadata and controls
451 lines (394 loc) · 14.6 KB
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
#include <curl/curl.h>
#include <iostream>
#include <vtkCollection.h>
#include <vtkCommand.h>
#include <vtkIdList.h>
#include <vtkNew.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRegularPolygonSource.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtksys/CommandLineArguments.hxx>
#include "vtkFeatureLayer.h"
#include "vtkGeoMapSelection.h"
#include "vtkInteractorStyleGeoMap.h"
#include "vtkMap.h"
#include "vtkMapMarkerSet.h"
#include "vtkMercator.h"
#include "vtkMultiThreadedOsmLayer.h"
#include "vtkOsmLayer.h"
#include "vtkPolydataFeature.h"
// ------------------------------------------------------------
class MoveCallback : public vtkCommand
{
public:
static MoveCallback* New() { return new MoveCallback; }
void Execute(vtkObject* caller, unsigned long event, void* data) override
{
switch (event)
{
case vtkCommand::KeyPressEvent:
{
auto interactor = vtkRenderWindowInteractor::SafeDownCast(caller);
const std::string key = interactor->GetKeySym();
vtkMapType::Move direction;
if (key == "Left")
{
direction = vtkMapType::Move::UP;
}
else if (key == "Right")
{
direction = vtkMapType::Move::DOWN;
}
else if (key == "Up")
{
direction = vtkMapType::Move::TOP;
}
else if (key == "Down")
{
direction = vtkMapType::Move::BOTTOM;
}
Map->MoveLayer(Layer, direction);
}
break;
}
}
vtkLayer* Layer = nullptr;
vtkMap* Map = nullptr;
};
// ------------------------------------------------------------
class PickCallback : public vtkCommand
{
public:
static PickCallback* New() { return new PickCallback; }
void Execute(vtkObject* caller, unsigned long event, void* data) override
{
switch (event)
{
case vtkInteractorStyleGeoMap::DisplayClickCompleteEvent:
{
double* latLonCoords = static_cast<double*>(data);
std::cout << "Point coordinates: \n"
<< " " << latLonCoords[0] << ", " << latLonCoords[1]
<< std::endl;
}
break;
case vtkInteractorStyleGeoMap::DisplayDrawCompleteEvent:
{
double* latLonCoords = static_cast<double*>(data);
std::cout << "Rectangle coordinates: \n"
<< " " << latLonCoords[0] << ", " << latLonCoords[1]
<< "\n " << latLonCoords[2] << ", " << latLonCoords[3]
<< std::endl;
}
break;
case vtkInteractorStyleGeoMap::SelectionCompleteEvent:
{
vtkObject* object = static_cast<vtkObject*>(data);
vtkGeoMapSelection* selection =
vtkGeoMapSelection::SafeDownCast(object);
double* latLonCoords = selection->GetLatLngBounds();
std::cout << "Selected coordinates: \n"
<< " " << latLonCoords[0] << ", " << latLonCoords[1]
<< "\n " << latLonCoords[2] << ", " << latLonCoords[3]
<< std::endl;
vtkCollection* collection = selection->GetSelectedFeatures();
std::cout << "Number of features: " << collection->GetNumberOfItems()
<< std::endl;
vtkNew<vtkIdList> cellIdList;
vtkNew<vtkIdList> markerIdList;
vtkNew<vtkIdList> clusterIdList;
for (int i = 0; i < collection->GetNumberOfItems(); i++)
{
vtkObject* object = collection->GetItemAsObject(i);
std::cout << " " << object->GetClassName() << "\n";
vtkFeature* feature = vtkFeature::SafeDownCast(object);
// Retrieve polydata cells (if relevant)
if (selection->GetPolyDataCellIds(feature, cellIdList.GetPointer()))
{
if (cellIdList->GetNumberOfIds() > 0)
{
std::cout << " Cell ids: ";
for (int j = 0; j < cellIdList->GetNumberOfIds(); j++)
{
std::cout << " " << cellIdList->GetId(j);
}
std::cout << std::endl;
}
}
// Retrieve marker ids (if relevant)
if (selection->GetMapMarkerIds(
feature, markerIdList.GetPointer(), clusterIdList.GetPointer()))
{
std::cout << " Marker ids: ";
for (int j = 0; j < markerIdList->GetNumberOfIds(); j++)
{
std::cout << " " << markerIdList->GetId(j);
}
std::cout << std::endl;
std::cout << " Cluster ids: ";
for (int j = 0; j < clusterIdList->GetNumberOfIds(); j++)
{
std::cout << " " << clusterIdList->GetId(j);
}
std::cout << std::endl;
}
}
}
break;
case vtkInteractorStyleGeoMap::ZoomCompleteEvent:
{
double* latLonCoords = static_cast<double*>(data);
std::cout << "Zoom coordinates: \n"
<< " " << latLonCoords[0] << ", " << latLonCoords[1]
<< "\n " << latLonCoords[2] << ", " << latLonCoords[3]
<< std::endl;
}
break;
case vtkInteractorStyleGeoMap::RightButtonCompleteEvent:
{
int* coords = static_cast<int*>(data);
std::cout << "Right mouse click at (" << coords[0] << ", " << coords[1]
<< ")" << std::endl;
}
break;
} // switch
}
void SetMap(vtkMap* map) { this->Map = map; }
protected:
vtkMap* Map;
};
// ------------------------------------------------------------
int main(int argc, char* argv[])
{
// Initialize libcurl for vtkMap to avoid bad surprises
curl_global_init(CURL_GLOBAL_DEFAULT);
// Setup command line arguments
int clusteringOff = false;
bool showHelp = false;
bool perspective = false;
bool rubberBandDisplayOnly = false;
bool rubberBandSelection = false;
bool drawPolygonSelection = false;
bool rubberBandZoom = false;
bool singleThreaded = false;
int zoomLevel = 2;
std::vector<double> centerLatLon;
std::string tileExtension = "png";
std::string tileServer;
std::string tileServerAttribution;
vtksys::CommandLineArguments arg;
arg.Initialize(argc, argv);
arg.StoreUnusedArguments(true);
arg.AddArgument("-h", vtksys::CommandLineArguments::NO_ARGUMENT, &showHelp,
"show help message");
arg.AddArgument("--help", vtksys::CommandLineArguments::NO_ARGUMENT,
&showHelp, "show help message");
arg.AddArgument("-a", vtksys::CommandLineArguments::SPACE_ARGUMENT,
&tileServerAttribution, "map-tile server attribution");
arg.AddArgument("-d", vtksys::CommandLineArguments::NO_ARGUMENT,
&rubberBandDisplayOnly, "set interactor to rubberband-draw mode");
arg.AddArgument("-e", vtksys::CommandLineArguments::SPACE_ARGUMENT,
&tileExtension, "map-tile file extension (jpg, png, etc.)");
arg.AddArgument("-c", vtksys::CommandLineArguments::MULTI_ARGUMENT,
¢erLatLon, "initial center (latitude longitude)");
arg.AddArgument("-m", vtksys::CommandLineArguments::SPACE_ARGUMENT,
&tileServer, "map-tile server (tile.openstreetmaps.org)");
arg.AddArgument("-o", vtksys::CommandLineArguments::NO_ARGUMENT,
&clusteringOff, "turn clustering off");
arg.AddArgument("-p", vtksys::CommandLineArguments::NO_ARGUMENT, &perspective,
"use perspective projection");
arg.AddArgument("-q", vtksys::CommandLineArguments::NO_ARGUMENT,
&rubberBandZoom, "set interactor to rubberband zoom mode");
arg.AddArgument("-r", vtksys::CommandLineArguments::NO_ARGUMENT,
&rubberBandSelection, "set interactor to rubberband selection mode");
arg.AddArgument("-P", vtksys::CommandLineArguments::NO_ARGUMENT,
&drawPolygonSelection, "set interactor to polygon selection mode");
arg.AddArgument("-s", vtksys::CommandLineArguments::NO_ARGUMENT,
&singleThreaded, "use single-threaded map I/O");
arg.AddArgument("-z", vtksys::CommandLineArguments::SPACE_ARGUMENT,
&zoomLevel, "initial zoom level (1-20)");
if (!arg.Parse() || showHelp)
{
std::cout << "\n" << arg.GetHelp() << std::endl;
return -1;
}
vtkNew<vtkMap> map;
// Note: Always set map's renderer *before* adding layers
vtkNew<vtkRenderer> rend;
map->SetRenderer(rend.GetPointer());
// Set viewport
if (centerLatLon.size() == 2)
{
map->SetCenter(centerLatLon[0], centerLatLon[1]);
}
else
{
// If center not specified, display CONUS
double latLngCoords[] = { 25.0, -115.0, 50.0, -75.0 };
map->SetVisibleBounds(latLngCoords);
}
map->SetPerspectiveProjection(perspective);
// Adjust zoom level to perspective vs orthographic projection
// Internally, perspective is zoomed in one extra level
// Do the same here for perspective.
zoomLevel += perspective ? 0 : 1;
map->SetZoom(zoomLevel);
vtkOsmLayer* osmLayer;
if (singleThreaded)
{
osmLayer = vtkOsmLayer::New();
}
else
{
osmLayer = vtkMultiThreadedOsmLayer::New();
}
// Layer 1 - Map
map->AddLayer(osmLayer);
if (tileServer != "")
{
osmLayer->SetMapTileServer(
tileServer.c_str(), tileServerAttribution.c_str(), tileExtension.c_str());
}
osmLayer->Delete();
vtkNew<vtkRenderWindow> wind;
wind->SetMultiSamples(0); // MSAA will create interpolated pixels
wind->AddRenderer(rend.GetPointer());
wind->SetSize(800, 600);
vtkNew<vtkRenderWindowInteractor> intr;
intr->SetRenderWindow(wind.GetPointer());
map->SetInteractor(intr.GetPointer());
// Interactor observer
vtkNew<MoveCallback> moveCallback;
intr->AddObserver(vtkCommand::KeyPressEvent, moveCallback.GetPointer());
vtkMapType::Interaction mode = vtkMapType::Interaction::Default;
if (rubberBandDisplayOnly)
{
mode = vtkMapType::Interaction::RubberBandDisplayOnly;
}
else if (rubberBandSelection)
{
mode = vtkMapType::Interaction::RubberBandSelection;
}
else if (drawPolygonSelection)
{
mode = vtkMapType::Interaction::PolygonSelection;
}
else if (rubberBandZoom)
{
mode = vtkMapType::Interaction::RubberBandZoom;
}
map->SetInteractionMode(mode);
// Picking Callback
vtkNew<PickCallback> pickCallback;
pickCallback->SetMap(map.GetPointer());
map->AddObserver(vtkInteractorStyleGeoMap::DisplayClickCompleteEvent,
pickCallback.GetPointer());
map->AddObserver(vtkInteractorStyleGeoMap::DisplayDrawCompleteEvent,
pickCallback.GetPointer());
map->AddObserver(vtkInteractorStyleGeoMap::SelectionCompleteEvent,
pickCallback.GetPointer());
map->AddObserver(
vtkInteractorStyleGeoMap::ZoomCompleteEvent, pickCallback.GetPointer());
map->AddObserver(vtkInteractorStyleGeoMap::RightButtonCompleteEvent,
pickCallback.GetPointer());
intr->Initialize();
map->Draw();
double latLon[4];
map->GetVisibleBounds(latLon);
std::cout << "lat-lon bounds: "
<< "(" << latLon[0] << ", " << latLon[1] << ")"
<< " "
<< "(" << latLon[2] << ", " << latLon[3] << ")" << std::endl;
///////////////////// Layer 2 - Markers /////////////////////////////////////
vtkNew<vtkFeatureLayer> markers1;
markers1->SetName("markers1");
map->AddLayer(markers1.GetPointer());
// Instantiate markers for array of lat-lon coordinates
vtkNew<vtkMapMarkerSet> markerSet;
markerSet->SetClustering(!clusteringOff);
markers1->AddFeature(markerSet.GetPointer());
const double kwLatitude = 42.849604; // Kitware HQ coords
const double kwLongitude = -73.758345;
double latlonCoords[][2] = {
0.0, 0.0, kwLatitude, kwLongitude, // KHQ
35.911373, -79.072205, // KRS
32.301393, -90.871495 // ERDC
};
const size_t numMarkers = sizeof(latlonCoords) / sizeof(double) / 2;
for (size_t i = 0; i < numMarkers; ++i)
{
const double lat = latlonCoords[i][0];
const double lon = latlonCoords[i][1];
markerSet->AddMarker(lat, lon);
}
///////////////////// Layer 3 - Markers2 /////////////////////////////////////
vtkNew<vtkFeatureLayer> markers2;
markers2->SetName("markers2");
map->AddLayer(markers2.GetPointer());
// Instantiate markers for array of lat-lon coordinates
vtkNew<vtkMapMarkerSet> markerSet2;
markerSet2->SetMarkerShape(static_cast<int>(vtkMapType::Shape::TRIANGLE));
markerSet2->SetClustering(!clusteringOff);
markers2->AddFeature(markerSet2.GetPointer());
const double offset = 0.5;
double coords[][2] = {
0.0, 0.0, kwLatitude, kwLongitude + offset, // KHQ
35.911373, -79.072205 + offset, // KRS
32.301393, -90.871495 + offset // ERDC
};
const size_t numMarkers2 = sizeof(coords) / sizeof(double) / 2;
for (size_t i = 0; i < numMarkers2; ++i)
{
const double lat = coords[i][0];
const double lon = coords[i][1];
markerSet2->AddMarker(lat, lon);
}
map->Draw();
///////////////////// Layer 4 - Circle //////////////////////////////////////
vtkNew<vtkFeatureLayer> circle;
circle->SetName("circle");
map->AddLayer(circle.GetPointer());
// Note: Always add vtkFeatureLayer to the map before adding features
vtkNew<vtkRegularPolygonSource> polygon;
polygon->SetNumberOfSides(50);
polygon->SetRadius(2.0);
vtkNew<vtkPolydataFeature> feature;
feature->GetMapper()->SetInputConnection(polygon->GetOutputPort());
feature->GetActor()->GetProperty()->SetColor(0.0, 0.0, 1.0);
feature->GetActor()->GetProperty()->SetOpacity(0.5);
feature->GetActor()->SetPosition(
kwLongitude, vtkMercator::lat2y(kwLatitude), 0.0);
circle->AddFeature(feature.GetPointer());
///////////////////// Layer 5 - Circle2 //////////////////////////////////////
vtkNew<vtkFeatureLayer> circle2;
circle2->SetName("circle2");
map->AddLayer(circle2.GetPointer());
// Note: Always add vtkFeatureLayer to the map before adding features
vtkNew<vtkRegularPolygonSource> polygon2;
polygon2->SetNumberOfSides(50);
polygon2->SetRadius(5.0);
vtkNew<vtkPolydataFeature> feature2;
feature2->GetMapper()->SetInputConnection(polygon2->GetOutputPort());
feature2->GetActor()->GetProperty()->SetColor(0.0, 1.0, 0.0);
feature2->GetActor()->GetProperty()->SetOpacity(0.5);
feature2->GetActor()->SetPosition(
-76.072205, vtkMercator::lat2y(38.911373), 0.0);
circle2->AddFeature(feature2.GetPointer());
//////////////////////// Manipulate layers ///////////////////////////////////
map->MoveLayer(circle.GetPointer(), vtkMapType::Move::DOWN);
map->MoveLayer(circle2.GetPointer(), vtkMapType::Move::BOTTOM);
map->MoveLayer(circle2.GetPointer(), vtkMapType::Move::UP);
markerSet2->SetMarkerShape(static_cast<int>(vtkMapType::Shape::SQUARE));
map->Draw();
markerSet2->SetMarkerShape(static_cast<int>(vtkMapType::Shape::PENTAGON));
moveCallback->Map = map.GetPointer();
moveCallback->Layer = circle.GetPointer();
intr->Start();
// global libcurl cleanup
curl_global_cleanup();
return 0;
}