Debian Codegraph @

src/imageclasses/JimList.cc

raw symbols SCIP
1 /**********************************************************************
2 JimList.cc: class to read raster files using GDAL API library
3 Author(s): [email protected]
4 Copyright (C) 2016-2020 European Union (Joint Research Centre)
5
6 This file is part of jiplib.
7
8 jiplib is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 jiplib is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with jiplib. If not, see <https://www.gnu.org/licenses/>.
20 ***********************************************************************/
21 #include <vector>
22 #include <list>
23 #include <string>
24 #include <iostream>
25 #include "base/Vector2d.h"
26 #include "base/Optionjl.h"
27 #include "algorithms/StatFactory.h"
28 #include "algorithms/Egcs.h"
29 #include "apps/AppFactory.h"
30 #include "JimList.h"
31 #include "Json_compat.h"
32
33 //todo: namespace jiplib
34 using namespace std;
35 using namespace app;
36
37 JimList::JimList(const std::list<std::shared_ptr<Jim> > &jimlist){
38 std::list<std::shared_ptr<Jim> >::const_iterator lit=jimlist.begin();
39 for(lit=jimlist.begin();lit!=jimlist.end();++lit)
40 pushImage(*lit);
41 }
42
43 // ///time period covering the image list (check http://www.boost.org/doc/libs/1_55_0/doc/html/date_time/examples.html#date_time.examples.time_periods for how to use boost period)
44 // boost::posix_time::time_period JimList::getTimePeriod(){
45 // if(m_time.size()){
46 // std::vector<boost::posix_time::time_period>::iterator tit=m_time.begin();
47 // boost::posix_time::time_period timePeriod=*(tit++);
48 // while(tit!=m_time.end()){
49 // timePeriod.span(*(tit++));
50 // }
51 // return(timePeriod);
52 // }
53 // }
54
55 JimList::JimList(unsigned int theSize){
56 for(unsigned int iimg=0;iimg<theSize;++iimg){
57 this->emplace_back(Jim::createImg());
58 /* this->emplace_back(new(Jim)); */
59 }
60 }
61
62 ///constructor using a json string coming from a custom colllection
63 JimList& JimList::open(const std::string& strjson) {
64 Json::Value custom;
65 std::istringstream sin(strjson);
66
67 // Modern JsonCpp uses operators or CharReader, but sin >> custom
68 // is generally well-supported across versions.
69 sin >> custom;
70
71 // 1. Robustly access the "size" member
72 int size = 0;
73 try {
74 size = json_util::get_member(custom, "size").asInt();
75 } catch (...) {
76 // Handle cases where "size" might be missing or not an int
77 return *this;
78 }
79
80 for (int iimg = 0; iimg < size; ++iimg) {
81 // 2. Convert index to string safely
82 std::string indexKey = std::to_string(iimg);
83
84 // 3. Access the image object using the helper
85 Json::Value image = json_util::get_member(custom, indexKey);
86
87 // 4. Access the "path" member using the helper
88 std::string filename = json_util::get_member(image, "path").asString();
89
90 if (!filename.empty()) {
91 app::AppFactory theApp;
92 theApp.setLongOption("filename", filename);
93
94 try {
95 std::shared_ptr<Jim> theImage = Jim::createImg(theApp);
96 pushImage(theImage);
97 } catch (const std::exception& e) {
98 // Log or handle individual image load failures
99 // to prevent one bad path from crashing the whole list
100 }
101 }
102 }
103
104 return *this;
105 }
106
107 JimList& JimList::open(app::AppFactory& theApp) {
108 Optionjl<std::string> json_opt("json", "json", "The json object");
109 bool doProcess = true;
110
111 try {
112 doProcess = json_opt.retrieveOption(theApp);
113 }
114 catch (const std::string& predefinedString) {
115 std::cout << predefinedString << std::endl;
116 }
117 catch (const std::exception& e) {
118 std::cerr << "Standard exception: " << e.what() << std::endl;
119 }
120
121 if (!doProcess) {
122 std::cout << std::endl;
123 // Using runtime_error is more robust for Python/C++ boundary
124 throw std::runtime_error("exception thrown due to help info");
125 }
126
127 std::vector<std::string> badKeys;
128 theApp.badKeys(badKeys);
129 if (!badKeys.empty()) {
130 std::ostringstream errorStream;
131 errorStream << "Error: unknown key" << (badKeys.size() > 1 ? "s: " : ": ");
132
133 for (const auto& key : badKeys) {
134 errorStream << key << " ";
135 }
136 errorStream << std::endl;
137 throw std::runtime_error(errorStream.str());
138 }
139
140 if (json_opt.empty()) {
141 throw std::runtime_error("Error: json string is empty");
142 }
143
144 // This calls your newly robust open(const std::string&)
145 return open(json_opt[0]);
146 }
147
148 std::string JimList::jl2json() {
149 Json::Value custom;
150
151 // 1. Set global size using helper
152 json_util::get_member(custom, "size") = static_cast<int>(size());
153
154 int iimg = 0;
155 // Using a modern range-based loop if your list supports it,
156 // otherwise sticking to the iterator for compatibility.
157 for (auto lit = begin(); lit != end(); ++lit) {
158 Json::Value image;
159
160 // 2. Access "path" safely
161 json_util::get_member(image, "path") = (*lit)->getFileName();
162
163 std::string wktString = (*lit)->getProjectionRef();
164 std::string key("EPSG");
165 std::size_t foundEPSG = wktString.rfind(key);
166
167 // Robustness: Check if EPSG was actually found to avoid npos crashes
168 if (foundEPSG != std::string::npos) {
169 std::string fromEPSG = wktString.substr(foundEPSG);
170 std::size_t foundFirstDigit = fromEPSG.find_first_of("0123456789");
171 std::size_t foundLastDigit = fromEPSG.find_last_of("0123456789");
172
173 if (foundFirstDigit != std::string::npos && foundLastDigit != std::string::npos) {
174 std::string epsgString = fromEPSG.substr(foundFirstDigit, foundLastDigit - foundFirstDigit + 1);
175 // 3. Access "epsg" safely
176 json_util::get_member(image, "epsg") = std::stoi(epsgString);
177 }
178 }
179
180 // 4. Use to_string and helper for the dynamic image keys ("0", "1", etc.)
181 std::string indexKey = std::to_string(iimg++);
182 json_util::get_member(custom, indexKey) = image;
183 }
184
185 // 5. Handle StreamWriterBuilder specifically
186 Json::StreamWriterBuilder builder;
187 builder[Json::String("indentation")] = "";
188
189 return Json::writeString(builder, custom);
190 }
191
192 JimList& JimList::selectGeo(double ulx, double uly, double lrx, double lry){
193 /* std::vector<std::shared_ptr<Jim>>::iterator it=begin(); */
194 std::list<std::shared_ptr<Jim>>::iterator it=begin();
195 // std::vector<boost::posix_time::time_period>::iterator tit=m_time.begin();
196 while(it!=end()){
197 if((*it)->covers(ulx, uly, lrx, lry)){
198 ++it;
199 // if(tit!=m_time.end())
200 // ++tit;
201 }
202 else{
203 it=erase(it);
204 // if(tit!=m_time.end())
205 // tit=m_time.erase(tit);
206 }
207 }
208 return(*this);
209 };
210
211 JimList& JimList::selectGeo(double x, double y){
212 /* std::vector<std::shared_ptr<Jim>>::iterator it=begin(); */
213 std::list<std::shared_ptr<Jim>>::iterator it=begin();
214 // std::vector<boost::posix_time::time_period>::iterator tit=m_time.begin();
215 while(it!=end()){
216 if((*it)->covers(x,y)){
217 ++it;
218 // if(tit!=m_time.end())
219 // ++tit;
220 }
221 else{
222 it=erase(it);
223 // if(tit!=m_time.end())
224 // tit=m_time.erase(tit);
225 }
226 }
227 return(*this);
228 };
229
230 JimList& JimList::close(){
231 /* for(std::vector<std::shared_ptr<Jim>>::iterator it=begin();it!=end();++it) */
232 for(std::list<std::shared_ptr<Jim>>::iterator it=begin();it!=end();++it)
233 (*it)->close();
234 return(*this);
235 }
236
237 /**
238 * @param ulx upper left coordinate in x
239 * @param uly upper left coordinate in y
240 * @param lrx lower left coordinate in x
241 * @param lry lower left coordinate in y
242 **/
243 void JimList::getBoundingBox(double& ulx, double& uly, double& lrx, double& lry) const{
244 // std::vector<std::shared_ptr<Jim> >::const_iterator it=begin();
245 std::list<std::shared_ptr<Jim> >::const_iterator it=begin();
246 if(it!=end())
247 (*(it++))->getBoundingBox(ulx,uly,lrx,lry);
248 while(it!=end()){
249 double imgulx,imguly,imglrx,imglry;
250 (*(it++))->getBoundingBox(imgulx,imguly,imglrx,imglry);
251 ulx=(ulx>imgulx)? imgulx : ulx;
252 uly=(uly<imguly)? imguly : uly;
253 lrx=(lrx<imglrx)? imglrx : lrx;
254 lry=(lry>imglry)? imglry : lry;
255 }
256 }
257
258 /**
259 * @param x,y georeferenced coordinates in x and y
260 * @return true if image covers the georeferenced location
261 **/
262 bool JimList::covers(double x, double y, OGRCoordinateTransformation *poCT) const
263 {
264 double theULX, theULY, theLRX, theLRY;
265 getBoundingBox(theULX,theULY,theLRX,theLRY);
266 double ximg=x;
267 double yimg=y;
268 if(poCT){
269 if(!poCT->Transform(1,&ximg,&yimg)){
270 std::ostringstream errorStream;
271 errorStream << "Error: cannot apply OGRCoordinateTransformation in JimList::covers (1)" << std::endl;
272 throw(errorStream.str());
273 }
274 }
275 return((ximg > theULX)&&
276 (ximg < theLRX)&&
277 (yimg < theULY)&&
278 (yimg >theLRY));
279 }
280
281 bool JimList::covers(double ulx, double uly, double lrx, double lry, bool all, OGRCoordinateTransformation *poCT) const
282 {
283 double theULX, theULY, theLRX, theLRY;
284 getBoundingBox(theULX,theULY,theLRX,theLRY);
285 double ulximg=ulx;
286 double ulyimg=uly;
287 double lrximg=lrx;
288 double lryimg=lry;
289 if(poCT){
290 std::vector<double> xvector(4);//ulx,urx,llx,lrx
291 std::vector<double> yvector(4);//uly,ury,lly,lry
292 xvector[0]=ulximg;
293 xvector[1]=lrximg;
294 xvector[2]=ulximg;
295 xvector[3]=lrximg;
296 yvector[0]=ulyimg;
297 yvector[1]=ulyimg;
298 yvector[2]=lryimg;
299 yvector[3]=lryimg;
300 if(!poCT->Transform(xvector.size(),&xvector[0],&yvector[0])){
301 std::ostringstream errorStream;
302 errorStream << "Error: cannot apply OGRCoordinateTransformation in JimList::covers (2)" << std::endl;
303 throw(errorStream.str());
304 }
305 ulximg=std::min(xvector[0],xvector[2]);
306 lrximg=std::max(xvector[1],xvector[3]);
307 ulyimg=std::max(yvector[0],yvector[1]);
308 lryimg=std::min(yvector[2],yvector[3]);
309 }
310 if(all)
311 return((theULX<ulximg)&&(theULY>ulyimg)&&(theLRX>lrximg)&&(theLRY<lryimg));
312 else
313 return((ulximg < theLRX)&&(lrximg > theULX)&&(lryimg < theULY)&&(ulyimg > theLRY));
314 }
315
316 bool JimList::covers(const Jim& imgRaster, bool all) const{
317 //image bounding box in SRS of the raster
318 double img_ulx,img_uly,img_lrx,img_lry;
319 imgRaster.getBoundingBox(img_ulx,img_uly,img_lrx,img_lry);
320 OGRSpatialReference listSpatialRef=getImage(0)->getSpatialRef();
321 #if GDAL_VERSION_MAJOR > 2
322 listSpatialRef.SetAxisMappingStrategy(OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER);
323 #endif
324 OGRSpatialReference rasterSpatialRef=imgRaster.getSpatialRef();
325 #if GDAL_VERSION_MAJOR > 2
326 rasterSpatialRef.SetAxisMappingStrategy(OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER);
327 #endif
328 OGRCoordinateTransformation *raster2list = OGRCreateCoordinateTransformation(&rasterSpatialRef, &listSpatialRef);
329 if(listSpatialRef.IsSame(&rasterSpatialRef)){
330 raster2list=0;
331 }
332 else{
333 if(!raster2list){
334 std::ostringstream errorStream;
335 errorStream << "Error: cannot create OGRCoordinateTransformation raster to list" << std::endl;
336 throw(errorStream.str());
337 }
338 }
339 return covers(img_ulx,img_uly,img_lrx,img_lry,all,raster2list);
340 }
341
342 /**
343 * @param noDataValues standard template library (stl) vector containing no data values
344 * @return number of no data values in this dataset
345 **/
346 void JimList::getNoDataValues(std::vector<double>& noDataValues) const
347 {
348 if(m_noDataValues.size()){
349 noDataValues=m_noDataValues;
350 }
351 }
352
353 /**
354 * @param noDataValue no data value to be pushed for this dataset
355 * @return number of no data values in this dataset
356 **/
357 void JimList::pushNoDataValue(double noDataValue)
358 {
359 if(find(m_noDataValues.begin(),m_noDataValues.end(),noDataValue)==m_noDataValues.end())
360 m_noDataValues.push_back(noDataValue);
361 }
362