1 | /* |
---|
2 | * Author : Gérald FENOY |
---|
3 | * |
---|
4 | * Copyright (c) 2015 GeoLabs SARL |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
7 | * of this software and associated documentation files (the "Software"), to deal |
---|
8 | * in the Software without restriction, including without limitation the rights |
---|
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
10 | * copies of the Software, and to permit persons to whom the Software is |
---|
11 | * furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
22 | * THE SOFTWARE. |
---|
23 | * |
---|
24 | * See Ref: http://hg.orfeo-toolbox.org/OTB/ Copyright |
---|
25 | * Some parts of this code are derived from ITK. See ITKCopyright.txt for |
---|
26 | * details. |
---|
27 | */ |
---|
28 | |
---|
29 | #include "service_internal_otb.h" |
---|
30 | |
---|
31 | using namespace otb::Wrapper; |
---|
32 | |
---|
33 | /** |
---|
34 | * The ZooWatcher list |
---|
35 | */ |
---|
36 | WatcherListType m_WatcherList; |
---|
37 | /** |
---|
38 | * A pointer to the conf maps containing the main.cfg settings |
---|
39 | */ |
---|
40 | maps* m_Conf; |
---|
41 | |
---|
42 | /** |
---|
43 | * The command to create a ZooWatcher and add it to the global m_WatcherList |
---|
44 | */ |
---|
45 | class MyCommand : public itk::Command |
---|
46 | { |
---|
47 | public: |
---|
48 | itkNewMacro( MyCommand ); |
---|
49 | public: |
---|
50 | |
---|
51 | /** |
---|
52 | * The method that defines the action to be taken by the command. |
---|
53 | * |
---|
54 | * @param caller an itk::Object pointer |
---|
55 | * @param event an itk::EventObject pointer |
---|
56 | */ |
---|
57 | void Execute(itk::Object *caller, const itk::EventObject & event) |
---|
58 | { |
---|
59 | Execute( (const itk::Object *)caller, event); |
---|
60 | } |
---|
61 | |
---|
62 | /** |
---|
63 | * The method that defines the action to be taken by the command. |
---|
64 | * Create a new ZooWatcher instance then add it to the m_WatcherList. |
---|
65 | * |
---|
66 | * @param caller a const itk::Object pointer |
---|
67 | * @param event an itk::EventObject pointer |
---|
68 | * @see ZooWatcher,ZooWatcher::SetConf |
---|
69 | */ |
---|
70 | void Execute(const itk::Object *caller, const itk::EventObject & event) |
---|
71 | { |
---|
72 | const AddProcessToWatchEvent* eventToWatch = dynamic_cast< const AddProcessToWatchEvent*> ( &event ); |
---|
73 | std::string m_CurrentDescription = eventToWatch->GetProcessDescription(); |
---|
74 | ZooWatcher * watch = new ZooWatcher(eventToWatch->GetProcess(), |
---|
75 | eventToWatch->GetProcessDescription()); |
---|
76 | watch->SetConf(&m_Conf); |
---|
77 | m_WatcherList.push_back(watch); |
---|
78 | } |
---|
79 | |
---|
80 | }; |
---|
81 | |
---|
82 | /** |
---|
83 | * Replace all occurence of from by to in a str string |
---|
84 | * |
---|
85 | * @param str the string to transform |
---|
86 | * @param from the string to replace |
---|
87 | * @param to the string used as replacement |
---|
88 | * @return the resulting string |
---|
89 | */ |
---|
90 | std::string ReplaceAll(std::string str, const std::string& from, const std::string& to) { |
---|
91 | size_t start_pos = 0; |
---|
92 | while((start_pos = str.find(from, start_pos)) != std::string::npos) { |
---|
93 | str.replace(start_pos, from.length(), to); |
---|
94 | start_pos += to.length(); // Handles case where 'to' is a substring of 'from' |
---|
95 | } |
---|
96 | return str; |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * Load and run an OTB Application corresponding to the service by using inputs parameters. |
---|
101 | * Define the m_Conf |
---|
102 | * |
---|
103 | * @param main_conf the conf maps containing the main.cfg settings |
---|
104 | * @param request the map containing the HTTP request |
---|
105 | * @param s the service structure |
---|
106 | * @param real_inputs the maps containing the inputs |
---|
107 | * @param real_outputs the maps containing the outputs |
---|
108 | */ |
---|
109 | int zoo_otb_support(maps** main_conf,map* request,service* s,maps **real_inputs,maps **real_outputs){ |
---|
110 | maps* m=*main_conf; |
---|
111 | maps* inputs=*real_inputs; |
---|
112 | maps* outputs=*real_outputs; |
---|
113 | map* tmp0=getMapFromMaps(*main_conf,"lenv","cwd"); |
---|
114 | char *ntmp=tmp0->value; |
---|
115 | map* tmp=NULL; |
---|
116 | int res=-1; |
---|
117 | |
---|
118 | std::vector<std::string> list = ApplicationRegistry::GetAvailableApplications(); |
---|
119 | if (list.size() == 0){ |
---|
120 | map* tmps=createMap("text","No OTB Application found."); |
---|
121 | addToMap(tmps,"code","InternalError"); |
---|
122 | printExceptionReportResponse(m,tmps); |
---|
123 | freeMap(&tmps); |
---|
124 | free(tmps); |
---|
125 | res=-1; |
---|
126 | } |
---|
127 | else{ |
---|
128 | for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it){ |
---|
129 | if(s->name==*it){ |
---|
130 | Application::Pointer m_Application=ApplicationRegistry::CreateApplication(*it); |
---|
131 | if (m_Application.IsNull()){ |
---|
132 | char tmpS[1024]; |
---|
133 | sprintf(tmpS, "The OTB Application %s cannot be loaded.", (*it).c_str()); |
---|
134 | map* tmps=createMap("text",tmpS); |
---|
135 | addToMap(tmps,"code","InternalError"); |
---|
136 | printExceptionReportResponse(m,tmps); |
---|
137 | freeMap(&tmps); |
---|
138 | free(tmps); |
---|
139 | res=-1; |
---|
140 | }else{ |
---|
141 | // Create Observer on AddProcessToWatchEvent |
---|
142 | m_Conf=m; |
---|
143 | MyCommand::Pointer myCommand = MyCommand::New(); |
---|
144 | m_Application->AddObserver(AddProcessToWatchEvent(), myCommand); |
---|
145 | char tmpS[1024]; |
---|
146 | const std::vector<std::string> appKeyList = m_Application->GetParametersKeys(true); |
---|
147 | for (unsigned int i = 0; i < appKeyList.size(); i++){ |
---|
148 | const std::string paramKey(appKeyList[i]); |
---|
149 | std::vector<std::string> values; |
---|
150 | Parameter::Pointer param = m_Application->GetParameterByKey(paramKey); |
---|
151 | ParameterType type = m_Application->GetParameterType(paramKey); |
---|
152 | if (type != ParameterType_Group && paramKey!="inxml" && paramKey!="outxml"){ |
---|
153 | map* test=getMapFromMaps(inputs,paramKey.c_str(),"cache_file"); |
---|
154 | if(test==NULL){ |
---|
155 | test=getMapFromMaps(inputs,paramKey.c_str(),"inRequest"); |
---|
156 | map* tmpPath=getMapFromMaps(m,"main","tmpPath"); |
---|
157 | map* tmpSid=getMapFromMaps(m,"lenv","usid"); |
---|
158 | char tmp[1024]; |
---|
159 | map* tmpVal=getMapFromMaps(outputs,paramKey.c_str(),"mimeType"); |
---|
160 | if(test!=NULL && test->value!=NULL && strncasecmp(test->value,"true",4)==0){ |
---|
161 | test=getMapFromMaps(inputs,paramKey.c_str(),"value"); |
---|
162 | if(type == ParameterType_OutputImage){ |
---|
163 | ImagePixelType outPixType = ImagePixelType_float; |
---|
164 | if (strncasecmp(test->value,"uint8",5)==0) |
---|
165 | outPixType = ImagePixelType_uint8; |
---|
166 | else if (strncasecmp(test->value,"int16",5)==0) |
---|
167 | outPixType = ImagePixelType_int16; |
---|
168 | else if (strncasecmp(test->value,"uint16",6)==0) |
---|
169 | outPixType = ImagePixelType_uint16; |
---|
170 | else if (strncasecmp(test->value,"int32",5)==0) |
---|
171 | outPixType = ImagePixelType_int32; |
---|
172 | else if (strncasecmp(test->value,"uint32",6)==0) |
---|
173 | outPixType = ImagePixelType_uint32; |
---|
174 | else if (strncasecmp(test->value,"double",6)==0) |
---|
175 | outPixType = ImagePixelType_double; |
---|
176 | const char* ext="tiff"; |
---|
177 | if(tmpVal!=NULL){ |
---|
178 | if(strncasecmp(tmpVal->value,"image/jp2",9)==0) |
---|
179 | ext="j2k"; |
---|
180 | else |
---|
181 | if(strncasecmp(tmpVal->value,"image/png",9)==0) |
---|
182 | ext="png"; |
---|
183 | else |
---|
184 | if(strncasecmp(tmpVal->value,"image/jpeg",10)==0) |
---|
185 | ext="jpeg"; |
---|
186 | } |
---|
187 | sprintf(tmp,"%s/%s_%s.%s",tmpPath->value,s->name,tmpSid->value,ext); |
---|
188 | m_Application->SetParameterString(paramKey, tmp); |
---|
189 | setMapInMaps(inputs,paramKey.c_str(),"generated_file",tmp); |
---|
190 | dynamic_cast<OutputImageParameter *> (param.GetPointer())->SetPixelType(outPixType); |
---|
191 | }else{ |
---|
192 | if(test->value!=NULL) |
---|
193 | m_Application->SetParameterString(paramKey, test->value); |
---|
194 | } |
---|
195 | |
---|
196 | }else{ |
---|
197 | if(type == ParameterType_OutputVectorData){ |
---|
198 | char* ext="json"; |
---|
199 | if(tmpVal!=NULL){ |
---|
200 | if(strncasecmp(tmpVal->value,"text/xml",8)==0) |
---|
201 | ext="gml"; |
---|
202 | else |
---|
203 | if(strncasecmp(tmpVal->value,"applicaton/json",15)==0) |
---|
204 | ext="json"; |
---|
205 | else |
---|
206 | if(strncasecmp(tmpVal->value,"application/zip",14)==0) |
---|
207 | ext="shp"; |
---|
208 | else |
---|
209 | if(strncasecmp(tmpVal->value,"application/vnd.google-earth.kml+xml",36)==0) |
---|
210 | ext="kml"; |
---|
211 | } |
---|
212 | sprintf(tmp,"%s/%s_%s.%s",tmpPath->value,s->name,tmpSid->value,ext); |
---|
213 | m_Application->SetParameterString(paramKey, tmp); |
---|
214 | setMapInMaps(inputs,paramKey.c_str(),"generated_file",tmp); |
---|
215 | } |
---|
216 | else |
---|
217 | if(type == ParameterType_OutputFilename){ |
---|
218 | char* ext="txt"; |
---|
219 | if(tmpVal!=NULL){ |
---|
220 | if(strncasecmp(tmpVal->value,"text/xml",8)==0) |
---|
221 | ext="xml"; |
---|
222 | else |
---|
223 | if(strncasecmp(tmpVal->value,"text/csv",15)==0) |
---|
224 | ext="csv"; |
---|
225 | else |
---|
226 | if(strncasecmp(tmpVal->value,"application/zip",14)==0) |
---|
227 | ext="shp"; |
---|
228 | else |
---|
229 | if(strncasecmp(tmpVal->value,"application/vnd.google-earth.kml+xml",36)==0) |
---|
230 | ext="kml"; |
---|
231 | else |
---|
232 | if(strncasecmp(tmpVal->value,"application/vnd.google-earth.kmz",32)==0){ |
---|
233 | ext="kmz"; |
---|
234 | sprintf(tmp,"%s/%s_%sxt.%s",tmpPath->value,s->name,tmpSid->value,ext); |
---|
235 | m_Application->SetParameterString(paramKey, tmp); |
---|
236 | setMapInMaps(outputs,paramKey.c_str(),"expected_generated_file",tmp); |
---|
237 | } |
---|
238 | |
---|
239 | } |
---|
240 | sprintf(tmp,"%s/%s_%s.%s",tmpPath->value,s->name,tmpSid->value,ext); |
---|
241 | m_Application->SetParameterString(paramKey, tmp); |
---|
242 | setMapInMaps(inputs,paramKey.c_str(),"generated_file",tmp); |
---|
243 | } |
---|
244 | |
---|
245 | } |
---|
246 | }else{ |
---|
247 | if(type == ParameterType_InputImageList){ |
---|
248 | values.push_back(test->value); |
---|
249 | map* tmpPath=getMapFromMaps(inputs,paramKey.c_str(),"length"); |
---|
250 | if(tmpPath!=NULL){ |
---|
251 | int len=atoi(tmpPath->value); |
---|
252 | for(int k=1;k<len;k++){ |
---|
253 | char tmp[15]; |
---|
254 | sprintf(tmp,"cache_file_%d",k); |
---|
255 | map* tmpVal=getMapFromMaps(inputs,paramKey.c_str(),tmp); |
---|
256 | if(tmpVal!=NULL){ |
---|
257 | values.push_back(tmpVal->value); |
---|
258 | } |
---|
259 | } |
---|
260 | } |
---|
261 | dynamic_cast<InputImageListParameter *> (param.GetPointer())->SetListFromFileName(values); |
---|
262 | } |
---|
263 | else |
---|
264 | if(type == ParameterType_InputVectorData || type == ParameterType_InputFilename){ |
---|
265 | map* tmpPath=getMapFromMaps(m,"main","tmpPath"); |
---|
266 | map* tmpSid=getMapFromMaps(m,"lenv","sid"); |
---|
267 | char tmp[1024]; |
---|
268 | map* tmpVal=getMapFromMaps(inputs,paramKey.c_str(),"mimeType"); |
---|
269 | char* ext="json"; |
---|
270 | if(tmpVal!=NULL){ |
---|
271 | if(strncasecmp(tmpVal->value,"application/zip",14)==0){ |
---|
272 | char tmpName[1024]; |
---|
273 | symlink(test->value,ReplaceAll(test->value,".zca",".zip").c_str()); |
---|
274 | sprintf(tmpName,"/vsizip/%s",ReplaceAll(test->value,".zca",".zip").c_str()); |
---|
275 | char **files=VSIReadDir(tmpName); |
---|
276 | int nFiles = CSLCount( files ); |
---|
277 | char tmpSSName[1024]; |
---|
278 | sprintf(tmpSSName,"%s/Input_%s_%s",tmpPath->value,s->name,tmpSid->value); |
---|
279 | mkdir(tmpSSName,0777); |
---|
280 | |
---|
281 | char tmpSName[1024]; |
---|
282 | for(int kk=0;kk<nFiles;kk++){ |
---|
283 | sprintf(tmpSName,"%s/%s",tmpName,files[kk]); |
---|
284 | VSILFILE* fmain=VSIFOpenL(tmpSName, "rb"); |
---|
285 | if(fmain!=NULL){ |
---|
286 | VSIFSeekL(fmain,0,SEEK_END); |
---|
287 | long count=VSIFTellL(fmain); |
---|
288 | VSIRewindL(fmain); |
---|
289 | |
---|
290 | char *content=(char*) malloc((count+1)*sizeof(char)); |
---|
291 | VSIFReadL(content,1,count*sizeof(char),fmain); |
---|
292 | |
---|
293 | char tmpSSSName[1024]; |
---|
294 | sprintf(tmpSSSName,"%s/%s",tmpSSName,files[kk]); |
---|
295 | |
---|
296 | FILE* fx=fopen(tmpSSSName, "wb"); |
---|
297 | fwrite(content,1,count,fx); |
---|
298 | fclose(fx); |
---|
299 | VSIFCloseL(fmain); |
---|
300 | free(content); |
---|
301 | std::string test1(tmpSSSName); |
---|
302 | if(test1.find(".shp")!=std::string::npos){ |
---|
303 | setMapInMaps(inputs,paramKey.c_str(),"cache_file",tmpSSSName); |
---|
304 | test=getMapFromMaps(inputs,paramKey.c_str(),"cache_file"); |
---|
305 | } |
---|
306 | } |
---|
307 | } |
---|
308 | } |
---|
309 | } |
---|
310 | |
---|
311 | m_Application->SetParameterString(paramKey, test->value); |
---|
312 | } |
---|
313 | else |
---|
314 | if(type == ParameterType_InputImage |
---|
315 | || type == ParameterType_ComplexInputImage || type == ParameterType_InputVectorData |
---|
316 | || type == ParameterType_InputFilename){ |
---|
317 | m_Application->SetParameterString(paramKey, test->value); |
---|
318 | } |
---|
319 | } |
---|
320 | } |
---|
321 | param->SetUserValue(true); |
---|
322 | m_Application->UpdateParameters(); |
---|
323 | } |
---|
324 | |
---|
325 | try{ |
---|
326 | if( m_Application->ExecuteAndWriteOutput() == 0 ){ |
---|
327 | std::vector< std::pair<std::string, std::string> > paramList; |
---|
328 | paramList = m_Application->GetOutputParametersSumUp(); |
---|
329 | if(paramList.size()>0) |
---|
330 | for( unsigned int i=0; i<paramList.size(); i++){ |
---|
331 | setMapInMaps(outputs,paramList[i].first.c_str(),"value",paramList[i].second.c_str()); |
---|
332 | } |
---|
333 | else{ |
---|
334 | const std::vector<std::string> appKeyList = m_Application->GetParametersKeys(true); |
---|
335 | for (unsigned int i = 0; i < appKeyList.size(); i++){ |
---|
336 | const std::string paramKey(appKeyList[i]); |
---|
337 | std::vector<std::string> values; |
---|
338 | Parameter::Pointer param = m_Application->GetParameterByKey(paramKey); |
---|
339 | ParameterType type = m_Application->GetParameterType(paramKey); |
---|
340 | if (type != ParameterType_Group && paramKey!="inxml" && paramKey!="outxml" |
---|
341 | && (type == ParameterType_OutputImage || type == ParameterType_OutputFilename |
---|
342 | || type == ParameterType_OutputVectorData ) ){ |
---|
343 | if(type == ParameterType_OutputImage || type == ParameterType_OutputFilename || type == ParameterType_OutputVectorData){ |
---|
344 | map* test=getMapFromMaps(outputs,paramKey.c_str(),"mimeType"); |
---|
345 | if(test!=NULL && strncasecmp(test->value,"application/zip",15)==0){ |
---|
346 | |
---|
347 | test=getMapFromMaps(inputs,paramKey.c_str(),"generated_file"); |
---|
348 | char tmpName[1024]; |
---|
349 | sprintf(tmpName,"/vsizip/%s",ReplaceAll(test->value,".shp",".zip").c_str()); |
---|
350 | VSILFILE* fmain=VSIFOpenL(tmpName, "w"); |
---|
351 | FILE * file; |
---|
352 | char *tmp; |
---|
353 | char tmpSName[1024]; |
---|
354 | long count; |
---|
355 | |
---|
356 | char *exts[4]; |
---|
357 | exts[0]=".shp"; |
---|
358 | exts[1]=".shx"; |
---|
359 | exts[2]=".dbf"; |
---|
360 | exts[3]=".prj"; |
---|
361 | for(int c=0;c<4;c++){ |
---|
362 | sprintf(tmpSName,"%s/result%s",tmpName,exts[c]); |
---|
363 | |
---|
364 | file=fopen(ReplaceAll(test->value,".shp",exts[c]).c_str(),"rb"); |
---|
365 | if(file!=NULL){ |
---|
366 | fseek(file, 0, SEEK_END); |
---|
367 | count = ftell(file); |
---|
368 | rewind(file); |
---|
369 | |
---|
370 | tmp=(char*) malloc((count+1)*sizeof(char)); |
---|
371 | fread(tmp,1,count*sizeof(char),file); |
---|
372 | |
---|
373 | VSILFILE* fx=VSIFOpenL(tmpSName, "wb"); |
---|
374 | VSIFWriteL(tmp,1,count,fx); |
---|
375 | VSIFCloseL(fx); |
---|
376 | fclose(file); |
---|
377 | free(tmp); |
---|
378 | } |
---|
379 | } |
---|
380 | |
---|
381 | VSIFCloseL(fmain); |
---|
382 | |
---|
383 | FILE* file1=fopen(ReplaceAll(test->value,".shp",".zip").c_str(), "rb"); |
---|
384 | fseek(file1, 0, SEEK_END); |
---|
385 | count=ftell(file1); |
---|
386 | rewind(file1); |
---|
387 | |
---|
388 | tmp=(char*) malloc((count+1)*sizeof(char)); |
---|
389 | fread(tmp,1,count*sizeof(char),file1); |
---|
390 | |
---|
391 | file=fopen(ReplaceAll(test->value,".shp",".zip").c_str(),"wb"); |
---|
392 | fwrite(tmp,1,count,file); |
---|
393 | fclose(file); |
---|
394 | free(tmp); |
---|
395 | fclose(file1); |
---|
396 | setMapInMaps(inputs,paramKey.c_str(),"generated_file",ReplaceAll(test->value,".shp",".zip").c_str()); |
---|
397 | } |
---|
398 | test=getMapFromMaps(inputs,paramKey.c_str(),"generated_file"); |
---|
399 | |
---|
400 | if(test!=NULL){ |
---|
401 | setMapInMaps(outputs,paramKey.c_str(),"generated_file",test->value); |
---|
402 | } |
---|
403 | |
---|
404 | } |
---|
405 | } |
---|
406 | } |
---|
407 | } |
---|
408 | res=3; |
---|
409 | break; |
---|
410 | } |
---|
411 | else{ |
---|
412 | sprintf(tmpS, "The OTB Application %s cannot be run.", s->name); |
---|
413 | setMapInMaps(m,"lenv","message",tmpS); |
---|
414 | res=SERVICE_FAILED; |
---|
415 | } |
---|
416 | } |
---|
417 | catch(std::exception& err){ |
---|
418 | setMapInMaps(m,"lenv","message",err.what()); |
---|
419 | return SERVICE_FAILED; |
---|
420 | |
---|
421 | } |
---|
422 | catch(...){ |
---|
423 | setMapInMaps(m,"lenv","message","An unknown exception has been raised during application execution"); |
---|
424 | res=SERVICE_FAILED; |
---|
425 | } |
---|
426 | break; |
---|
427 | } |
---|
428 | } |
---|
429 | } |
---|
430 | } |
---|
431 | |
---|
432 | for (unsigned int i = 0; i < m_WatcherList.size(); i++){ |
---|
433 | m_WatcherList[i]->FreeConf(); |
---|
434 | delete m_WatcherList[i]; |
---|
435 | m_WatcherList[i] = NULL; |
---|
436 | } |
---|
437 | m_WatcherList.clear(); |
---|
438 | |
---|
439 | return res; |
---|
440 | } |
---|