SpatialDataView* Nitf::NitfImporterShell::createView() const { SpatialDataView* pView = RasterElementImporterShell::createView(); VERIFYRV(pView != NULL, pView); RasterElement* pRaster = getRasterElement(); VERIFYRV(pRaster != NULL, pView); const DynamicObject* pMetadata = pRaster->getMetadata(); VERIFYRV(pMetadata != NULL, pView); const string backgroundColorPath[] = { Nitf::NITF_METADATA, Nitf::FILE_HEADER, Nitf::FileHeaderFieldNames::BACKGROUND_COLOR, END_METADATA_NAME }; const DataVariant& dvBackground = pMetadata->getAttributeByPath(backgroundColorPath); if (dvBackground.isValid() == true) { ColorType ctBackground; VERIFY(dvBackground.getValue(ctBackground) == true); pView->setBackgroundColor(ctBackground); pView->refresh(); } return pView; }
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 AceAlgorithm::processAll() { auto_ptr<Wavelengths> pWavelengths; ProgressTracker progress(getProgress(), "Starting Ace", "spectral", "C4320027-6359-4F5B-8820-8BC72BF1B8F0"); RasterElement* pElement = getRasterElement(); if (pElement == NULL) { progress.report(ACEERR012, 0, ERRORS, true); return false; } const RasterDataDescriptor* pDescriptor = static_cast<RasterDataDescriptor*>(pElement->getDataDescriptor()); VERIFY(pDescriptor != NULL); BitMaskIterator iter(getPixelsToProcess(), pElement); unsigned int numRows = iter.getNumSelectedRows(); unsigned int numColumns = iter.getNumSelectedColumns(); unsigned int numBands = pDescriptor->getBandCount(); Opticks::PixelOffset layerOffset(iter.getColumnOffset(), iter.getRowOffset()); // get cube wavelengths DynamicObject* pMetadata = pElement->getMetadata(); if (pMetadata != NULL) { pWavelengths.reset(new Wavelengths(pMetadata)); if (!pWavelengths->isEmpty() && (!pWavelengths->hasEndValues() || !pWavelengths->hasStartValues())) { pWavelengths->calculateFwhm(); } } VERIFY(pWavelengths.get() != NULL); int sig_index = 0; bool bSuccess = true; if (mInputs.mSignatures.empty()) { progress.report(ACEERR005, 0, ERRORS, true); return false; } int iSignatureCount = mInputs.mSignatures.size(); // Create a vector for the signature names vector<string> sigNames; RasterElement* pResults = NULL; bool resultsIsTemp = false; Signature* pSignature = mInputs.mSignatures[sig_index]; sigNames.push_back(pSignature->getName()); std::string rname = mInputs.mResultsName; if (iSignatureCount > 1 && !mInputs.mbCreatePseudocolor) { rname += " " + sigNames.back(); } else if (iSignatureCount > 1) { rname += "AceTemp"; resultsIsTemp = true; } pResults = createResults(numRows, numColumns, rname); if (pResults == NULL) { return false; } vector<double> spectrumValues; vector<int> resampledBands; bSuccess = resampleSpectrum(pSignature, spectrumValues, *pWavelengths.get(), resampledBands); // Check for limited spectral coverage and warning log if (bSuccess && pWavelengths->hasCenterValues() && resampledBands.size() != pWavelengths->getCenterValues().size()) { QString buf = QString("Warning AceAlg014: The spectrum only provides spectral coverage for %1 of %2 bands.") .arg(resampledBands.size()).arg(pWavelengths->getCenterValues().size()); progress.report(buf.toStdString(), 0, WARNING, true); } BitMaskIterator iterChecker(getPixelsToProcess(), pElement); EncodingType type = pDescriptor->getDataType(); switchOnEncoding(type, aceAlg, NULL, pElement, pResults, spectrumValues, mpProgress); ///////////////////////////////////////////////////// vector<ColorType> layerColors, excludeColors; excludeColors.push_back(ColorType(0, 0, 0)); excludeColors.push_back(ColorType(255, 255, 255)); ColorType::getUniqueColors(iSignatureCount, layerColors, excludeColors); ColorType color; if (0 <= static_cast<int>(layerColors.size())) { color = layerColors[0]; } double dMaxValue = pResults->getStatistics()->getMax(); // Displays results for current signature displayThresholdResults(pResults, color, UPPER, mInputs.mThreshold, dMaxValue, layerOffset); // displayThresholdResults(pResults, color, UPPER, 0.6, dMaxValue, layerOffset); ////////////////////////////////////////////////////// /* Service<DesktopServices> pDesktop; SpatialDataWindow* pWindow = static_cast<SpatialDataWindow*>(pDesktop->createWindow(rname, SPATIAL_DATA_WINDOW)); SpatialDataView* pView = (pWindow == NULL) ? NULL : pWindow->getSpatialDataView(); if (pView == NULL) { std::string msg = "Unable to create view."; progress.report(msg, 0, ERRORS, true); return false; } pView->setPrimaryRasterElement(pResults); pView->createLayer(RASTER, pResults); */ return true; }
bool SamAlgorithm::processAll() { auto_ptr<Wavelengths> pWavelengths; ProgressTracker progress(getProgress(), "Starting SAM", "spectral", "C4320027-6359-4F5B-8820-8BC72BF1B8F0"); progress.getCurrentStep()->addProperty("Interactive", isInteractive()); RasterElement* pElement = getRasterElement(); if (pElement == NULL) { progress.report(SAMERR012, 0, ERRORS, true); return false; } progress.getCurrentStep()->addProperty("Cube", pElement->getName()); const RasterDataDescriptor* pDescriptor = static_cast<RasterDataDescriptor*>(pElement->getDataDescriptor()); VERIFY(pDescriptor != NULL); BitMaskIterator iter(getPixelsToProcess(), pElement); unsigned int numRows = iter.getNumSelectedRows(); unsigned int numColumns = iter.getNumSelectedColumns(); unsigned int numBands = pDescriptor->getBandCount(); Opticks::PixelOffset layerOffset(iter.getColumnOffset(), iter.getRowOffset()); // get cube wavelengths DynamicObject* pMetadata = pElement->getMetadata(); if (pMetadata != NULL) { pWavelengths.reset(new Wavelengths(pMetadata)); if (!pWavelengths->isEmpty() && (!pWavelengths->hasEndValues() || !pWavelengths->hasStartValues())) { pWavelengths->calculateFwhm(); } } VERIFY(pWavelengths.get() != NULL); int sig_index = 0; bool bSuccess = true; if (mInputs.mSignatures.empty()) { progress.report(SAMERR005, 0, ERRORS, true); return false; } int iSignatureCount = mInputs.mSignatures.size(); // Get colors for all the signatures vector<ColorType> layerColors, excludeColors; excludeColors.push_back(ColorType(0, 0, 0)); excludeColors.push_back(ColorType(255, 255, 255)); ColorType::getUniqueColors(iSignatureCount, layerColors, excludeColors); // Create a vector for the signature names vector<string> sigNames; // Create a pseudocolor results matrix if necessary RasterElement* pPseudocolorMatrix = NULL; RasterElement* pLowestSAMValueMatrix = NULL; // Check for multiple Signatures and if the user has selected // to combined multiple results in one pseudocolor output layer if (iSignatureCount > 1 && mInputs.mbCreatePseudocolor) { pPseudocolorMatrix = createResults(numRows, numColumns, mInputs.mResultsName); pLowestSAMValueMatrix = createResults(numRows, numColumns, "LowestSAMValue"); if (pPseudocolorMatrix == NULL || pLowestSAMValueMatrix == NULL ) { progress.report(SAMERR007, 0, ERRORS, true); return false; } FactoryResource<DataRequest> pseudoRequest; pseudoRequest->setWritable(true); string failedDataRequestErrorMessage = SpectralUtilities::getFailedDataRequestErrorMessage(pseudoRequest.get(), pPseudocolorMatrix); DataAccessor pseudoAccessor = pPseudocolorMatrix->getDataAccessor(pseudoRequest.release()); if (!pseudoAccessor.isValid()) { string msg = "Unable to access results."; if (!failedDataRequestErrorMessage.empty()) { msg += "\n" + failedDataRequestErrorMessage; } progress.report(msg, 0, ERRORS, true); return false; } FactoryResource<DataRequest> lsvRequest; lsvRequest->setWritable(true); failedDataRequestErrorMessage = SpectralUtilities::getFailedDataRequestErrorMessage(lsvRequest.get(), pLowestSAMValueMatrix); DataAccessor lowestSamValueAccessor = pLowestSAMValueMatrix->getDataAccessor(lsvRequest.release()); if (!lowestSamValueAccessor.isValid()) { string msg = "Unable to access results."; if (!failedDataRequestErrorMessage.empty()) { msg += "\n" + failedDataRequestErrorMessage; } progress.report(msg, 0, ERRORS, true); return false; } //Lets zero out all the results incase we connect to an existing matrix. float* pPseudoValue = NULL; float* pLowestValue = NULL; for (unsigned int row_ctr = 0; row_ctr < numRows; row_ctr++) { for (unsigned int col_ctr = 0; col_ctr < numColumns; col_ctr++) { if (!pseudoAccessor.isValid() || !lowestSamValueAccessor.isValid()) { progress.report("Unable to access results.", 0, ERRORS, true); return false; } pLowestValue = reinterpret_cast<float*>(lowestSamValueAccessor->getColumn()); pPseudoValue = reinterpret_cast<float*>(pseudoAccessor->getColumn()); //Initialize the matrices *pPseudoValue = 0.0f; *pLowestValue = 180.0f; pseudoAccessor->nextColumn(); lowestSamValueAccessor->nextColumn(); } pseudoAccessor->nextRow(); lowestSamValueAccessor->nextRow(); } } RasterElement* pResults = NULL; bool resultsIsTemp = false; // Processes each selected signature one at a time and // accumulates results for (sig_index = 0; bSuccess && (sig_index < iSignatureCount) && !mAbortFlag; sig_index++) { // Get the spectrum Signature* pSignature = mInputs.mSignatures[sig_index]; // Create the results matrix sigNames.push_back(pSignature->getName()); std::string rname = mInputs.mResultsName; if (iSignatureCount > 1 && !mInputs.mbCreatePseudocolor) { rname += " " + sigNames.back(); } else if (iSignatureCount > 1) { rname += "SamTemp"; resultsIsTemp = true; } pResults = createResults(numRows, numColumns, rname); if (pResults == NULL) { bSuccess = false; break; } //Send the message to the progress object QString messageSigNumber = QString("Processing Signature %1 of %2 : SAM running on signature %3") .arg(sig_index+1).arg(iSignatureCount).arg(QString::fromStdString(sigNames.back())); string message = messageSigNumber.toStdString(); vector<double> spectrumValues; vector<int> resampledBands; bSuccess = resampleSpectrum(pSignature, spectrumValues, *pWavelengths.get(), resampledBands); // Check for limited spectral coverage and warning log if (bSuccess && pWavelengths->hasCenterValues() && resampledBands.size() != pWavelengths->getCenterValues().size()) { QString buf = QString("Warning SamAlg014: The spectrum only provides spectral coverage for %1 of %2 bands.") .arg(resampledBands.size()).arg(pWavelengths->getCenterValues().size()); progress.report(buf.toStdString(), 0, WARNING, true); } if (bSuccess) { BitMaskIterator iterChecker(getPixelsToProcess(), pElement); SamAlgInput samInput(pElement, pResults, spectrumValues, &mAbortFlag, iterChecker, resampledBands); //Output Structure SamAlgOutput samOutput; // Reports current Spectrum SAM is running on mta::ProgressObjectReporter reporter(message, getProgress()); // Initializes all threads mta::MultiThreadedAlgorithm<SamAlgInput, SamAlgOutput, SamThread> mtaSam(Service<ConfigurationSettings>()->getSettingThreadCount(), samInput, samOutput, &reporter); // Calculates spectral angle for current signature mtaSam.run(); if (samInput.mpResultsMatrix == NULL) { Service<ModelServices>()->destroyElement(pResults); progress.report(SAMERR006, 0, ERRORS, true); mAbortFlag = false; return false; } if ((isInteractive() || mInputs.mbDisplayResults) && iSignatureCount > 1 && mInputs.mbCreatePseudocolor) { // Merges results in to one output layer if a Pseudocolor // output layer has been selected FactoryResource<DataRequest> pseudoRequest, currentRequest, lowestRequest; pseudoRequest->setWritable(true); string failedDataRequestErrorMessage = SpectralUtilities::getFailedDataRequestErrorMessage(pseudoRequest.get(), pPseudocolorMatrix); DataAccessor daPseudoAccessor = pPseudocolorMatrix->getDataAccessor(pseudoRequest.release()); if (!daPseudoAccessor.isValid()) { string msg = "Unable to access data."; if (!failedDataRequestErrorMessage.empty()) { msg += "\n" + failedDataRequestErrorMessage; } progress.report(msg, 0, ERRORS, true); return false; } DataAccessor daCurrentAccessor = pResults->getDataAccessor(currentRequest.release()); lowestRequest->setWritable(true); failedDataRequestErrorMessage = SpectralUtilities::getFailedDataRequestErrorMessage(lowestRequest.get(), pLowestSAMValueMatrix); DataAccessor daLowestSAMValue = pLowestSAMValueMatrix->getDataAccessor(lowestRequest.release()); if (!daLowestSAMValue.isValid()) { string msg = "Unable to access data."; if (!failedDataRequestErrorMessage.empty()) { msg += "\n" + failedDataRequestErrorMessage; } progress.report(msg, 0, ERRORS, true); return false; } float* pPseudoValue = NULL; float* pCurrentValue = NULL; float* pLowestValue = NULL; for (unsigned int row_ctr = 0; row_ctr < numRows; row_ctr++) { for (unsigned int col_ctr = 0; col_ctr < numColumns; col_ctr++) { if (!daPseudoAccessor.isValid() || !daCurrentAccessor.isValid()) { Service<ModelServices>()->destroyElement(pResults); progress.report("Unable to access data.", 0, ERRORS, true); return false; } daPseudoAccessor->toPixel(row_ctr, col_ctr); daCurrentAccessor->toPixel(row_ctr, col_ctr); pPseudoValue = reinterpret_cast<float*>(daPseudoAccessor->getColumn()); pCurrentValue = reinterpret_cast<float*>(daCurrentAccessor->getColumn()); daLowestSAMValue->toPixel(row_ctr, col_ctr); pLowestValue = reinterpret_cast<float*>(daLowestSAMValue->getColumn()); if (*pCurrentValue <= mInputs.mThreshold) { if (*pCurrentValue < *pLowestValue) { *pPseudoValue = sig_index+1; *pLowestValue = *pCurrentValue; } } } } } else { ColorType color; if (sig_index <= static_cast<int>(layerColors.size())) { color = layerColors[sig_index]; } double dMaxValue = pResults->getStatistics()->getMax(); // Displays results for current signature displayThresholdResults(pResults, color, LOWER, mInputs.mThreshold, dMaxValue, layerOffset); } //If we are on the last signature then destroy the lowest value Matrix if (sig_index == iSignatureCount-1) { if (pLowestSAMValueMatrix != NULL) { Service<ModelServices>()->destroyElement(pLowestSAMValueMatrix); pLowestSAMValueMatrix = NULL; } } } } //End of Signature Loop Counter if (resultsIsTemp || !bSuccess) { Service<ModelServices>()->destroyElement(pResults); pResults = NULL; } if (bSuccess) { // Displays final Pseudocolor output layer results if ((isInteractive() || mInputs.mbDisplayResults) && iSignatureCount > 1 && mInputs.mbCreatePseudocolor) { displayPseudocolorResults(pPseudocolorMatrix, sigNames, layerOffset); } } // Aborts gracefully after clean up if (mAbortFlag) { progress.abort(); mAbortFlag = false; return false; } if (bSuccess) { if (pPseudocolorMatrix != NULL) { mpResults = pPseudocolorMatrix; mpResults->updateData(); } else if (pResults != NULL) { mpResults = pResults; mpResults->updateData(); } else { progress.report(SAMERR016, 0, ERRORS, true); return false; } progress.report(SAMNORM200, 100, NORMAL); } progress.getCurrentStep()->addProperty("Display Layer", mInputs.mbDisplayResults); progress.getCurrentStep()->addProperty("Threshold", mInputs.mThreshold); progress.upALevel(); return bSuccess; }