1
2 ///////////////////////////////////////////////////////////
3 // //
4 // SAGA //
5 // //
6 // System for Automated Geoscientific Analyses //
7 // //
8 // Application Programming Interface //
9 // //
10 // Library: SAGA_API //
11 // //
12 //-------------------------------------------------------//
13 // //
14 // data_manager.cpp //
15 // //
16 // Copyright (C) 2013 by Olaf Conrad //
17 // //
18 //-------------------------------------------------------//
19 // //
20 // This file is part of 'SAGA - System for Automated //
21 // Geoscientific Analyses'. //
22 // //
23 // This library is free software; you can redistribute //
24 // it and/or modify it under the terms of the GNU Lesser //
25 // General Public License as published by the Free //
26 // Software Foundation, either version 2.1 of the //
27 // License, or (at your option) any later version. //
28 // //
29 // This library is distributed in the hope that it will //
30 // be useful, but WITHOUT ANY WARRANTY; without even the //
31 // implied warranty of MERCHANTABILITY or FITNESS FOR A //
32 // PARTICULAR PURPOSE. See the GNU Lesser General Public //
33 // License for more details. //
34 // //
35 // You should have received a copy of the GNU Lesser //
36 // General Public License along with this program; if //
37 // not, see <http://www.gnu.org/licenses/>. //
38 // //
39 //-------------------------------------------------------//
40 // //
41 // contact: Olaf Conrad //
42 // Institute of Geography //
43 // University of Hamburg //
44 // Germany //
45 // //
46 // e-mail: oconrad@saga-gis.org //
47 // //
48 ///////////////////////////////////////////////////////////
49
50 //---------------------------------------------------------
51 #include "data_manager.h"
52 #include "tool_library.h"
53
54
55 ///////////////////////////////////////////////////////////
56 // //
57 // //
58 // //
59 ///////////////////////////////////////////////////////////
60
61 //---------------------------------------------------------
62 CSG_Data_Manager g_Data_Manager;
63
64 //---------------------------------------------------------
SG_Get_Data_Manager(void)65 CSG_Data_Manager & SG_Get_Data_Manager (void)
66 {
67 return( g_Data_Manager );
68 }
69
70
71 ///////////////////////////////////////////////////////////
72 // //
73 // //
74 // //
75 ///////////////////////////////////////////////////////////
76
77 //---------------------------------------------------------
CSG_Data_Collection(CSG_Data_Manager * pManager,TSG_Data_Object_Type Type)78 CSG_Data_Collection::CSG_Data_Collection(CSG_Data_Manager *pManager, TSG_Data_Object_Type Type)
79 {
80 m_pManager = pManager;
81 m_Type = Type;
82 }
83
84 //---------------------------------------------------------
~CSG_Data_Collection(void)85 CSG_Data_Collection::~CSG_Data_Collection(void)
86 {
87 Delete_All();
88 }
89
90 //---------------------------------------------------------
Get(const CSG_String & File,bool bNative) const91 CSG_Data_Object * CSG_Data_Collection::Get(const CSG_String &File, bool bNative) const
92 {
93 for(size_t i=0; i<Count(); i++)
94 {
95 if( !File.Cmp(Get(i)->Get_File_Name(bNative)) )
96 {
97 return( Get(i) );
98 }
99 }
100
101 return( NULL );
102 }
103
104 //---------------------------------------------------------
Exists(CSG_Data_Object * pObject) const105 bool CSG_Data_Collection::Exists(CSG_Data_Object *pObject) const
106 {
107 for(size_t i=0; i<Count(); i++)
108 {
109 if( pObject == Get(i) )
110 {
111 return( true );
112 }
113 }
114
115 return( false );
116 }
117
118 //---------------------------------------------------------
Add(CSG_Data_Object * pObject)119 bool CSG_Data_Collection::Add(CSG_Data_Object *pObject)
120 {
121 if( pObject != DATAOBJECT_NOTSET && pObject != DATAOBJECT_CREATE )
122 {
123 if( Exists(pObject) )
124 {
125 return( true );
126 }
127
128 if( m_Objects.Inc_Array() )
129 {
130 m_Objects[Count() - 1] = pObject;
131
132 if( m_pManager == &g_Data_Manager )
133 {
134 SG_UI_DataObject_Add(pObject, SG_UI_DATAOBJECT_UPDATE_ONLY);
135 }
136
137 return( true );
138 }
139 }
140
141 return( false );
142 }
143
144 //---------------------------------------------------------
Delete(CSG_Data_Object * pObject,bool bDetachOnly)145 bool CSG_Data_Collection::Delete(CSG_Data_Object *pObject, bool bDetachOnly)
146 {
147 CSG_Data_Object **pObjects = (CSG_Data_Object **)m_Objects.Get_Array();
148
149 size_t i, n;
150
151 for(i=0, n=0; i<Count(); i++)
152 {
153 if( pObject == Get(i) )
154 {
155 if( !bDetachOnly )
156 {
157 delete(Get(i));
158
159 bDetachOnly = true; // just in case the same object has been added more than once
160 }
161 }
162 else
163 {
164 pObjects[n++] = pObjects[i];
165 }
166 }
167
168 if( n < m_Objects.Get_Size() )
169 {
170 m_Objects.Set_Array(n);
171
172 return( true );
173 }
174
175 return( false );
176 }
177
178 //---------------------------------------------------------
Delete(size_t i,bool bDetachOnly)179 bool CSG_Data_Collection::Delete(size_t i, bool bDetachOnly)
180 {
181 return( Delete(Get(i), bDetachOnly) );
182 }
183
184 //---------------------------------------------------------
Delete_All(bool bDetachOnly)185 bool CSG_Data_Collection::Delete_All(bool bDetachOnly)
186 {
187 if( !bDetachOnly )
188 {
189 for(size_t i=0; i<Count(); i++)
190 {
191 delete(Get(i));
192 }
193 }
194
195 m_Objects.Set_Array(0);
196
197 return( true );
198 }
199
200 //---------------------------------------------------------
Delete_Unsaved(bool bDetachOnly)201 bool CSG_Data_Collection::Delete_Unsaved(bool bDetachOnly)
202 {
203 for(size_t i=Count(); i>0; i--)
204 {
205 if( !SG_File_Exists(Get(i - 1)->Get_File_Name()) )
206 {
207 Delete(i, bDetachOnly);
208 }
209 }
210
211 return( true );
212 }
213
214
215 ///////////////////////////////////////////////////////////
216 // //
217 // //
218 // //
219 ///////////////////////////////////////////////////////////
220
221 //---------------------------------------------------------
CSG_Grid_Collection(CSG_Data_Manager * pManager)222 CSG_Grid_Collection::CSG_Grid_Collection(CSG_Data_Manager *pManager)
223 : CSG_Data_Collection(pManager, SG_DATAOBJECT_TYPE_Grid)
224 {}
225
226 //---------------------------------------------------------
Exists(CSG_Data_Object * pObject) const227 bool CSG_Grid_Collection::Exists(CSG_Data_Object *pObject) const
228 {
229 if( pObject == DATAOBJECT_NOTSET || pObject == DATAOBJECT_CREATE )
230 {
231 return( false );
232 }
233
234 for(size_t i=0; i<Count(); i++)
235 {
236 if( pObject == Get(i) )
237 {
238 return( true );
239 }
240 else if( Get(i)->Get_ObjectType() == SG_DATAOBJECT_TYPE_Grids )
241 { // does object (CSG_Grid) belong to a grid collection (CSG_Grids)
242 CSG_Grids *pGrids = (CSG_Grids *)Get(i);
243
244 for(int j=0; j<pGrids->Get_NZ(); j++)
245 {
246 if( pObject == pGrids->Get_Grid_Ptr(j) )
247 {
248 return( true );
249 }
250 }
251 }
252 }
253
254 return( false );
255 }
256
257 //---------------------------------------------------------
Add(CSG_Data_Object * pObject)258 bool CSG_Grid_Collection::Add(CSG_Data_Object *pObject)
259 {
260 if( pObject != DATAOBJECT_NOTSET && pObject != DATAOBJECT_CREATE )
261 {
262 CSG_Grid_System System;
263
264 switch( pObject->Get_ObjectType() )
265 {
266 case SG_DATAOBJECT_TYPE_Grid : System = ((CSG_Grid *)pObject)->Get_System(); break;
267 case SG_DATAOBJECT_TYPE_Grids: System = ((CSG_Grids *)pObject)->Get_System(); break;
268
269 default: return( false );
270 }
271
272 if( System.is_Valid() )
273 {
274 if( Count() == 0 || !m_System.is_Valid() )
275 {
276 m_System = System;
277 }
278
279 if( m_System == System )
280 {
281 return( CSG_Data_Collection::Add(pObject) );
282 }
283 }
284 }
285
286 return( false );
287 }
288
289
290 ///////////////////////////////////////////////////////////
291 // //
292 // //
293 // //
294 ///////////////////////////////////////////////////////////
295
296 //---------------------------------------------------------
CSG_Data_Manager(void)297 CSG_Data_Manager::CSG_Data_Manager(void)
298 {
299 m_pTable = new CSG_Data_Collection(this, SG_DATAOBJECT_TYPE_Table );
300 m_pTIN = new CSG_Data_Collection(this, SG_DATAOBJECT_TYPE_TIN );
301 m_pPoint_Cloud = new CSG_Data_Collection(this, SG_DATAOBJECT_TYPE_PointCloud);
302 m_pShapes = new CSG_Data_Collection(this, SG_DATAOBJECT_TYPE_Shapes );
303 }
304
305 //---------------------------------------------------------
~CSG_Data_Manager(void)306 CSG_Data_Manager::~CSG_Data_Manager(void)
307 {
308 Delete_All();
309
310 delete(m_pTable );
311 delete(m_pTIN );
312 delete(m_pPoint_Cloud);
313 delete(m_pShapes );
314 }
315
316
317 ///////////////////////////////////////////////////////////
318 // //
319 ///////////////////////////////////////////////////////////
320
321 //---------------------------------------------------------
_Get_Collection(CSG_Data_Object * pObject) const322 CSG_Data_Collection * CSG_Data_Manager::_Get_Collection(CSG_Data_Object *pObject) const
323 {
324 if( pObject != DATAOBJECT_NOTSET && pObject != DATAOBJECT_CREATE )
325 {
326 switch( pObject->Get_ObjectType() )
327 {
328 case SG_DATAOBJECT_TYPE_Table : return( m_pTable );
329 case SG_DATAOBJECT_TYPE_TIN : return( m_pTIN );
330 case SG_DATAOBJECT_TYPE_PointCloud: return( m_pPoint_Cloud );
331 case SG_DATAOBJECT_TYPE_Shapes : return( m_pShapes );
332 case SG_DATAOBJECT_TYPE_Grid : return( Get_Grid_System(((CSG_Grid *)pObject)->Get_System()) );
333 case SG_DATAOBJECT_TYPE_Grids : return( Get_Grid_System(((CSG_Grids *)pObject)->Get_System()) );
334
335 default: break;
336 }
337 }
338
339 return( NULL );
340 }
341
342
343 ///////////////////////////////////////////////////////////
344 // //
345 ///////////////////////////////////////////////////////////
346
347 //---------------------------------------------------------
Get_Grid_System(const CSG_Grid_System & System) const348 CSG_Grid_Collection * CSG_Data_Manager::Get_Grid_System(const CSG_Grid_System &System) const
349 {
350 for(size_t i=0; i<Grid_System_Count(); i++)
351 {
352 CSG_Grid_Collection *pSystem = Get_Grid_System(i);
353
354 if( pSystem->is_Equal(System) )
355 {
356 return( pSystem );
357 }
358 }
359
360 return( NULL );
361 }
362
363 //---------------------------------------------------------
Exists(const CSG_Grid_System & System) const364 bool CSG_Data_Manager::Exists(const CSG_Grid_System &System) const
365 {
366 return( Get_Grid_System(System) != NULL );
367 }
368
369 //---------------------------------------------------------
Exists(CSG_Data_Object * pObject) const370 bool CSG_Data_Manager::Exists(CSG_Data_Object *pObject) const
371 {
372 if( m_pTable ->Exists(pObject) ) return( true );
373 if( m_pTIN ->Exists(pObject) ) return( true );
374 if( m_pPoint_Cloud->Exists(pObject) ) return( true );
375 if( m_pShapes ->Exists(pObject) ) return( true );
376
377 for(size_t i=0; i<Grid_System_Count(); i++)
378 {
379 if( Get_Grid_System(i)->Exists(pObject) ) return( true );
380 }
381
382 return( false );
383 }
384
385 //---------------------------------------------------------
Find(const CSG_String & File,bool bNative) const386 CSG_Data_Object * CSG_Data_Manager::Find(const CSG_String &File, bool bNative) const
387 {
388 CSG_Data_Object *pObject;
389
390 if( (pObject = m_pTable ->Get(File, bNative)) != NULL ) return( pObject );
391 if( (pObject = m_pTIN ->Get(File, bNative)) != NULL ) return( pObject );
392 if( (pObject = m_pPoint_Cloud->Get(File, bNative)) != NULL ) return( pObject );
393 if( (pObject = m_pShapes ->Get(File, bNative)) != NULL ) return( pObject );
394
395 for(size_t i=0; i<Grid_System_Count(); i++)
396 {
397 if( (pObject = Get_Grid_System(i)->Get(File, bNative)) != NULL ) return( pObject );
398 }
399
400 return( NULL );
401 }
402
403
404 ///////////////////////////////////////////////////////////
405 // //
406 ///////////////////////////////////////////////////////////
407
408 //---------------------------------------------------------
Add(CSG_Data_Object * pObject)409 bool CSG_Data_Manager::Add(CSG_Data_Object *pObject)
410 {
411 CSG_Data_Collection *pCollection = _Get_Collection(pObject);
412
413 if( pCollection == NULL && pObject != DATAOBJECT_NOTSET && pObject != DATAOBJECT_CREATE && (pObject->Get_ObjectType() == SG_DATAOBJECT_TYPE_Grid || pObject->Get_ObjectType() == SG_DATAOBJECT_TYPE_Grids) && m_Grid_Systems.Inc_Array() )
414 {
415 pCollection = new CSG_Grid_Collection(this);
416
417 m_Grid_Systems[m_Grid_Systems.Get_Size() - 1] = pCollection;
418 }
419
420 return( pCollection && pCollection->Add(pObject) );
421 }
422
423 //---------------------------------------------------------
Add(const CSG_String & File,TSG_Data_Object_Type Type)424 CSG_Data_Object * CSG_Data_Manager::Add(const CSG_String &File, TSG_Data_Object_Type Type)
425 {
426 //-----------------------------------------------------
427 if( Type == SG_DATAOBJECT_TYPE_Undefined )
428 {
429 if( SG_File_Cmp_Extension(File, "txt" )
430 || SG_File_Cmp_Extension(File, "csv" )
431 || SG_File_Cmp_Extension(File, "dbf" ) )
432 {
433 Type = SG_DATAOBJECT_TYPE_Table;
434 }
435
436 if( SG_File_Cmp_Extension(File, "shp" ) )
437 {
438 Type = SG_DATAOBJECT_TYPE_Shapes;
439 }
440
441 if( SG_File_Cmp_Extension(File, "sg-pts-z")
442 || SG_File_Cmp_Extension(File, "sg-pts" )
443 || SG_File_Cmp_Extension(File, "spc" ) )
444 {
445 Type = SG_DATAOBJECT_TYPE_PointCloud;
446 }
447
448 if( SG_File_Cmp_Extension(File, "sg-grd-z")
449 || SG_File_Cmp_Extension(File, "sg-grd" )
450 || SG_File_Cmp_Extension(File, "sgrd" )
451 || SG_File_Cmp_Extension(File, "dgm" )
452 || SG_File_Cmp_Extension(File, "grd" ) )
453 {
454 Type = SG_DATAOBJECT_TYPE_Grid;
455 }
456
457 if( SG_File_Cmp_Extension(File, "sg-gds-z")
458 || SG_File_Cmp_Extension(File, "sg-gds" ) )
459 {
460 Type = SG_DATAOBJECT_TYPE_Grids;
461 }
462 }
463
464 //-----------------------------------------------------
465 CSG_Data_Object *pObject;
466
467 switch( Type )
468 {
469 case SG_DATAOBJECT_TYPE_Table : pObject = new CSG_Table (File); break;
470 case SG_DATAOBJECT_TYPE_Shapes : pObject = new CSG_Shapes (File); break;
471 case SG_DATAOBJECT_TYPE_TIN : pObject = new CSG_TIN (File); break;
472 case SG_DATAOBJECT_TYPE_PointCloud: pObject = new CSG_PointCloud(File); break;
473 case SG_DATAOBJECT_TYPE_Grid : pObject = new CSG_Grid (File); break;
474 case SG_DATAOBJECT_TYPE_Grids : pObject = new CSG_Grids (File); break;
475 default : pObject = NULL ; break;
476 }
477
478 if( pObject )
479 {
480 if( pObject->is_Valid() && Add(pObject) )
481 {
482 return( pObject );
483 }
484
485 delete(pObject);
486 }
487
488 //-----------------------------------------------------
489 return( _Add_External(File) );
490 }
491
492 //---------------------------------------------------------
_Add_External(const CSG_String & File)493 CSG_Data_Object * CSG_Data_Manager::_Add_External(const CSG_String &File)
494 {
495 CSG_Data_Object *pData = NULL;
496
497 if( !SG_File_Exists(File) )
498 {
499 return( pData );
500 }
501
502 CSG_Tool *pImport = NULL;
503
504 SG_UI_Msg_Lock(true);
505
506 //-----------------------------------------------------
507 // Image Import
508
509 if( ( SG_File_Cmp_Extension(File, "bmp")
510 || SG_File_Cmp_Extension(File, "gif")
511 || SG_File_Cmp_Extension(File, "jpg")
512 || SG_File_Cmp_Extension(File, "png")
513 || SG_File_Cmp_Extension(File, "pcx")
514 || SG_File_Cmp_Extension(File, "xpm") )
515 && (pImport = SG_Get_Tool_Library_Manager().Create_Tool("io_grid_image", 1)) != NULL
516 && pImport->Set_Parameter("FILE", File, PARAMETER_TYPE_FilePath) )
517 {
518 pImport->Set_Manager(this);
519
520 if( pImport->Execute() )
521 {
522 pData = pImport->Get_Parameter("OUT_GRID")->asDataObject();
523 }
524 }
525
526 SG_Get_Tool_Library_Manager().Delete_Tool(pImport);
527
528 //-----------------------------------------------------
529 // GDAL Import
530
531 if( !pData
532 && (pImport = SG_Get_Tool_Library_Manager().Create_Tool("io_gdal", 0)) != NULL
533 && pImport->Set_Parameter("FILES", File, PARAMETER_TYPE_FilePath) )
534 {
535 pImport->Set_Manager(this);
536
537 if( pImport->Execute() )
538 {
539 pData = pImport->Get_Parameter("GRIDS")->asList()->Get_Item(0);
540 }
541 }
542
543 SG_Get_Tool_Library_Manager().Delete_Tool(pImport);
544
545 //-----------------------------------------------------
546 // OGR Import
547
548 if( !pData
549 && (pImport = SG_Get_Tool_Library_Manager().Create_Tool("io_gdal", 3)) != NULL
550 && pImport->Set_Parameter("FILES", File, PARAMETER_TYPE_FilePath) )
551 {
552 pImport->Set_Manager(this);
553
554 if( pImport->Execute() )
555 {
556 pData = pImport->Get_Parameter("SHAPES")->asList()->Get_Item(0);
557 }
558 }
559
560 SG_Get_Tool_Library_Manager().Delete_Tool(pImport);
561
562 //-----------------------------------------------------
563 // LAZ Import
564
565 if( !pData && (SG_File_Cmp_Extension(File, "las") || SG_File_Cmp_Extension(File, "laz"))
566 && (pImport = SG_Get_Tool_Library_Manager().Create_Tool("io_pdal", 0)) != NULL
567 && pImport->Set_Parameter("FILES", File, PARAMETER_TYPE_FilePath) )
568 {
569 pImport->Set_Manager(this);
570
571 if( pImport->Execute() )
572 {
573 pData = pImport->Get_Parameter("POINTS")->asList()->Get_Item(0);
574 }
575 }
576
577 SG_Get_Tool_Library_Manager().Delete_Tool(pImport);
578
579 //-----------------------------------------------------
580 SG_UI_Msg_Lock(false);
581
582 return( pData );
583 }
584
585
586 ///////////////////////////////////////////////////////////
587 // //
588 ///////////////////////////////////////////////////////////
589
590 //---------------------------------------------------------
Add_Table(void)591 CSG_Table * CSG_Data_Manager::Add_Table(void)
592 {
593 CSG_Table *pObject = new CSG_Table();
594
595 if( pObject && !Add(pObject) )
596 {
597 delete(pObject); pObject = NULL;
598 }
599
600 return( pObject );
601 }
602
603 //---------------------------------------------------------
Add_TIN(void)604 CSG_TIN * CSG_Data_Manager::Add_TIN(void)
605 {
606 CSG_TIN *pObject = new CSG_TIN();
607
608 if( pObject && !Add(pObject) )
609 {
610 delete(pObject); pObject = NULL;
611 }
612
613 return( pObject );
614 }
615
616 //---------------------------------------------------------
Add_PointCloud(void)617 CSG_PointCloud * CSG_Data_Manager::Add_PointCloud(void)
618 {
619 CSG_PointCloud *pObject = new CSG_PointCloud();
620
621 if( pObject && !Add(pObject) )
622 {
623 delete(pObject); pObject = NULL;
624 }
625
626 return( pObject );
627 }
628
629 //---------------------------------------------------------
Add_Shapes(TSG_Shape_Type Type)630 CSG_Shapes * CSG_Data_Manager::Add_Shapes(TSG_Shape_Type Type)
631 {
632 CSG_Shapes *pObject = new CSG_Shapes(Type);
633
634 if( pObject && !Add(pObject) )
635 {
636 delete(pObject); pObject = NULL;
637 }
638
639 return( pObject );
640 }
641
642 //---------------------------------------------------------
Add_Grid(const CSG_Grid_System & System,TSG_Data_Type Type)643 CSG_Grid * CSG_Data_Manager::Add_Grid(const CSG_Grid_System &System, TSG_Data_Type Type)
644 {
645 CSG_Grid *pObject = System.is_Valid() ? new CSG_Grid(System, Type) : NULL;
646
647 if( pObject && !Add(pObject) )
648 {
649 delete(pObject); pObject = NULL;
650 }
651
652 return( pObject );
653 }
654
655 //---------------------------------------------------------
Add_Grid(int NX,int NY,double Cellsize,double xMin,double yMin,TSG_Data_Type Type)656 CSG_Grid * CSG_Data_Manager::Add_Grid(int NX, int NY, double Cellsize, double xMin, double yMin, TSG_Data_Type Type)
657 {
658 return( Add_Grid(CSG_Grid_System(Cellsize, xMin, yMin, NX, NY), Type) );
659 }
660
661
662 ///////////////////////////////////////////////////////////
663 // //
664 ///////////////////////////////////////////////////////////
665
666 //---------------------------------------------------------
Delete(CSG_Data_Collection * pCollection,bool bDetachOnly)667 bool CSG_Data_Manager::Delete(CSG_Data_Collection *pCollection, bool bDetachOnly)
668 {
669 if( pCollection == NULL || pCollection->m_pManager != this )
670 {
671 return( false );
672 }
673
674 //-----------------------------------------------------
675 if( pCollection == m_pTable ) { return( pCollection->Delete_All(bDetachOnly) ); }
676 if( pCollection == m_pTIN ) { return( pCollection->Delete_All(bDetachOnly) ); }
677 if( pCollection == m_pPoint_Cloud ) { return( pCollection->Delete_All(bDetachOnly) ); }
678 if( pCollection == m_pShapes ) { return( pCollection->Delete_All(bDetachOnly) ); }
679
680 //-----------------------------------------------------
681 if( pCollection->m_Type == SG_DATAOBJECT_TYPE_Grid )
682 {
683 CSG_Grid_Collection **pSystems = (CSG_Grid_Collection **)m_Grid_Systems.Get_Array();
684
685 size_t i, n;
686
687 for(i=0, n=0; i<m_Grid_Systems.Get_Size(); i++)
688 {
689 if( pCollection == pSystems[i] )
690 {
691 if( bDetachOnly )
692 {
693 pSystems[i]->Delete_All(bDetachOnly);
694 }
695
696 delete(pSystems[i]);
697 }
698 else
699 {
700 pSystems[n++] = pSystems[i];
701 }
702 }
703
704 if( n < m_Grid_Systems.Get_Size() )
705 {
706 m_Grid_Systems.Set_Array(n);
707
708 return( true );
709 }
710 }
711
712 //-----------------------------------------------------
713 return( false );
714 }
715
716 //---------------------------------------------------------
Delete(CSG_Data_Object * pObject,bool bDetachOnly)717 bool CSG_Data_Manager::Delete(CSG_Data_Object *pObject, bool bDetachOnly)
718 {
719 CSG_Data_Collection *pCollection = _Get_Collection(pObject);
720
721 if( pCollection && pCollection->Delete(pObject, bDetachOnly) )
722 {
723 if( pCollection->m_Type == SG_DATAOBJECT_TYPE_Grid && pCollection->Count() == 0 )
724 {
725 Delete(pCollection, bDetachOnly);
726 }
727
728 return( true );
729 }
730
731 return( false );
732 }
733
734 //---------------------------------------------------------
Delete(const CSG_Grid_System & System,bool bDetachOnly)735 bool CSG_Data_Manager::Delete(const CSG_Grid_System &System, bool bDetachOnly)
736 {
737 return( Delete(Get_Grid_System(System), bDetachOnly) );
738 }
739
740 //---------------------------------------------------------
Delete_All(bool bDetachOnly)741 bool CSG_Data_Manager::Delete_All(bool bDetachOnly)
742 {
743 m_pTable ->Delete_All(bDetachOnly);
744 m_pTIN ->Delete_All(bDetachOnly);
745 m_pPoint_Cloud->Delete_All(bDetachOnly);
746 m_pShapes ->Delete_All(bDetachOnly);
747
748 for(size_t i=0; i<Grid_System_Count(); i++)
749 {
750 CSG_Grid_Collection *pSystem = Get_Grid_System(i);
751
752 pSystem->Delete_All(bDetachOnly);
753
754 delete(pSystem);
755 }
756
757 m_Grid_Systems.Set_Array(0);
758
759 return( true );
760 }
761
762 //---------------------------------------------------------
Delete_Unsaved(bool bDetachOnly)763 bool CSG_Data_Manager::Delete_Unsaved(bool bDetachOnly)
764 {
765 m_pTable ->Delete_Unsaved(bDetachOnly);
766 m_pTIN ->Delete_Unsaved(bDetachOnly);
767 m_pPoint_Cloud->Delete_Unsaved(bDetachOnly);
768 m_pShapes ->Delete_Unsaved(bDetachOnly);
769
770 for(size_t i=Grid_System_Count(); i>0; i--)
771 {
772 CSG_Grid_Collection *pSystem = Get_Grid_System(i - 1);
773
774 pSystem->Delete_Unsaved(bDetachOnly);
775
776 if( pSystem->Count() == 0 )
777 {
778 Delete(pSystem);
779 }
780 }
781
782 return( true );
783 }
784
785
786 ///////////////////////////////////////////////////////////
787 // //
788 ///////////////////////////////////////////////////////////
789
790 //---------------------------------------------------------
Get_Summary(void) const791 CSG_String CSG_Data_Manager::Get_Summary(void) const
792 {
793 CSG_String s;
794
795 //-----------------------------------------------------
796 if( Get_Table()->Count() > 0 )
797 {
798 s += CSG_String::Format("___\n%s [%d %s]\n", _TL("Table"), Get_Table()->Count(), _TL("objects"));
799
800 for(size_t i=0; i<Get_Table()->Count(); i++)
801 {
802 CSG_Table *pObject = (CSG_Table *)Get_Table()->Get(i);
803
804 s += CSG_String::Format("- [%d %s] %s\n",
805 pObject->Get_Count(), _TL("records"),
806 pObject->Get_Name()
807 );
808 }
809 }
810
811 //-----------------------------------------------------
812 if( Get_Shapes()->Count() > 0 )
813 {
814 s += CSG_String::Format("___\n%s [%d %s]\n", _TL("Shapes"), Get_Shapes()->Count(), _TL("objects"));
815
816 for(size_t i=0; i<Get_Shapes()->Count(); i++)
817 {
818 CSG_Shapes *pObject = (CSG_Shapes *)Get_Shapes()->Get(i);
819
820 s += CSG_String::Format("- [%s; %d %s] %s\n",
821 pObject->Get_Type() == SHAPE_TYPE_Point ? _TL("point" ) :
822 pObject->Get_Type() == SHAPE_TYPE_Points ? _TL("points" ) :
823 pObject->Get_Type() == SHAPE_TYPE_Line ? _TL("line" ) :
824 pObject->Get_Type() == SHAPE_TYPE_Polygon ? _TL("polygon") : _TL("unknown"),
825 pObject->Get_Count(), _TL("records"),
826 pObject->Get_Name()
827 );
828 }
829 }
830
831 //-----------------------------------------------------
832 if( Get_Point_Cloud()->Count() > 0 )
833 {
834 s += CSG_String::Format("___\n%s [%d %s]\n", _TL("Point Cloud"), Get_Point_Cloud()->Count(), _TL("objects"));
835
836 for(size_t i=0; i<Get_Point_Cloud()->Count(); i++)
837 {
838 CSG_PointCloud *pObject = (CSG_PointCloud *)Get_Point_Cloud()->Get(i);
839
840 s += CSG_String::Format("- [%d %s] %s\n",
841 pObject->Get_Count(), _TL("records"),
842 pObject->Get_Name()
843 );
844 }
845 }
846
847 //-----------------------------------------------------
848 // if( Get_TIN()->Count() > 0 )
849 // {
850 // s += CSG_String::Format("___\n%s [%d %s]\n", _TL("TIN"), Get_TIN()->Count(), _TL("objects"));
851 //
852 // for(size_t i=0; i<Get_TIN()->Count(); i++)
853 // {
854 // CSG_TIN *pObject = (CSG_TIN *)Get_TIN()->Get(i);
855 //
856 // s += CSG_String::Format("- [%d %s] %s\n",
857 // pObject->Get_Count(), _TL("nodes"),
858 // pObject->Get_Name()
859 // );
860 // }
861 // }
862
863 //-----------------------------------------------------
864 if( Grid_System_Count() > 0 )
865 {
866 sLong memory = 0;
867
868 for(size_t i=0; i<Grid_System_Count(); i++)
869 {
870 CSG_Grid_Collection *pSystem = Get_Grid_System(i);
871
872 s += CSG_String::Format("___\n%s [%s; %d %s]\n", _TL("Grid System"), pSystem->m_System.Get_Name(), pSystem->Count(), _TL("objects"));
873
874 for(size_t j=0; j<pSystem->Count(); j++)
875 {
876 if( pSystem->Get(j)->Get_ObjectType() == SG_DATAOBJECT_TYPE_Grid )
877 {
878 CSG_Grid *pObject = (CSG_Grid *)pSystem->Get(j);
879
880 s += CSG_String::Format("- [%s; %.1fmb] %s\n",
881 SG_Data_Type_Get_Name(pObject->Get_Type()).c_str(),
882 pObject->Get_Memory_Size() / (double)N_MEGABYTE_BYTES,
883 pObject->Get_Name()
884 );
885
886 memory += pObject->Get_Memory_Size();
887 }
888
889 if( pSystem->Get(j)->Get_ObjectType() == SG_DATAOBJECT_TYPE_Grids )
890 {
891 CSG_Grids *pObject = (CSG_Grids *)pSystem->Get(j);
892
893 s += CSG_String::Format("- [%s; %d %s; %.1fmb] %s\n",
894 SG_Data_Type_Get_Name(pObject->Get_Type()).c_str(),
895 pObject->Get_NZ(), _TL("grids"),
896 pObject->Get_Memory_Size() / (double)N_MEGABYTE_BYTES,
897 pObject->Get_Name()
898 );
899
900 memory += pObject->Get_Memory_Size();
901 }
902 }
903 }
904
905 s += CSG_String::Format("_\n%s: %.1fmb\n", _TL("Total memory in use by grids"), memory / (double)N_MEGABYTE_BYTES);
906 }
907
908 //-----------------------------------------------------
909 if( s.is_Empty() )
910 {
911 s += CSG_String::Format("%s - %s\n--- %s ---\n", _TL("Data Manager"), _TL("Summary"), _TL("no data"));
912 }
913 else
914 {
915 s.Prepend(CSG_String::Format("%s - %s\n", _TL("Data Manager"), _TL("Summary")));
916 }
917
918 //-----------------------------------------------------
919 return( s );
920 }
921
922
923 ///////////////////////////////////////////////////////////
924 // //
925 // //
926 // //
927 ///////////////////////////////////////////////////////////
928
929 //---------------------------------------------------------
930