diff --git a/app/otbSARMetadataCorrection.cxx b/app/otbSARMetadataCorrection.cxx index ff4b86f60db1f13efbefa968411f8a0f583f737e..669a59fc820e088962762dafaa85a77f24d7846d 100644 --- a/app/otbSARMetadataCorrection.cxx +++ b/app/otbSARMetadataCorrection.cxx @@ -182,30 +182,67 @@ void getCorners(otb::ImageKeywordlist imgKWL, std::vector<double *>* vector_lonl // Get GCP // void getGCP(otb::ImageMetadataInterfaceBase::Pointer metadataInterface, + int gcpcount, otb::ImageKeywordlist kwl, std::vector<double *>* vector_lonlat, std::vector<int *>* vector_colrow, std::vector<int> & vector_ind) { - // Get the count f GCPs - int gcpcount = metadataInterface->GetGCPCount(); + // Get the count of GCPs and check if metadataInterface is consistent + // Sometimes (for S1 IW) metadataInterace is not updated + int gcpcount_interface = metadataInterface->GetGCPCount(); - // Loop on all GCPs - for (int gcpIdx = 0; gcpIdx < gcpcount; ++gcpIdx) + // if consistent, use it + if (gcpcount == gcpcount_interface) { - // Fill our vectors - // Lon lat - double * gcpLonLat = new double[2]; - gcpLonLat[0] = static_cast<double>(metadataInterface->GetGCPX(gcpIdx)); - gcpLonLat[1] = static_cast<double>(metadataInterface->GetGCPY(gcpIdx)); - vector_lonlat->push_back(gcpLonLat); - - // idx - vector_ind.push_back(std::atoi(metadataInterface->GetGCPId(gcpIdx).c_str())); - - // col row - int * gcpColRow = new int[2]; - gcpColRow[0] = metadataInterface->GetGCPCol(gcpIdx); - gcpColRow[1] = metadataInterface->GetGCPRow(gcpIdx); - vector_colrow->push_back(gcpColRow); + // Loop on all GCPs + for (int gcpIdx = 0; gcpIdx < gcpcount; ++gcpIdx) + { + // Fill our vectors + // Lon lat + double * gcpLonLat = new double[2]; + gcpLonLat[0] = static_cast<double>(metadataInterface->GetGCPX(gcpIdx)); + gcpLonLat[1] = static_cast<double>(metadataInterface->GetGCPY(gcpIdx)); + vector_lonlat->push_back(gcpLonLat); + + // idx + vector_ind.push_back(std::atoi(metadataInterface->GetGCPId(gcpIdx).c_str())); + + // col row + int * gcpColRow = new int[2]; + gcpColRow[0] = metadataInterface->GetGCPCol(gcpIdx); + gcpColRow[1] = metadataInterface->GetGCPRow(gcpIdx); + vector_colrow->push_back(gcpColRow); + } + } + // else use the kwl + else + { + // Loop on all GCPs + for (int gcpIdx = 0; gcpIdx < gcpcount; ++gcpIdx) + { + char prefix[1024]; + + std::stringstream stream; + + // Fill our vectors + // Lon lat + double * gcpLonLat = new double[2]; + sprintf(prefix, "%s[%d].%s", GCP_PREFIX.c_str(), gcpIdx, keyWorldPtLon.c_str()); + gcpLonLat[0] = std::atof(kwl.GetMetadataByKey(prefix).c_str()); + sprintf(prefix, "%s[%d].%s", GCP_PREFIX.c_str(), gcpIdx, keyWorldPtLat.c_str()); + gcpLonLat[1] = std::atof(kwl.GetMetadataByKey(prefix).c_str()); + vector_lonlat->push_back(gcpLonLat); + + // idx + vector_ind.push_back(gcpIdx); + + // col row + int * gcpColRow = new int[2]; + sprintf(prefix, "%s[%d].%s", GCP_PREFIX.c_str(), gcpIdx, keyImPtX.c_str()); + gcpColRow[0] = std::atoi(kwl.GetMetadataByKey(prefix).c_str()); + sprintf(prefix, "%s[%d].%s", GCP_PREFIX.c_str(), gcpIdx, keyImPtY.c_str()); + gcpColRow[1] = std::atoi(kwl.GetMetadataByKey(prefix).c_str()); + vector_colrow->push_back(gcpColRow); + } } } @@ -377,8 +414,18 @@ private: ImageKeywordlist outputKWL = SARPtr->GetImageKeywordlist(); + // Check with metadataInterface and keyword List int gcpcount = metadataInterface->GetGCPCount(); + if (gcpcount == 0) + { + int gcpcount_kwl = std::atoi(outputKWL.GetMetadataByKey(GCP_NUMBER_KEY).c_str()); + if (gcpcount_kwl > 5) + { + gcpcount = gcpcount_kwl; + } + } + std::vector<double *> * vector_lonlat = new std::vector<double *>(); std::vector<int *> * vector_colrow = new std::vector<int *>(); std::vector<int> vector_ind; @@ -404,7 +451,7 @@ private: { // If GCPs => Retrieve its // Get latitude and longitude for each of its - getGCP(metadataInterface, vector_lonlat, vector_colrow, vector_ind); + getGCP(metadataInterface, gcpcount, outputKWL, vector_lonlat, vector_colrow, vector_ind); } @@ -429,7 +476,6 @@ private: filterDEMClosestHgt->Update(); filterDEMClosestHgt->GetDEMHgt(vector_hgt); - // Add or update GCP into the output KWL (according to the sensor (GCPs already present or not)) if (gcpcount == 0) {