forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkGDALVectorReader.cxx
693 lines (620 loc) · 17.9 KB
/
vtkGDALVectorReader.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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
/*=========================================================================
Program: Visualization Toolkit
Module: vtkPNGReader.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 "vtkGDALVectorReader.h"
// VTK includes
#include "vtkCellData.h"
#include "vtkCellType.h"
#include "vtkDoubleArray.h"
#include "vtkCellArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkIntArray.h"
#include "vtkMultiBlockDataSet.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkStringArray.h"
#include "vtkPolyData.h"
// GDAL OGR includes
#include <ogrsf_frmts.h>
// C++ includes
#include <vector> // Requires STL vector
vtkStandardNewMacro(vtkGDALVectorReader);
int vtkGDALVectorReader::OGRRegistered = 0;
// -----------------------------------------------------------------------------
class vtkGDALVectorReader::Internal
{
public:
Internal( const char* srcName, int appendFeatures, int addFeatIds )
{
#if GDAL_VERSION_MAJOR < 2
OGRSFDriver* driver;
this->Source = OGRSFDriverRegistrar::Open( srcName, 0, &driver );
#else
GDALAllRegister();
this->Source = static_cast<GDALDataset*>(
GDALOpenEx(srcName, GDAL_OF_VECTOR, nullptr, nullptr, nullptr));
#endif
if ( ! this->Source )
{
this->LastError = CPLGetLastErrorMsg();
}
else
{
this->LastError = 0;
}
this->LayerIdx = 0;
this->AppendFeatures = appendFeatures;
this->AddFeatureIds = addFeatIds;
}
~Internal()
{
if ( this->Source )
{
#if GDAL_VERSION_MAJOR < 2
OGRDataSource::DestroyDataSource( this->Source );
#else
GDALClose(this->Source);
#endif
}
}
void SetupPolyData( vtkPolyData **pd, vtkCellArray **lines,
vtkCellArray **verts, std::vector<vtkAbstractArray*> *fields,
int numFields, OGRFeatureDefn* fdef )
{
*pd = vtkPolyData::New();
vtkAbstractArray* arr;
int f;
for ( f = 0; f < numFields; ++f )
{
OGRFieldDefn* ffdef = fdef->GetFieldDefn( f );
switch ( ffdef->GetType() )
{
case OFTInteger:
arr = vtkIntArray::New();
break;
case OFTReal:
arr = vtkDoubleArray::New();
break;
case OFTString:
default: // When in doubt, it's a string!
arr = vtkStringArray::New();
break;
}
arr->SetName( ffdef->GetNameRef() );
fields->push_back( arr );
(*pd)->GetCellData()->AddArray( arr );
arr->FastDelete();
}
if (this->AddFeatureIds)
{
vtkNew<vtkIdTypeArray> featIds;
featIds->SetName("_vtkPedigreeIds");
(*pd)->GetCellData()->SetPedigreeIds(featIds);
fields->push_back(featIds);
}
*lines = vtkCellArray::New();
*verts = vtkCellArray::New();
(*pd)->SetLines(*lines);
(*pd)->SetVerts(*verts);
(*lines)->FastDelete();
(*verts)->FastDelete();
}
bool ReadLayer( OGRLayer* layer, vtkMultiBlockDataSet* mbds )
{
OGRFeature* feat;
vtkPolyData *pd = 0;
vtkIdType nTotPoly = 0;
vtkCellArray *lines = 0, *verts = 0;
OGRFeatureDefn* fdef = layer->GetLayerDefn();
int numFields = fdef->GetFieldCount();
std::vector<vtkAbstractArray*> fields;
if ( this->AppendFeatures )
{
this->SetupPolyData(&pd, &lines, &verts, &fields, numFields, fdef);
}
while ( ( feat = layer->GetNextFeature() ) )
{
if ( !this->AppendFeatures )
{
fields.clear();
this->SetupPolyData(&pd, &lines, &verts, &fields, numFields, fdef);
mbds->SetBlock( this->LayerIdx, pd );
++this->LayerIdx;
pd->FastDelete();
nTotPoly = 0;
}
vtkPoints* pts = pd->GetPoints();
if ( ! pts )
{
pts = vtkPoints::New();
pts->SetDataTypeToDouble();
pd->SetPoints( pts );
pts->FastDelete();
}
// Insert points and lines to represent the geometry of each feature.
OGRGeometry* geom = feat->GetGeometryRef();
vtkIdType nPoly = this->insertGeometryRecursive( geom, pd, pts, lines, verts );
if ( ! nPoly )
{
continue;
}
nTotPoly += nPoly;
// Now insert the field values for this geometry once for each cell created
// (We have to copy the values when there are multiple polygons or polygons
// with inner rings.)
vtkIdType i;
int ival;
double rval;
const char* sval;
vtkIntArray* iarr;
vtkDoubleArray* rarr;
vtkStringArray* sarr;
for ( int f = 0; f < numFields; ++f )
{
OGRFieldDefn* ffdef = fdef->GetFieldDefn( f );
switch ( ffdef->GetType() )
{
case OFTInteger:
iarr = vtkArrayDownCast<vtkIntArray>( fields[f] );
ival = feat->GetFieldAsInteger( f );
for ( i = 0; i < nPoly; ++i )
{
iarr->InsertNextValue( ival );
}
break;
case OFTReal:
rarr = vtkArrayDownCast<vtkDoubleArray>( fields[f] );
rval = feat->GetFieldAsDouble( f );
for ( i = 0; i < nPoly; ++i )
{
rarr->InsertNextValue( rval );
}
break;
case OFTString:
default:
sarr = vtkArrayDownCast<vtkStringArray>( fields[f] );
sval = feat->GetFieldAsString( f );
for ( i = 0; i < nPoly; ++i )
{
sarr->InsertNextValue( sval );
}
}
}
if (this->AddFeatureIds)
{
vtkIdTypeArray* idarr = vtkArrayDownCast<vtkIdTypeArray>(fields[numFields]);
for ( i = 0; i < nPoly; ++i )
{
idarr->InsertNextValue(feat->GetFID());
}
}
OGRFeature::DestroyFeature(feat);
}
if ( this->AppendFeatures )
{
mbds->SetBlock( this->LayerIdx, pd );
++this->LayerIdx;
pd->FastDelete();
}
return nTotPoly ? true : false;
}
vtkIdType insertGeometryRecursive( OGRGeometry* geom, vtkPolyData* pd,
vtkPoints* pts, vtkCellArray* lines, vtkCellArray* verts )
{
if ( ! geom )
{
return 0;
}
std::vector<vtkIdType> ptIds;
OGRPoint* gpt;
OGRLineString* gls;
OGRLinearRing* grng;
OGRPolygon* gpl;
OGRGeometryCollection* gcol;
//OGRMultiPolygon* gmpl;
//OGRMultiLineString* gmls;
//OGRMultiPoint* gmpt;
int num;
vtkIdType i;
vtkIdType nCells = 0;
switch ( geom->getGeometryType() )
{
case wkbUnknown:
return 0;
case wkbPoint:
case wkbPoint25D:
gpt = (OGRPoint*) geom;
ptIds.push_back( pts->InsertNextPoint( gpt->getX(), gpt->getY(), gpt->getZ() ) );
verts->InsertNextCell(1, &(ptIds[0]));
++nCells;
break;
case wkbLinearRing: // OGR docs imply that wkbLinearRing may not inherit wkbLineString in the future.
case wkbLineString:
case wkbLineString25D:
gls = (OGRLineString*) geom;
for ( int p = 0; p < gls->getNumPoints(); ++p )
{
// insert ring points
ptIds.push_back( pts->InsertNextPoint(
gls->getX( p ), gls->getY( p ), gls->getZ( p ) ) );
}
// insert ring line segments
lines->InsertNextCell( (int) ptIds.size(), &(ptIds[0]) );
++nCells;
break;
case wkbPolygon:
case wkbPolygon25D:
gpl = (OGRPolygon*) geom;
grng = gpl->getExteriorRing();
nCells += this->insertGeometryRecursive( grng, pd, pts, lines, verts );
num = gpl->getNumInteriorRings();
for ( i = 0 ; i < num; ++i )
{
grng = gpl->getInteriorRing( i );
nCells += this->insertGeometryRecursive( grng, pd, pts, lines, verts );
}
break;
case wkbMultiPoint:
case wkbMultiPoint25D:
case wkbMultiLineString:
case wkbMultiLineString25D:
case wkbMultiPolygon:
case wkbMultiPolygon25D:
case wkbGeometryCollection:
case wkbGeometryCollection25D:
gcol = (OGRGeometryCollection*) geom;
num = gcol->getNumGeometries();
for ( i = 0 ; i < num; ++i )
{
nCells += this->insertGeometryRecursive(
gcol->getGeometryRef( i ), pd, pts, lines, verts );
}
break;
case wkbNone:
return 0;
#if GDAL_VERSION_MAJOR >= 2
case wkbCircularString:
case wkbCircularStringZ:
case wkbCompoundCurve:
case wkbCompoundCurveZ:
case wkbCurvePolygon:
case wkbCurvePolygonZ:
case wkbMultiCurve:
case wkbMultiCurveZ:
case wkbMultiSurface:
case wkbMultiSurfaceZ:
return 0;
#endif
#if GDAL_VERSION_MAJOR >= 3 || \
(GDAL_VERSION_MAJOR == 2 && GDAL_VERSION_MINOR > 0)
case wkbCircularStringM:
case wkbCircularStringZM:
case wkbCompoundCurveM:
case wkbCompoundCurveZM:
case wkbCurve:
case wkbCurveM:
case wkbCurvePolygonM:
case wkbCurvePolygonZM:
case wkbCurveZ:
case wkbCurveZM:
case wkbGeometryCollectionM:
case wkbGeometryCollectionZM:
case wkbLineStringM:
case wkbLineStringZM:
case wkbMultiCurveM:
case wkbMultiCurveZM:
case wkbMultiLineStringM:
case wkbMultiLineStringZM:
case wkbMultiPointM:
case wkbMultiPointZM:
case wkbMultiPolygonM:
case wkbMultiPolygonZM:
case wkbMultiSurfaceM:
case wkbMultiSurfaceZM:
case wkbPointM:
case wkbPointZM:
case wkbPolygonM:
case wkbPolygonZM:
case wkbPolyhedralSurface:
case wkbPolyhedralSurfaceM:
case wkbPolyhedralSurfaceZ:
case wkbPolyhedralSurfaceZM:
case wkbSurface:
case wkbSurfaceM:
case wkbSurfaceZ:
case wkbSurfaceZM:
case wkbTIN:
case wkbTINM:
case wkbTINZ:
case wkbTINZM:
case wkbTriangle:
case wkbTriangleM:
case wkbTriangleZ:
case wkbTriangleZM:
return 0;
#endif
}
return nCells;
}
#if GDAL_VERSION_MAJOR < 2
OGRDataSource* Source;
#else
GDALDataset* Source;
#endif
const char* LastError;
int LayerIdx;
int AppendFeatures;
int AddFeatureIds;
};
// -----------------------------------------------------------------------------
vtkGDALVectorReader::vtkGDALVectorReader()
{
this->FileName = 0;
this->Implementation = 0;
this->ActiveLayer = -1;
this->SetNumberOfInputPorts( 0 );
if ( ! vtkGDALVectorReader::OGRRegistered )
{
vtkGDALVectorReader::OGRRegistered = 1;
OGRRegisterAll();
}
this->AppendFeatures = 0;
this->AddFeatureIds = 0;
}
// -----------------------------------------------------------------------------
vtkGDALVectorReader::~vtkGDALVectorReader()
{
this->SetFileName( 0 );
delete this->Implementation;
}
// -----------------------------------------------------------------------------
void vtkGDALVectorReader::PrintSelf( ostream& os, vtkIndent indent )
{
this->Superclass::PrintSelf( os, indent );
os << indent << "FileName: " << ( this->FileName ? this->FileName : "(null)" ) << "\n";
os << indent << "Implementation: " << this->Implementation << "\n";
os << indent << "AppendFeatures: " << (this->AppendFeatures ? "ON" : "OFF") << "\n";
os << indent << "AddFeatureIds: " << (this->AddFeatureIds ? "ON" : "OFF") << "\n";
}
// -----------------------------------------------------------------------------
int vtkGDALVectorReader::GetNumberOfLayers()
{
if ( this->InitializeInternal() == VTK_ERROR )
{
return -1;
}
return this->Implementation->Source->GetLayerCount();
}
// -----------------------------------------------------------------------------
int vtkGDALVectorReader::GetLayerType(int layerIndex)
{
if ( this->InitializeInternal() == VTK_ERROR )
{
return -1;
}
OGRLayer* layer = this->Implementation->Source->GetLayer( layerIndex );
if ( !layer )
{
return -1;
}
switch ( layer->GetGeomType() )
{
case wkbUnknown:
return VTK_EMPTY_CELL;
case wkbPoint:
case wkbPoint25D:
return VTK_VERTEX;
case wkbLinearRing: // OGR docs imply that wkbLinearRing may not inherit wkbLineString in the future.
case wkbLineString:
case wkbLineString25D:
return VTK_LINE;
case wkbPolygon:
case wkbPolygon25D:
return VTK_POLYGON;
case wkbMultiPoint:
case wkbMultiPoint25D:
return VTK_POLY_VERTEX;
case wkbMultiLineString:
case wkbMultiLineString25D:
return VTK_POLY_LINE;
case wkbMultiPolygon:
case wkbMultiPolygon25D:
return VTK_POLYGON;
case wkbGeometryCollection:
case wkbGeometryCollection25D:
return VTK_NUMBER_OF_CELL_TYPES;
case wkbNone:
return -1;
default:
return -1;
}
}
// -----------------------------------------------------------------------------
int vtkGDALVectorReader::GetFeatureCount(int layerIndex)
{
if ( this->InitializeInternal() == VTK_ERROR )
{
return -1;
}
OGRLayer* layer = this->Implementation->Source->GetLayer( layerIndex );
if ( !layer )
{
return -1;
}
return layer->GetFeatureCount();
}
// -----------------------------------------------------------------------------
int vtkGDALVectorReader::GetActiveLayerType()
{
return
this->ActiveLayer < 0 || this->ActiveLayer >= this->GetNumberOfLayers() ?
-1 : this->GetLayerType(this->ActiveLayer);
}
// -----------------------------------------------------------------------------
int vtkGDALVectorReader::GetActiveLayerFeatureCount()
{
return
this->ActiveLayer < 0 || this->ActiveLayer >= this->GetNumberOfLayers() ?
0 : this->GetFeatureCount(this->ActiveLayer);
}
// -----------------------------------------------------------------------------
const char* vtkGDALVectorReader::GetLayerProjection(int layerIndex)
{
if ( layerIndex < 0 )
{
vtkErrorMacro( << "Layer index cannot be negative");
}
std::map<int, std::string>::const_iterator itr =
this->LayersProjection.find (layerIndex );
if ( itr != this->LayersProjection.end() )
{
return itr->second.c_str();
}
else
{
return nullptr;
}
}
// -----------------------------------------------------------------------------
const char* vtkGDALVectorReader::GetLayerProjectionAsProj4(int layerIndex)
{
if ( layerIndex < 0 )
{
vtkErrorMacro(<< "Layer index cannot be negative");
return nullptr;
}
vtkGDALVectorReader::Internal* p = this->Implementation;
if (!p->Source)
{
vtkErrorMacro(<< "Source dataset not provided");
return nullptr;
}
int layerCount = p->Source->GetLayerCount();
if ( layerIndex >= layerCount)
{
vtkErrorMacro(<< "Layer index " << layerIndex <<
" exceeds number of layers in dataset: " << layerCount);
return nullptr;
}
OGRLayer* layer = p->Source->GetLayer(layerIndex);
if (!layer)
{
vtkErrorMacro(<< "Cannot access the GDAL dataset layer at index " <<
layerIndex);
return nullptr;
}
if (!layer->GetSpatialRef())
{
vtkErrorMacro(<< "Cannot access the spatial ref object.");
return nullptr;
}
char *projStr;
layer->GetSpatialRef()->exportToProj4(&projStr);
char* returnStr = new char[strlen(projStr) + 1];
strcpy(returnStr, projStr);
CPLFree(projStr);
return returnStr;
}
// -----------------------------------------------------------------------------
std::map<int, std::string> vtkGDALVectorReader::GetLayersProjection()
{
return this->LayersProjection;
}
// -----------------------------------------------------------------------------
int vtkGDALVectorReader::RequestInformation( vtkInformation* request,
vtkInformationVector** inputVector, vtkInformationVector* outputVector )
{
(void)request;
(void)inputVector;
(void)outputVector;
return 1;
}
// -----------------------------------------------------------------------------
int vtkGDALVectorReader::RequestData( vtkInformation* request,
vtkInformationVector** inputVector, vtkInformationVector* outputVector )
{
(void)request;
(void)inputVector;
if ( ! this->FileName )
{
//vtkWarningMacro( "No filename specified" );
return 0;
}
vtkMultiBlockDataSet* mbds = 0;
vtkInformation* oi = outputVector->GetInformationObject( 0 );
if ( ! oi )
{
return 0;
}
mbds = vtkMultiBlockDataSet::SafeDownCast( oi->Get( vtkDataObject::DATA_OBJECT() ) );
if ( ! mbds )
{
return 0;
}
// Deleting this->Implementation is required in order to force re-reading each
// time RequestData() is executed.
delete this->Implementation;
this->Implementation = 0;
if (this->InitializeInternal() == VTK_ERROR)
{
return 1;
}
vtkGDALVectorReader::Internal* p = this->Implementation;
int lastLayer = p->Source->GetLayerCount() - 1;
int startLayer =
this->ActiveLayer < 0 || this->ActiveLayer >= lastLayer ?
0 : this->ActiveLayer;
int endLayer = this->ActiveLayer < 0 || this->ActiveLayer >= lastLayer ?
lastLayer : this->ActiveLayer;
for ( int layerIdx = startLayer; layerIdx <= endLayer; ++layerIdx )
{
OGRLayer* layer = p->Source->GetLayer( layerIdx );
if ( ! layer )
{
continue;
}
if(layer->GetSpatialRef())
{
char *projStr;
layer->GetSpatialRef()->exportToWkt(&projStr);
this->LayersProjection[layerIdx] = std::string(projStr);
CPLFree(projStr);
}
p->ReadLayer( layer, mbds );
}
return 1;
}
// -----------------------------------------------------------------------------
int vtkGDALVectorReader::InitializeInternal()
{
if ( !this->FileName )
{
vtkErrorMacro(<< "FileName not set or empty:");
return VTK_ERROR;
}
if ( !this->Implementation )
{
this->Implementation = new vtkGDALVectorReader::Internal(
this->FileName,
this->AppendFeatures, this->AddFeatureIds );
if ( ! this->Implementation || this->Implementation->LastError )
{
if ( this->Implementation )
{
vtkErrorMacro( << this->Implementation->LastError );
delete this->Implementation;
this->Implementation = 0;
}
return VTK_ERROR;
}
}
return VTK_OK;
}