forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkGeoJSONFeature.cxx
632 lines (538 loc) · 16.8 KB
/
vtkGeoJSONFeature.cxx
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
/*=========================================================================
Program: Visualization Toolkit
Module: vtkGeoJSONFeature.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkGeoJSONFeature.h"
// VTK Includes
#include "vtkAbstractArray.h"
#include "vtkBitArray.h"
#include "vtkCellArray.h"
#include "vtkCellData.h"
#include "vtkDoubleArray.h"
#include "vtkIntArray.h"
#include "vtkLine.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkPolygon.h"
#include "vtkPolyLine.h"
#include "vtkStringArray.h"
#include <sstream>
#include <string>
vtkStandardNewMacro(vtkGeoJSONFeature);
namespace
{
vtkOStreamWrapper& operator<<(vtkOStreamWrapper& os, const Json::Value& root)
{
Json::StreamWriterBuilder builder;
builder["commentStyle"] = "All";
builder["indentation"] = " ";
std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
writer->write(root, &os.GetOStream());
return os;
}
}
//----------------------------------------------------------------------------
vtkGeoJSONFeature::vtkGeoJSONFeature()
{
this->FeatureId = nullptr;
this->OutlinePolygons = false;
}
//----------------------------------------------------------------------------
vtkGeoJSONFeature::~vtkGeoJSONFeature()
{
free(this->FeatureId);
}
//----------------------------------------------------------------------------
bool vtkGeoJSONFeature::CreatePoint(const Json::Value& coordinates,
double point[3])
{
//Check if Coordinates corresponds to Point
if ( ! IsPoint( coordinates ) )
{
vtkErrorMacro(<< "Wrong data format for a point!");
return false;
}
//Do isDouble before asDouble to prevent inconsistency
//Probably check for float/int too
if ( coordinates.size() == 1 )
{
//Update the 3D Coordinates using the 1 Value in the array and rest of the 2 as 0
Json::Value x = coordinates[0];
point[0] = x.asDouble();
point[1] = 0;
point[2] = 0;
}
else if ( coordinates.size() == 2 )
{
//Update the 3D Coordinates using the 2 Values in the array and 3rd as 0
Json::Value x = coordinates[0];
Json::Value y = coordinates[1];
point[0] = x.asDouble();
point[1] = y.asDouble();
point[2] = 0;
}
else if ( coordinates.size() == 3 )
{
//Update the 3D Coordinates using the 3 Values in the array
Json::Value x = coordinates[0];
Json::Value y = coordinates[1];
Json::Value z = coordinates[2];
point[0] = x.asDouble();
point[1] = y.asDouble();
point[2] = z.asDouble();
}
//Return that we properly created the point
return true;
}
//----------------------------------------------------------------------------
vtkPolyData *vtkGeoJSONFeature::
ExtractPoint(const Json::Value& coordinates, vtkPolyData *outputData)
{
//Check if Coordinates corresponds to Single Point
if ( ! IsPoint( coordinates ) )
{
vtkErrorMacro (<< "Wrong data format for a point!");
return nullptr;
}
//Obtain point data from Json structure and add to outputData
double point[3]; CreatePoint(coordinates, point);
const int PID_SIZE = 1;
vtkIdType pid;
vtkPoints *points = outputData->GetPoints();
pid = points->InsertNextPoint( point );
vtkCellArray *verts = outputData->GetVerts();
verts->InsertNextCell(PID_SIZE, &pid);
vtkAbstractArray *array =
outputData->GetCellData()->GetAbstractArray("feature-id");
vtkStringArray *ids = vtkArrayDownCast<vtkStringArray>(array);
ids->InsertNextValue(this->FeatureId);
return outputData;
}
//----------------------------------------------------------------------------
vtkPolyData *vtkGeoJSONFeature::
ExtractMultiPoint(const Json::Value& coordinates, vtkPolyData *outputData)
{
//Check if Coordinates corresponds to Multi Points
if ( ! IsMultiPoint( coordinates ) )
{
vtkErrorMacro (<< "Wrong data format for a Multi Point!");
return nullptr;
}
if ( coordinates.isArray() )
{
vtkPoints *points = outputData->GetPoints();//Contain the locations of the points
vtkCellArray *verts = outputData->GetVerts();//Contain the indices corresponding to the position of the vertices
vtkAbstractArray *array =
outputData->GetCellData()->GetAbstractArray("feature-id");
vtkStringArray *ids = vtkArrayDownCast<vtkStringArray>(array);
const int PID_SIZE = coordinates.size();
vtkIdType* pids = new vtkIdType[ PID_SIZE ];
double point[3];
for (int i = 0; i < PID_SIZE; i++)
{
//Parse point from Json object to double array and add it to the points array
CreatePoint(coordinates, point);
pids[i] = points->InsertNextPoint(point);
}
//Update polyData vertices to store multiple points
verts->InsertNextCell(PID_SIZE, pids);
ids->InsertNextValue(this->FeatureId);
delete[] pids;
}
return outputData;
}
//----------------------------------------------------------------------------
vtkPolyData *vtkGeoJSONFeature::
ExtractLineString(const Json::Value& coordinates, vtkPolyData *outputData)
{
//Check if Coordinates corresponds to Line String
if (!IsLineString(coordinates))
{
vtkErrorMacro (<< "Wrong data format for a Line String!");
return nullptr;
}
vtkPoints *points = outputData->GetPoints();
vtkNew<vtkPolyLine> polyLine;
double xyz[3];
vtkIdList *pointIdList = polyLine->GetPointIds();
vtkIdType pointId;
for (Json::Value::ArrayIndex i = 0; i < coordinates.size(); i++)
{
this->CreatePoint(coordinates[i], xyz);
pointId = points->InsertNextPoint(xyz);
pointIdList->InsertNextId(pointId);
}
outputData->GetLines()->InsertNextCell(polyLine);
vtkAbstractArray *array =
outputData->GetCellData()->GetAbstractArray("feature-id");
vtkStringArray *ids = vtkArrayDownCast<vtkStringArray>(array);
ids->InsertNextValue(this->FeatureId);
return outputData;
}
//----------------------------------------------------------------------------
vtkPolyData *vtkGeoJSONFeature::
ExtractMultiLineString(const Json::Value& coordinateArray,
vtkPolyData *outputData)
{
//Check if Coordinate Array corresponds to Multi Line String
if ( ! IsMultiLineString( coordinateArray ) )
{
vtkErrorMacro(<< "Wrong data format for a Multi Line String!");
return nullptr;
}
for (Json::Value::ArrayIndex i = 0; i < coordinateArray.size(); i++)
{
this->ExtractLineString(coordinateArray[i], outputData);
}
return outputData;
}
//----------------------------------------------------------------------------
vtkPolyData *vtkGeoJSONFeature::
ExtractPolygon(const Json::Value& coordinate, vtkPolyData *outputData)
{
//Check if Coordinate Array corresponds to Polygon
if ( ! IsPolygon( coordinate ) )
{
vtkErrorMacro (<< "Wrong data format for a Polygon!");
return nullptr;
}
bool POLYGON_WITH_HOLES = coordinate.size() > 1 ? true : false;
vtkPoints *points = outputData->GetPoints();
vtkAbstractArray *array =
outputData->GetCellData()->GetAbstractArray("feature-id");
vtkStringArray *ids = vtkArrayDownCast<vtkStringArray>(array);
// Output is either vtkPolygon or vtkPolyLine,
// depending on OutputPolygons option.
vtkCellArray *polys = nullptr;
vtkCell *exteriorPoly = nullptr;
if (this->OutlinePolygons)
{
polys = outputData->GetLines();
exteriorPoly = vtkPolyLine::New();
}
else
{
polys = outputData->GetPolys();
exteriorPoly = vtkPolygon::New();
}
//For exterior Polygon
Json::Value exteriorPolygon = coordinate[0];
int EXTERIOR_POLYGON_VERTEX_COUNT = exteriorPolygon.size() - 1;
exteriorPoly->GetPointIds()->SetNumberOfIds(EXTERIOR_POLYGON_VERTEX_COUNT);
double point[3];
// Remember first point, if needed for polyline
this->CreatePoint(exteriorPolygon[0], point);
vtkIdType idPoint0 = points->InsertNextPoint(point);
exteriorPoly->GetPointIds()->SetId(0, idPoint0);
// Add points for rest of polygon
for (int i = 1; i < EXTERIOR_POLYGON_VERTEX_COUNT; i++)
{
this->CreatePoint(exteriorPolygon[i], point);
vtkIdType id = points->InsertNextPoint(point);
exteriorPoly->GetPointIds()->SetId(i, id);
}
// For outline mode, add first point to the end
if (this->OutlinePolygons)
{
exteriorPoly->GetPointIds()->InsertNextId(idPoint0);
}
polys->InsertNextCell(exteriorPoly);
ids->InsertNextValue(this->FeatureId);
exteriorPoly->Delete();
if ( ! POLYGON_WITH_HOLES )
return outputData;
// Todo Modify polydata to support polygon with holes
return outputData;
}
//----------------------------------------------------------------------------
vtkPolyData *vtkGeoJSONFeature::
ExtractMultiPolygon(const Json::Value& coordinateArray, vtkPolyData *outputData)
{
//Check if Coordinate Array corresponds to Multi Polygon
if ( ! IsMultiPolygon( coordinateArray ) )
{
vtkErrorMacro (<< "Wrong data format for a Multi Polygon!");
return nullptr;
}
for (Json::Value::ArrayIndex i = 0; i < coordinateArray.size(); i++)
{
//Extract polygon into different polyData and append into a common polyData using the appendPolyData Filter
this->ExtractPolygon(coordinateArray[i], outputData);
}
return outputData;
}
//----------------------------------------------------------------------------
void vtkGeoJSONFeature::
ExtractGeoJSONFeature(const Json::Value& root, vtkPolyData *outputData)
{
this->featureRoot = root;
// Check that type is Feature
Json::Value typeNode = root["type"];
if (typeNode.isNull() || "Feature" != typeNode.asString())
{
vtkErrorMacro (<< "Unknown type. \"Feature\" expected");
return;
}
// Check for geometry node
Json::Value geometryNode = root["geometry"];
if (geometryNode.isNull())
{
vtkErrorMacro (<< "Missing geometry node");
return;
}
// Check for properties node
Json::Value propertiesNode = root["properties"];
if (propertiesNode.isNull())
{
vtkErrorMacro (<< "Missing properties node");
return;
}
// Check for feature id
std::string featureString;
Json::Value idNode = root["id"];
// No Json::Value::toString() method, so homebrew one here
std::stringstream oss;
switch (idNode.type())
{
case Json::nullValue:
break;
case Json::intValue:
case Json::uintValue:
oss << idNode.asInt();
featureString = oss.str();
break;
case Json::realValue:
oss << idNode.asDouble();
featureString = oss.str();
break;
case Json::stringValue:
featureString = idNode.asString();
break;
default:
vtkWarningMacro(<< "Unsupported \"id\" type: " << idNode.type());
break;
}
this->FeatureId = strdup(featureString.c_str());
this->ExtractGeoJSONFeatureGeometry(geometryNode, outputData);
}
//----------------------------------------------------------------------------
void vtkGeoJSONFeature::
ExtractGeoJSONFeatureGeometry(const Json::Value& geometryRoot,
vtkPolyData *outputData)
{
// Check for geometry-type node
Json::Value geometryTypeNode = geometryRoot["type"];
if (geometryTypeNode.isNull())
{
vtkErrorMacro (<< "Missing geometry-type node");
return;
}
if (!geometryTypeNode.isString())
{
vtkErrorMacro(<< "Invalid geometry-type node");
return;
}
std::string typeString = geometryTypeNode.asString();
if (typeString == GeoJSON_GEOMETRY_COLLECTION)
{
//For GeometryCollection
Json::Value geometries = geometryRoot["geometries"];
for (Json::Value::ArrayIndex i = 0; i < geometries.size(); i++)
{
Json::Value child = geometries[i];
this->ExtractGeoJSONFeatureGeometry(child, outputData);
}
return;
}
// (else)
Json::Value coordinates = geometryRoot["coordinates"];
if (typeString == GeoJSON_POINT)
{
this->ExtractPoint(coordinates, outputData);
}
else if (typeString == GeoJSON_MULTI_POINT)
{
this->ExtractMultiPoint(coordinates, outputData);
}
else if (typeString == GeoJSON_LINE_STRING)
{
this->ExtractLineString(coordinates, outputData);
}
else if (typeString == GeoJSON_MULTI_LINE_STRING)
{
this->ExtractMultiLineString(coordinates, outputData);
}
else if (typeString == GeoJSON_POLYGON)
{
this->ExtractPolygon(coordinates, outputData);
}
else if (typeString == GeoJSON_MULTI_POLYGON)
{
this->ExtractMultiPolygon(coordinates, outputData);
}
else
{
vtkErrorMacro (<< "Unknown or unsupported geometry type " << geometryTypeNode);
}
}
//----------------------------------------------------------------------------
bool vtkGeoJSONFeature::IsLineString(const Json::Value& root)
{
if ( ! root.isArray() )
{
vtkErrorMacro (<<"Expected Arrays as input for point at " << root);
return false;
}
if ( root.size() < 1 )
{
vtkErrorMacro (<< "Expected at least 1 value at " << root);
return false;
}
for (Json::Value::ArrayIndex i = 0; i < root.size(); i++)
{
Json::Value child = root[i];
if ( ! IsPoint( child ) )
{
return false;
}
}
return true;
}
//----------------------------------------------------------------------------
bool vtkGeoJSONFeature::IsMultiLineString(const Json::Value& root)
{
if ( ! root.isArray() )
{
vtkErrorMacro (<< "Expected Array as input for point at " << root);
return false;
}
if ( root.size() < 1 )
{
vtkErrorMacro (<< "Expected at least 1 value at " << root);
return false;
}
for (Json::Value::ArrayIndex i = 0; i < root.size(); i++)
{
Json::Value child = root[i];
if ( ! IsLineString( child ) )
{
return false;
}
}
return true;
}
//----------------------------------------------------------------------------
bool vtkGeoJSONFeature::IsPoint(const Json::Value& root)
{
if ( ! root.isArray() )
{
vtkErrorMacro ("Expected Array as input for point at " << root);
return false;
}
if ( ! ( root.size() > 0 && root.size() < 4 ) )
{
vtkErrorMacro (<< "Expected 3 or less dimension values at " << root << " for point");
return false;
}
for (Json::Value::ArrayIndex i = 0; i < root.size(); i++)
{
Json::Value child = root[i];
if ( ! child.isNumeric() )
{
vtkErrorMacro (<<"Value not Numeric as expected at " << child);
return false;
}
}
return true;
}
//----------------------------------------------------------------------------
bool vtkGeoJSONFeature::IsMultiPoint(const Json::Value& root)
{
if( ! root.isArray() )
{
vtkErrorMacro (<< "Expected Array as input for multi point at " << root);
return false;
}
if( root.size() < 1 )
{
vtkErrorMacro (<< "Expected at least 1 value at " << root << " for multipoint");
return false;
}
for (Json::Value::ArrayIndex i = 0; i < root.size(); i++)
{
Json::Value child = root[i];
if ( ! IsPoint( child ) )
{
return false;
}
}
return true;
}
//----------------------------------------------------------------------------
bool vtkGeoJSONFeature::IsPolygon(const Json::Value& root)
{
if ( ! root.isArray() )
{
vtkErrorMacro (<< "Expected Array as input for polygon at " << root);
return false;
}
if ( root.size() < 1 )
{
vtkErrorMacro (<< "Expected at least 1 value at " << root << "for polygon");
return false;
}
for (Json::Value::ArrayIndex i = 0; i < root.size(); i++)
{
Json::Value child = root[i];
if( ! IsLineString( child ) )
{
return false;
}
//First and last element of child must be same to complete the polygon loop.
//Check not added.
}
return true;
}
//----------------------------------------------------------------------------
bool vtkGeoJSONFeature::IsMultiPolygon(const Json::Value& root)
{
if ( ! root.isArray() )
{
vtkErrorMacro (<< "Expected Array as input for multi polygon at " << root);
return false;
}
if ( root.size() < 1 )
{
vtkErrorMacro (<< "Expected at least 1 value at " << root << " for multi polygon");
return false;
}
for (Json::Value::ArrayIndex i = 0; i < root.size(); i++)
{
Json::Value child = root[i];
if( ! IsPolygon( child ) )
{
return false;
}
}
return true;
}
//----------------------------------------------------------------------------
void vtkGeoJSONFeature::PrintSelf(ostream &os, vtkIndent indent)
{
Superclass::PrintSelf(os, indent);
os << indent << "vtkGeoJSONFeature" << std::endl;
os << indent << "Root: ";
Json::StreamWriterBuilder builder;
builder["commentStyle"] = "All";
builder["indentation"] = " ";
std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
writer->write(this->featureRoot, &os);
}