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