Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to read MIE4NITF files #59

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Applications/VpView/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ endif()

# Needed for GDAL readers
if(VISGUI_ENABLE_GDAL)
list(APPEND vgSdkTargets vtkVgIO)
list(APPEND vgSdkTargets vtkVgIO ${GDAL_LIBRARY})
endif()

vg_include_library_sdk_directories(${vgSdkTargets} vvData)
Expand Down
102 changes: 96 additions & 6 deletions Applications/VpView/vpFileDataSource.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
#include <QStringList>

#include <algorithm>
#include <iostream>

#ifdef VISGUI_USE_GDAL
#include <gdal_priv.h>
#include <cpl_conv.h>
#endif

namespace // anonymous
{
Expand Down Expand Up @@ -128,6 +134,57 @@ void vpFileDataSource::setMonitoringEnabled(bool enable)
}
}

bool add_mie4nitf_subdatasets (const QString &path, QStringList &list) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh... your code style is rather all over the place. Please try to conform to the existing style. (n.b. T& x, not T &x, and Allman braces. Fortunately this is not one of the files still using historic VTK-style braces. Your indenting is also inconsistent.)

#ifdef VISGUI_USE_GDAL
GDALAllRegister();

GDALDataset *dataset = \
static_cast<GDALDataset *>(GDALOpen(path.toStdString().c_str(),
GA_ReadOnly));

if (!dataset)
{
qWarning().nospace()<< "GDAL could not load file." <<
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is nospace() needed here? Also, please put << at the start of the next line, not the end of a line, and align with the prior <<.

path.toStdString().c_str();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides that this is the locale-incorrect way to convert to std::string (that would be stdString) or char* (that would be qPrintable), why are you doing this at all? Just pass the QString...

}

const char* subdatasets_domain_name = "SUBDATASETS";
char **str = GDALGetMetadata(dataset, subdatasets_domain_name);

int num_key_val_pairs = 0;
while((*str) != NULL) {
char *key_c_str= nullptr;
QString key, value;
const char *val_c_str = CPLParseNameValue((*str), &key_c_str);
if (key_c_str != nullptr && val_c_str != nullptr)
{
key = QString(key_c_str);
value = QString(val_c_str);

// Subdataset # 2 inside `/A.ntf` would be indicated by:
// SUBDATASET_2_NAME=NITF_IM:1:/A.ntf
// SUBDATASET_2_DESC=Image 2 of A.ntf

const QRegExp rgx = QRegExp("SUBDATASET_\\d+_NAME");

if(key.contains(rgx))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing braces.

list.append(value);
}
CPLFree(key_c_str);
++(str);
++num_key_val_pairs;
}
assert(num_key_val_pairs % 2 == 0);
assert(num_key_val_pairs / 2 == list.size());
GDALClose(dataset);
return true;
#else
qWarning() << "ERROR: GDAL reader not found to open file: " <<
path.toStdString();
return false;
#endif

}
//-----------------------------------------------------------------------------
void vpFileDataSource::update()
{
Expand All @@ -154,21 +211,54 @@ void vpFileDataSource::update()
{
while (!file.atEnd())
{
const auto line = file.readLine();
QString line = file.readLine().trimmed();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't decrease use of AAA.

if (line.isEmpty())
{
continue;
}

const auto path = baseDir.absoluteFilePath(QString::fromUtf8(line));
if (!QFileInfo{path}.exists())
// The path we'd check existence of. It may so happen, like in the case
// of MIE4NITF, that the `path` to a frame doesn't exist on the disk.
// For ex., in MIE4NITF, frame # 3 inside `/A.ntf` (0-indexed) is
// stored in:
// `NITF_IM:2:/A.ntf`.
QString path_to_check;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use camelCase for variable names.

QStringList all_paths;

// MIE4NITF are a bunch of frames inside an `NITF` file. Just like we
// use glob to get more than one frames, we use the prefix `MIE4NITF:`
// to indicate that this file contains more than one frame in NITF
// format.
const QString mie4nitf_prefix = "MIE4NITF:";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use QStringLiteral.


if (line.startsWith(mie4nitf_prefix))
{
int L = mie4nitf_prefix.length();
QString p = line.mid(L);
path_to_check = baseDir.absoluteFilePath(p);

if(!add_mie4nitf_subdatasets(path_to_check, all_paths))
{
continue;
}
}
else
{
path_to_check = baseDir.absoluteFilePath(line);
all_paths.append(path_to_check);
}

if (!QFileInfo{path_to_check}.exists())
{
qWarning() << "Image data file" << path << "does not exist";
qWarning() << "Image data file" << path_to_check << "does not exist";
continue;
}

qDebug() << "Archiving" << path;
d->DataFiles.append(path);
for(auto path: all_paths)
{
qDebug() << "Archiving" << path;
d->DataFiles.append(path);
}
}
}
else
Expand Down
1 change: 1 addition & 0 deletions Libraries/VtkVgIO/vtkGDALReader.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ void vtkGDALReader::vtkGDALReaderInternal::ReadMetaData(
this->ReleaseData();

this->GDALData = (GDALDataset*) GDALOpen(fileName.c_str(), GA_ReadOnly);
std::cout << "Using GDAL reader to open file: " << fileName << std::endl;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder how useful this is?


if (this->GDALData == NULL)
{
Expand Down