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
|
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkGeoJSONReader.h"
// VTK Includes
#include "vtkAbstractArray.h"
#include "vtkBitArray.h"
#include "vtkCellArray.h"
#include "vtkCellData.h"
#include "vtkDoubleArray.h"
#include "vtkGeoJSONFeature.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkIntArray.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkPolyData.h"
#include "vtkStringArray.h"
#include "vtkTriangleFilter.h"
#include "vtk_jsoncpp.h"
#include "vtksys/FStream.hxx"
// C++ includes
#include <fstream>
#include <iostream>
#include <sstream>
VTK_ABI_NAMESPACE_BEGIN
vtkStandardNewMacro(vtkGeoJSONReader);
//------------------------------------------------------------------------------
class vtkGeoJSONReader::GeoJSONReaderInternal
{
public:
struct GeoJSONProperty_t
{
std::string Name;
vtkVariant Value;
};
using GeoJSONProperty = struct GeoJSONProperty_t;
// List of property names to read. Property value is used the default
std::vector<GeoJSONProperty> PropertySpecs;
// Parse the Json Value corresponding to the root of the geoJSON data from the file
void ParseRoot(const Json::Value& root, vtkPolyData* output, bool outlinePolygons,
const char* serializedPropertiesArrayName);
// Verify if file exists and can be read by the parser
// If exists, parse into Jsoncpp data structure
int CanParseFile(const char* filename, Json::Value& root);
// Verify if string can be read by the parser
// If exists, parse into Jsoncpp data structure
int CanParseString(char* input, Json::Value& root);
// Extract property values from json node
void ParseFeatureProperties(const Json::Value& propertiesNode,
std::vector<GeoJSONProperty>& properties, const char* serializedPropertiesArrayName);
void InsertFeatureProperties(
vtkPolyData* polyData, const std::vector<GeoJSONProperty>& featureProperties);
};
//------------------------------------------------------------------------------
void vtkGeoJSONReader::GeoJSONReaderInternal::ParseRoot(const Json::Value& root,
vtkPolyData* output, bool outlinePolygons, const char* serializedPropertiesArrayName)
{
// Initialize geometry containers
vtkNew<vtkPoints> points;
points->SetDataTypeToDouble();
output->SetPoints(points);
vtkNew<vtkCellArray> verts;
output->SetVerts(verts);
vtkNew<vtkCellArray> lines;
output->SetLines(lines);
vtkNew<vtkCellArray> polys;
output->SetPolys(polys);
// Initialize feature-id array
vtkStringArray* featureIdArray = vtkStringArray::New();
featureIdArray->SetName("feature-id");
output->GetCellData()->AddArray(featureIdArray);
featureIdArray->Delete();
// Initialize properties arrays
if (serializedPropertiesArrayName)
{
vtkStringArray* propertiesArray = vtkStringArray::New();
propertiesArray->SetName(serializedPropertiesArrayName);
output->GetCellData()->AddArray(propertiesArray);
propertiesArray->Delete();
}
vtkAbstractArray* array;
std::vector<GeoJSONProperty>::iterator iter = this->PropertySpecs.begin();
for (; iter != this->PropertySpecs.end(); ++iter)
{
array = nullptr;
switch (iter->Value.GetType())
{
case VTK_BIT:
array = vtkBitArray::New();
break;
case VTK_INT:
array = vtkIntArray::New();
break;
case VTK_DOUBLE:
array = vtkDoubleArray::New();
break;
case VTK_STRING:
array = vtkStringArray::New();
break;
default:
vtkGenericWarningMacro("unexpected data type " << iter->Value.GetType());
break;
}
// Skip if array not created for some reason
if (!array)
{
continue;
}
array->SetName(iter->Name.c_str());
output->GetCellData()->AddArray(array);
array->Delete();
}
// Check type
Json::Value const& rootType = root["type"];
if (rootType.isNull())
{
vtkGenericWarningMacro(<< "ParseRoot: Missing type node");
return;
}
// Parse features
Json::Value rootFeatures;
std::string strRootType = rootType.asString();
std::vector<GeoJSONProperty> properties;
if ("FeatureCollection" == strRootType)
{
rootFeatures = root["features"];
if (rootFeatures.isNull())
{
vtkGenericWarningMacro(<< "ParseRoot: Missing \"features\" node");
return;
}
if (!rootFeatures.isArray())
{
vtkGenericWarningMacro(<< "ParseRoot: features node is not an array");
return;
}
GeoJSONProperty property;
for (Json::Value::ArrayIndex i = 0; i < rootFeatures.size(); i++)
{
// Append extracted geometry to existing outputData
Json::Value featureNode = rootFeatures[i];
Json::Value propertiesNode = featureNode["properties"];
this->ParseFeatureProperties(propertiesNode, properties, serializedPropertiesArrayName);
vtkNew<vtkGeoJSONFeature> feature;
feature->SetOutlinePolygons(outlinePolygons);
feature->ExtractGeoJSONFeature(featureNode, output);
this->InsertFeatureProperties(output, properties);
}
}
else if ("Feature" == strRootType)
{
// Process single feature
this->ParseFeatureProperties(root, properties, serializedPropertiesArrayName);
vtkNew<vtkGeoJSONFeature> feature;
feature->SetOutlinePolygons(outlinePolygons);
// Next call adds (exactly) one cell to the polydata
feature->ExtractGeoJSONFeature(root, output);
// Next call adds (exactly) one tuple to the polydata's cell data
this->InsertFeatureProperties(output, properties);
}
else
{
vtkGenericWarningMacro(<< "ParseRoot: do not support root type \"" << strRootType << "\"");
}
}
//------------------------------------------------------------------------------
int vtkGeoJSONReader::GeoJSONReaderInternal::CanParseFile(const char* filename, Json::Value& root)
{
if (!filename)
{
vtkGenericWarningMacro(<< "Input filename not specified");
return VTK_ERROR;
}
vtksys::ifstream file;
file.open(filename);
if (!file.is_open())
{
vtkGenericWarningMacro(<< "Unable to Open File " << filename);
return VTK_ERROR;
}
Json::CharReaderBuilder builder;
builder["collectComments"] = false;
std::string formattedErrors;
// parse the entire geoJSON data into the Json::Value root
bool parsedSuccess = parseFromStream(builder, file, &root, &formattedErrors);
if (!parsedSuccess)
{
// Report failures and their locations in the document
vtkGenericWarningMacro(<< "Failed to parse JSON" << endl << formattedErrors);
return VTK_ERROR;
}
return VTK_OK;
}
//------------------------------------------------------------------------------
int vtkGeoJSONReader::GeoJSONReaderInternal::CanParseString(char* input, Json::Value& root)
{
if (!input)
{
vtkGenericWarningMacro(<< "Input string is empty");
return VTK_ERROR;
}
Json::CharReaderBuilder builder;
builder["collectComments"] = false;
std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
std::string formattedErrors;
// parse the entire geoJSON data into the Json::Value root
bool parsedSuccess = reader->parse(input, input + strlen(input), &root, &formattedErrors);
if (!parsedSuccess)
{
// Report failures and their locations in the document
vtkGenericWarningMacro(<< "Failed to parse JSON" << endl << formattedErrors);
return VTK_ERROR;
}
return VTK_OK;
}
//------------------------------------------------------------------------------
void vtkGeoJSONReader::GeoJSONReaderInternal::ParseFeatureProperties(
const Json::Value& propertiesNode, std::vector<GeoJSONProperty>& featureProperties,
const char* serializedPropertiesArrayName)
{
featureProperties.clear();
GeoJSONProperty spec;
GeoJSONProperty property;
std::vector<GeoJSONProperty>::iterator iter = this->PropertySpecs.begin();
for (; iter != this->PropertySpecs.end(); ++iter)
{
spec = *iter;
property.Name = spec.Name;
Json::Value const& propertyNode = propertiesNode[spec.Name];
if (propertyNode.isNull())
{
property.Value = spec.Value;
featureProperties.push_back(property);
continue;
}
// (else)
switch (spec.Value.GetType())
{
case VTK_BIT:
property.Value = vtkVariant(propertyNode.asBool());
break;
case VTK_DOUBLE:
property.Value = vtkVariant(propertyNode.asDouble());
break;
case VTK_INT:
property.Value = vtkVariant(propertyNode.asInt());
break;
case VTK_STRING:
property.Value = vtkVariant(propertyNode.asString());
break;
}
featureProperties.push_back(property);
}
// Add GeoJSON string if enabled
if (serializedPropertiesArrayName)
{
property.Name = serializedPropertiesArrayName;
Json::StreamWriterBuilder builder;
builder["commentStyle"] = "None";
builder["indentation"] = "";
std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
std::stringstream stream;
writer->write(propertiesNode, &stream);
std::string propString = stream.str();
if (!propString.empty() && *propString.rbegin() == '\n')
{
propString.resize(propString.size() - 1);
}
property.Value = vtkVariant(propString);
featureProperties.push_back(property);
}
}
//------------------------------------------------------------------------------
void vtkGeoJSONReader::GeoJSONReaderInternal::InsertFeatureProperties(
vtkPolyData* polyData, const std::vector<GeoJSONProperty>& featureProperties)
{
std::vector<GeoJSONProperty>::const_iterator iter = featureProperties.begin();
for (; iter != featureProperties.end(); ++iter)
{
std::string name = iter->Name;
vtkVariant value = iter->Value;
vtkAbstractArray* array = polyData->GetCellData()->GetAbstractArray(name.c_str());
switch (array->GetDataType())
{
case VTK_BIT:
vtkArrayDownCast<vtkBitArray>(array)->InsertNextValue(value.ToChar());
break;
case VTK_DOUBLE:
vtkArrayDownCast<vtkDoubleArray>(array)->InsertNextValue(value.ToDouble());
break;
case VTK_INT:
vtkArrayDownCast<vtkIntArray>(array)->InsertNextValue(value.ToInt());
break;
case VTK_STRING:
vtkArrayDownCast<vtkStringArray>(array)->InsertNextValue(value.ToString());
break;
}
}
}
//------------------------------------------------------------------------------
vtkGeoJSONReader::vtkGeoJSONReader()
{
this->FileName = nullptr;
this->StringInput = nullptr;
this->StringInputMode = false;
this->TriangulatePolygons = false;
this->OutlinePolygons = false;
this->SerializedPropertiesArrayName = nullptr;
this->SetNumberOfInputPorts(0);
this->SetNumberOfOutputPorts(1);
this->Internal = new GeoJSONReaderInternal;
}
//------------------------------------------------------------------------------
vtkGeoJSONReader::~vtkGeoJSONReader()
{
delete[] FileName;
delete[] StringInput;
delete Internal;
}
//------------------------------------------------------------------------------
void vtkGeoJSONReader::AddFeatureProperty(const char* name, vtkVariant& typeAndDefaultValue)
{
GeoJSONReaderInternal::GeoJSONProperty property;
// Traverse internal list checking if name already used
std::vector<GeoJSONReaderInternal::GeoJSONProperty>::iterator iter =
this->Internal->PropertySpecs.begin();
for (; iter != this->Internal->PropertySpecs.end(); ++iter)
{
if (iter->Name == name)
{
vtkGenericWarningMacro(<< "Overwriting property spec for name " << name);
property.Name = name;
property.Value = typeAndDefaultValue;
*iter = property;
break;
}
}
// If not found, add to list
if (iter == this->Internal->PropertySpecs.end())
{
property.Name = name;
property.Value = typeAndDefaultValue;
this->Internal->PropertySpecs.push_back(property);
vtkDebugMacro(<< "Added feature property " << property.Name);
}
}
//------------------------------------------------------------------------------
int vtkGeoJSONReader::RequestData(vtkInformation* vtkNotUsed(request),
vtkInformationVector** vtkNotUsed(request), vtkInformationVector* outputVector)
{
// Get the info object
vtkInformation* outInfo = outputVector->GetInformationObject(0);
// Get the output
vtkPolyData* output = vtkPolyData::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()));
// Parse either string input of file, depending on mode
Json::Value root;
int parseResult = 0;
if (this->StringInputMode)
{
parseResult = this->Internal->CanParseString(this->StringInput, root);
}
else
{
parseResult = this->Internal->CanParseFile(this->FileName, root);
}
if (parseResult != VTK_OK)
{
return VTK_ERROR;
}
// If parsed successfully into Json, then convert it
// into appropriate vtkPolyData
if (root.isObject())
{
this->Internal->ParseRoot(
root, output, this->OutlinePolygons, this->SerializedPropertiesArrayName);
// Convert Concave Polygons to convex polygons using triangulation
if (output->GetNumberOfPolys() && this->TriangulatePolygons)
{
vtkNew<vtkTriangleFilter> filter;
filter->SetInputData(output);
filter->Update();
output->ShallowCopy(filter->GetOutput());
}
}
return VTK_OK;
}
//------------------------------------------------------------------------------
void vtkGeoJSONReader::PrintSelf(ostream& os, vtkIndent indent)
{
Superclass::PrintSelf(os, indent);
os << "vtkGeoJSONReader" << std::endl;
os << "Filename: " << this->FileName << std::endl;
}
VTK_ABI_NAMESPACE_END
|