source: trunk/zoo-project/zoo-kernel/service_internal_saga.c @ 756

Last change on this file since 756 was 752, checked in by djay, 8 years ago

Add optional CORS support. Fix issue when dealing with Array in JavaScript? support.

  • Property svn:keywords set to Id
File size: 34.3 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 <stdlib.h>
26#include <limits.h>
27#include <locale.h>
28#include <wx/string.h>
29#include <wx/app.h>
30#include <api_core.h>
31#include <data_manager.h>
32#include <module_library.h>
33#define _ZOO_SAGA
34#include "service_internal_saga.h"
35#include "server_internal.h"
36#include "mimetypes.h"
37
38/**
39 * Global SAGA-GIS output counter
40 */
41int sagaOutputCounter=0;
42
43/**
44 * Observer used to access the ongoing status of a running OTB Application
45 */
46class SagaWatcher
47{
48 public:
49  static int Callback(TSG_UI_Callback_ID ID, CSG_UI_Parameter &Param_1, CSG_UI_Parameter &Param_2);
50  /**
51   * Define the message value
52   *
53   * @param conf the maps pointer to copy
54   */
55  static void SetMessage(const char *mess)
56  {
57    FreeMessage();
58    message=zStrdup(mess);
59  }
60  /**
61   * Free the message value
62   *
63   */
64  static void FreeMessage()
65  {
66    if(message!=NULL)
67      free(message);
68    message=NULL;
69  }
70  /**
71   * Copy the original conf in the m_conf property
72   *
73   * @param conf the maps pointer to copy
74   */
75  void SetConf(maps **conf)
76  {
77    m_conf=dupMaps(conf);
78  }
79  /** 
80   * Get Configuration maps (m_conf)
81   * @return the m_conf property
82   */
83  const maps& GetConf()
84  {
85    return *m_conf;
86  }
87  /** 
88   * Free Configuration maps (m_Conf)
89   */
90  void FreeConf(){
91    freeMaps(&m_conf);
92    free(m_conf);
93  }
94 private:
95  /** Main conf maps */
96  static maps* m_conf;
97  /** Status */
98  static int status;
99  /** Message */
100  static char* message;
101};
102
103maps* SagaWatcher::m_conf;
104char* SagaWatcher::message=zStrdup("No message left");
105int SagaWatcher::status=1;
106
107/**
108 * The callback function called at any SAGA-GIS module step
109 *
110 * @param id a TSG_UI_Callback_ID as defined in api_core.h (line 1290)
111 * @param param1
112 * @param param2
113 */
114int
115SagaWatcher::
116Callback(TSG_UI_Callback_ID id, CSG_UI_Parameter &param1, CSG_UI_Parameter &param2)
117{
118
119  int res = 1;
120  switch( id )
121    {
122    default:
123      return 0;
124      break;
125
126    case CALLBACK_DLG_ERROR:
127      return 1;
128      break;
129
130    case CALLBACK_DLG_PARAMETERS:
131    case CALLBACK_PROCESS_SET_OKAY:
132    case CALLBACK_DATAOBJECT_COLORS_GET:
133    case CALLBACK_DATAOBJECT_COLORS_SET:
134    case CALLBACK_DATAOBJECT_PARAMS_GET:
135    case CALLBACK_DATAOBJECT_PARAMS_SET:
136    case CALLBACK_DATAOBJECT_UPDATE:
137    case CALLBACK_DATAOBJECT_SHOW:
138    case CALLBACK_DLG_CONTINUE:
139    case CALLBACK_PROCESS_SET_READY:
140    case CALLBACK_PROCESS_GET_OKAY:
141      return res;
142      break;
143
144    case CALLBACK_PROCESS_SET_PROGRESS:
145      {
146        int cPercent= param2.Number != 0.0 ? 1 + (int)(100.0 * param1.Number / param2.Number) : 100 ;
147        if( cPercent != status ){
148          status=cPercent;
149        }else
150          return res;
151      }
152      break;
153
154    case CALLBACK_PROCESS_SET_TEXT:
155      SetMessage(param1.String.b_str());
156      break;
157
158    case CALLBACK_MESSAGE_ADD:
159      SetMessage(param1.String.b_str());
160      break;
161
162    case CALLBACK_MESSAGE_ADD_ERROR:
163      SetMessage(param1.String.b_str());
164      break;
165
166    case CALLBACK_MESSAGE_ADD_EXECUTION:
167      SetMessage(param1.String.b_str());
168      break;
169
170    case CALLBACK_DLG_MESSAGE:
171      SetMessage((param2.String + ": " + param1.String).b_str());
172      break;
173
174    case CALLBACK_DATAOBJECT_ADD:
175      if(SG_Get_Data_Manager().Add((CSG_Data_Object *)param1.Pointer))
176        res = 1 ;
177      else
178        res = 0;
179      return res;
180      break;
181
182    }
183  updateStatus(m_conf,status,message);
184  return( res );
185}
186
187TSG_PFNC_UI_Callback Get_Callback (SagaWatcher watcher){
188  return( &(watcher.Callback) );
189}
190
191
192/**
193 * Get the default file extension for SAGA-GIS parameter type.
194 * Extensions are the following:
195 *  - sgrd for grid and data_object
196 *  - shp for shapes and tin
197 *  - csv for tables
198 *  - spc for points
199 *
200 * @param param a SAGA-GIS Parameter
201 */ 
202const char* sagaGetDefaultExt(CSG_Parameter* param){
203  if(CSG_String(param->Get_Type_Identifier()).Contains(CSG_String("grid"))
204     || CSG_String(param->Get_Type_Identifier()).Contains(CSG_String("data_object"))){
205    return "sgrd";
206  }
207  else if(CSG_String(param->Get_Type_Identifier()).Contains(CSG_String("shapes")) ||
208          CSG_String(param->Get_Type_Identifier()).Contains(CSG_String("tin"))){
209    return "shp";
210  }
211  else if(CSG_String(param->Get_Type_Identifier()).Contains(CSG_String("table"))){
212    return "csv";
213  }
214  else if(CSG_String(param->Get_Type_Identifier()).Contains(CSG_String("points"))){
215    return "spc";
216  }
217  return "unknown";
218}
219
220/**
221 * Load a datasource in the SAGA-GIS Data Manager.
222 *
223 * @param param a SAGA-GIS Parameter
224 * @param arg the arguments map passed to a SAGA-GIS module
225 * @return false on failure, true in case of success
226 */
227bool sagaLoadInput(CSG_Parameter* param,map* arg){
228  if(!param->is_Input() || !param->is_Enabled()){
229    return true;
230  }
231  map* carg=getMap(arg,CSG_String(param->Get_Identifier()).b_str());
232  if(carg!=NULL){
233    wxString fileName(carg->value);
234    if(param->is_DataObject()){
235      // In case it there is a single datasource
236      if(!SG_Get_Data_Manager().Find(&fileName) && !SG_Get_Data_Manager().Add(&fileName) && !param->is_Optional() ){
237        return false;
238      }
239      return( param->Set_Value(SG_Get_Data_Manager().Find(&fileName)) );
240    }
241    else
242      if(param->is_DataObject_List()){
243        // In case there are multiple datasources
244        param->asList()->Del_Items();
245        wxString fileNames(fileName);
246        while( fileNames.Length() > 0 ){
247          fileName = fileNames.BeforeFirst(';').Trim(false);
248          fileNames = fileNames.AfterFirst (';');           
249          if( !SG_Get_Data_Manager().Find(&fileName) ){
250            SG_Get_Data_Manager().Add(&fileName);
251          }
252          param->asList()->Add_Item(SG_Get_Data_Manager().Find(&fileName));
253        }
254      }
255  }
256  return true;
257}
258
259/**
260 * Extract all SAGA-GIS parameters from a parameters list and set its values to
261 * the one defined in the map.
262 *
263 * @parap params the parameters list
264 * @parap argument the argument map containing the value to use
265 * @return true in success, false in other case
266 */
267bool sagaSetParameters(CSG_Parameters *params,map* argument){
268
269  int pc=params->Get_Count();
270  params->Restore_Defaults();
271
272  for(int k=0;k<pc;k++){
273    CSG_Parameter * param=params->Get_Parameter(k);
274    if( param->is_Output() ){
275      map* omap=getMap(argument,CSG_String(param->Get_Identifier()).b_str());
276      if( param->is_DataObject() && param->is_Optional() && !param->asDataObject() && omap!=NULL){
277        param->Set_Value(DATAOBJECT_CREATE);
278      }
279    }
280    else
281      if( param->is_Option() && !param->is_Information() ){
282        map* inmap=getMap(argument,CSG_String(param->Get_Identifier()).b_str());
283        if(inmap!=NULL){
284            switch( param->Get_Type() ){
285            case PARAMETER_TYPE_Bool:
286              if(strncasecmp(inmap->value,"true",4)==0 || strncasecmp(inmap->value,"1",1)==0){
287                param->Set_Value(1);
288              }else
289                param->Set_Value(0);
290              break;
291            case PARAMETER_TYPE_Parameters:
292              // TODO: nested inputs gesture
293              break;
294            case PARAMETER_TYPE_Int:
295              param->Set_Value((int)strtol(inmap->value,NULL,10));
296              break;
297            case PARAMETER_TYPE_Double:
298            case PARAMETER_TYPE_Degree:
299              param->Set_Value((double)strtod(inmap->value,NULL));
300              break;
301            case PARAMETER_TYPE_String:
302              param->Set_Value(CSG_String(inmap->value));
303              break;
304            case PARAMETER_TYPE_FilePath:
305              param->Set_Value(CSG_String(inmap->value));
306              break;
307            case PARAMETER_TYPE_FixedTable:
308              {
309                CSG_Table Table(inmap->value);
310                param->asTable()->Assign_Values(&Table);
311              }
312              break;
313            case PARAMETER_TYPE_Choice:
314              {
315                int val=(int)strtol(inmap->value,(char**)NULL,10);
316                if(val==0 && strncasecmp(inmap->value,"0",1)!=0)
317                  param->Set_Value(CSG_String(inmap->value));
318                else
319                  param->Set_Value(val);
320              }
321              break;
322            default:
323              break;
324            }
325        }else{
326          if(param->Get_Type()==PARAMETER_TYPE_Range){
327            inmap=getMap(argument,(CSG_String(param->Get_Identifier())+"_MIN").b_str());
328            if(inmap!=NULL)
329              param->asRange()->Set_LoVal(strtod(inmap->value,NULL));
330            inmap=getMap(argument,(CSG_String(param->Get_Identifier())+"_MAX").b_str());
331            if(inmap!=NULL)
332              param->asRange()->Set_HiVal(strtod(inmap->value,NULL));       
333          }
334          if(inmap==NULL){
335            param->Restore_Default();
336          }
337        }
338      }
339  }
340
341  for(int k=0;k<pc;k++){
342    CSG_Parameter * param=params->Get_Parameter(k);
343    if( param->is_Input() )
344      if(!sagaLoadInput(param,argument)){
345        fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
346        return false;
347      }
348  }
349  return true;
350}
351
352/**
353 * Save all values outputed by a SAGA-GIS module invocation to files
354 *
355 * @param params the parameters list
356 * @param main_conf the conf maps containing the main.cfg settings
357 * @param outputs the maps to set the generated_file for each output
358 */
359bool sagaSaveOutputs(CSG_Parameters *params,maps* main_conf,maps** outputs)
360{
361  for(int j=0; j<params->Get_Count(); j++)
362    {
363      CSG_Parameter *param = params->Get_Parameter(j);
364      maps* cMaps=getMaps(*outputs,CSG_String(param->Get_Identifier()).b_str());
365      // Specific TIN case
366      if(cMaps==NULL && CSG_String(param->Get_Type_Identifier()).Contains(CSG_String("tin")))
367        cMaps=getMaps(*outputs,(CSG_String(param->Get_Identifier())+"_POINTS").b_str());
368      if(cMaps!=NULL){
369        map* tmpPath=getMapFromMaps(main_conf,"main","tmpPath");
370        map* sid=getMapFromMaps(main_conf,"lenv","usid");
371        const char *file_ext=sagaGetDefaultExt(param);
372
373        if( param->is_Input() )
374          {
375            if( param->is_DataObject() )
376              {
377                CSG_Data_Object *pObject = param->asDataObject();
378               
379                if( pObject && pObject->is_Modified() && SG_File_Exists(pObject->Get_File_Name()) )
380                  {
381                    pObject->Save(pObject->Get_File_Name());
382                    addToMap(cMaps->content,"generated_file",CSG_String(pObject->Get_File_Name()).b_str());
383                  }
384              }
385           
386            else if( param->is_DataObject_List() )
387              {
388                for(int i=0; i<param->asList()->Get_Count(); i++)
389                  {
390                    CSG_Data_Object *pObject = param->asList()->asDataObject(i);
391                   
392                    if( pObject->is_Modified() && SG_File_Exists(pObject->Get_File_Name()) )
393                      {
394                        pObject->Save(pObject->Get_File_Name());
395                        setMapArray(cMaps->content,"generated_file",i,CSG_String(pObject->Get_File_Name()).b_str());
396                      }
397                  }
398              }
399          }
400        else
401          if( param->is_Output() )
402            {
403              char *realFileName=(char*)malloc((strlen(file_ext)+strlen(sid->value)+strlen(cMaps->name)+14)*sizeof(char));
404              char *fullFileName=(char*)malloc((strlen(file_ext)+strlen(sid->value)+strlen(cMaps->name)+strlen(tmpPath->value)+16)*sizeof(char));
405              sprintf(realFileName,"Output_%s_%s_%d",cMaps->name,sid->value,sagaOutputCounter);
406              sprintf(fullFileName,"%s/Output_%s_%s_%d.%s",tmpPath->value,cMaps->name,sid->value,sagaOutputCounter,file_ext);
407              sagaOutputCounter+=1;
408              wxString fileName(fullFileName);
409              addToMap(cMaps->content,"generated_name",realFileName);
410              free(realFileName);
411              free(fullFileName);
412
413              if( param->is_DataObject() )
414                {
415                  if( param->asDataObject() )
416                    {
417                      param->asDataObject()->Save(&fileName);
418                      addToMap(cMaps->content,"generated_file",CSG_String(param->asDataObject()->Get_File_Name()).b_str());
419                    }
420                }
421           
422              else if( param->is_DataObject_List() )
423                {
424                  CSG_Strings   fileNames;
425               
426                  while( fileName.Length() > 0 )
427                    {
428                      CSG_String current_file(&fileName);
429                      current_file = current_file.BeforeFirst(';');
430                      if( current_file.Length() > 0 )
431                        {
432                          fileNames += current_file;
433                          fileName = fileName.AfterFirst(';');
434                        }
435                      else
436                        {
437                          fileNames += &fileName;
438                          fileName.Clear();
439                        }
440                    }
441                 
442                  int nFileNames = param->asList()->Get_Count() <= fileNames.Get_Count() ? fileNames.Get_Count() : fileNames.Get_Count() - 1;
443                  for(int i=0; i<param->asList()->Get_Count(); i++)
444                    {
445                      fileNames[i].Trim();
446                      if( i < nFileNames )
447                        {
448                          param->asList()->asDataObject(i)->Save(fileNames[i]);
449                        }
450                      else
451                        {
452                          param->asList()->asDataObject(i)->Save(CSG_String::Format(SG_T("%s_%0*d"),
453                                                                                    fileNames[fileNames.Get_Count() - 1].c_str(),
454                                                                                    SG_Get_Digit_Count(param->asList()->Get_Count()),
455                                                                                    1 + i - nFileNames
456                                                                                    ));
457                        }
458                      setMapArray(cMaps->content,"generated_file",i,
459                                  CSG_String(param->asList()->asDataObject(i)->Get_File_Name()).b_str());
460                    }
461                }
462            }
463      }
464    }
465  return( true );
466}
467
468/**
469 * Invoke the execution of a SAGA-GIS module.
470 *
471 * @param main_conf the conf maps containing the main.cfg settings
472 * @param lib_name the SAGA-GIS library name
473 * @param module_name the SAGA-GIS module name
474 * @param arguments the map containing the arguments to pass to the module
475 * @param outputs default to NULL, contains the maps to fill with the result
476 */
477int sagaExecuteCmd(maps** main_conf,const char* lib_name,const char* module_name,map* arguments,maps** outputs=NULL){
478  int res=SERVICE_FAILED;
479
480  CSG_Module_Library * library=SG_Get_Module_Library_Manager().Get_Library(CSG_String(lib_name),true);
481  if( library == NULL){
482    char tmp[255];
483    sprintf(tmp,"Counld not load the %s SAGA library",lib_name);
484    setMapInMaps(*main_conf,"lenv","message",tmp);
485    res=SERVICE_FAILED;
486    return res;
487  }
488
489  CSG_Module * module=library->Get_Module(atoi(module_name));
490  if(module == NULL){
491    char tmp[255];
492    sprintf(tmp,"Counld not load the %s module from the %s SAGA library",module_name,lib_name);
493    setMapInMaps(*main_conf,"lenv","message",tmp);
494    res=SERVICE_FAILED;
495    return res;
496  }
497
498  CSG_Parameters * params=module->Get_Parameters();
499  if(!params){
500    char tmp[255];
501    sprintf(tmp,"Counld not find any param for the %s module from the %s SAGA library",module_name,lib_name);
502    setMapInMaps(*main_conf,"lenv","message",tmp);
503    res=SERVICE_FAILED;
504    return res;
505  }
506 
507  sagaSetParameters(params,arguments);
508
509  module->Update_Parameter_States();
510
511  bool retval=false;
512  if(module->On_Before_Execution()){
513    retval=module->Execute();
514    module->On_After_Execution();
515  }
516 
517  if(retval && outputs!=NULL){
518    sagaSaveOutputs(module->Get_Parameters(),*main_conf,outputs);
519    SG_Get_Data_Manager().Delete_Unsaved();
520    return SERVICE_SUCCEEDED;
521  }
522
523  return SERVICE_FAILED;
524
525}
526
527/**
528 * Export a SAGA-GIS Shapes to a file in a specific format (GML,KML,GeoJSON).
529 * saga_cmd io_gdal 4 -FILE my.format -SHAPES my.shp -FORMAT XXX
530 *
531 * @param main_conf the conf maps containing the main.cfg settings
532 * @param in the output maps to fill with the resulting file
533 */
534int sagaExportOGR(maps** conf, maps** in){
535  map* mtype=getMap((*in)->content,"mimeType");
536  map* gfile=getMap((*in)->content,"generated_file");
537  char* fext=NULL;
538  map* arg=NULL;
539  if(strncasecmp(mtype->value,"text/xml",8)==0){
540    fext=zStrdup("xml");
541  }
542  else if(strncasecmp(mtype->value,"application/json",16)==0){
543    fext=zStrdup("json");
544  }
545  else{
546    fext=zStrdup("kml");
547  }
548  char* tmpName=(char*)malloc((strlen(gfile->value)+2)*sizeof(char));
549  strncpy(tmpName,gfile->value,(strlen(gfile->value)-3)*sizeof(char));
550  strncpy(&tmpName[0]+(strlen(gfile->value)-3),fext,(strlen(fext))*sizeof(char));
551  tmpName[strlen(fext)+(strlen(gfile->value)-3)]=0;
552  arg=createMap("SHAPES",gfile->value);
553  addToMap(arg,"FILE",tmpName);
554  if(strncasecmp(mtype->value,"text/xml",8)==0){
555    addToMap(arg,"FORMAT","GML");
556  }
557  else if(strncasecmp(mtype->value,"application/json",16)==0){
558    addToMap(arg,"FORMAT","GeoJSON");
559  }
560  else{
561#ifdef HAVE_LIBKML
562    addToMap(arg,"FORMAT","LIBKML");
563#else
564    addToMap(arg,"FORMAT","KML");
565#endif
566  }
567  free(fext);
568  free(gfile->value);
569  gfile->value=zStrdup(tmpName);
570  free(tmpName);
571 
572  sagaExecuteCmd(conf,"io_gdal","4",arg);
573  freeMap(&arg);
574  free(arg);
575}
576
577/**
578 * Export a SAGA-GIS pointcloud to a las file.
579 * saga_cmd io_shapes_las 0 -POINTS my.spc -FILE my.las
580 *
581 * @param main_conf the conf maps containing the main.cfg settings
582 * @param in the output maps to fill with the resulting file
583 */
584void sagaExportPC(maps** conf, maps** in){
585  map* mtype=getMap((*in)->content,"mimeType");
586  map* gfile=getMap((*in)->content,"generated_file");
587  char* fext="las";
588  map* arg=NULL;
589  char* tmpName=(char*)malloc((strlen(gfile->value)+2)*sizeof(char));
590  strncpy(tmpName,gfile->value,(strlen(gfile->value)-3)*sizeof(char));
591  strncpy(&tmpName[0]+(strlen(gfile->value)-3),fext,(strlen(fext))*sizeof(char));
592  tmpName[strlen(fext)+(strlen(gfile->value)-3)]=0;
593  arg=createMap("POINTS",gfile->value);
594  addToMap(arg,"FILE",tmpName);
595  free(gfile->value);
596  gfile->value=zStrdup(tmpName);
597  sagaExecuteCmd(conf,"io_shapes_las","0",arg);
598  freeMap(&arg);
599  free(arg);
600  free(tmpName);
601}
602
603/**
604 * Export a SAGA-GIS Grid to a file in a specific format (tiff,hdr,aa).
605 * saga_cmd io_gdal 1 -FILE my.format -GRIDS my.sgrd -FORMAT XXX
606 *
607 * @param main_conf the conf maps containing the main.cfg settings
608 * @param in the output maps to fill with the resulting file
609 */
610int sagaExportGDAL(maps** conf, maps** in/*,CSG_Parameter* param*/){
611  map* mtype=getMap((*in)->content,"extension");
612  map* gfile=getMap((*in)->content,"generated_file");
613  char* fext=NULL;
614  map* arg;
615
616  if(mtype!=NULL)
617    fext=zStrdup(mtype->value);
618  else{
619    fext=zStrdup("tiff");
620  }
621
622  mtype=getMap((*in)->content,"mimeType");
623  if(strncasecmp(mtype->value,"image/tiff",10)==0){
624    arg=createMap("FORMAT","1");
625  }
626  else if(strncasecmp(mtype->value,"application/x-ogc-envi",22)==0){
627    arg=createMap("FORMAT","ENVI .hdr Labelled");
628  }
629  else{
630    arg=createMap("FORMAT","ARC Digitized Raster Graphics");
631  }
632
633  if(gfile!=NULL){
634    char* tmpName=(char*)malloc((strlen(gfile->value)+1)*sizeof(char));
635    strncpy(tmpName,gfile->value,(strlen(gfile->value)-4)*sizeof(char));
636    strncpy(&tmpName[0]+(strlen(gfile->value)-4),fext,(strlen(fext))*sizeof(char));
637    tmpName[strlen(fext)+(strlen(gfile->value)-4)]=0;
638    addToMap(arg,"FILE",tmpName);
639    addToMap(arg,"GRIDS",gfile->value);
640    free(tmpName);
641    free(fext);
642    free(gfile->value);
643    map* tmp=getMap(arg,"FILE");
644    gfile->value=zStrdup(tmp->value);
645    sagaExecuteCmd(conf,"io_gdal","1",arg);
646  }
647  else{
648    // Empty result
649    return true;
650  }
651  freeMap(&arg);
652  free(arg);
653}
654
655/**
656 * Export a SAGA-GIS TIN to a file in a specific format (GML,KML,GeoJSON).
657 * Exporting TIN produce 5 separated files (POINTS, CENTER, EDGES, TRIANGLES
658 * and POLYGONS). Even if a client can choose which result it want to have,
659 * SAGA-GIS module will be invoked in a way that it will produce in any case
660 * each possible outputs. The selection of a specific output is made in the
661 * ZOO-Kernel itself and not specifically at this level.
662 * saga_cmd tin_tools 3 -TIN my.shp -POINTS p.shp ...
663 *
664 * @param conf the conf maps containing the main.cfg settings
665 * @param in the output maps to fill with the resulting file
666 * @see sagaExportOGR
667 */
668int sagaExportTIN(maps** conf, maps** in,const char* tname/*,CSG_Parameter* param*/){
669  map* mtype=getMap((*in)->content,"mimeType");
670  map* gfile=getMap((*in)->content,"generated_file");
671  char* fext="shp";
672  map* arg=createMap("TIN",gfile->value);
673
674  char* tinOut[5]={
675    "POINTS",
676    "CENTER",
677    "EDGES",
678    "TRIANGLES",
679    "POLYGONS"
680  };
681  maps* resouts=NULL;
682
683  int i=0;
684  for(i=0;i<5;i++){
685    char* tmpName=(char*)malloc((strlen(gfile->value)+strlen(tinOut[i])+4)*sizeof(char));
686    strncpy(tmpName,gfile->value,(strlen(gfile->value)-3)*sizeof(char));
687    char *tmpSubName=(char*) malloc((strlen(tinOut[i])+3)*sizeof(char));
688    sprintf(tmpSubName,"_%s.",tinOut[i]);
689    strncpy(&tmpName[0]+(strlen(gfile->value)-4),tmpSubName,(strlen(tmpSubName))*sizeof(char));
690    strncpy(&tmpName[0]+(strlen(gfile->value)+strlen(tmpSubName)-4),fext,(strlen(fext))*sizeof(char));
691    tmpName[strlen(fext)+(strlen(gfile->value)+strlen(tmpSubName)-4)]=0;
692
693    maps* louts=(maps*)malloc(MAPS_SIZE);
694    louts->name=zStrdup(tinOut[i]);
695    louts->content=createMap("mimeType","UNKOWN");
696    louts->next=NULL;
697   
698    addToMap(arg,tinOut[i],tmpName);
699   
700    free(tmpName);
701    if(resouts==NULL)
702      resouts=dupMaps(&louts);
703    else
704      addMapsToMaps(&resouts,louts);
705    freeMaps(&louts);
706    free(louts);
707  }
708 
709  sagaExecuteCmd(conf,"tin_tools","3",arg,&resouts);
710
711  for(i=0;i<5;i++){
712    map* generatedFile=getMapFromMaps(resouts,tinOut[i],"generated_file");
713    setMapInMaps(*in,(CSG_String(tname)+"_"+tinOut[i]).b_str(),"generated_file",generatedFile->value);
714    maps* cout=getMaps(*in,(CSG_String(tname)+"_"+tinOut[i]).b_str());
715    sagaExportOGR(conf,&cout);
716  }
717  return true;
718}
719
720/**
721 * Import GDAL Datasource into SAGA-GIS.
722 * saga_cmd io_gdal 0 -TRANSFORM 0 -FILES my.format -GRIDS /tmpPath/MyGridXXX.sgrd
723 *
724 * @param conf the conf maps containing the main.cfg settings
725 * @param in in the inputs maps
726 */
727int sagaImportGDAL(maps** conf, maps** in){
728  map* l=getMap((*in)->content,"length");
729  bool shouldClean=false;
730  if(l==NULL){
731    l=createMap("length","1");
732    shouldClean=true;
733  }
734  int len=strtol(l->value,NULL,10);
735  int i=0;
736  for(i=0;i<len;i++){
737    map* arg=createMap("TRANSFORM","0");
738    addToMap(arg,"INTERPOL","4");
739    map* v=getMapArray((*in)->content,"cache_file",i);
740    if(v!=NULL)
741      addToMap(arg,"FILES",v->value);
742    addToMap(arg,"GRIDS","");
743
744    maps* louts=(maps*)malloc(MAPS_SIZE);
745    louts->name=zStrdup("GRIDS");
746    louts->content=createMap("mimeType","UNKOWN");
747    louts->next=NULL;
748
749    sagaExecuteCmd(conf,"io_gdal","0",arg,&louts);
750
751    map* tmp=getMapFromMaps(louts,"GRIDS","generated_file");
752    setMapArray((*in)->content,"saga_value",i,tmp->value);
753
754    freeMaps(&louts);
755    free(louts);
756    freeMap(&arg);
757    free(arg);
758  }
759  if(shouldClean){
760    freeMap(&l);
761    free(l);
762  }
763}
764
765/**
766 * Import OGR Datasource into SAGA-GIS.
767 * saga_cmd io_gdal 3 -SHAPES my.shp -FILES my.format
768 *
769 * @param conf the conf maps containing the main.cfg settings
770 * @param in in the inputs maps
771 */
772int sagaImportOGR(maps** conf, maps** in){
773  char *ext;
774  map* arg;
775  map* l=getMap((*in)->content,"length");
776  bool shouldClean=false;
777  if(l==NULL){
778    l=createMap("length","1");
779    shouldClean=true;
780  }
781  int len=strtol(l->value,NULL,10);
782  int i=0;
783  for(i=0;i<len;i++){
784    map* v=getMapArray((*in)->content,"cache_file",i);
785    arg=createMap("SHAPES","");
786    if(v!=NULL)
787      addToMap(arg,"FILES",v->value);
788
789    maps* louts=(maps*)malloc(MAPS_SIZE);
790    louts->name=zStrdup("SHAPES");
791    louts->content=createMap("mimeType","UNKOWN");
792    louts->next=NULL;
793
794    sagaExecuteCmd(conf,"io_gdal","3",arg,&louts);
795
796    map* tmp=getMapFromMaps(louts,"SHAPES","generated_file");
797    setMapArray((*in)->content,"saga_value",i,tmp->value);
798
799    freeMaps(&louts);
800    free(louts);
801    freeMap(&arg);
802    free(arg);
803  }
804  if(shouldClean){
805    freeMap(&l);
806    free(l);
807  }
808}
809
810/**
811 * Import TIN into SAGA-GIS. Calling this function suppose that sagaImportOGR
812 * was called first.
813 * saga_cmd tin_tools 2 -SHAPES myShapes.shp -TIN myTin.shp
814 *
815 * @param conf the conf maps containing the main.cfg settings
816 * @param in in the inputs maps
817 * @see sagaImportOGR
818 */
819bool sagaImportTIN(maps** conf, maps** in){
820  char *ext;
821  map* arg;
822  map* l=getMap((*in)->content,"length");
823  bool shouldClean=false;
824  if(l==NULL){
825    l=createMap("length","1");
826    shouldClean=true;
827  }
828  int len=strtol(l->value,NULL,10);
829  int i=0;
830  for(i=0;i<len;i++){
831    map* v=getMapArray((*in)->content,"saga_value",i);
832    arg=createMap("TIN","");
833    if(v!=NULL)
834      addToMap(arg,"SHAPES",v->value);
835    maps* louts=(maps*)malloc(MAPS_SIZE);
836    louts->name=zStrdup("TIN");
837    louts->content=createMap("mimeType","UNKOWN");
838    louts->next=NULL;
839    sagaExecuteCmd(conf,"tin_tools","2",arg,&louts);
840    map* tmp=getMapFromMaps(louts,"TIN","generated_file");
841    v=getMapArray((*in)->content,"saga_value",i);
842    if(tmp!=NULL){
843      if(v!=NULL){
844        free(v->value);
845        v->value=zStrdup(tmp->value);
846      }
847      else
848        setMapArray((*in)->content,"saga_value",i,tmp->value);
849    }
850    freeMaps(&louts);
851    free(louts);
852    freeMap(&arg);
853    free(arg);
854  }
855  if(shouldClean){
856    freeMap(&l);
857    free(l);
858  }
859  return true;
860}
861
862/**
863 * Import table into SAGA-GIS.
864 * saga_cmd io_table 1 -TABLE myTable -FILENAME myFile -SEPARATOR 2
865 *
866 * @param conf the conf maps containing the main.cfg settings
867 * @param in in the inputs maps
868 */
869int sagaImportTable(maps** conf, maps** in){
870  char *ext;
871  map* arg;
872  map* l=getMap((*in)->content,"length");
873  bool shouldClean=false;
874  if(l==NULL){
875    l=createMap("length","1");
876    shouldClean=true;
877  }
878  int len=strtol(l->value,NULL,10);
879  int i=0;
880  for(i=0;i<len;i++){
881
882    // Create and fill arg map
883    arg=createMap("SEPARATOR","2");
884    addToMap(arg,"TABLE","");
885    map* v=getMapArray((*in)->content,"cache_file",i);
886    if(v!=NULL)
887      addToMap(arg,"FILENAME",v->value);
888
889    // Create the output maps
890    maps* louts=(maps*)malloc(MAPS_SIZE);
891    louts->name=zStrdup("TABLE");
892    louts->content=createMap("mimeType","UNKOWN");
893    louts->next=NULL;
894
895    // Execute the saga command
896    sagaExecuteCmd(conf,"io_table","1",arg,&louts);
897
898    // Fetch result and add it to the original map as saga_value
899    map* tmp=getMapFromMaps(louts,"TABLE","generated_file");
900    setMapArray((*in)->content,"saga_value",i,tmp->value);
901
902    // Cleanup
903    freeMaps(&louts);
904    free(louts);
905    freeMap(&arg);
906    free(arg);
907
908  }
909  // Cleanup if required
910  if(shouldClean){
911    freeMap(&l);
912    free(l);
913  }
914}
915
916/**
917 * Import las file as pointcloud into SAGA-GIS.
918 * saga_cmd io_shapes_las 1 -POINTS my.spc -FILENAME my.las
919 *
920 * @param conf the conf maps containing the main.cfg settings
921 * @param in in the inputs maps
922 */
923int sagaImportPC(maps** conf, maps** in){
924  char *ext;
925  map* arg;
926  map* l=getMap((*in)->content,"length");
927  bool shouldClean=false;
928  if(l==NULL){
929    l=createMap("length","1");
930    shouldClean=true;
931  }
932  int len=strtol(l->value,NULL,10);
933  int i=0;
934  for(i=0;i<len;i++){
935
936    // Create and fill arg map
937    arg=createMap("POINTS","");
938    map* v=getMapArray((*in)->content,"cache_file",i);
939    if(v!=NULL)
940      addToMap(arg,"FILES",v->value);
941
942    // Create the output maps
943    maps* louts=(maps*)malloc(MAPS_SIZE);
944    louts->name=zStrdup("POINTS");
945    louts->content=createMap("mimeType","UNKOWN");
946    louts->next=NULL;
947
948    // Execute the saga command
949    sagaExecuteCmd(conf,"io_shapes_las","1",arg,&louts);
950
951    // Fetch result and add it to the original map as saga_value
952    map* tmp=getMapFromMaps(louts,"POINTS","generated_file");
953    setMapArray((*in)->content,"saga_value",i,tmp->value);
954
955    // Cleanup
956    freeMaps(&louts);
957    free(louts);
958    freeMap(&arg);
959    free(arg);
960
961  }
962  // Cleanup if required
963  if(shouldClean){
964    freeMap(&l);
965    free(l);
966  }
967}
968
969/**
970 * Load and invoke a SAGA-GIS module defined in a service metadata definitions.
971 * Load all the input data into SAGA-GIS using io_gdal, io_tables and
972 * io_shapes_las for SAGA grids/shapes, tables and pointcloud respectively.
973 * Load and run the module from its library and invoke it using the data
974 * imported in SAGA-GIS at first stage. After the execution, export the outputs
975 * to files using io_gdal and io_shapes_las for grids/shapes and pointcloud
976 * respectively.
977 *
978 * @param main_conf the conf maps containing the main.cfg settings
979 * @param request the map containing the HTTP request
980 * @param s the service structure
981 * @param inputs the maps containing the inputs
982 * @param outputs the maps containing the outputs
983 */
984int zoo_saga_support(maps** main_conf,map* request,service* s,maps** inputs,maps** outputs){
985  int res=SERVICE_FAILED;
986  if( !wxInitialize() ){
987    fprintf(stderr,"initialisation failed");
988    return SERVICE_FAILED;
989  }
990  setlocale(LC_NUMERIC, "C");
991  static bool g_bShow_Messages = false;
992
993  dumpMapsValuesToFiles(main_conf,inputs);
994
995  SagaWatcher watcher=SagaWatcher();
996  watcher.SetConf(main_conf);
997
998  SG_Set_UI_Callback(Get_Callback(watcher));
999
1000  int n = SG_Get_Module_Library_Manager().Add_Directory(wxT(MODULE_LIBRARY_PATH),false);
1001  if( SG_Get_Module_Library_Manager().Get_Count() <= 0 ){
1002    setMapInMaps(*main_conf,"lenv","message","Could not load any SAGA tool library");
1003    res=SERVICE_FAILED;
1004    return res;
1005  }
1006
1007  map* serviceProvider=getMap(s->content,"serviceProvider");
1008
1009  // Load the SAGA-GIS library corresponding to the serviceProvider
1010  CSG_Module_Library * library=SG_Get_Module_Library_Manager().Get_Library(CSG_String(serviceProvider->value),true);
1011  if( library == NULL){
1012    char tmp[255];
1013    sprintf(tmp,"Counld not load the %s SAGA library",serviceProvider->value);
1014    setMapInMaps(*main_conf,"lenv","message",tmp);
1015    res=SERVICE_FAILED;
1016    return res;
1017  }
1018 
1019  // Load the SAGA-GIS module corresponding to the service name from the library
1020  CSG_Module * module=library->Get_Module(atoi(s->name));
1021  if(module == NULL){
1022    char tmp[255];
1023    sprintf(tmp,"Counld not load the %s module from the %s SAGA library",
1024            s->name,serviceProvider->value);
1025    setMapInMaps(*main_conf,"lenv","message",tmp);
1026    res=SERVICE_FAILED;
1027    return res;
1028  }
1029
1030  // Load all the parameters defined for the module
1031  CSG_Parameters * params=module->Get_Parameters();
1032  int pc=params->Get_Count();
1033  if(!params){
1034    char tmp[255];
1035    sprintf(tmp,"Counld not find any param for the %s module from the %s SAGA library",
1036            s->name,serviceProvider->value);
1037    setMapInMaps(*main_conf,"lenv","message",tmp);
1038    res=SERVICE_FAILED;
1039    return res;
1040  }
1041
1042  // Loop over each inputs to transform raster files to grid when needed,
1043  // import tables, shapes or point clouds
1044  for(int k=0;k<pc;k++){
1045    CSG_Parameter * param=params->Get_Parameter(k);
1046    if(param!=NULL && !param->is_Output()){
1047      maps* inmap=getMaps(*inputs,CSG_String(param->Get_Identifier()).b_str());
1048      if(inmap!=NULL){
1049        map* tmp=getMap(inmap->content,"value");
1050        if(tmp==NULL || strncasecmp(tmp->value,"NULL",4)!=0){
1051          if(CSG_String(param->Get_Type_Identifier()).Contains(CSG_String("grid"))){
1052            sagaImportGDAL(main_conf,&inmap);
1053          }
1054          else if(CSG_String(param->Get_Type_Identifier()).Contains(CSG_String("tin"))){
1055            sagaImportOGR(main_conf,&inmap);
1056            sagaImportTIN(main_conf,&inmap);
1057          }
1058          else if(CSG_String(param->Get_Type_Identifier()).Contains(CSG_String("shapes"))){
1059            sagaImportOGR(main_conf,&inmap);
1060          }
1061          else{
1062            if(CSG_String(param->Get_Type_Identifier()).Contains(CSG_String("table"))){
1063              sagaImportTable(main_conf,&inmap);
1064            }
1065            else
1066              if(CSG_String(param->Get_Type_Identifier()).Contains(CSG_String("points"))){
1067                sagaImportPC(main_conf,&inmap);
1068              }
1069          }
1070        }
1071      }
1072    }
1073  }
1074
1075  // Create a map conraining arguments to pass to the SAGA-GIS module
1076  // Fetch all input value (specifically for data imported into SAGA-GIS)
1077  maps* inp=*inputs;
1078  int k=0;
1079  map* cParams=NULL;
1080  while(inp!=NULL){
1081    map* len=getMap(inp->content,"length");
1082    bool shouldClean=false;
1083    if(len==NULL){
1084      len=createMap("length","1");
1085      shouldClean=true;
1086    }
1087    int len0=strtol(len->value,NULL,10);
1088    int i=0;
1089    char *cinput=NULL;
1090    int clen=0;
1091    for(i=0;i<len0;i++){
1092      map* val=getMapArray(inp->content,"saga_value",i);
1093      if(val==NULL)
1094        val=getMapArray(inp->content,"value",i);
1095      if(val!=NULL && strncasecmp(val->value,"NULL",4)!=0){
1096        if(cinput==NULL){
1097          cinput=zStrdup(val->value);
1098        }
1099        else{
1100          cinput=(char*)realloc(cinput,(clen+strlen(val->value)+1)*sizeof(char));
1101          strncpy(&cinput[0]+clen,";",1);
1102          strncpy(&cinput[0]+(clen+1),val->value,strlen(val->value));
1103          clen+=1;
1104        }
1105        clen+=strlen(val->value);
1106        cinput[clen]=0;
1107      }
1108    }
1109    if(cinput!=NULL && strncasecmp(cinput,"NULL",4)!=0){
1110      if(cParams==NULL)
1111        cParams=createMap(inp->name,cinput);
1112      else
1113        addToMap(cParams,inp->name,cinput);
1114      free(cinput);
1115    }
1116    inp=inp->next;
1117  }
1118
1119  // Fetch all output and define a resulting filename
1120  inp=*outputs;
1121  map* tmpPath=getMapFromMaps(*main_conf,"main","tmpPath");
1122  map* sid=getMapFromMaps(*main_conf,"lenv","usid");
1123  while(inp!=NULL){
1124    for(int k=0;k<pc;k++){
1125      CSG_Parameter * param=params->Get_Parameter(k);
1126      if(CSG_String(param->Get_Identifier()).Cmp(inp->name)==0){
1127        const char *file_ext=sagaGetDefaultExt(param);
1128        char *fileName=(char*)malloc((strlen(file_ext)+strlen(sid->value)+strlen(inp->name)+strlen(tmpPath->value)+11)*sizeof(char));
1129        sprintf(fileName,"%s/Output_%s_%s.%s",tmpPath->value,inp->name,sid->value,file_ext);
1130        if(cParams==NULL)
1131          cParams=createMap(inp->name,fileName);
1132        else
1133          addToMap(cParams,inp->name,fileName);
1134      }
1135    }
1136    inp=inp->next;
1137  }
1138
1139  sagaSetParameters(params,cParams);
1140
1141  module->Update_Parameter_States();
1142 
1143  bool retval=false;
1144  if(module->On_Before_Execution()){
1145    retval=module->Execute();
1146    module->On_After_Execution();
1147  }
1148
1149  sagaSaveOutputs(params,*main_conf,outputs);
1150
1151  // Loop over each outputs to transform grid to raster file when needed,
1152  // export tables, shapes or point clouds
1153  for(int k=0;k<pc;k++){
1154    CSG_Parameter * param=params->Get_Parameter(k);
1155    if(param!=NULL && param->is_Output()){
1156      maps* inmap=getMaps(*outputs,CSG_String(param->Get_Identifier()).b_str());
1157      if(inmap!=NULL){
1158        if(CSG_String(param->Get_Type_Identifier()).Contains(CSG_String("grid"))
1159           || CSG_String(param->Get_Type_Identifier()).Contains(CSG_String("data_object"))){
1160          sagaExportGDAL(main_conf,&inmap);
1161        }else{
1162          if(CSG_String(param->Get_Type_Identifier()).Contains(CSG_String("shapes"))){
1163            sagaExportOGR(main_conf,&inmap);
1164          }
1165          else{
1166            if(CSG_String(param->Get_Type_Identifier()).Contains(CSG_String("table"))){
1167            }
1168            else{
1169              if(CSG_String(param->Get_Type_Identifier()).Contains(CSG_String("tin"))){
1170                sagaExportTIN(main_conf,&inmap,"TIN");
1171              }
1172              else
1173                if(CSG_String(param->Get_Type_Identifier()).Contains(CSG_String("points"))){
1174                  sagaExportPC(main_conf,&inmap);
1175              }
1176            }
1177          }
1178        }
1179      }
1180      else if(CSG_String(param->Get_Type_Identifier()).Contains(CSG_String("tin"))){
1181        sagaExportTIN(main_conf,outputs,CSG_String(param->Get_Identifier()).b_str());
1182      }
1183             
1184    }
1185  }
1186
1187  wxUninitialize();
1188
1189  return SERVICE_SUCCEEDED;
1190}
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