vector<ImportDescriptor*> SignatureImporter::getImportDescriptors(const string& filename) { vector<ImportDescriptor*> descriptors; if (filename.empty()) { return descriptors; } LargeFileResource pSigFile; if (!pSigFile.open(filename, O_RDONLY | O_BINARY, S_IREAD)) { return descriptors; } // load the data FactoryResource<DynamicObject> pMetadata; VERIFYRV(pMetadata.get() != NULL, descriptors); bool readError = false; string line; string unitName("Reflectance"); UnitType unitType(REFLECTANCE); double unitScale(1.0); // parse the metadata for (line = pSigFile.readLine(&readError); (readError == false) && (line.find('=') != string::npos); line = pSigFile.readLine(&readError)) { vector<string> metadataEntry; trim(line); split(metadataEntry, line, is_any_of("=")); if (metadataEntry.size() == 2) { string key = metadataEntry[0]; string value = metadataEntry[1]; trim(key); trim(value); if (ends_with(key, "Bands") || key == "Pixels") { pMetadata->setAttribute(key, StringUtilities::fromXmlString<unsigned long>(value)); } else if (key == "UnitName") { unitName = value; } else if (key == "UnitType") { unitType = StringUtilities::fromXmlString<UnitType>(value); } else if (key == "UnitScale") { unitScale = StringUtilities::fromXmlString<double>(value); } else { pMetadata->setAttribute(key, value); } } } if ((readError == true) && (pSigFile.eof() != 1)) { return descriptors; } // Verify that the next line contains float float pairs vector<string> dataEntry; trim(line); split(dataEntry, line, is_space()); if (dataEntry.size() != 2) { return descriptors; } bool error = false; StringUtilities::fromXmlString<float>(dataEntry[0], &error); !error && StringUtilities::fromXmlString<float>(dataEntry[1], &error); if (error) { return descriptors; } string datasetName = dv_cast<string>(pMetadata->getAttribute("Name"), filename); ImportDescriptorResource pImportDescriptor(datasetName, "Signature"); VERIFYRV(pImportDescriptor.get() != NULL, descriptors); SignatureDataDescriptor* pDataDescriptor = dynamic_cast<SignatureDataDescriptor*>(pImportDescriptor->getDataDescriptor()); VERIFYRV(pDataDescriptor != NULL, descriptors); FactoryResource<SignatureFileDescriptor> pFileDescriptor; VERIFYRV(pFileDescriptor.get() != NULL, descriptors); pFileDescriptor->setFilename(filename); FactoryResource<Units> pReflectanceUnits; VERIFYRV(pReflectanceUnits.get() != NULL, descriptors); pReflectanceUnits->setUnitName(unitName); pReflectanceUnits->setUnitType(unitType); if (unitScale != 0.0) { pReflectanceUnits->setScaleFromStandard(1.0 / unitScale); } pDataDescriptor->setUnits("Reflectance", pReflectanceUnits.get()); pFileDescriptor->setUnits("Reflectance", pReflectanceUnits.get()); pDataDescriptor->setFileDescriptor(pFileDescriptor.get()); pDataDescriptor->setMetadata(pMetadata.get()); descriptors.push_back(pImportDescriptor.release()); return descriptors; }
bool LandsatEtmPlusImporter::readHeader(const string& strInFstHeaderFileName) { if (strInFstHeaderFileName.empty()) { return false; } //The HTM header file contains information for Band 6 Channel 1 and 2. //This set extracts the info needed to process HTM(band 6) header mFieldHTM.clear(); string baseFileName = strInFstHeaderFileName.substr(0, strInFstHeaderFileName.length() - 7); FactoryResource<Filename> filename; filename->setFullPathAndName(baseFileName); FactoryResource<FileFinder> fileFinder; string htmFileName = filename->getFileName() + "HTM.FST"; if (fileFinder->findFile(filename->getPath(), htmFileName)) { fileFinder->findNextFile(); fileFinder->getFullPath(htmFileName); } LargeFileResource htmFile; if (htmFile.open(htmFileName, O_RDONLY, S_IREAD)) { //process HTM header vector<char> buf(5120); size_t count = static_cast<size_t>(htmFile.read(&buf.front(), 5120)); VERIFY(htmFile.eof()); string htmHeaderFull(&buf.front(), count); vector<string> htmHeaderLines = StringUtilities::split(htmHeaderFull, '\n'); if (!parseHeader(htmHeaderLines, mFieldHTM)) //parse band 6 header { mFieldHTM.clear(); } } htmFile.close(); // The HRF Header file contains the parameters for Bands 1-5 and 7. mFieldHRF.clear(); string hrfFileName = filename->getFileName() + "HRF.FST"; if (fileFinder->findFile(filename->getPath(), hrfFileName)) { fileFinder->findNextFile(); fileFinder->getFullPath(hrfFileName); } LargeFileResource hrfFile; if (hrfFile.open(hrfFileName, O_RDONLY, S_IREAD)) { //process HRF header vector<char> buf(5120); size_t count = static_cast<size_t>(hrfFile.read(&buf.front(), 5120)); VERIFY(hrfFile.eof()); string hrfHeaderFull(&buf.front(), count); vector<string> hrfHeaderLines = StringUtilities::split(hrfHeaderFull, '\n'); if (!parseHeader(hrfHeaderLines, mFieldHRF)) //parse bands 1-5 and 7 header { mFieldHRF.clear(); } } hrfFile.close(); // The HPN Header file contains the parameters for Band 8. mFieldHPN.clear(); string hpnFileName = filename->getFileName() + "HPN.FST"; if (fileFinder->findFile(filename->getPath(), hpnFileName)) { fileFinder->findNextFile(); fileFinder->getFullPath(hpnFileName); } LargeFileResource hpnFile; if (hpnFile.open(hpnFileName, O_RDONLY, S_IREAD)) { //process HPN header vector<char> buf(5120); size_t count = static_cast<size_t>(hpnFile.read(&buf.front(), 5120)); VERIFY(hpnFile.eof()); string hpnHeaderFull(&buf.front(), count); vector<string> hpnHeaderLines = StringUtilities::split(hpnHeaderFull, '\n'); if (!parseHeader(hpnHeaderLines, mFieldHPN)) //parse band 8 header { mFieldHPN.clear(); } } hpnFile.close(); return (!mFieldHRF.empty() || !mFieldHTM.empty() || !mFieldHPN.empty()); }
bool SignatureImporter::execute(PlugInArgList* pInArgList, PlugInArgList* OutArgList) { VERIFY(pInArgList != NULL); ProgressTracker progress(pInArgList->getPlugInArgValue<Progress>(Executable::ProgressArg()), "Loading spectral signature", "spectral", "5A9F8379-7D7D-4575-B78B-305AE0DFC66D"); Signature* pSignature = pInArgList->getPlugInArgValue<Signature>(Importer::ImportElementArg()); VERIFY(pSignature != NULL); DataDescriptor* pDataDescriptor = pSignature->getDataDescriptor(); VERIFY(pDataDescriptor != NULL); FileDescriptor* pFileDescriptor = pDataDescriptor->getFileDescriptor(); VERIFY(pFileDescriptor != NULL); progress.getCurrentStep()->addProperty("filename", pFileDescriptor->getFilename().getFullPathAndName()); DynamicObject* pMetadata = pSignature->getMetadata(); VERIFY(pMetadata != NULL); string warningMsg; LargeFileResource pSigFile; VERIFY(pSigFile.open(pFileDescriptor->getFilename().getFullPathAndName(), O_RDONLY | O_BINARY, S_IREAD)); const Units* pUnits = pSignature->getUnits("Reflectance"); VERIFY(pUnits != NULL); // Read the signature data vector<double> wavelengthData, reflectanceData; int64_t fileSize = pSigFile.fileLength(); bool readError = false; size_t largeValueCount(0); for (string line = pSigFile.readLine(&readError); readError == false; line = pSigFile.readLine(&readError)) { if (isAborted()) { progress.report("Importer aborted", 0, ABORT, true); return false; } int64_t fileLocation = pSigFile.tell(); progress.report("Loading signature data", static_cast<int>(fileLocation * 100.0 / fileSize), NORMAL); trim(line); if (line.empty()) { continue; } if (line.find('=') == string::npos) { double wavelength = 0.0, reflectance = 0.0; vector<string> dataEntry; split(dataEntry, line, is_space()); bool error = true; if (dataEntry.size() >= 1) { wavelength = StringUtilities::fromXmlString<double>(dataEntry[0], &error); if (wavelength > 50.0) { // Assume wavelength values are in nanometers and convert to microns wavelength = Wavelengths::convertValue(wavelength, NANOMETERS, MICRONS); } } if (!error && dataEntry.size() == 2) { reflectance = StringUtilities::fromXmlString<double>(dataEntry[1], &error); // Since the signature file may not have contained info on units and unitScale (defaults to values of // "REFLECTANCE" and "1.0"), we need to check that the reflectance value is properly scaled. // In theory, a valid reflectance value should be between 0 and 1, but real data may extend beyond these // limits due to errors that occurred in collection, calibration, conversion, etc. We're assuming that a // value greater than 2.0 indicates that the value was scaled by a factor other than 1.0 - a common data // collection practice is to store a data value as an integer value equal to the actual value multiplied // by a scaling factor. This saves storage space while preserving precision. 10000 is a very common // scaling factor and the one we will assume was used. Right now we'll just count the number of large values. // If more than half the values are large, we will assume they were scaled and divide all the values by 10000. if (pUnits->getUnitType() == REFLECTANCE && pUnits->getScaleFromStandard() == 1.0 && fabs(reflectance) > 2.0) { ++largeValueCount; } } if (error) { progress.report("Error parsing signature data", 0, ERRORS, true); } wavelengthData.push_back(wavelength); reflectanceData.push_back(reflectance); } } if ((readError == true) && (pSigFile.eof() != 1)) { progress.report("Unable to read signature file", 0, ERRORS, true); return false; } // check for need to scale the values, i.e., at least half the values are large if (reflectanceData.empty() == false && largeValueCount > 0 && largeValueCount >= (reflectanceData.size() / 2)) { warningMsg += (warningMsg.empty() ? "" : "\n"); warningMsg += "Values appear to have been scaled - values have been divided by 10000"; for (vector<double>::iterator it = reflectanceData.begin(); it != reflectanceData.end(); ++it) { *it *= 0.0001; // divide by 10000 } } pSignature->setData("Wavelength", wavelengthData); pSignature->setData("Reflectance", reflectanceData); if (warningMsg.empty()) { progress.report("Spectral signature loaded", 100, NORMAL); } else { progress.report(warningMsg, 100, WARNING); progress.getCurrentStep()->addMessage(warningMsg, "spectral", "770EB61A-71CD-4f83-8C7B-E0FEF3D7EB8D"); } progress.upALevel(); return true; }