forked from jatin3893/vtkDataSetReaders
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvtkGeoJSONReader.cxx
More file actions
150 lines (123 loc) · 4.39 KB
/
vtkGeoJSONReader.cxx
File metadata and controls
150 lines (123 loc) · 4.39 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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkGeoJSONReader.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 "vtkGeoJSONReader.h"
// VTK Includes
#include "vtkPolyData.h"
#include "vtkStdString.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkTriangleFilter.h"
#include "vtkCellArray.h"
#include "vtkGeoJSONFeature.h"
// C++ includes
#include <fstream>
#include <iostream>
vtkStandardNewMacro(vtkGeoJSONReader);
//----------------------------------------------------------------------------
vtkGeoJSONReader::vtkGeoJSONReader()
{
this->FileName = NULL;
this->SetNumberOfInputPorts(0);
this->SetNumberOfOutputPorts(1);
}
//----------------------------------------------------------------------------
vtkGeoJSONReader::~vtkGeoJSONReader()
{
delete[] FileName;
}
//----------------------------------------------------------------------------
int vtkGeoJSONReader::CanParse(const char *filename, Json::Value &root)
{
ifstream file;
file.open( filename );
if ( ! file.is_open() )
{
vtkErrorMacro(<< "Unable to Open File " << this->FileName);
return VTK_ERROR;
}
Json::Reader reader;
//parse the entire geoJSON data into the Json::Value root
bool parsedSuccess = reader.parse(file, root, false);
if ( ! parsedSuccess )
{
// Report failures and their locations in the document
vtkErrorMacro(<<"Failed to parse JSON" << endl << reader.getFormatedErrorMessages());
return VTK_ERROR;
}
return VTK_OK;
}
//----------------------------------------------------------------------------
int vtkGeoJSONReader::RequestData(vtkInformation* vtkNotUsed(request),
vtkInformationVector** vtkNotUsed(request),
vtkInformationVector* outputVector)
{
// Get the info object
vtkInformation* outInfo = outputVector->GetInformationObject(0);
// Get the ouptut
vtkPolyData* output = vtkPolyData::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()));
Json::Value root;
if ( CanParse(this->FileName, root) == VTK_ERROR )
{
return VTK_ERROR;
}
// If parsed successfully into Json parser Values and Arrays, then convert it
// into appropriate vtkPolyData
if ( root.isObject() )
{
ParseRoot(root, output);
//Convert Concave Polygons to convex polygons using triangulation
vtkTriangleFilter *filter = vtkTriangleFilter::New();
filter->SetInputData(output);
filter->Update();
output->ShallowCopy(filter->GetOutput());
}
return VTK_OK;
}
//----------------------------------------------------------------------------
void vtkGeoJSONReader::ParseRoot(Json::Value root, vtkPolyData *output)
{
Json::Value rootType = root.get( "type", -1 );
Json::Value rootFeatures = root.get( "features", -1 );
if ( rootFeatures == -1 )
{
vtkErrorMacro (<<"Parse Root :: Features :: -1");
return;
}
output->SetPoints( vtkPoints::New() );//Initialising containers for points,
output->SetVerts( vtkCellArray::New() );//Vertices,
output->SetLines( vtkCellArray::New() );//Lines and
output->SetPolys( vtkCellArray::New() );//Polygons
if ( rootFeatures.isArray() )
{
// If it is a collection of features
for (int i = 0; i < rootFeatures.size(); i++)
{
// Append extracted geometry to existing outputData
Json::Value child = rootFeatures[i];
vtkGeoJSONFeature *feature = vtkGeoJSONFeature::New();
feature->ExtractGeoJSONFeature(child, output);
}
}
else
{
// Single feature in the geoJSON data
vtkGeoJSONFeature *feature = vtkGeoJSONFeature::New();
feature->ExtractGeoJSONFeature(rootFeatures, output);
}
}
//----------------------------------------------------------------------------
void vtkGeoJSONReader::PrintSelf(ostream &os, vtkIndent indent)
{
Superclass::PrintSelf(os, indent);
os << "vtkGeoJSONReader" << std::endl;
os << "Filename: " << this->FileName << std::endl;
}