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 | */ |
---|
41 | int sagaOutputCounter=0; |
---|
42 | |
---|
43 | /** |
---|
44 | * Observer used to access the ongoing status of a running OTB Application |
---|
45 | */ |
---|
46 | class 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 | |
---|
103 | maps* SagaWatcher::m_conf; |
---|
104 | char* SagaWatcher::message=zStrdup("No message left"); |
---|
105 | int 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 | */ |
---|
114 | int |
---|
115 | SagaWatcher:: |
---|
116 | Callback(TSG_UI_Callback_ID id, CSG_UI_Parameter ¶m1, CSG_UI_Parameter ¶m2) |
---|
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 | |
---|
187 | TSG_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 | */ |
---|
202 | const 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 | */ |
---|
227 | bool 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 | */ |
---|
267 | bool 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 | */ |
---|
359 | bool 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 | if( i < nFileNames ) |
---|
446 | { |
---|
447 | param->asList()->asDataObject(i)->Save(fileNames[i]); |
---|
448 | } |
---|
449 | else |
---|
450 | { |
---|
451 | param->asList()->asDataObject(i)->Save(CSG_String::Format(SG_T("%s_%0*d"), |
---|
452 | fileNames[fileNames.Get_Count() - 1].c_str(), |
---|
453 | SG_Get_Digit_Count(param->asList()->Get_Count()), |
---|
454 | 1 + i - nFileNames |
---|
455 | )); |
---|
456 | } |
---|
457 | setMapArray(cMaps->content,"generated_file",i, |
---|
458 | CSG_String(param->asList()->asDataObject(i)->Get_File_Name()).b_str()); |
---|
459 | } |
---|
460 | } |
---|
461 | } |
---|
462 | } |
---|
463 | } |
---|
464 | return( true ); |
---|
465 | } |
---|
466 | |
---|
467 | /** |
---|
468 | * Invoke the execution of a SAGA-GIS module. |
---|
469 | * |
---|
470 | * @param main_conf the conf maps containing the main.cfg settings |
---|
471 | * @param lib_name the SAGA-GIS library name |
---|
472 | * @param module_name the SAGA-GIS module name |
---|
473 | * @param arguments the map containing the arguments to pass to the module |
---|
474 | * @param outputs default to NULL, contains the maps to fill with the result |
---|
475 | */ |
---|
476 | int sagaExecuteCmd(maps** main_conf,const char* lib_name,const char* module_name,map* arguments,maps** outputs=NULL){ |
---|
477 | int res=SERVICE_FAILED; |
---|
478 | |
---|
479 | CSG_Module_Library * library=SG_Get_Module_Library_Manager().Get_Library(CSG_String(lib_name),true); |
---|
480 | if( library == NULL){ |
---|
481 | char tmp[255]; |
---|
482 | sprintf(tmp,"Counld not load the %s SAGA library",lib_name); |
---|
483 | setMapInMaps(*main_conf,"lenv","message",tmp); |
---|
484 | res=SERVICE_FAILED; |
---|
485 | return res; |
---|
486 | } |
---|
487 | |
---|
488 | CSG_Module * module=library->Get_Module(atoi(module_name)); |
---|
489 | if(module == NULL){ |
---|
490 | char tmp[255]; |
---|
491 | sprintf(tmp,"Counld not load the %s module from the %s SAGA library",module_name,lib_name); |
---|
492 | setMapInMaps(*main_conf,"lenv","message",tmp); |
---|
493 | res=SERVICE_FAILED; |
---|
494 | return res; |
---|
495 | } |
---|
496 | |
---|
497 | CSG_Parameters * params=module->Get_Parameters(); |
---|
498 | if(!params){ |
---|
499 | char tmp[255]; |
---|
500 | sprintf(tmp,"Counld not find any param for the %s module from the %s SAGA library",module_name,lib_name); |
---|
501 | setMapInMaps(*main_conf,"lenv","message",tmp); |
---|
502 | res=SERVICE_FAILED; |
---|
503 | return res; |
---|
504 | } |
---|
505 | |
---|
506 | sagaSetParameters(params,arguments); |
---|
507 | |
---|
508 | module->Update_Parameter_States(); |
---|
509 | |
---|
510 | bool retval=false; |
---|
511 | if(module->On_Before_Execution()){ |
---|
512 | retval=module->Execute(); |
---|
513 | module->On_After_Execution(); |
---|
514 | } |
---|
515 | |
---|
516 | if(retval && outputs!=NULL){ |
---|
517 | sagaSaveOutputs(module->Get_Parameters(),*main_conf,outputs); |
---|
518 | SG_Get_Data_Manager().Delete_Unsaved(); |
---|
519 | return SERVICE_SUCCEEDED; |
---|
520 | } |
---|
521 | |
---|
522 | return SERVICE_FAILED; |
---|
523 | |
---|
524 | } |
---|
525 | |
---|
526 | /** |
---|
527 | * Export a SAGA-GIS Shapes to a file in a specific format (GML,KML,GeoJSON). |
---|
528 | * saga_cmd io_gdal 4 -FILE my.format -SHAPES my.shp -FORMAT XXX |
---|
529 | * |
---|
530 | * @param main_conf the conf maps containing the main.cfg settings |
---|
531 | * @param in the output maps to fill with the resulting file |
---|
532 | */ |
---|
533 | int sagaExportOGR(maps** conf, maps** in){ |
---|
534 | map* mtype=getMap((*in)->content,"mimeType"); |
---|
535 | map* gfile=getMap((*in)->content,"generated_file"); |
---|
536 | char* fext=NULL; |
---|
537 | map* arg=NULL; |
---|
538 | if(strncasecmp(mtype->value,"text/xml",8)==0){ |
---|
539 | fext=zStrdup("xml"); |
---|
540 | } |
---|
541 | else if(strncasecmp(mtype->value,"application/json",16)==0){ |
---|
542 | fext=zStrdup("json"); |
---|
543 | } |
---|
544 | else{ |
---|
545 | fext=zStrdup("kml"); |
---|
546 | } |
---|
547 | char* tmpName=(char*)malloc((strlen(gfile->value)+2)*sizeof(char)); |
---|
548 | strncpy(tmpName,gfile->value,(strlen(gfile->value)-3)*sizeof(char)); |
---|
549 | strncpy(&tmpName[0]+(strlen(gfile->value)-3),fext,(strlen(fext))*sizeof(char)); |
---|
550 | tmpName[strlen(fext)+(strlen(gfile->value)-3)]=0; |
---|
551 | arg=createMap("SHAPES",gfile->value); |
---|
552 | addToMap(arg,"FILE",tmpName); |
---|
553 | if(strncasecmp(mtype->value,"text/xml",8)==0){ |
---|
554 | addToMap(arg,"FORMAT","GML"); |
---|
555 | } |
---|
556 | else if(strncasecmp(mtype->value,"application/json",16)==0){ |
---|
557 | addToMap(arg,"FORMAT","GeoJSON"); |
---|
558 | } |
---|
559 | else{ |
---|
560 | #ifdef HAVE_LIBKML |
---|
561 | addToMap(arg,"FORMAT","LIBKML"); |
---|
562 | #else |
---|
563 | addToMap(arg,"FORMAT","KML"); |
---|
564 | #endif |
---|
565 | } |
---|
566 | free(fext); |
---|
567 | free(gfile->value); |
---|
568 | gfile->value=zStrdup(tmpName); |
---|
569 | free(tmpName); |
---|
570 | |
---|
571 | sagaExecuteCmd(conf,"io_gdal","4",arg); |
---|
572 | freeMap(&arg); |
---|
573 | free(arg); |
---|
574 | } |
---|
575 | |
---|
576 | /** |
---|
577 | * Export a SAGA-GIS pointcloud to a las file. |
---|
578 | * saga_cmd io_shapes_las 0 -POINTS my.spc -FILE my.las |
---|
579 | * |
---|
580 | * @param main_conf the conf maps containing the main.cfg settings |
---|
581 | * @param in the output maps to fill with the resulting file |
---|
582 | */ |
---|
583 | void sagaExportPC(maps** conf, maps** in){ |
---|
584 | map* mtype=getMap((*in)->content,"mimeType"); |
---|
585 | map* gfile=getMap((*in)->content,"generated_file"); |
---|
586 | char* fext="las"; |
---|
587 | map* arg=NULL; |
---|
588 | char* tmpName=(char*)malloc((strlen(gfile->value)+2)*sizeof(char)); |
---|
589 | strncpy(tmpName,gfile->value,(strlen(gfile->value)-3)*sizeof(char)); |
---|
590 | strncpy(&tmpName[0]+(strlen(gfile->value)-3),fext,(strlen(fext))*sizeof(char)); |
---|
591 | tmpName[strlen(fext)+(strlen(gfile->value)-3)]=0; |
---|
592 | arg=createMap("POINTS",gfile->value); |
---|
593 | addToMap(arg,"FILE",tmpName); |
---|
594 | free(gfile->value); |
---|
595 | gfile->value=zStrdup(tmpName); |
---|
596 | sagaExecuteCmd(conf,"io_shapes_las","0",arg); |
---|
597 | freeMap(&arg); |
---|
598 | free(arg); |
---|
599 | free(tmpName); |
---|
600 | } |
---|
601 | |
---|
602 | /** |
---|
603 | * Export a SAGA-GIS Grid to a file in a specific format (tiff,hdr,aa). |
---|
604 | * saga_cmd io_gdal 1 -FILE my.format -GRIDS my.sgrd -FORMAT XXX |
---|
605 | * |
---|
606 | * @param main_conf the conf maps containing the main.cfg settings |
---|
607 | * @param in the output maps to fill with the resulting file |
---|
608 | */ |
---|
609 | int sagaExportGDAL(maps** conf, maps** in/*,CSG_Parameter* param*/){ |
---|
610 | map* mtype=getMap((*in)->content,"extension"); |
---|
611 | map* gfile=getMap((*in)->content,"generated_file"); |
---|
612 | char* fext=NULL; |
---|
613 | map* arg; |
---|
614 | |
---|
615 | if(mtype!=NULL) |
---|
616 | fext=zStrdup(mtype->value); |
---|
617 | else{ |
---|
618 | fext=zStrdup("tiff"); |
---|
619 | } |
---|
620 | |
---|
621 | mtype=getMap((*in)->content,"mimeType"); |
---|
622 | if(strncasecmp(mtype->value,"image/tiff",10)==0){ |
---|
623 | arg=createMap("FORMAT","1"); |
---|
624 | } |
---|
625 | else if(strncasecmp(mtype->value,"application/x-ogc-envi",22)==0){ |
---|
626 | arg=createMap("FORMAT","ENVI .hdr Labelled"); |
---|
627 | } |
---|
628 | else{ |
---|
629 | arg=createMap("FORMAT","ARC Digitized Raster Graphics"); |
---|
630 | } |
---|
631 | |
---|
632 | if(gfile!=NULL){ |
---|
633 | char* tmpName=(char*)malloc((strlen(gfile->value)+1)*sizeof(char)); |
---|
634 | strncpy(tmpName,gfile->value,(strlen(gfile->value)-4)*sizeof(char)); |
---|
635 | strncpy(&tmpName[0]+(strlen(gfile->value)-4),fext,(strlen(fext))*sizeof(char)); |
---|
636 | tmpName[strlen(fext)+(strlen(gfile->value)-4)]=0; |
---|
637 | addToMap(arg,"FILE",tmpName); |
---|
638 | addToMap(arg,"GRIDS",gfile->value); |
---|
639 | free(tmpName); |
---|
640 | free(fext); |
---|
641 | free(gfile->value); |
---|
642 | map* tmp=getMap(arg,"FILE"); |
---|
643 | gfile->value=zStrdup(tmp->value); |
---|
644 | sagaExecuteCmd(conf,"io_gdal","1",arg); |
---|
645 | } |
---|
646 | else{ |
---|
647 | // Empty result |
---|
648 | return true; |
---|
649 | } |
---|
650 | freeMap(&arg); |
---|
651 | free(arg); |
---|
652 | } |
---|
653 | |
---|
654 | /** |
---|
655 | * Export a SAGA-GIS TIN to a file in a specific format (GML,KML,GeoJSON). |
---|
656 | * Exporting TIN produce 5 separated files (POINTS, CENTER, EDGES, TRIANGLES |
---|
657 | * and POLYGONS). Even if a client can choose which result it want to have, |
---|
658 | * SAGA-GIS module will be invoked in a way that it will produce in any case |
---|
659 | * each possible outputs. The selection of a specific output is made in the |
---|
660 | * ZOO-Kernel itself and not specifically at this level. |
---|
661 | * saga_cmd tin_tools 3 -TIN my.shp -POINTS p.shp ... |
---|
662 | * |
---|
663 | * @param conf the conf maps containing the main.cfg settings |
---|
664 | * @param in the output maps to fill with the resulting file |
---|
665 | * @see sagaExportOGR |
---|
666 | */ |
---|
667 | int sagaExportTIN(maps** conf, maps** in,const char* tname/*,CSG_Parameter* param*/){ |
---|
668 | map* mtype=getMap((*in)->content,"mimeType"); |
---|
669 | map* gfile=getMap((*in)->content,"generated_file"); |
---|
670 | char* fext="shp"; |
---|
671 | map* arg=createMap("TIN",gfile->value); |
---|
672 | |
---|
673 | char* tinOut[5]={ |
---|
674 | "POINTS", |
---|
675 | "CENTER", |
---|
676 | "EDGES", |
---|
677 | "TRIANGLES", |
---|
678 | "POLYGONS" |
---|
679 | }; |
---|
680 | maps* resouts=NULL; |
---|
681 | |
---|
682 | int i=0; |
---|
683 | for(i=0;i<5;i++){ |
---|
684 | char* tmpName=(char*)malloc((strlen(gfile->value)+strlen(tinOut[i])+4)*sizeof(char)); |
---|
685 | strncpy(tmpName,gfile->value,(strlen(gfile->value)-3)*sizeof(char)); |
---|
686 | char *tmpSubName=(char*) malloc((strlen(tinOut[i])+3)*sizeof(char)); |
---|
687 | sprintf(tmpSubName,"_%s.",tinOut[i]); |
---|
688 | strncpy(&tmpName[0]+(strlen(gfile->value)-4),tmpSubName,(strlen(tmpSubName))*sizeof(char)); |
---|
689 | strncpy(&tmpName[0]+(strlen(gfile->value)+strlen(tmpSubName)-4),fext,(strlen(fext))*sizeof(char)); |
---|
690 | tmpName[strlen(fext)+(strlen(gfile->value)+strlen(tmpSubName)-4)]=0; |
---|
691 | |
---|
692 | maps* louts=createMaps(tinOut[i]); |
---|
693 | louts->content=createMap("mimeType","UNKOWN"); |
---|
694 | |
---|
695 | addToMap(arg,tinOut[i],tmpName); |
---|
696 | |
---|
697 | free(tmpName); |
---|
698 | if(resouts==NULL) |
---|
699 | resouts=dupMaps(&louts); |
---|
700 | else |
---|
701 | addMapsToMaps(&resouts,louts); |
---|
702 | freeMaps(&louts); |
---|
703 | free(louts); |
---|
704 | } |
---|
705 | |
---|
706 | sagaExecuteCmd(conf,"tin_tools","3",arg,&resouts); |
---|
707 | |
---|
708 | for(i=0;i<5;i++){ |
---|
709 | map* generatedFile=getMapFromMaps(resouts,tinOut[i],"generated_file"); |
---|
710 | setMapInMaps(*in,(CSG_String(tname)+"_"+tinOut[i]).b_str(),"generated_file",generatedFile->value); |
---|
711 | maps* cout=getMaps(*in,(CSG_String(tname)+"_"+tinOut[i]).b_str()); |
---|
712 | sagaExportOGR(conf,&cout); |
---|
713 | } |
---|
714 | return true; |
---|
715 | } |
---|
716 | |
---|
717 | /** |
---|
718 | * Import GDAL Datasource into SAGA-GIS. |
---|
719 | * saga_cmd io_gdal 0 -TRANSFORM 0 -FILES my.format -GRIDS /tmpPath/MyGridXXX.sgrd |
---|
720 | * |
---|
721 | * @param conf the conf maps containing the main.cfg settings |
---|
722 | * @param in in the inputs maps |
---|
723 | */ |
---|
724 | int sagaImportGDAL(maps** conf, maps** in){ |
---|
725 | map* l=getMap((*in)->content,"length"); |
---|
726 | bool shouldClean=false; |
---|
727 | if(l==NULL){ |
---|
728 | l=createMap("length","1"); |
---|
729 | shouldClean=true; |
---|
730 | } |
---|
731 | int len=strtol(l->value,NULL,10); |
---|
732 | int i=0; |
---|
733 | for(i=0;i<len;i++){ |
---|
734 | map* arg=createMap("TRANSFORM","0"); |
---|
735 | addToMap(arg,"INTERPOL","4"); |
---|
736 | map* v=getMapArray((*in)->content,"cache_file",i); |
---|
737 | if(v!=NULL) |
---|
738 | addToMap(arg,"FILES",v->value); |
---|
739 | addToMap(arg,"GRIDS",""); |
---|
740 | |
---|
741 | maps* louts=createMaps("GRIDS"); |
---|
742 | louts->content=createMap("mimeType","UNKOWN"); |
---|
743 | |
---|
744 | sagaExecuteCmd(conf,"io_gdal","0",arg,&louts); |
---|
745 | |
---|
746 | map* tmp=getMapFromMaps(louts,"GRIDS","generated_file"); |
---|
747 | setMapArray((*in)->content,"saga_value",i,tmp->value); |
---|
748 | |
---|
749 | freeMaps(&louts); |
---|
750 | free(louts); |
---|
751 | freeMap(&arg); |
---|
752 | free(arg); |
---|
753 | } |
---|
754 | if(shouldClean){ |
---|
755 | freeMap(&l); |
---|
756 | free(l); |
---|
757 | } |
---|
758 | } |
---|
759 | |
---|
760 | /** |
---|
761 | * Import OGR Datasource into SAGA-GIS. |
---|
762 | * saga_cmd io_gdal 3 -SHAPES my.shp -FILES my.format |
---|
763 | * |
---|
764 | * @param conf the conf maps containing the main.cfg settings |
---|
765 | * @param in in the inputs maps |
---|
766 | */ |
---|
767 | int sagaImportOGR(maps** conf, maps** in){ |
---|
768 | char *ext; |
---|
769 | map* arg; |
---|
770 | map* l=getMap((*in)->content,"length"); |
---|
771 | bool shouldClean=false; |
---|
772 | if(l==NULL){ |
---|
773 | l=createMap("length","1"); |
---|
774 | shouldClean=true; |
---|
775 | } |
---|
776 | int len=strtol(l->value,NULL,10); |
---|
777 | int i=0; |
---|
778 | for(i=0;i<len;i++){ |
---|
779 | map* v=getMapArray((*in)->content,"cache_file",i); |
---|
780 | arg=createMap("SHAPES",""); |
---|
781 | if(v!=NULL) |
---|
782 | addToMap(arg,"FILES",v->value); |
---|
783 | |
---|
784 | maps* louts=createMaps("SHAPES"); |
---|
785 | louts->content=createMap("mimeType","UNKOWN"); |
---|
786 | |
---|
787 | sagaExecuteCmd(conf,"io_gdal","3",arg,&louts); |
---|
788 | |
---|
789 | map* tmp=getMapFromMaps(louts,"SHAPES","generated_file"); |
---|
790 | setMapArray((*in)->content,"saga_value",i,tmp->value); |
---|
791 | |
---|
792 | freeMaps(&louts); |
---|
793 | free(louts); |
---|
794 | freeMap(&arg); |
---|
795 | free(arg); |
---|
796 | } |
---|
797 | if(shouldClean){ |
---|
798 | freeMap(&l); |
---|
799 | free(l); |
---|
800 | } |
---|
801 | } |
---|
802 | |
---|
803 | /** |
---|
804 | * Import TIN into SAGA-GIS. Calling this function suppose that sagaImportOGR |
---|
805 | * was called first. |
---|
806 | * saga_cmd tin_tools 2 -SHAPES myShapes.shp -TIN myTin.shp |
---|
807 | * |
---|
808 | * @param conf the conf maps containing the main.cfg settings |
---|
809 | * @param in in the inputs maps |
---|
810 | * @see sagaImportOGR |
---|
811 | */ |
---|
812 | bool sagaImportTIN(maps** conf, maps** in){ |
---|
813 | char *ext; |
---|
814 | map* arg; |
---|
815 | map* l=getMap((*in)->content,"length"); |
---|
816 | bool shouldClean=false; |
---|
817 | if(l==NULL){ |
---|
818 | l=createMap("length","1"); |
---|
819 | shouldClean=true; |
---|
820 | } |
---|
821 | int len=strtol(l->value,NULL,10); |
---|
822 | int i=0; |
---|
823 | for(i=0;i<len;i++){ |
---|
824 | map* v=getMapArray((*in)->content,"saga_value",i); |
---|
825 | arg=createMap("TIN",""); |
---|
826 | if(v!=NULL) |
---|
827 | addToMap(arg,"SHAPES",v->value); |
---|
828 | maps* louts=createMaps("TIN"); |
---|
829 | louts->content=createMap("mimeType","UNKOWN"); |
---|
830 | sagaExecuteCmd(conf,"tin_tools","2",arg,&louts); |
---|
831 | map* tmp=getMapFromMaps(louts,"TIN","generated_file"); |
---|
832 | v=getMapArray((*in)->content,"saga_value",i); |
---|
833 | if(tmp!=NULL){ |
---|
834 | if(v!=NULL){ |
---|
835 | free(v->value); |
---|
836 | v->value=zStrdup(tmp->value); |
---|
837 | } |
---|
838 | else |
---|
839 | setMapArray((*in)->content,"saga_value",i,tmp->value); |
---|
840 | } |
---|
841 | freeMaps(&louts); |
---|
842 | free(louts); |
---|
843 | freeMap(&arg); |
---|
844 | free(arg); |
---|
845 | } |
---|
846 | if(shouldClean){ |
---|
847 | freeMap(&l); |
---|
848 | free(l); |
---|
849 | } |
---|
850 | return true; |
---|
851 | } |
---|
852 | |
---|
853 | /** |
---|
854 | * Import table into SAGA-GIS. |
---|
855 | * saga_cmd io_table 1 -TABLE myTable -FILENAME myFile -SEPARATOR 2 |
---|
856 | * |
---|
857 | * @param conf the conf maps containing the main.cfg settings |
---|
858 | * @param in in the inputs maps |
---|
859 | */ |
---|
860 | int sagaImportTable(maps** conf, maps** in){ |
---|
861 | char *ext; |
---|
862 | map* arg; |
---|
863 | map* l=getMap((*in)->content,"length"); |
---|
864 | bool shouldClean=false; |
---|
865 | if(l==NULL){ |
---|
866 | l=createMap("length","1"); |
---|
867 | shouldClean=true; |
---|
868 | } |
---|
869 | int len=strtol(l->value,NULL,10); |
---|
870 | int i=0; |
---|
871 | for(i=0;i<len;i++){ |
---|
872 | |
---|
873 | // Create and fill arg map |
---|
874 | arg=createMap("SEPARATOR","2"); |
---|
875 | addToMap(arg,"TABLE",""); |
---|
876 | map* v=getMapArray((*in)->content,"cache_file",i); |
---|
877 | if(v!=NULL) |
---|
878 | addToMap(arg,"FILENAME",v->value); |
---|
879 | |
---|
880 | // Create the output maps |
---|
881 | maps* louts=createMaps("TABLE"); |
---|
882 | louts->content=createMap("mimeType","UNKOWN"); |
---|
883 | |
---|
884 | // Execute the saga command |
---|
885 | sagaExecuteCmd(conf,"io_table","1",arg,&louts); |
---|
886 | |
---|
887 | // Fetch result and add it to the original map as saga_value |
---|
888 | map* tmp=getMapFromMaps(louts,"TABLE","generated_file"); |
---|
889 | setMapArray((*in)->content,"saga_value",i,tmp->value); |
---|
890 | |
---|
891 | // Cleanup |
---|
892 | freeMaps(&louts); |
---|
893 | free(louts); |
---|
894 | freeMap(&arg); |
---|
895 | free(arg); |
---|
896 | |
---|
897 | } |
---|
898 | // Cleanup if required |
---|
899 | if(shouldClean){ |
---|
900 | freeMap(&l); |
---|
901 | free(l); |
---|
902 | } |
---|
903 | } |
---|
904 | |
---|
905 | /** |
---|
906 | * Import las file as pointcloud into SAGA-GIS. |
---|
907 | * saga_cmd io_shapes_las 1 -POINTS my.spc -FILENAME my.las |
---|
908 | * |
---|
909 | * @param conf the conf maps containing the main.cfg settings |
---|
910 | * @param in in the inputs maps |
---|
911 | */ |
---|
912 | int sagaImportPC(maps** conf, maps** in){ |
---|
913 | char *ext; |
---|
914 | map* arg; |
---|
915 | map* l=getMap((*in)->content,"length"); |
---|
916 | bool shouldClean=false; |
---|
917 | if(l==NULL){ |
---|
918 | l=createMap("length","1"); |
---|
919 | shouldClean=true; |
---|
920 | } |
---|
921 | int len=strtol(l->value,NULL,10); |
---|
922 | int i=0; |
---|
923 | for(i=0;i<len;i++){ |
---|
924 | |
---|
925 | // Create and fill arg map |
---|
926 | arg=createMap("POINTS",""); |
---|
927 | map* v=getMapArray((*in)->content,"cache_file",i); |
---|
928 | if(v!=NULL) |
---|
929 | addToMap(arg,"FILES",v->value); |
---|
930 | |
---|
931 | // Create the output maps |
---|
932 | maps* louts=createMaps("POINTS"); |
---|
933 | louts->content=createMap("mimeType","UNKOWN"); |
---|
934 | |
---|
935 | // Execute the saga command |
---|
936 | sagaExecuteCmd(conf,"io_shapes_las","1",arg,&louts); |
---|
937 | |
---|
938 | // Fetch result and add it to the original map as saga_value |
---|
939 | map* tmp=getMapFromMaps(louts,"POINTS","generated_file"); |
---|
940 | setMapArray((*in)->content,"saga_value",i,tmp->value); |
---|
941 | |
---|
942 | // Cleanup |
---|
943 | freeMaps(&louts); |
---|
944 | free(louts); |
---|
945 | freeMap(&arg); |
---|
946 | free(arg); |
---|
947 | |
---|
948 | } |
---|
949 | // Cleanup if required |
---|
950 | if(shouldClean){ |
---|
951 | freeMap(&l); |
---|
952 | free(l); |
---|
953 | } |
---|
954 | } |
---|
955 | |
---|
956 | /** |
---|
957 | * Load and invoke a SAGA-GIS module defined in a service metadata definitions. |
---|
958 | * Load all the input data into SAGA-GIS using io_gdal, io_tables and |
---|
959 | * io_shapes_las for SAGA grids/shapes, tables and pointcloud respectively. |
---|
960 | * Load and run the module from its library and invoke it using the data |
---|
961 | * imported in SAGA-GIS at first stage. After the execution, export the outputs |
---|
962 | * to files using io_gdal and io_shapes_las for grids/shapes and pointcloud |
---|
963 | * respectively. |
---|
964 | * |
---|
965 | * @param main_conf the conf maps containing the main.cfg settings |
---|
966 | * @param request the map containing the HTTP request |
---|
967 | * @param s the service structure |
---|
968 | * @param inputs the maps containing the inputs |
---|
969 | * @param outputs the maps containing the outputs |
---|
970 | */ |
---|
971 | int zoo_saga_support(maps** main_conf,map* request,service* s,maps** inputs,maps** outputs){ |
---|
972 | int res=SERVICE_FAILED; |
---|
973 | if( !wxInitialize() ){ |
---|
974 | fprintf(stderr,"initialisation failed"); |
---|
975 | return SERVICE_FAILED; |
---|
976 | } |
---|
977 | setlocale(LC_NUMERIC, "C"); |
---|
978 | static bool g_bShow_Messages = false; |
---|
979 | |
---|
980 | dumpMapsValuesToFiles(main_conf,inputs); |
---|
981 | |
---|
982 | SagaWatcher watcher=SagaWatcher(); |
---|
983 | watcher.SetConf(main_conf); |
---|
984 | |
---|
985 | SG_Set_UI_Callback(Get_Callback(watcher)); |
---|
986 | |
---|
987 | int n = SG_Get_Module_Library_Manager().Add_Directory(wxT(MODULE_LIBRARY_PATH),false); |
---|
988 | if( SG_Get_Module_Library_Manager().Get_Count() <= 0 ){ |
---|
989 | setMapInMaps(*main_conf,"lenv","message","Could not load any SAGA tool library"); |
---|
990 | res=SERVICE_FAILED; |
---|
991 | return res; |
---|
992 | } |
---|
993 | |
---|
994 | map* serviceProvider=getMap(s->content,"serviceProvider"); |
---|
995 | |
---|
996 | // Load the SAGA-GIS library corresponding to the serviceProvider |
---|
997 | CSG_Module_Library * library=SG_Get_Module_Library_Manager().Get_Library(CSG_String(serviceProvider->value),true); |
---|
998 | if( library == NULL){ |
---|
999 | char tmp[255]; |
---|
1000 | sprintf(tmp,"Counld not load the %s SAGA library",serviceProvider->value); |
---|
1001 | setMapInMaps(*main_conf,"lenv","message",tmp); |
---|
1002 | res=SERVICE_FAILED; |
---|
1003 | return res; |
---|
1004 | } |
---|
1005 | |
---|
1006 | // Load the SAGA-GIS module corresponding to the service name from the library |
---|
1007 | CSG_Module * module=library->Get_Module(atoi(s->name)); |
---|
1008 | if(module == NULL){ |
---|
1009 | char tmp[255]; |
---|
1010 | sprintf(tmp,"Counld not load the %s module from the %s SAGA library", |
---|
1011 | s->name,serviceProvider->value); |
---|
1012 | setMapInMaps(*main_conf,"lenv","message",tmp); |
---|
1013 | res=SERVICE_FAILED; |
---|
1014 | return res; |
---|
1015 | } |
---|
1016 | |
---|
1017 | // Load all the parameters defined for the module |
---|
1018 | CSG_Parameters * params=module->Get_Parameters(); |
---|
1019 | int pc=params->Get_Count(); |
---|
1020 | if(!params){ |
---|
1021 | char tmp[255]; |
---|
1022 | sprintf(tmp,"Counld not find any param for the %s module from the %s SAGA library", |
---|
1023 | s->name,serviceProvider->value); |
---|
1024 | setMapInMaps(*main_conf,"lenv","message",tmp); |
---|
1025 | res=SERVICE_FAILED; |
---|
1026 | return res; |
---|
1027 | } |
---|
1028 | |
---|
1029 | // Loop over each inputs to transform raster files to grid when needed, |
---|
1030 | // import tables, shapes or point clouds |
---|
1031 | for(int k=0;k<pc;k++){ |
---|
1032 | CSG_Parameter * param=params->Get_Parameter(k); |
---|
1033 | if(param!=NULL && !param->is_Output()){ |
---|
1034 | maps* inmap=getMaps(*inputs,CSG_String(param->Get_Identifier()).b_str()); |
---|
1035 | if(inmap!=NULL){ |
---|
1036 | map* tmp=getMap(inmap->content,"value"); |
---|
1037 | if(tmp==NULL || strncasecmp(tmp->value,"NULL",4)!=0){ |
---|
1038 | if(CSG_String(param->Get_Type_Identifier()).Contains(CSG_String("grid"))){ |
---|
1039 | sagaImportGDAL(main_conf,&inmap); |
---|
1040 | } |
---|
1041 | else if(CSG_String(param->Get_Type_Identifier()).Contains(CSG_String("tin"))){ |
---|
1042 | sagaImportOGR(main_conf,&inmap); |
---|
1043 | sagaImportTIN(main_conf,&inmap); |
---|
1044 | } |
---|
1045 | else if(CSG_String(param->Get_Type_Identifier()).Contains(CSG_String("shapes"))){ |
---|
1046 | sagaImportOGR(main_conf,&inmap); |
---|
1047 | } |
---|
1048 | else{ |
---|
1049 | if(CSG_String(param->Get_Type_Identifier()).Contains(CSG_String("table"))){ |
---|
1050 | sagaImportTable(main_conf,&inmap); |
---|
1051 | } |
---|
1052 | else |
---|
1053 | if(CSG_String(param->Get_Type_Identifier()).Contains(CSG_String("points"))){ |
---|
1054 | sagaImportPC(main_conf,&inmap); |
---|
1055 | } |
---|
1056 | } |
---|
1057 | } |
---|
1058 | } |
---|
1059 | } |
---|
1060 | } |
---|
1061 | |
---|
1062 | // Create a map conraining arguments to pass to the SAGA-GIS module |
---|
1063 | // Fetch all input value (specifically for data imported into SAGA-GIS) |
---|
1064 | maps* inp=*inputs; |
---|
1065 | int k=0; |
---|
1066 | map* cParams=NULL; |
---|
1067 | while(inp!=NULL){ |
---|
1068 | map* len=getMap(inp->content,"length"); |
---|
1069 | bool shouldClean=false; |
---|
1070 | if(len==NULL){ |
---|
1071 | len=createMap("length","1"); |
---|
1072 | shouldClean=true; |
---|
1073 | } |
---|
1074 | int len0=strtol(len->value,NULL,10); |
---|
1075 | int i=0; |
---|
1076 | char *cinput=NULL; |
---|
1077 | int clen=0; |
---|
1078 | for(i=0;i<len0;i++){ |
---|
1079 | map* val=getMapArray(inp->content,"saga_value",i); |
---|
1080 | if(val==NULL) |
---|
1081 | val=getMapArray(inp->content,"value",i); |
---|
1082 | if(val!=NULL && strncasecmp(val->value,"NULL",4)!=0){ |
---|
1083 | if(cinput==NULL){ |
---|
1084 | cinput=zStrdup(val->value); |
---|
1085 | } |
---|
1086 | else{ |
---|
1087 | cinput=(char*)realloc(cinput,(clen+strlen(val->value)+1)*sizeof(char)); |
---|
1088 | strncpy(&cinput[0]+clen,";",1); |
---|
1089 | strncpy(&cinput[0]+(clen+1),val->value,strlen(val->value)); |
---|
1090 | clen+=1; |
---|
1091 | } |
---|
1092 | clen+=strlen(val->value); |
---|
1093 | cinput[clen]=0; |
---|
1094 | } |
---|
1095 | } |
---|
1096 | if(cinput!=NULL && strncasecmp(cinput,"NULL",4)!=0){ |
---|
1097 | if(cParams==NULL) |
---|
1098 | cParams=createMap(inp->name,cinput); |
---|
1099 | else |
---|
1100 | addToMap(cParams,inp->name,cinput); |
---|
1101 | free(cinput); |
---|
1102 | } |
---|
1103 | inp=inp->next; |
---|
1104 | } |
---|
1105 | |
---|
1106 | // Fetch all output and define a resulting filename |
---|
1107 | inp=*outputs; |
---|
1108 | map* tmpPath=getMapFromMaps(*main_conf,"main","tmpPath"); |
---|
1109 | map* sid=getMapFromMaps(*main_conf,"lenv","usid"); |
---|
1110 | while(inp!=NULL){ |
---|
1111 | for(int k=0;k<pc;k++){ |
---|
1112 | CSG_Parameter * param=params->Get_Parameter(k); |
---|
1113 | if(CSG_String(param->Get_Identifier()).Cmp(inp->name)==0){ |
---|
1114 | const char *file_ext=sagaGetDefaultExt(param); |
---|
1115 | char *fileName=(char*)malloc((strlen(file_ext)+strlen(sid->value)+strlen(inp->name)+strlen(tmpPath->value)+11)*sizeof(char)); |
---|
1116 | sprintf(fileName,"%s/Output_%s_%s.%s",tmpPath->value,inp->name,sid->value,file_ext); |
---|
1117 | if(cParams==NULL) |
---|
1118 | cParams=createMap(inp->name,fileName); |
---|
1119 | else |
---|
1120 | addToMap(cParams,inp->name,fileName); |
---|
1121 | } |
---|
1122 | } |
---|
1123 | inp=inp->next; |
---|
1124 | } |
---|
1125 | |
---|
1126 | sagaSetParameters(params,cParams); |
---|
1127 | |
---|
1128 | module->Update_Parameter_States(); |
---|
1129 | |
---|
1130 | bool retval=false; |
---|
1131 | if(module->On_Before_Execution()){ |
---|
1132 | retval=module->Execute(); |
---|
1133 | module->On_After_Execution(); |
---|
1134 | } |
---|
1135 | |
---|
1136 | sagaSaveOutputs(params,*main_conf,outputs); |
---|
1137 | |
---|
1138 | // Loop over each outputs to transform grid to raster file when needed, |
---|
1139 | // export tables, shapes or point clouds |
---|
1140 | for(int k=0;k<pc;k++){ |
---|
1141 | CSG_Parameter * param=params->Get_Parameter(k); |
---|
1142 | if(param!=NULL && param->is_Output()){ |
---|
1143 | maps* inmap=getMaps(*outputs,CSG_String(param->Get_Identifier()).b_str()); |
---|
1144 | if(inmap!=NULL){ |
---|
1145 | if(CSG_String(param->Get_Type_Identifier()).Contains(CSG_String("grid")) |
---|
1146 | || CSG_String(param->Get_Type_Identifier()).Contains(CSG_String("data_object"))){ |
---|
1147 | sagaExportGDAL(main_conf,&inmap); |
---|
1148 | }else{ |
---|
1149 | if(CSG_String(param->Get_Type_Identifier()).Contains(CSG_String("shapes"))){ |
---|
1150 | sagaExportOGR(main_conf,&inmap); |
---|
1151 | } |
---|
1152 | else{ |
---|
1153 | if(CSG_String(param->Get_Type_Identifier()).Contains(CSG_String("table"))){ |
---|
1154 | } |
---|
1155 | else{ |
---|
1156 | if(CSG_String(param->Get_Type_Identifier()).Contains(CSG_String("tin"))){ |
---|
1157 | sagaExportTIN(main_conf,&inmap,"TIN"); |
---|
1158 | } |
---|
1159 | else |
---|
1160 | if(CSG_String(param->Get_Type_Identifier()).Contains(CSG_String("points"))){ |
---|
1161 | sagaExportPC(main_conf,&inmap); |
---|
1162 | } |
---|
1163 | } |
---|
1164 | } |
---|
1165 | } |
---|
1166 | } |
---|
1167 | else if(CSG_String(param->Get_Type_Identifier()).Contains(CSG_String("tin"))){ |
---|
1168 | sagaExportTIN(main_conf,outputs,CSG_String(param->Get_Identifier()).b_str()); |
---|
1169 | } |
---|
1170 | |
---|
1171 | } |
---|
1172 | } |
---|
1173 | |
---|
1174 | wxUninitialize(); |
---|
1175 | |
---|
1176 | return SERVICE_SUCCEEDED; |
---|
1177 | } |
---|