DataInfo* createDataInfo(DataElement* pElement) { RasterElement* pRasterElement = dynamic_cast<RasterElement*>(pElement); if (pRasterElement == NULL) { setLastError(SIMPLE_BAD_PARAMS); return NULL; } RasterDataDescriptor* pDescriptor = dynamic_cast<RasterDataDescriptor*>(pRasterElement->getDataDescriptor()); if (pDescriptor == NULL) { setLastError(SIMPLE_OTHER_FAILURE); return NULL; } DataInfo* pDataInfo = new DataInfo; pDataInfo->numRows = pDescriptor->getRowCount(); pDataInfo->numColumns = pDescriptor->getColumnCount(); pDataInfo->numBands = pDescriptor->getBandCount(); pDataInfo->encodingType = static_cast<uint32_t>(pDescriptor->getDataType()); pDataInfo->encodingTypeSize = RasterUtilities::bytesInEncoding(pDescriptor->getDataType()); pDataInfo->interleaveFormat = static_cast<uint32_t>(pDescriptor->getInterleaveFormat()); const std::vector<int>& badValues = pDescriptor->getBadValues(); pDataInfo->numBadValues = badValues.size(); if (pDataInfo->numBadValues == 0) { pDataInfo->pBadValues = NULL; } else { pDataInfo->pBadValues = new int32_t[pDataInfo->numBadValues]; memcpy(pDataInfo->pBadValues, &badValues[0], pDataInfo->numBadValues * sizeof(int32_t)); } setLastError(SIMPLE_NO_ERROR); return pDataInfo; }
bool BackgroundSuppressionShell::execute(PlugInArgList *pInArgList, PlugInArgList *pOutArgList) { if(pInArgList == NULL) { return false; } mProgress = ProgressTracker(pInArgList->getPlugInArgValue<Progress>(Executable::ProgressArg()), "Suppressing background", "temporal", "{01F54B12-0323-4ac4-8C04-C89F1C45EAD9}"); mpRaster = pInArgList->getPlugInArgValue<RasterElement>(Executable::DataElementArg()); if(mpRaster == NULL) { mProgress.report("No raster element specified.", 0, ERRORS, true); return false; } mpView = pInArgList->getPlugInArgValue<SpatialDataView>(Executable::ViewArg()); RasterDataDescriptor *pDescriptor = static_cast<RasterDataDescriptor*>(mpRaster->getDataDescriptor()); VERIFY(pDescriptor != NULL); mIsStreaming = !isBatch(); if(!isBatch()) { BackgroundSuppressionDialog dlg; if(dlg.exec() == QDialog::Rejected) { mProgress.report("Suppression canceled by user.", 0, ABORT, true); return false; } mIsStreaming = dlg.isStreaming(); mpAnimation.reset(dlg.animation()); } unsigned int totalFrames = pDescriptor->getBandCount(); mCurrentFrame = 0; mProgressStep = 25.0 / (totalFrames - mCurrentFrame); mCurrentProgress = mProgressStep; if(!mIsStreaming) { for(InitializeReturnType rval = INIT_CONTINUE; rval == INIT_CONTINUE; mCurrentFrame++) { rval = initializeModel(); if(rval == INIT_ERROR) { return false; } } for(; mCurrentFrame < totalFrames; mCurrentFrame++) { if(isAborted()) { try { mProgress.abort(); } catch(const AlgorithmAbort&) { } cleanup(false); return false; } if(!processFrame()) { cleanup(false); return false; } } if(!displayResults()) { cleanup(false); mProgress.report("Unable to display results.", 0, ERRORS, true); return false; } cleanup(true); mProgress.report("Extraction complete", 100, NORMAL); mProgress.upALevel(); } else { for(InitializeReturnType rval = INIT_CONTINUE; rval == INIT_CONTINUE; mCurrentFrame++) { rval = initializeModel(); if(rval == INIT_ERROR) { return false; } } if(mpAnimation.get() == NULL) { mProgress.report("No animation specified, unable to perform streaming execution. Provide and animation or run in batch mode.", 0, ERRORS, true); return false; } mCurrentFrame = 0; const AnimationFrame *pFrame = mpAnimation->getCurrentFrame(); if(pFrame != NULL) { mCurrentFrame = pFrame->mFrameNumber; } if(!processFrame() || !displayResults()) { mProgress.report("Unable to initialize streaming mode.", 0, ERRORS, true); return false; } mProgress.report("Streaming mode setup complete.", 100, NORMAL); mProgress.upALevel(); mProgress.initialize(NULL, "Suppressing background", "temporal", "{01F54B12-0323-4ac4-8C04-C89F1C45EAD9}"); destroyAfterExecute(false); } return true; }
bool ResultsExporter::execute(PlugInArgList* pInArgList, PlugInArgList* pOutArgList) { StepResource pStep("Execute results exporter", "app", "ABF9EDE4-4672-4361-86BB-3258ADFB0793"); mpStep = pStep.get(); if (!extractInputArgs(pInArgList)) { return false; } // Check for complex data RasterDataDescriptor* pDescriptor = dynamic_cast<RasterDataDescriptor*>(mpResults->getDataDescriptor()); if (pDescriptor == NULL) { mMessage = "Could not get the data descriptor from the results matrix!"; if (mpProgress != NULL) { mpProgress->updateProgress(mMessage, 0, ERRORS); } pStep->finalize(Message::Failure, mMessage); return false; } EncodingType eDataType = pDescriptor->getDataType(); if ((eDataType == INT4SCOMPLEX) || (eDataType == FLT8COMPLEX)) { mMessage = "Cannot save complex data in text format!"; if (mpProgress != NULL) { mpProgress->updateProgress(mMessage, 0, ERRORS); } pStep->finalize(Message::Failure, mMessage); return false; } if (pDescriptor->getBandCount() > 1) { mMessage = "Can only export single band data."; if (mpProgress != NULL) { mpProgress->updateProgress(mMessage, 0, ERRORS); } pStep->finalize(Message::Failure, mMessage); return false; } pDescriptor->addToMessageLog(pStep.get()); const string& filename = mpFileDescriptor->getFilename(); pStep->addProperty("filename", filename); pStep->addProperty("firstThreshold", mFirstThreshold); pStep->addProperty("secondThreshold", mSecondThreshold); pStep->addProperty("passArea", mPassArea); pStep->addProperty("geocoordType", mGeocoordType); pStep->addProperty("metadata", mbMetadata); pStep->addProperty("appendFile", mbAppendFile); DataElement* pParent = mpResults->getParent(); if (pParent != NULL) { pStep->addProperty("sourceDataSet", pParent->getName()); } ofstream fileOutput; fileOutput.open(filename.c_str(), mbAppendFile ? ios_base::app : ios_base::trunc); if (!fileOutput.is_open()) { mMessage = "Could not open the output file for writing!"; if (mpProgress != NULL) { mpProgress->updateProgress(mMessage, 0, ERRORS); } pStep->finalize(Message::Failure, mMessage); return false; } if (!writeOutput(fileOutput)) { if (mbAbort) { pStep->finalize(Message::Abort); } fileOutput.close(); remove(filename.c_str()); } mMessage = "Results matrix export complete!"; if (mpProgress != NULL) { mpProgress->updateProgress(mMessage, 100, NORMAL); } pStep->finalize(Message::Success); return true; }
bool SpectralLibraryManager::generateResampledLibrary(const RasterElement* pRaster) { VERIFY(pRaster != NULL); // check that lib sigs are in same units as the raster element const RasterDataDescriptor* pDesc = dynamic_cast<const RasterDataDescriptor*>(pRaster->getDataDescriptor()); VERIFY(pDesc != NULL); const Units* pUnits = pDesc->getUnits(); if (pDesc->getUnits()->getUnitType() != mLibraryUnitType) { if (Service<DesktopServices>()->showMessageBox("Mismatched Units", "The data are not in the " "same units as the spectral library.\n Do you want to continue anyway?", "Yes", "No") == 1) { return false; } } FactoryResource<Wavelengths> pWavelengths; pWavelengths->initializeFromDynamicObject(pRaster->getMetadata(), false); // populate the library with the resampled signatures PlugInResource pPlugIn("Resampler"); Resampler* pResampler = dynamic_cast<Resampler*>(pPlugIn.get()); VERIFY(pResampler != NULL); if (pWavelengths->getNumWavelengths() != pDesc->getBandCount()) { mpProgress->updateProgress("Wavelength information in metadata does not match the number of bands " "in the raster element", 0, ERRORS); return false; } // get resample suitable signatures - leave out signatures that don't cover the spectral range of the data std::vector<std::vector<double> > resampledData; resampledData.reserve(mSignatures.size()); std::vector<Signature*> resampledSignatures; resampledSignatures.reserve(mSignatures.size()); std::vector<std::string> unsuitableSignatures; std::vector<double> sigValues; std::vector<double> sigWaves; std::vector<double> rasterWaves = pWavelengths->getCenterValues(); std::vector<double> rasterFwhm = pWavelengths->getFwhm(); std::vector<double> resampledValues; std::vector<int> bandIndex; DataVariant data; for (std::vector<Signature*>::const_iterator it = mSignatures.begin(); it != mSignatures.end(); ++it) { data = (*it)->getData(SpectralLibraryMatch::getNameSignatureWavelengthData()); VERIFY(data.isValid()); VERIFY(data.getValue(sigWaves)); resampledValues.clear(); data = (*it)->getData(SpectralLibraryMatch::getNameSignatureAmplitudeData()); VERIFY(data.isValid()); VERIFY(data.getValue(sigValues)); double scaleFactor = (*it)->getUnits( SpectralLibraryMatch::getNameSignatureAmplitudeData())->getScaleFromStandard(); for (std::vector<double>::iterator sit = sigValues.begin(); sit != sigValues.end(); ++sit) { *sit *= scaleFactor; } std::string msg; if (pResampler->execute(sigValues, resampledValues, sigWaves, rasterWaves, rasterFwhm, bandIndex, msg) == false || resampledValues.size() != rasterWaves.size()) { unsuitableSignatures.push_back((*it)->getName()); continue; } resampledData.push_back(resampledValues); resampledSignatures.push_back(*it); } if (resampledSignatures.empty()) { std::string errMsg = "None of the signatures in the library cover the spectral range of the data."; if (mpProgress != NULL) { mpProgress->updateProgress(errMsg, 0, ERRORS); return false; } } if (unsuitableSignatures.empty() == false) { std::string warningMsg = "The following library signatures do not cover the spectral range of the data:\n"; for (std::vector<std::string>::iterator it = unsuitableSignatures.begin(); it != unsuitableSignatures.end(); ++it) { warningMsg += *it + "\n"; } warningMsg += "These signatures will not be searched for in the data."; Service<DesktopServices>()->showMessageBox("SpectralLibraryManager", warningMsg); StepResource pStep("Spectral LibraryManager", "spectral", "64B6C87A-A6C3-4378-9B6E-221D89D8707B"); pStep->finalize(Message::Unresolved, warningMsg); } std::string libName = "Resampled Spectral Library"; // Try to get the resampled lib element in case session was restored. If NULL, create a new raster element with // num rows = num valid signatures, num cols = 1, num bands = pRaster num bands RasterElement* pLib = dynamic_cast<RasterElement*>(Service<ModelServices>()->getElement(libName, TypeConverter::toString<RasterElement>(), pRaster)); if (pLib != NULL) { // check that pLib has same number of sigs as SpectralLibraryManager RasterDataDescriptor* pLibDesc = dynamic_cast<RasterDataDescriptor*>(pLib->getDataDescriptor()); VERIFY(pLibDesc != NULL); if (pLibDesc->getRowCount() != mSignatures.size()) { mpProgress->updateProgress("An error occurred during session restore and some signatures were not restored." " Check the spectral library before using.", 0, ERRORS); Service<ModelServices>()->destroyElement(pLib); pLib = NULL; } } bool isNewElement(false); if (pLib == NULL) { pLib = RasterUtilities::createRasterElement(libName, static_cast<unsigned int>(resampledData.size()), 1, pDesc->getBandCount(), FLT8BYTES, BIP, true, const_cast<RasterElement*>(pRaster)); isNewElement = true; } if (pLib == NULL) { mpProgress->updateProgress("Error occurred while trying to create the resampled spectral library", 0, ERRORS); return false; } RasterDataDescriptor* pLibDesc = dynamic_cast<RasterDataDescriptor*>(pLib->getDataDescriptor()); VERIFY(pLibDesc != NULL); // copy resampled data into new element if (isNewElement) { FactoryResource<DataRequest> pRequest; pRequest->setWritable(true); pRequest->setRows(pLibDesc->getActiveRow(0), pLibDesc->getActiveRow(pLibDesc->getRowCount()-1), 1); DataAccessor acc = pLib->getDataAccessor(pRequest.release()); for (std::vector<std::vector<double> >::iterator sit = resampledData.begin(); sit != resampledData.end(); ++sit) { VERIFY(acc->isValid()); void* pData = acc->getColumn(); memcpy(acc->getColumn(), &(sit->begin()[0]), pLibDesc->getBandCount() * sizeof(double)); acc->nextRow(); } // set wavelength info in resampled library pWavelengths->applyToDynamicObject(pLib->getMetadata()); FactoryResource<Units> libUnits; libUnits->setUnitType(mLibraryUnitType); libUnits->setUnitName(StringUtilities::toDisplayString<UnitType>(mLibraryUnitType)); pLibDesc->setUnits(libUnits.get()); } pLib->attach(SIGNAL_NAME(Subject, Deleted), Slot(this, &SpectralLibraryManager::resampledElementDeleted)); mLibraries[pRaster] = pLib; mResampledSignatures[pLib] = resampledSignatures; const_cast<RasterElement*>(pRaster)->attach(SIGNAL_NAME(Subject, Deleted), Slot(this, &SpectralLibraryManager::elementDeleted)); return true; }
bool RasterTimingTest::execute(PlugInArgList* pInArgList, PlugInArgList* pOutArgList) { if (isBatch()) { VERIFY(pOutArgList != NULL); } Service<DesktopServices> pDesktop; SpatialDataView* pView = dynamic_cast<SpatialDataView*>(pDesktop->getCurrentWorkspaceWindowView()); if (pView) { UndoLock lock(pView); RasterElement* pElement = pView->getLayerList()->getPrimaryRasterElement(); RasterDataDescriptor* pDesc = dynamic_cast<RasterDataDescriptor*>(pElement->getDataDescriptor()); int bands = pDesc->getBandCount(); int frameNumber = 0; RasterLayer* pLayer = NULL; vector<Layer*> layers; pView->getLayerList()->getLayers(RASTER, layers); for (vector<Layer*>::iterator iter = layers.begin(); iter != layers.end(); ++iter) { RasterLayer* pRasterLayer = static_cast<RasterLayer*>(*iter); if (pRasterLayer != NULL) { RasterElement* pCurrentRasterElement = dynamic_cast<RasterElement*>(pRasterLayer->getDataElement()); if (pCurrentRasterElement == pElement) { pLayer = pRasterLayer; break; } } } for (int i = 0; i < bands; ++i) { pElement->getStatistics(pDesc->getActiveBand(i))->getMin(); } // set grayscale display mode DisplayMode initialDisplayMode = pLayer->getDisplayMode(); pLayer->setDisplayMode(GRAYSCALE_MODE); const int frameiterations = 10000; clock_t startTime = clock(); QWidget* pWidget = pView->getWidget(); int i = 0; for (i = 0; i < frameiterations; ++i, ++frameNumber) { if (frameNumber >= bands) { frameNumber = 0; } pLayer->setDisplayedBand(GRAY, pDesc->getActiveBand(frameNumber)); if (pWidget) { pWidget->repaint(); } if ((i + 1) % (frameiterations / 100) == 0) { QString message = QString("Frame ") + QString::number(i+1) + QString(" of ") + QString::number(frameiterations); pDesktop->setStatusBarMessage(message.toStdString()); } if ((i + 1) % 20 == 0) { clock_t stopTime = clock(); double elapsedTime = static_cast<double>(stopTime - startTime) / CLOCKS_PER_SEC; if (elapsedTime > 30) { ++i; break; } } } clock_t stopTime = clock(); double framesPerSec = i / (static_cast<double>(stopTime - startTime) / CLOCKS_PER_SEC); // restore display mode pLayer->setDisplayMode(initialDisplayMode); if (isBatch()) { pOutArgList->setPlugInArgValue<double>("Framerate", &framesPerSec); } else { QMessageBox::information(pDesktop->getMainWidget(), "Frame Rate", QString("The number of frames per second was: %1\nGPU Acceleration was%2 enabled\n").arg(framesPerSec) .arg(pLayer->isGpuImageEnabled() ? "" : " not")); } return true; } return false; }
ImportDescriptor* Nitf::NitfImporterShell::getImportDescriptor(const string& filename, ossim_uint32 imageSegment, const Nitf::OssimFileResource& pFile, const ossimNitfFileHeaderV2_X* pFileHeader, const ossimNitfImageHeaderV2_X* pImageSubheader) { if (pImageSubheader == NULL) { return NULL; } EncodingType dataType = ossimImageHeaderToEncodingType(pImageSubheader); if (dataType.isValid() == false) { return NULL; } stringstream imageNameStream; imageNameStream << "I" << imageSegment + 1; string imageName = imageNameStream.str(); ImportDescriptorResource pImportDescriptor(filename + "-" + imageName, TypeConverter::toString<RasterElement>(), NULL); VERIFYRV(pImportDescriptor.get() != NULL, NULL); pImportDescriptor->setImported(pImageSubheader->getRepresentation() != "NODISPLY"); RasterDataDescriptor* pDescriptor = dynamic_cast<RasterDataDescriptor*>(pImportDescriptor->getDataDescriptor()); VERIFYRV(pDescriptor != NULL, NULL); vector<DimensionDescriptor> bands = RasterUtilities::generateDimensionVector(pImageSubheader->getNumberOfBands(), true, false, true); pDescriptor->setBands(bands); vector<DimensionDescriptor> rows = RasterUtilities::generateDimensionVector(pImageSubheader->getNumberOfRows(), true, false, true); pDescriptor->setRows(rows); vector<DimensionDescriptor> cols = RasterUtilities::generateDimensionVector(pImageSubheader->getNumberOfCols(), true, false, true); pDescriptor->setColumns(cols); if (pImageSubheader->getIMode() == "P") { pDescriptor->setInterleaveFormat(BIP); } else if (pImageSubheader->getIMode() == "R") { pDescriptor->setInterleaveFormat(BIL); } else { pDescriptor->setInterleaveFormat(BSQ); } pDescriptor->setDataType(dataType); pDescriptor->setValidDataTypes(vector<EncodingType>(1, dataType)); pDescriptor->setProcessingLocation(IN_MEMORY); map<string, TrePlugInResource> parsers; string errorMessage; // Set the file descriptor RasterFileDescriptor* pFileDescriptor = dynamic_cast<RasterFileDescriptor*>( RasterUtilities::generateAndSetFileDescriptor(pDescriptor, filename, imageName, LITTLE_ENDIAN_ORDER)); if (pFileDescriptor == NULL) { return NULL; } // Set the bits per element, which may be different than the data type in the data descriptor, // using NBPP instead of ABPP as is done in ossimNitfTileSource.cpp. unsigned int bitsPerPixel = static_cast<unsigned int>(pImageSubheader->getBitsPerPixelPerBand()); pFileDescriptor->setBitsPerElement(bitsPerPixel); // Populate the metadata and set applicable values in the data descriptor if (Nitf::importMetadata(imageSegment + 1, pFile, pFileHeader, pImageSubheader, pDescriptor, parsers, errorMessage) == true) { // Populate specific fields in the data descriptor or file descriptor from the TREs const DynamicObject* pMetadata = pDescriptor->getMetadata(); VERIFYRV(pMetadata, NULL); // Pixel size - This info is contained in multiple TREs, but there is no documentation on which // TRE contains the more precise value if multiple TREs containing the info are present. Choosing // the order ACFTA, BANDSA, ACFTB, and BANDSB where the later "B" TREs will overwrite the values // contained in the earlier "A" TREs. The BANDSB TRE contains GSD values for each band, which is // currently not supported, so only set the pixel size if the values in all bands are the same. double xGsd = 1.0; double yGsd = 1.0; const string acrftaPath[] = { Nitf::NITF_METADATA, Nitf::TRE_METADATA, "ACFTA", "0", END_METADATA_NAME }; const DynamicObject* pAcrftA = dv_cast<DynamicObject>(&pMetadata->getAttributeByPath(acrftaPath)); if (pAcrftA != NULL) { // The ACFTA spec calls out specific spacing units for "SAR" and "EO-IR" data, but does not indicate how // this is determined. It seems to be related to the ACFTB SENSOR_ID_TYPE field, but that field is not // present in the ACFTA TRE. So just check for "SAR" data from the ICAT field in the image subheader // and assume every other data type is "EO-IR" data. const string imageCategory = pImageSubheader->getCategory().trim(); const DataVariant& rowSpacing = pAcrftA->getAttribute(Nitf::TRE::ACFTA::ROW_SPACING); if (rowSpacing.isValid() == true) { if (imageCategory == "SAR") { yGsd = getGsd(rowSpacing, "f"); // Feet } else { yGsd = getGsd(rowSpacing, "r"); // Micro-radians } } const DataVariant& columnSpacing = pAcrftA->getAttribute(Nitf::TRE::ACFTA::COL_SPACING); if (columnSpacing.isValid() == true) { if (imageCategory == "SAR") { xGsd = getGsd(columnSpacing, "f"); // Feet } else { xGsd = getGsd(columnSpacing, "r"); // Micro-radians } } } const string bandsaPath[] = { Nitf::NITF_METADATA, Nitf::TRE_METADATA, "BANDSA", "0", END_METADATA_NAME }; const DynamicObject* pBandsA = dv_cast<DynamicObject>(&pMetadata->getAttributeByPath(bandsaPath)); if (pBandsA != NULL) { const DataVariant& rowSpacing = pBandsA->getAttribute(Nitf::TRE::BANDSA::ROW_SPACING); if (rowSpacing.isValid() == true) { const DataVariant& rowSpacingUnits = pBandsA->getAttribute(Nitf::TRE::BANDSA::ROW_SPACING_UNITS); if (rowSpacingUnits.isValid() == true) { yGsd = getGsd(rowSpacing, rowSpacingUnits.toXmlString()); } } const DataVariant& columnSpacing = pBandsA->getAttribute(Nitf::TRE::BANDSA::COL_SPACING); if (columnSpacing.isValid() == true) { const DataVariant& columnSpacingUnits = pBandsA->getAttribute(Nitf::TRE::BANDSA::COL_SPACING_UNITS); if (columnSpacingUnits.isValid() == true) { xGsd = getGsd(columnSpacing, columnSpacingUnits.toXmlString()); } } } const string acrftbPath[] = { Nitf::NITF_METADATA, Nitf::TRE_METADATA, "ACFTB", "0", END_METADATA_NAME }; const DynamicObject* pAcrftB = dv_cast<DynamicObject>(&pMetadata->getAttributeByPath(acrftbPath)); if (pAcrftB != NULL) { const DataVariant& rowSpacing = pAcrftB->getAttribute(Nitf::TRE::ACFTB::ROW_SPACING); if (rowSpacing.isValid() == true) { const DataVariant& rowSpacingUnits = pAcrftB->getAttribute(Nitf::TRE::ACFTB::ROW_SPACING_UNITS); if (rowSpacingUnits.isValid() == true) { yGsd = getGsd(rowSpacing, rowSpacingUnits.toXmlString()); } } const DataVariant& columnSpacing = pAcrftB->getAttribute(Nitf::TRE::ACFTB::COL_SPACING); if (columnSpacing.isValid() == true) { const DataVariant& columnSpacingUnits = pAcrftB->getAttribute(Nitf::TRE::ACFTB::COL_SPACING_UNITS); if (columnSpacingUnits.isValid() == true) { xGsd = getGsd(columnSpacing, columnSpacingUnits.toXmlString()); } } } const string bandsbPath[] = { Nitf::NITF_METADATA, Nitf::TRE_METADATA, "BANDSB", "0", END_METADATA_NAME }; const DynamicObject* pBandsB = dv_cast<DynamicObject>(&pMetadata->getAttributeByPath(bandsbPath)); if (pBandsB != NULL) { bool validRowGsd = false; const DataVariant& rowGsd = pBandsB->getAttribute(Nitf::TRE::BANDSB::ROW_GSD); if (rowGsd.isValid() == true) { const DataVariant& rowGsdUnits = pBandsB->getAttribute(Nitf::TRE::BANDSB::ROW_GSD_UNIT); if (rowGsdUnits.isValid() == true) { yGsd = getGsd(rowGsd, rowGsdUnits.toXmlString()); validRowGsd = true; } } if (validRowGsd == false) { if (pBandsB->getAttribute(Nitf::TRE::BANDSB::ROW_GSD + "#0").isValid()) { double commonYGsd = -1.0; unsigned int numBands = pDescriptor->getBandCount(); for (unsigned int i = 0; i < numBands; ++i) { double bandYGsd = -1.0; string bandPostfix = "#" + StringUtilities::toDisplayString(i); const DataVariant& bandRowGsd = pBandsB->getAttribute(Nitf::TRE::BANDSB::ROW_GSD + bandPostfix); if (bandRowGsd.isValid() == true) { const DataVariant& bandRowGsdUnits = pBandsB->getAttribute(Nitf::TRE::BANDSB::ROW_GSD_UNIT + bandPostfix); if (bandRowGsdUnits.isValid() == true) { bandYGsd = getGsd(bandRowGsd, bandRowGsdUnits.toXmlString()); } } if (bandYGsd == commonYGsd) { continue; } if (commonYGsd != -1.0) { commonYGsd = -1.0; break; } commonYGsd = bandYGsd; } if (commonYGsd != 1.0) { yGsd = commonYGsd; } } } bool validColumnGsd = false; const DataVariant& columnGsd = pBandsB->getAttribute(Nitf::TRE::BANDSB::COL_GSD); if (columnGsd.isValid() == true) { const DataVariant& columnGsdUnits = pBandsB->getAttribute(Nitf::TRE::BANDSB::COL_GSD_UNITS); if (columnGsdUnits.isValid() == true) { xGsd = getGsd(columnGsd, columnGsdUnits.toXmlString()); validColumnGsd = true; } } if (validColumnGsd == false) { if (pBandsB->getAttribute(Nitf::TRE::BANDSB::COL_GSD + "#0").isValid()) { double commonXGsd = -1.0; unsigned int numBands = pDescriptor->getBandCount(); for (unsigned int i = 0; i < numBands; ++i) { double bandXGsd = -1.0; string bandPostfix = "#" + StringUtilities::toDisplayString(i); const DataVariant& bandRowGsd = pBandsB->getAttribute(Nitf::TRE::BANDSB::COL_GSD + bandPostfix); if (bandRowGsd.isValid() == true) { const DataVariant& bandRowGsdUnits = pBandsB->getAttribute(Nitf::TRE::BANDSB::COL_GSD_UNIT + bandPostfix); if (bandRowGsdUnits.isValid() == true) { bandXGsd = getGsd(bandRowGsd, bandRowGsdUnits.toXmlString()); } } if (bandXGsd == commonXGsd) { continue; } if (commonXGsd != -1.0) { commonXGsd = -1.0; break; } commonXGsd = bandXGsd; } if (commonXGsd != 1.0) { xGsd = commonXGsd; } } } } double magFactor = 1.0; ossimString imag = pImageSubheader->getImageMagnification().trim(); if (imag.empty() == false) { // Need to multiply the GSD values by the image magnification (IMAG) value in the image subheader if (imag[0] == '/') { ossimString reciprocal = imag.substr(1); magFactor = 1.0 / reciprocal.toDouble(); } else { magFactor = imag.toDouble(); } xGsd *= magFactor; yGsd *= magFactor; } pDescriptor->setXPixelSize(xGsd); pDescriptor->setYPixelSize(yGsd); // Higher precision GCPs const string blockaPath[] = { Nitf::NITF_METADATA, Nitf::TRE_METADATA, "BLOCKA", "0", END_METADATA_NAME }; const DynamicObject* pBlockA = dv_cast<DynamicObject>(&pMetadata->getAttributeByPath(blockaPath)); if (pBlockA != NULL) { const DataVariant& blockLines = pBlockA->getAttribute(Nitf::TRE::BLOCKA::L_LINES); if (blockLines.isValid() == true) { unsigned int numBlockRows = 0; if (blockLines.getValue<unsigned int>(numBlockRows) == true) { // Need to multiply the number of rows by the image magnification (IMAG) value in the image subheader numBlockRows = static_cast<unsigned int>(static_cast<double>(numBlockRows) * magFactor); if (numBlockRows == pFileDescriptor->getRowCount()) { list<GcpPoint> updatedGcps; list<GcpPoint> gcps = pFileDescriptor->getGcps(); for (list<GcpPoint>::iterator iter = gcps.begin(); iter != gcps.end(); ++iter) { GcpPoint gcp = *iter; string coordinateText; list<GcpPoint>::size_type index = updatedGcps.size(); if (index == 0) { const DataVariant& gcp1 = pBlockA->getAttribute(Nitf::TRE::BLOCKA::FRFC_LOC); if (gcp1.isValid() == true) { coordinateText = gcp1.toXmlString(); } } else if (index == 1) { const DataVariant& gcp2 = pBlockA->getAttribute(Nitf::TRE::BLOCKA::FRLC_LOC); if (gcp2.isValid() == true) { coordinateText = gcp2.toXmlString(); } } else if (index == 2) { const DataVariant& gcp3 = pBlockA->getAttribute(Nitf::TRE::BLOCKA::LRLC_LOC); if (gcp3.isValid() == true) { coordinateText = gcp3.toXmlString(); } } else if (index == 3) { const DataVariant& gcp4 = pBlockA->getAttribute(Nitf::TRE::BLOCKA::LRFC_LOC); if (gcp4.isValid() == true) { coordinateText = gcp4.toXmlString(); } } if (StringUtilities::isAllBlank(coordinateText) == false) { coordinateText.insert(10, ", "); LatLonPoint latLon(coordinateText); gcp.mCoordinate = latLon.getCoordinates(); } updatedGcps.push_back(gcp); } pFileDescriptor->setGcps(updatedGcps); } } } } // This only checks the first BANDSB. It is possible to have multiple BANDSB TREs. // If someone runs across real data where the bad band info is in another BANDSB TRE // this code will need to be modified. if (pBandsB != NULL && pBandsB->getAttribute(Nitf::TRE::BANDSB::BAD_BAND + "#0").isValid()) { const vector<DimensionDescriptor>& curBands = pDescriptor->getBands(); vector<DimensionDescriptor> newBands; for (size_t idx = 0; idx < curBands.size(); ++idx) { const int* pVal = dv_cast<int>(&pBandsB->getAttribute( Nitf::TRE::BANDSB::BAD_BAND + "#" + StringUtilities::toDisplayString(idx))); if (pVal == NULL || *pVal == 1) // 0 == invalid or suspect band, 1 = valid band { newBands.push_back(curBands[idx]); } } pDescriptor->setBands(newBands); } // Bad values if (pImageSubheader->hasTransparentCode() == true) { vector<int> badValues; badValues.push_back(static_cast<int>(pImageSubheader->getTransparentCode())); pDescriptor->setBadValues(badValues); } // If red, green, OR blue bands are valid, set the display mode to RGB. if (pDescriptor->getDisplayBand(RED).isValid() == true || pDescriptor->getDisplayBand(GREEN).isValid() == true || pDescriptor->getDisplayBand(BLUE).isValid() == true) { pDescriptor->setDisplayMode(RGB_MODE); } // Otherwise, if the gray band is valid, set the display mode to GRAYSCALE. else if (pDescriptor->getDisplayBand(GRAY).isValid() == true) { pDescriptor->setDisplayMode(GRAYSCALE_MODE); } // Otherwise, if at least 3 bands are available, set the display mode to RGB, // and set the first three bands to red, green, and blue respectively. else if (bands.size() >= 3) { pDescriptor->setDisplayBand(RED, bands[0]); pDescriptor->setDisplayBand(GREEN, bands[1]); pDescriptor->setDisplayBand(BLUE, bands[2]); pDescriptor->setDisplayMode(RGB_MODE); } // Otherwise, if at least 1 band is available, set the display mode to GRAYSCALE, // and set the first band to GRAY. else if (bands.empty() == false) { pDescriptor->setDisplayBand(GRAY, bands[0]); pDescriptor->setDisplayMode(GRAYSCALE_MODE); } else { return NULL; } // Special initialization for J2K compressed image segments const string compressionPath[] = { Nitf::NITF_METADATA, Nitf::IMAGE_SUBHEADER, Nitf::ImageSubheaderFieldNames::COMPRESSION, END_METADATA_NAME }; string imageCompression = pMetadata->getAttributeByPath(compressionPath).toDisplayString(); if ((imageCompression == Nitf::ImageSubheaderFieldValues::IC_C8) || (imageCompression == Nitf::ImageSubheaderFieldValues::IC_M8)) { // Per Section 8.1 of the BIIF Profile for JPEG 2000 Version 01.10 (BPJ2K01.10), // if the values in the J2K data differ from the values in the image subheader, // the J2K values are given precedence. opj_image_t* pImage = getImageInfo(filename, imageSegment, OPJ_CODEC_J2K); if (pImage == NULL) { pImage = getImageInfo(filename, imageSegment, OPJ_CODEC_JP2); } if (pImage != NULL) { // Bits per element unsigned int bitsPerElement = pImage->comps->prec; if (bitsPerElement != pFileDescriptor->getBitsPerElement()) { pFileDescriptor->setBitsPerElement(bitsPerElement); } // Data type EncodingType dataType = INT1UBYTE; if (bitsPerElement <= 8) { if (pImage->comps->sgnd) { dataType = INT1SBYTE; } else { dataType = INT1UBYTE; } } else if (bitsPerElement <= 16) { if (pImage->comps->sgnd) { dataType = INT2SBYTES; } else { dataType = INT2UBYTES; } } else if (bitsPerElement <= 32) { if (pImage->comps->sgnd) { dataType = INT4SBYTES; } else { dataType = INT4UBYTES; } } else if (bitsPerElement <= 64) { dataType = FLT8BYTES; } if (dataType != pDescriptor->getDataType()) { pDescriptor->setDataType(dataType); } // Rows unsigned int numRows = pImage->comps->h; if (numRows != pFileDescriptor->getRowCount()) { vector<DimensionDescriptor> rows = RasterUtilities::generateDimensionVector(numRows, true, false, true); pDescriptor->setRows(rows); pFileDescriptor->setRows(rows); } // Columns unsigned int numColumns = pImage->comps->w; if (numColumns != pFileDescriptor->getColumnCount()) { vector<DimensionDescriptor> columns = RasterUtilities::generateDimensionVector(numColumns, true, false, true); pDescriptor->setColumns(columns); pFileDescriptor->setColumns(columns); } // Bands unsigned int numBands = pImage->numcomps; if (numBands != pFileDescriptor->getBandCount()) { vector<DimensionDescriptor> bands = RasterUtilities::generateDimensionVector(numBands, true, false, true); pDescriptor->setBands(bands); pFileDescriptor->setBands(bands); } // Cleanup opj_image_destroy(pImage); } // Set the interleave format as BIP, which is the interleave format for J2K data pDescriptor->setInterleaveFormat(BIP); pFileDescriptor->setInterleaveFormat(BIP); } mParseMessages[imageSegment] = errorMessage; } return pImportDescriptor.release(); }
std::vector<ImportDescriptor*> LandsatGeotiffImporter::createImportDescriptors(const std::string& filename, const DynamicObject* pImageMetadata, Landsat::LandsatImageType type) { std::string suffix; if (type == Landsat::LANDSAT_VNIR) { suffix = "vnir"; } else if (type == Landsat::LANDSAT_PAN) { suffix = "pan"; } else if (type == Landsat::LANDSAT_TIR) { suffix = "tir"; } std::vector<ImportDescriptor*> descriptors; std::string spacecraft = dv_cast<std::string>( pImageMetadata->getAttributeByPath("LANDSAT_MTL/L1_METADATA_FILE/PRODUCT_METADATA/SPACECRAFT_ID"), ""); std::vector<std::string> bandNames = Landsat::getSensorBandNames(spacecraft, type); if (bandNames.empty()) { //this spacecraft and iamge type //isn't meant to have any bands, so terminate early //e.g. spacecraft == "Landsat5" && type == Landsat::LANDSAT_PAN return descriptors; } std::vector<unsigned int> validBands; std::vector<std::string> bandFiles = Landsat::getGeotiffBandFilenames( pImageMetadata, filename, type, validBands); if (bandFiles.empty()) { mWarnings.push_back("Unable to locate band files for " + suffix + " product."); return descriptors; } ImportDescriptorResource pImportDescriptor(filename + "-" + suffix, TypeConverter::toString<RasterElement>(), NULL, false); if (pImportDescriptor.get() == NULL) { return descriptors; } RasterDataDescriptor* pDescriptor = dynamic_cast<RasterDataDescriptor*>(pImportDescriptor->getDataDescriptor()); if (pDescriptor == NULL) { return descriptors; } pDescriptor->setProcessingLocation(ON_DISK); DynamicObject* pMetadata = pDescriptor->getMetadata(); pMetadata->merge(pImageMetadata); FactoryResource<RasterFileDescriptor> pFileDescriptorRes; pDescriptor->setFileDescriptor(pFileDescriptorRes.get()); RasterFileDescriptor* pFileDescriptor = dynamic_cast<RasterFileDescriptor*>(pDescriptor->getFileDescriptor()); pFileDescriptor->setFilename(filename); std::string tiffFile = bandFiles[0]; if (!Landsat::parseBasicsFromTiff(tiffFile, pDescriptor)) { mWarnings.push_back("Unable to parse basic information about image from tiff file for " + suffix + " product."); return descriptors; } if (pDescriptor->getBandCount() != 1 || pDescriptor->getDataType() != INT1UBYTE) { mWarnings.push_back("Improperly formatted tiff file for " + suffix + " product."); return descriptors; } pDescriptor->setInterleaveFormat(BSQ); //one tiff file per band. pFileDescriptor->setInterleaveFormat(BSQ); std::vector<DimensionDescriptor> bands = RasterUtilities::generateDimensionVector( bandFiles.size(), true, false, true); pDescriptor->setBands(bands); pFileDescriptor->setBands(bands); pDescriptor->setBadValues(std::vector<int>(1, 0)); pFileDescriptor->setDatasetLocation(suffix); //special metadata here Landsat::fixMtlMetadata(pMetadata, type, validBands); std::vector<std::string> defaultImport = OptionsLandsatImport::getSettingDefaultImport(); bool fallbackToDn = false; descriptors.push_back(pImportDescriptor.release()); if (type == Landsat::LANDSAT_VNIR) { //attempt to display true-color DimensionDescriptor redBand = RasterUtilities::findBandWavelengthMatch(0.630, 0.690, pDescriptor); DimensionDescriptor greenBand = RasterUtilities::findBandWavelengthMatch(0.510, 0.590, pDescriptor); DimensionDescriptor blueBand = RasterUtilities::findBandWavelengthMatch(0.410, 0.490, pDescriptor); if (redBand.isValid() && greenBand.isValid() && blueBand.isValid()) { pDescriptor->setDisplayMode(RGB_MODE); pDescriptor->setDisplayBand(RED, redBand); pDescriptor->setDisplayBand(GREEN, greenBand); pDescriptor->setDisplayBand(BLUE, blueBand); } } std::vector<std::pair<double, double> > radianceFactors = Landsat::determineRadianceConversionFactors( pMetadata, type, validBands); bool shouldDefaultImportRadiance = std::find(defaultImport.begin(), defaultImport.end(), suffix + "-Radiance") != defaultImport.end(); if (radianceFactors.size() == bandFiles.size()) { //we have enough to create radiance import descriptor RasterDataDescriptor* pRadianceDescriptor = dynamic_cast<RasterDataDescriptor*>( pDescriptor->copy(filename + "-" + suffix + "-radiance", NULL)); if (pRadianceDescriptor != NULL) { pRadianceDescriptor->setDataType(FLT4BYTES); pRadianceDescriptor->setValidDataTypes(std::vector<EncodingType>(1, pRadianceDescriptor->getDataType())); pRadianceDescriptor->setBadValues(std::vector<int>(1, -100)); FactoryResource<Units> pUnits; pUnits->setUnitType(RADIANCE); pUnits->setUnitName("w/(m^2*sr*um)"); pUnits->setScaleFromStandard(1.0); pRadianceDescriptor->setUnits(pUnits.get()); FileDescriptor* pRadianceFileDescriptor = pRadianceDescriptor->getFileDescriptor(); if (pRadianceFileDescriptor != NULL) { pRadianceFileDescriptor->setDatasetLocation(suffix + "-radiance"); ImportDescriptorResource pRadianceImportDescriptor(pRadianceDescriptor, shouldDefaultImportRadiance); descriptors.push_back(pRadianceImportDescriptor.release()); } } } else if (shouldDefaultImportRadiance) { fallbackToDn = true; } std::vector<double> reflectanceFactors = Landsat::determineReflectanceConversionFactors( pMetadata, type, validBands); bool shouldDefaultImportReflectance = std::find(defaultImport.begin(), defaultImport.end(), suffix + "-Reflectance") != defaultImport.end(); if (radianceFactors.size() == bandFiles.size() && reflectanceFactors.size() == bandFiles.size()) { //we have enough to create reflectance import descriptor RasterDataDescriptor* pReflectanceDescriptor = dynamic_cast<RasterDataDescriptor*>( pDescriptor->copy(filename + "-" + suffix + "-reflectance", NULL)); if (pReflectanceDescriptor != NULL) { pReflectanceDescriptor->setDataType(INT2SBYTES); pReflectanceDescriptor->setValidDataTypes( std::vector<EncodingType>(1, pReflectanceDescriptor->getDataType())); pReflectanceDescriptor->setBadValues(std::vector<int>(1, std::numeric_limits<short>::max())); FactoryResource<Units> pUnits; pUnits->setUnitType(REFLECTANCE); pUnits->setUnitName("Reflectance"); pUnits->setScaleFromStandard(1/10000.0); pReflectanceDescriptor->setUnits(pUnits.get()); FileDescriptor* pReflectanceFileDescriptor = pReflectanceDescriptor->getFileDescriptor(); if (pReflectanceFileDescriptor != NULL) { pReflectanceFileDescriptor->setDatasetLocation(suffix + "-reflectance"); ImportDescriptorResource pReflectanceImportDescriptor(pReflectanceDescriptor, shouldDefaultImportReflectance); descriptors.push_back(pReflectanceImportDescriptor.release()); } } } else if (shouldDefaultImportReflectance) { fallbackToDn = true; } double K1 = 0.0; double K2 = 0.0; bool haveTemperatureFactors = Landsat::getTemperatureConstants(pMetadata, type, K1, K2); bool shouldDefaultImportTemperature = std::find(defaultImport.begin(), defaultImport.end(), suffix + "-Temperature") != defaultImport.end(); if (radianceFactors.size() == bandFiles.size() && haveTemperatureFactors) { //we have enough to create temperature import descriptor RasterDataDescriptor* pTemperatureDescriptor = dynamic_cast<RasterDataDescriptor*>( pDescriptor->copy(filename + "-" + suffix + "-temperature", NULL)); if (pTemperatureDescriptor != NULL) { pTemperatureDescriptor->setDataType(FLT4BYTES); pTemperatureDescriptor->setValidDataTypes( std::vector<EncodingType>(1, pTemperatureDescriptor->getDataType())); pTemperatureDescriptor->setBadValues(std::vector<int>(1, -1)); FactoryResource<Units> pUnits; pUnits->setUnitType(EMISSIVITY); pUnits->setUnitName("K"); pUnits->setScaleFromStandard(1.0); pTemperatureDescriptor->setUnits(pUnits.get()); FileDescriptor* pTemperatureFileDescriptor = pTemperatureDescriptor->getFileDescriptor(); if (pTemperatureFileDescriptor != NULL) { pTemperatureFileDescriptor->setDatasetLocation(suffix + "-temperature"); ImportDescriptorResource pTemperatureImportDescriptor(pTemperatureDescriptor, shouldDefaultImportTemperature); descriptors.push_back(pTemperatureImportDescriptor.release()); } } } else if (shouldDefaultImportTemperature) { fallbackToDn = true; } if (fallbackToDn || std::find(defaultImport.begin(), defaultImport.end(), suffix + "-DN") != defaultImport.end()) { pImportDescriptor->setImported(true); } return descriptors; }
bool SampleGeoref::execute(PlugInArgList* pInArgList, PlugInArgList* pOutArgList) { // Do any kind of setup we need before converting coordinates. // In this case, get our X and Y factors. StepResource pStep("Run Sample Georef", "app", "CFCB8AA9-D504-42e9-86F0-547DF9B4798A"); Progress* pProgress = pInArgList->getPlugInArgValue<Progress>(Executable::ProgressArg()); FAIL_IF(!isBatch(), "Interactive mode is not supported.", return false); // Default values bool animated = false; // get factors from pInArgList pInArgList->getPlugInArgValue("XSize", mXSize); pInArgList->getPlugInArgValue("YSize", mYSize); pInArgList->getPlugInArgValue("Extrapolate", mExtrapolate); pInArgList->getPlugInArgValue("Animated", animated); pInArgList->getPlugInArgValue("Rotate", mRotate); View* pView = pInArgList->getPlugInArgValue<View>(Executable::ViewArg()); mpRaster = pInArgList->getPlugInArgValue<RasterElement>(Executable::DataElementArg()); FAIL_IF(mpRaster == NULL, "Could not find raster element", return false); if (mpGui != NULL) { mXSize = mpGui->getXSize(); mYSize = mpGui->getYSize(); animated = mpGui->getAnimated(); mRotate = mpGui->getRotate(); mExtrapolate = mpGui->getExtrapolate(); } if (animated) { SpatialDataView* pSpatialView = dynamic_cast<SpatialDataView*>(pView); FAIL_IF(pSpatialView == NULL, "Could not find spatial data view.", return false); LayerList* pLayerList = pSpatialView->getLayerList(); FAIL_IF(pLayerList == NULL, "Could not find layer list.", return false); RasterLayer* pLayer = dynamic_cast<RasterLayer*>(pLayerList->getLayer(RASTER, mpRaster)); FAIL_IF(pLayer == NULL, "Could not find raster layer", return false); Animation* pAnim = pLayer->getAnimation(); FAIL_IF(pAnim == NULL, "Could not find animation", return false); const std::vector<AnimationFrame>& frames = pAnim->getFrames(); FAIL_IF(frames.empty(), "No frames in animation.", return false); mpAnimation.reset(pAnim); mFrames = frames.size(); mCurrentFrame = 0; } RasterDataDescriptor* pDescriptor = dynamic_cast<RasterDataDescriptor*>(mpRaster->getDataDescriptor()); FAIL_IF(pDescriptor == NULL, "Could not get data descriptor.", return false); unsigned int rows = pDescriptor->getRowCount(); unsigned int cols = pDescriptor->getColumnCount(); unsigned int bands = pDescriptor->getBandCount(); mXScale = static_cast<double>(mXSize) / rows; mYScale = static_cast<double>(mYSize) / cols; mpRaster->setGeoreferencePlugin(this); mpGui = NULL; // Pointer not guaranteed to be valid after execute() is called return true; }