Commit 677104a0 authored by John Tourtellott's avatar John Tourtellott
Browse files

Only apply NoDataValue when it is set in the input image

Also explicitly cast NoDataValue to GDAL data type
Include test based on USGS elevation data
parent 042c4212
Pipeline #16756 passed with stage
set(TestGDALRasterNoDataValue_ARGS
-D DATA{../Data/Input/TestGDALRasterNoDataValue.tif}
)
vtk_add_test_cxx(${vtk-module}CxxTests tests
TestGDALVectorReader.cxx
TestGDALRasterReader.cxx
TestGDALRasterNoDataValue.cxx,NO_VALID,NO_OUTPUT
)
vtk_test_cxx_executable(${vtk-module}CxxTests tests)
/*=========================================================================
Program: Visualization Toolkit
Module: TestGDALRasterReader.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 <vtkGDALRasterReader.h>
#include <vtkNew.h>
#include <vtkUniformGrid.h>
#include <iostream>
// Main program
int TestGDALRasterNoDataValue(int argc, char** argv)
{
if (argc < 3)
{
std::cerr << "Expected TestName -D InputFile.tif";
return -1;
}
std::string inputFileName(argv[2]);
// Create reader to read shape file.
vtkNew<vtkGDALRasterReader> reader;
reader->SetFileName(inputFileName.c_str());
reader->Update();
vtkUniformGrid *rasterImage = vtkUniformGrid::SafeDownCast(
reader->GetOutput());
int numErrors = 0;
if (!rasterImage->HasAnyBlankPoints())
{
std::cerr << "Error image has no blank points" << std::endl;
++numErrors;
}
if (!rasterImage->HasAnyBlankCells())
{
std::cerr << "Error image has no blank cells" << std::endl;
++numErrors;
}
double *scalarRange = rasterImage->GetScalarRange();
if ((scalarRange[0] < -888.5) || (scalarRange[0]) > -887.5)
{
std::cerr << "Error scalarRange[0] should be -888.0, not "
<< scalarRange[0] << std::endl;
++numErrors;
}
if ((scalarRange[1] < 9998.5) || (scalarRange[1] > 9999.5))
{
std::cerr << "Error scalarRange[1] should be 9999.0, not "
<< scalarRange[1] << std::endl;
++numErrors;
}
//std::cout << "numErrors: " << numErrors << std::endl;
return numErrors;
}
......@@ -101,6 +101,7 @@ public:
// Upper left, lower left, upper right, lower right
double CornerPoints[8];
int HasNoDataValue;
double NoDataValue;
vtkIdType NumberOfPoints;
......@@ -287,7 +288,8 @@ void vtkGDALRasterReader::vtkGDALRasterReaderInternal::GenericReadData()
for (int i = 1; i <= this->NumberOfBands; ++i)
{
GDALRasterBand* rasterBand = this->GDALData->GetRasterBand(i);
NoDataValue = rasterBand->GetNoDataValue();
this->HasNoDataValue = 0;
this->NoDataValue = rasterBand->GetNoDataValue(&this->HasNoDataValue);
if (this->NumberOfBytesPerPixel == 0)
{
this->TargetDataType = rasterBand->GetRasterDataType();
......@@ -480,6 +482,12 @@ void vtkGDALRasterReader::vtkGDALRasterReaderInternal::Convert(
scArr->SetNumberOfComponents(this->NumberOfBands);
scArr->SetNumberOfTuples(targetWidth * targetHeight);
RAW_TYPE TNoDataValue = 0;
if (this->HasNoDataValue)
{
TNoDataValue = static_cast<RAW_TYPE>(this->NoDataValue);
}
for (int j = 0; j < targetHeight; ++j)
{
for (int i = 0; i < targetWidth; ++i)
......@@ -491,11 +499,18 @@ void vtkGDALRasterReader::vtkGDALRasterReaderInternal::Convert(
j * targetWidth * NumberOfBands + bandIndex;
sourceIndex = i + j * targetWidth +
bandIndex * targetWidth * targetHeight;
RAW_TYPE tmp = rawUniformGridData[sourceIndex];
if(tmp < min) min = tmp;
if(tmp > max) max = tmp;
if(tmp == NoDataValue) this->UniformGridData->BlankPoint(targetIndex);
else this->NumberOfPoints++;
if (this->HasNoDataValue && tmp == TNoDataValue)
{
this->UniformGridData->BlankPoint(targetIndex);
}
else
{
if(tmp < min) min = tmp;
if(tmp > max) max = tmp;
this->NumberOfPoints++;
}
scArr->InsertValue(targetIndex, rawUniformGridData[sourceIndex]);
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment