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
|
/*=========================================================================
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 <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 srcMode, int appendFeatures )
{
this->Source = OGRSFDriverRegistrar::Open( srcName, srcMode, &this->Driver );
if ( ! this->Source )
{
this->LastError = CPLGetLastErrorMsg();
}
else
{
this->LastError = 0;
}
this->LayerIdx = 0;
this->AppendFeatures = appendFeatures;
}
~Internal()
{
if ( this->Source )
{
OGRDataSource::DestroyDataSource( this->Source );
}
}
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();
}
*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();
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 = vtkIntArray::SafeDownCast( fields[f] );
ival = feat->GetFieldAsInteger( f );
for ( i = 0; i < nPoly; ++i )
{
iarr->InsertNextValue( ival );
}
break;
case OFTReal:
rarr = vtkDoubleArray::SafeDownCast( fields[f] );
rval = feat->GetFieldAsDouble( f );
for ( i = 0; i < nPoly; ++i )
{
rarr->InsertNextValue( rval );
}
break;
case OFTString:
default:
sarr = vtkStringArray::SafeDownCast( fields[f] );
sval = feat->GetFieldAsString( f );
for ( i = 0; i < nPoly; ++i )
{
sarr->InsertNextValue( sval );
}
}
}
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;
break;
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;
break;
}
return nCells;
}
OGRDataSource* Source;
OGRSFDriver* Driver;
const char* LastError;
int LayerIdx;
int AppendFeatures;
};
// -----------------------------------------------------------------------------
vtkGDALVectorReader::vtkGDALVectorReader()
{
this->FileName = 0;
this->Implementation = 0;
this->SetNumberOfInputPorts( 0 );
if ( ! vtkGDALVectorReader::OGRRegistered )
{
vtkGDALVectorReader::OGRRegistered = 1;
OGRRegisterAll();
}
this->AppendFeatures = 0;
}
// -----------------------------------------------------------------------------
vtkGDALVectorReader::~vtkGDALVectorReader()
{
this->SetFileName( 0 );
if ( this->Implementation )
{
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";
}
// -----------------------------------------------------------------------------
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->GetLayerType(ActiveLayer);
}
// -----------------------------------------------------------------------------
int vtkGDALVectorReader::GetActiveLayerFeatureCount()
{
return this->GetFeatureCount(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 NULL;
}
}
// -----------------------------------------------------------------------------
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;
for ( int layerIdx = 0; layerIdx < p->Source->GetLayerCount(); ++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);
OGRFree(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, 0 , this->AppendFeatures );
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;
}
|