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