source: trunk/zoo-project/zoo-kernel/service_internal_otb.c @ 556

Last change on this file since 556 was 555, checked in by djay, 9 years ago

Fixes for OTB support. Add --with-itk and --with-itk-version options.

  • Property svn:keywords set to Id
File size: 12.6 KB
Line 
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
25#include "service_internal_otb.h"
26#include <vector>
27#include <string>
28
29using namespace otb::Wrapper;
30
31std::string ReplaceAll(std::string str, const std::string& from, const std::string& to) {
32    size_t start_pos = 0;
33    while((start_pos = str.find(from, start_pos)) != std::string::npos) {
34        str.replace(start_pos, from.length(), to);
35        start_pos += to.length(); // Handles case where 'to' is a substring of 'from'
36    }
37    return str;
38}
39
40int zoo_otb_support(maps** main_conf,map* request,service* s,maps **real_inputs,maps **real_outputs){
41  maps* m=*main_conf;
42  maps* inputs=*real_inputs;
43  maps* outputs=*real_outputs;
44  map* tmp=NULL;
45  int res=-1;
46
47  std::vector<std::string> list = ApplicationRegistry::GetAvailableApplications();
48  if (list.size() == 0){
49    map* tmps=createMap("text","No OTB Application found.");
50    addToMap(tmps,"code","InternalError");
51    printExceptionReportResponse(m,tmps);
52    freeMap(&tmps);
53    free(tmps);
54    res=-1;
55  }
56  else{
57    for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it){
58      if(s->name==*it){
59        Application::Pointer m_Application=ApplicationRegistry::CreateApplication(*it);
60        if (m_Application.IsNull()){
61          char tmpS[1024];
62          sprintf(tmpS, "The OTB Application %s cannot be loaded.", (*it).c_str());
63          map* tmps=createMap("text",tmpS);
64          addToMap(tmps,"code","InternalError");
65          printExceptionReportResponse(m,tmps);
66          freeMap(&tmps);
67          free(tmps);
68          res=-1;
69        }else{
70          char tmpS[1024];
71          sprintf(tmpS, "The OTB Application %s was loaded correctly.", (*it).c_str());
72          const std::vector<std::string> appKeyList = m_Application->GetParametersKeys(true);
73          for (unsigned int i = 0; i < appKeyList.size(); i++){
74            const std::string paramKey(appKeyList[i]);
75            std::vector<std::string> values;
76            Parameter::Pointer param = m_Application->GetParameterByKey(paramKey);
77            ParameterType type = m_Application->GetParameterType(paramKey);
78            if (type != ParameterType_Group && paramKey!="inxml" && paramKey!="outxml"){
79              map* test=getMapFromMaps(inputs,paramKey.c_str(),"cache_file");
80              if(test==NULL){
81                test=getMapFromMaps(inputs,paramKey.c_str(),"inRequest");
82                if(test!=NULL && test->value!=NULL && strncasecmp(test->value,"true",4)==0){
83                  test=getMapFromMaps(inputs,paramKey.c_str(),"value");
84                  if(type == ParameterType_OutputImage){
85                    ImagePixelType outPixType = ImagePixelType_float;
86                    if (strncasecmp(test->value,"uint8",5)==0)
87                      outPixType = ImagePixelType_uint8;
88                    else if (strncasecmp(test->value,"int16",5)==0)
89                      outPixType = ImagePixelType_int16;
90                    else if (strncasecmp(test->value,"uint16",6)==0)
91                      outPixType = ImagePixelType_uint16;
92                    else if (strncasecmp(test->value,"int32",5)==0)
93                      outPixType = ImagePixelType_int32;
94                    else if (strncasecmp(test->value,"uint32",6)==0)
95                      outPixType = ImagePixelType_uint32;
96                    else if (strncasecmp(test->value,"double",6)==0)
97                      outPixType = ImagePixelType_double;
98                    map* tmpPath=getMapFromMaps(m,"main","tmpPath");
99                    map* tmpSid=getMapFromMaps(m,"lenv","sid");
100                    char tmp[1024];
101                    map* tmpVal=getMapFromMaps(outputs,paramKey.c_str(),"mimeType");
102                    char* ext="tiff";
103                    if(tmpVal!=NULL){
104                      if(strncasecmp(tmpVal->value,"image/jp2",9)==0)
105                         ext="j2k";
106                      else
107                        if(strncasecmp(tmpVal->value,"image/png",9)==0)
108                         ext="png";
109                        else
110                          if(strncasecmp(tmpVal->value,"image/jpeg",10)==0)
111                            ext="jpeg";
112                    }
113                    sprintf(tmp,"%s/%s_%s.%s",tmpPath->value,s->name,tmpSid->value,ext);
114                    m_Application->SetParameterString(paramKey, tmp);
115                    setMapInMaps(inputs,paramKey.c_str(),"generated_file",tmp);
116                    dynamic_cast<OutputImageParameter *> (param.GetPointer())->SetPixelType(outPixType);
117                  }else{
118                    if(test->value!=NULL)
119                      m_Application->SetParameterString(paramKey, test->value);
120                  }
121
122                }else{
123                  if(type == ParameterType_OutputVectorData){
124                      map* tmpPath=getMapFromMaps(m,"main","tmpPath");
125                      map* tmpSid=getMapFromMaps(m,"lenv","sid");
126                      char tmp[1024];
127                      map* tmpVal=getMapFromMaps(outputs,paramKey.c_str(),"mimeType");
128                      char* ext="json";
129                      if(tmpVal!=NULL){
130                        if(strncasecmp(tmpVal->value,"text/xml",8)==0)
131                        ext="gml";
132                      else
133                        if(strncasecmp(tmpVal->value,"applicaton/json",15)==0)
134                          ext="json";
135                        else
136                          if(strncasecmp(tmpVal->value,"application/zip",14)==0)
137                            ext="shp";
138                          else
139                            if(strncasecmp(tmpVal->value,"application/vnd.google-earth.kml+xml",36)==0)
140                              ext="kml";
141                      }
142                      sprintf(tmp,"%s/%s_%s.%s",tmpPath->value,s->name,tmpSid->value,ext);
143                      m_Application->SetParameterString(paramKey, tmp);
144                      setMapInMaps(inputs,paramKey.c_str(),"generated_file",tmp);
145                  }
146                  else
147                    if(type == ParameterType_OutputFilename){
148                      map* tmpPath=getMapFromMaps(m,"main","tmpPath");
149                      map* tmpSid=getMapFromMaps(m,"lenv","sid");
150                      char tmp[1024];
151                      map* tmpVal=getMapFromMaps(outputs,paramKey.c_str(),"mimeType");
152                      char* ext="txt";
153                      if(tmpVal!=NULL){
154                        if(strncasecmp(tmpVal->value,"text/xml",8)==0)
155                          ext="xml";
156                        else
157                          if(strncasecmp(tmpVal->value,"text/csv",15)==0)
158                            ext="csv";
159                          else
160                            if(strncasecmp(tmpVal->value,"application/zip",14)==0)
161                              ext="shp";
162                            else
163                            if(strncasecmp(tmpVal->value,"application/vnd.google-earth.kml+xml",36)==0)
164                              ext="kml";
165                      }
166                      sprintf(tmp,"%s/%s_%s.%s",tmpPath->value,s->name,tmpSid->value,ext);
167                      m_Application->SetParameterString(paramKey, tmp);
168                      setMapInMaps(inputs,paramKey.c_str(),"generated_file",tmp);
169                    }
170
171                }
172              }else{
173                if(type == ParameterType_InputImageList){
174                  values.push_back(test->value);
175                  map* tmpPath=getMapFromMaps(inputs,paramKey.c_str(),"length");
176                  if(tmpPath!=NULL){
177                    int len=atoi(tmpPath->value);
178                    for(int k=1;k<len;k++){
179                      char tmp[15];
180                      sprintf(tmp,"cache_file_%d",k);
181                      map* tmpVal=getMapFromMaps(inputs,paramKey.c_str(),tmp);
182                      if(tmpVal!=NULL){
183                        values.push_back(tmpVal->value);
184                      }
185                    }
186                  }
187                  dynamic_cast<InputImageListParameter *> (param.GetPointer())->SetListFromFileName(values);
188                }
189                else
190                  if(type == ParameterType_InputVectorData || type == ParameterType_InputFilename){
191                    map* tmpPath=getMapFromMaps(m,"main","tmpPath");
192                    map* tmpSid=getMapFromMaps(m,"lenv","sid");
193                    char tmp[1024];
194                    map* tmpVal=getMapFromMaps(inputs,paramKey.c_str(),"mimeType");
195                    char* ext="json";
196                    if(tmpVal!=NULL){
197                      if(strncasecmp(tmpVal->value,"application/zip",14)==0){
198                        char tmpName[1024];
199                        symlink(test->value,ReplaceAll(test->value,".zca",".zip").c_str());
200                        sprintf(tmpName,"/vsizip/%s",ReplaceAll(test->value,".zca",".zip").c_str());
201                        char **files=VSIReadDir(tmpName);
202                        int nFiles = CSLCount( files );
203                        char tmpSSName[1024];
204                        sprintf(tmpSSName,"%s/Input_%s_%s",tmpPath->value,s->name,tmpSid->value);
205                        mkdir(tmpSSName,0777);
206                           
207                        char tmpSName[1024];
208                        for(int kk=0;kk<nFiles;kk++){
209                          sprintf(tmpSName,"%s/%s",tmpName,files[kk]);
210                          VSILFILE* fmain=VSIFOpenL(tmpSName, "rb");
211                          if(fmain!=NULL){
212                            VSIFSeekL(fmain,0,SEEK_END);
213                            long count=VSIFTellL(fmain);
214                            VSIRewindL(fmain);
215
216                            char *content=(char*) malloc((count+1)*sizeof(char)); 
217                            VSIFReadL(content,1,count*sizeof(char),fmain);
218                         
219                            char tmpSSSName[1024];
220                            sprintf(tmpSSSName,"%s/%s",tmpSSName,files[kk]);
221                           
222                            FILE* fx=fopen(tmpSSSName, "wb");
223                            fwrite(content,1,count,fx);
224                            fclose(fx);
225                            VSIFCloseL(fmain);
226                            free(content);
227                            std::string test1(tmpSSSName);
228                            if(test1.find(".shp")!=std::string::npos){
229                              setMapInMaps(inputs,paramKey.c_str(),"cache_file",tmpSSSName);
230                              test=getMapFromMaps(inputs,paramKey.c_str(),"cache_file");
231                            }
232                          }
233                        }
234                      }
235                    }
236                   
237                    m_Application->SetParameterString(paramKey, test->value);
238                  }
239                  else
240                    if(type == ParameterType_InputImage
241                       || type == ParameterType_ComplexInputImage || type == ParameterType_InputVectorData
242                       || type == ParameterType_InputFilename){
243                      m_Application->SetParameterString(paramKey, test->value);
244                  }               
245              }
246            }
247            param->SetUserValue(true);
248            m_Application->UpdateParameters();
249          }
250
251          try{
252            if( m_Application->ExecuteAndWriteOutput() == 0 ){
253              std::vector< std::pair<std::string, std::string> > paramList;
254              paramList = m_Application->GetOutputParametersSumUp();
255              if(paramList.size()>0)
256                for( unsigned int i=0; i<paramList.size(); i++){
257                  setMapInMaps(outputs,paramList[i].first.c_str(),"value",paramList[i].second.c_str());
258                }
259              else{
260                const std::vector<std::string> appKeyList = m_Application->GetParametersKeys(true);
261                for (unsigned int i = 0; i < appKeyList.size(); i++){
262                  const std::string paramKey(appKeyList[i]);
263                  std::vector<std::string> values;
264                  Parameter::Pointer param = m_Application->GetParameterByKey(paramKey);
265                  ParameterType type = m_Application->GetParameterType(paramKey);
266                  if (type != ParameterType_Group && paramKey!="inxml" && paramKey!="outxml"
267                      && (type == ParameterType_OutputImage || type == ParameterType_OutputFilename
268                          || type == ParameterType_OutputVectorData ) ){
269                    if(type == ParameterType_OutputImage || type == ParameterType_OutputFilename || type == ParameterType_OutputVectorData){
270                      map* test=getMapFromMaps(outputs,paramKey.c_str(),"mimeType");
271                      if(test!=NULL && strncasecmp(test->value,"application/zip",15)==0){
272                       
273                        test=getMapFromMaps(inputs,paramKey.c_str(),"generated_file");
274                        char tmpName[1024];
275                        sprintf(tmpName,"/vsizip/%s",ReplaceAll(test->value,".shp",".zip").c_str());
276                        VSILFILE* fmain=VSIFOpenL(tmpName, "w");
277                        FILE * file;
278                        char *tmp;
279                        char tmpSName[1024];
280                        long count;
281                       
282                        char *exts[4];
283                        exts[0]=".shp";
284                        exts[1]=".shx";
285                        exts[2]=".dbf";
286                        exts[3]=".prj";
287                        for(int c=0;c<4;c++){
288                          sprintf(tmpSName,"%s/result%s",tmpName,exts[c]);
289                         
290                          file=fopen(ReplaceAll(test->value,".shp",exts[c]).c_str(),"rb");
291                          if(file!=NULL){
292                            fseek(file, 0, SEEK_END);
293                            count = ftell(file);
294                            rewind(file);
295                           
296                            tmp=(char*) malloc((count+1)*sizeof(char)); 
297                            fread(tmp,1,count*sizeof(char),file);
298                           
299                            VSILFILE* fx=VSIFOpenL(tmpSName, "wb");
300                            VSIFWriteL(tmp,1,count,fx);
301                            VSIFCloseL(fx);
302                            fclose(file);
303                            free(tmp);
304                          }
305                        }
306                       
307                        VSIFCloseL(fmain);
308                       
309                        FILE* file1=fopen(ReplaceAll(test->value,".shp",".zip").c_str(), "rb");
310                        fseek(file1, 0, SEEK_END);
311                        count=ftell(file1);
312                        rewind(file1);
313                       
314                        tmp=(char*) malloc((count+1)*sizeof(char)); 
315                        fread(tmp,1,count*sizeof(char),file1);
316                       
317                        file=fopen(ReplaceAll(test->value,".shp",".zip").c_str(),"wb");
318                        fwrite(tmp,1,count,file);
319                        fclose(file);
320                        free(tmp);
321                        fclose(file1);
322                        setMapInMaps(inputs,paramKey.c_str(),"generated_file",ReplaceAll(test->value,".shp",".zip").c_str());
323                      }
324                      test=getMapFromMaps(inputs,paramKey.c_str(),"generated_file");
325
326                      if(test!=NULL){
327                        setMapInMaps(outputs,paramKey.c_str(),"generated_file",test->value);
328                      }
329
330                    }
331                  }
332                }
333              }
334              res=3;
335              break;
336            }
337            else{
338              sprintf(tmpS, "The OTB Application %s cannot be run.", s->name);
339              setMapInMaps(m,"lenv","message",tmpS);
340              res=SERVICE_FAILED;
341            }
342          }
343          catch(std::exception& err){
344            setMapInMaps(m,"lenv","message",err.what());
345            return SERVICE_FAILED;
346           
347          }
348          catch(...){
349            setMapInMaps(m,"lenv","message","An unknown exception has been raised during application execution");
350            res=SERVICE_FAILED;
351          }
352          break;
353        }
354      }
355    }
356  }
357  return res;
358}
Note: See TracBrowser for help on using the repository browser.

Search

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png