SAGA API v9.10
Loading...
Searching...
No Matches
shapes.cpp
Go to the documentation of this file.
1
3// //
4// SAGA //
5// //
6// System for Automated Geoscientific Analyses //
7// //
8// Application Programming Interface //
9// //
10// Library: SAGA_API //
11// //
12//-------------------------------------------------------//
13// //
14// shapes.cpp //
15// //
16// Copyright (C) 2005 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 Goettingen //
44// Goldschmidtstr. 5 //
45// 37077 Goettingen //
46// Germany //
47// //
48// e-mail: oconrad@saga-gis.org //
49// //
51
52//---------------------------------------------------------
53#include "shapes.h"
54#include "pointcloud.h"
55#include "tool_library.h"
56
57
59// //
60// //
61// //
63
64//---------------------------------------------------------
66{
67 switch( Type )
68 {
69 case SHAPE_TYPE_Point : return( _TL("Point" ) );
70 case SHAPE_TYPE_Points : return( _TL("Points" ) );
71 case SHAPE_TYPE_Line : return( _TL("Line" ) );
72 case SHAPE_TYPE_Polygon: return( _TL("Polygon" ) );
73 default : return( _TL("Undefined") );
74 }
75}
76
77
79// //
80// //
81// //
83
84//---------------------------------------------------------
86{
87 return( new CSG_Shapes );
88}
89
90//---------------------------------------------------------
92{
93 switch( Shapes.Get_ObjectType() )
94 {
96 return( new CSG_Shapes(Shapes) );
97
99 return( SG_Create_PointCloud(*((CSG_PointCloud *)&Shapes)) );
100
101 default:
102 return( NULL );
103 }
104}
105
106//---------------------------------------------------------
107CSG_Shapes * SG_Create_Shapes(const char *File) { return( SG_Create_Shapes(CSG_String(File)) ); }
108CSG_Shapes * SG_Create_Shapes(const wchar_t *File) { return( SG_Create_Shapes(CSG_String(File)) ); }
110{
111 CSG_Shapes *pShapes = new CSG_Shapes();
112
113 if( pShapes->Create(File) )
114 {
115 return( pShapes );
116 }
117
118 delete(pShapes); return( NULL );
119}
120
121//---------------------------------------------------------
122CSG_Shapes * SG_Create_Shapes(TSG_Shape_Type Type, const SG_Char *Name, CSG_Table *pTemplate, TSG_Vertex_Type Vertex_Type)
123{
124 return( new CSG_Shapes(Type, Name, pTemplate, Vertex_Type) );
125}
126
127//---------------------------------------------------------
129{
130 if( pTemplate )
131 {
132 switch( pTemplate->Get_ObjectType() )
133 {
135 return( new CSG_Shapes(pTemplate->Get_Type(), pTemplate->Get_Name(), pTemplate, pTemplate->Get_Vertex_Type()) );
136
138 return( SG_Create_PointCloud((CSG_PointCloud *)pTemplate) );
139
140 default:
141 break;
142 }
143 }
144
145 return( new CSG_Shapes() );
146}
147
148
150// //
151// //
152// //
154
155//---------------------------------------------------------
157 : CSG_Table()
158{
160}
161
162//---------------------------------------------------------
164 : CSG_Table()
165{
166 _On_Construction(); Create(Shapes);
167}
168
169//---------------------------------------------------------
170CSG_Shapes::CSG_Shapes(const char *File) : CSG_Shapes(CSG_String(File)) {}
171CSG_Shapes::CSG_Shapes(const wchar_t *File) : CSG_Shapes(CSG_String(File)) {}
173 : CSG_Table()
174{
175 _On_Construction(); Create(File);
176}
177
178//---------------------------------------------------------
179CSG_Shapes::CSG_Shapes(TSG_Shape_Type Type, const SG_Char *Name, CSG_Table *pTemplate, TSG_Vertex_Type Vertex_Type)
180 : CSG_Table()
181{
182 _On_Construction(); Create(Type, Name, pTemplate, Vertex_Type);
183}
184
185
187// //
188// //
189// //
191
192//---------------------------------------------------------
202
203
205// //
207
208//---------------------------------------------------------
210{
211 return( Assign((CSG_Data_Object *)&Shapes) );
212}
213
214//---------------------------------------------------------
215bool CSG_Shapes::Create(const char *File) { return( Create(CSG_String(File)) ); }
216bool CSG_Shapes::Create(const wchar_t *File) { return( Create(CSG_String(File)) ); }
218{
219 Destroy();
220
221 SG_UI_Msg_Add(CSG_String::Format("%s %s: %s...", _TL("Loading"), _TL("shapes"), File.c_str()), true);
222
223 bool bResult = false;
224
225 //-----------------------------------------------------
226 if( File.BeforeFirst(':').Cmp("PGSQL") == 0 ) // database source
227 {
228 CSG_String s(File);
229
230 s = s.AfterFirst(':'); CSG_String Host (s.BeforeFirst(':'));
231 s = s.AfterFirst(':'); CSG_String Port (s.BeforeFirst(':'));
232 s = s.AfterFirst(':'); CSG_String DBase(s.BeforeFirst(':'));
233 s = s.AfterFirst(':'); CSG_String Table(s.BeforeFirst(':'));
234
235 CSG_Tool *pTool = SG_Get_Tool_Library_Manager().Create_Tool("db_pgsql", 20, true); // CPGIS_Shapes_Load
236
237 if( pTool != NULL )
238 {
240
241 CSG_String Connection(DBase + " [" + Host + ":" + Port + "]");
242
243 bResult = pTool->Set_Manager(NULL) && pTool->On_Before_Execution()
244 && pTool->Set_Parameter("CONNECTION", Connection)
245 && pTool->Set_Parameter("DB_TABLE" , Table )
246 && pTool->Set_Parameter("SHAPES" , this )
247 && pTool->Execute();
248
250
252 }
253 }
254 else
255 {
256 if( SG_File_Cmp_Extension(File, "shp") )
257 {
258 bResult = _Load_ESRI(File);
259 }
260
261 if( !bResult )
262 {
263 bResult = _Load_GDAL(File);
264 }
265 }
266
267 //-----------------------------------------------------
268 if( bResult )
269 {
270 Set_Modified(false);
272
275
276 return( true );
277 }
278
279 //-----------------------------------------------------
280 for(sLong iShape=Get_Count()-1; iShape>=0; iShape--) // be kind, keep at least those shapes that have been loaded successfully
281 {
282 if( !Get_Shape(iShape)->is_Valid() )
283 {
284 Del_Shape(iShape);
285 }
286 }
287
288 if( Get_Count() <= 0 )
289 {
290 Destroy();
291 }
292
293 //-----------------------------------------------------
295 SG_UI_Msg_Add(_TL("failed"), false, SG_UI_MSG_STYLE_FAILURE);
296
297 return( false );
298}
299
300//---------------------------------------------------------
301bool CSG_Shapes::Create(TSG_Shape_Type Type, const SG_Char *Name, CSG_Table *pTemplate, TSG_Vertex_Type Vertex_Type)
302{
303 Destroy();
304
305 CSG_Table::Create(pTemplate);
306
307 if( Name )
308 {
309 Set_Name(CSG_String(Name));
310 }
311
312 m_Type = Type; m_Vertex_Type = Vertex_Type;
313
314 return( true );
315}
316
317
319// //
320// //
321// //
323
324//---------------------------------------------------------
326{
327 Destroy();
328}
329
330//---------------------------------------------------------
332{
333 if( CSG_Table::Destroy() )
334 {
336
337 return( true );
338 }
339
340 return( false );
341}
342
343
345// //
346// //
347// //
349
350//---------------------------------------------------------
351bool CSG_Shapes::Assign(CSG_Data_Object *pObject, bool bProgress)
352{
353 if( pObject->asShapes(true) && CSG_Data_Object::Assign(pObject, bProgress) )
354 {
355 CSG_Shapes *pShapes = pObject->asShapes(true);
356
357 Create(pShapes->Get_Type(), pShapes->Get_Name(), pShapes, pShapes->Get_Vertex_Type());
358
360
361 for(sLong i=0; i<pShapes->Get_Count() && (!bProgress || SG_UI_Process_Set_Progress(i, pShapes->Get_Count())); i++)
362 {
363 Add_Shape(pShapes->Get_Shape(i));
364 }
365
366 return( true );
367 }
368
369 return( false );
370}
371
372
374// //
375// //
376// //
378
379//---------------------------------------------------------
381{
382 switch( m_Type )
383 {
384 case SHAPE_TYPE_Polygon : return( new CSG_Shape_Polygon (this, Index) );
385 case SHAPE_TYPE_Line : return( new CSG_Shape_Line (this, Index) );
386 case SHAPE_TYPE_Points : return( new CSG_Shape_Points (this, Index) );
387 case SHAPE_TYPE_Point :
388 switch( m_Vertex_Type )
389 {
390 case SG_VERTEX_TYPE_XY : return( new CSG_Shape_Point (this, Index) );
391 case SG_VERTEX_TYPE_XYZ : return( new CSG_Shape_Point_Z (this, Index) );
392 case SG_VERTEX_TYPE_XYZM: return( new CSG_Shape_Point_ZM(this, Index) );
393 default:
394 return( NULL );
395 }
396 default:
397 return( NULL );
398 }
399}
400
401//---------------------------------------------------------
403{
404 CSG_Shape *pShape = (CSG_Shape *)Add_Record();
405
406 if( pShape && pCopy && mCopy != SHAPE_NO_COPY )
407 {
408 pShape->Assign(pCopy, mCopy);
409 }
410
411 return( pShape );
412}
413
414//---------------------------------------------------------
416{
417 return( Del_Record(pShape->Get_Index()) );
418}
419
421{
422 return( Del_Record(Index) );
423}
424
425
427// //
428// //
429// //
431
432//---------------------------------------------------------
434{
435 if( Get_Count() > 0 )
436 {
437 CSG_Shape *pShape = Get_Shape(0);
438
439 m_Extent = pShape->Get_Extent();
440 m_ZMin = pShape->Get_ZMin();
441 m_ZMax = pShape->Get_ZMax();
442 m_MMin = pShape->Get_MMin();
443 m_MMax = pShape->Get_MMax();
444
445 for(sLong i=1; i<Get_Count(); i++)
446 {
447 pShape = Get_Shape(i);
448
449 m_Extent.Union(pShape->Get_Extent());
450
451 switch( m_Vertex_Type )
452 {
454 if( m_MMin > pShape->Get_MMin() ) m_MMin = pShape->Get_MMin();
455 if( m_MMax < pShape->Get_MMax() ) m_MMax = pShape->Get_MMax();
456
458 if( m_ZMin > pShape->Get_ZMin() ) m_ZMin = pShape->Get_ZMin();
459 if( m_ZMax < pShape->Get_ZMax() ) m_ZMax = pShape->Get_ZMax();
460 break;
461
462 default:
463 break;
464 }
465 }
466 }
467 else
468 {
469 m_Extent.Create(0., 0., 0., 0.);
470 }
471
472 return( CSG_Table::On_Update() );
473}
474
475
477// //
478// //
479// //
481
482//---------------------------------------------------------
483CSG_Shape * CSG_Shapes::Get_Shape(const CSG_Point &Point, double Epsilon)
484{
485 CSG_Rect r(Point.x - Epsilon, Point.y - Epsilon, Point.x + Epsilon, Point.y + Epsilon);
486
487 CSG_Shape *pNearest = NULL;
488
490 {
491 double dNearest = -1.;
492
493 for(sLong iShape=0; iShape<Get_Count(); iShape++)
494 {
495 CSG_Shape *pShape = Get_Shape(iShape);
496
497 if( pShape->Intersects(r) )
498 {
499 double d = pShape->Get_Distance(Point);
500
501 if( d == 0. )
502 {
503 return( pShape );
504 }
505 else if( d > 0. && d <= Epsilon && (pNearest == NULL || d < dNearest) )
506 {
507 dNearest = d;
508 pNearest = pShape;
509 }
510 }
511 }
512 }
513
514 return( pNearest );
515}
516
517
519// //
520// //
521// //
523
524//---------------------------------------------------------
526{
528 {
529 return( true );
530 }
531
532 for(sLong iShape=0; iShape<Get_Count() && SG_UI_Process_Set_Progress(iShape, Get_Count()); iShape++)
533 {
534 CSG_Shape_Polygon *pPolygon = (CSG_Shape_Polygon *)Get_Shape(iShape);
535
536 for(int iPart=0; iPart<pPolygon->Get_Part_Count(); iPart++)
537 {
538 // currently we have to disable this check for 3D shapefiles since the
539 // _Update_Area() method can not handle polygons with no horizontal extent
541 {
542 if( pPolygon->is_Lake(iPart) == pPolygon->is_Clockwise(iPart) )
543 {// ring direction: outer rings > clockwise, inner rings (lakes) > counterclockwise !
544 pPolygon->Revert_Points(iPart);
545 }
546 }
547
548 //---------------------------------------------
549 if( !CSG_Point(pPolygon->Get_Point(0, iPart)).is_Equal(pPolygon->Get_Point(pPolygon->Get_Point_Count(iPart) - 1, iPart)) )
550 { // last point == first point !
551 ((CSG_Shape *)pPolygon)->Add_Point(pPolygon->Get_Point(0, iPart), iPart);
552
554 {
555 pPolygon->Set_Z(pPolygon->Get_Z(0, iPart), pPolygon->Get_Point_Count(iPart) - 1, iPart);
556
558 {
559 pPolygon->Set_M(pPolygon->Get_M(0, iPart), pPolygon->Get_Point_Count(iPart) - 1, iPart);
560 }
561 }
562 }
563
564 //--------------------------------------------
565 // to do: ensure there is no self intersection !
566 }
567 }
568
569 return( true );
570}
571
572
574// //
575// //
576// //
578
579//---------------------------------------------------------
void SG_UI_Msg_Add(const char *Message, bool bNewLine, TSG_UI_MSG_STYLE Style)
bool SG_UI_Process_Set_Ready(void)
bool SG_UI_Process_Set_Progress(int Position, int Range)
void SG_UI_ProgressAndMsg_Lock(bool bOn)
@ SG_UI_MSG_STYLE_FAILURE
Definition api_core.h:1585
@ SG_UI_MSG_STYLE_SUCCESS
Definition api_core.h:1584
SAGA_API_DLL_EXPORT bool SG_File_Cmp_Extension(const CSG_String &File, const CSG_String &Extension)
signed long long sLong
Definition api_core.h:158
#define SG_Char
Definition api_core.h:536
#define _TL(s)
Definition api_core.h:1568
@ SG_FILE_ENCODING_UTF8
Definition api_core.h:552
const SG_Char * Get_Name(void) const
void Set_Name(const CSG_String &Name)
void Set_Update_Flag(bool bOn=true)
Definition dataobject.h:285
class CSG_Shapes * asShapes(bool bPolymorph=false) const
virtual bool Assign(CSG_Data_Object *pObject, bool bProgress=false)
CSG_Projection & Get_Projection(void)
virtual bool is_Equal(const CSG_Point &Point, double epsilon=0.) const
Definition geo_tools.h:175
bool Create(const CSG_Projection &Projection)
TSG_Intersection Intersects(const CSG_Rect &Rect) const
virtual void Set_M(double m, int iPoint=0, int iPart=0)
Definition shapes.h:535
virtual TSG_Point Get_Point(int iPoint=0) const
virtual bool Revert_Points(int iPart)
Definition shapes.h:549
virtual void Set_Z(double z, int iPoint=0, int iPart=0)
Definition shapes.h:530
virtual double Get_Z(int iPoint=0, int iPart=0, bool bAscending=true) const
Definition shapes.h:531
virtual int Get_Part_Count(void) const
Definition shapes.h:495
virtual int Get_Point_Count(void) const
Definition shapes.h:493
virtual double Get_M(int iPoint=0, int iPart=0, bool bAscending=true) const
Definition shapes.h:536
bool is_Lake(int iPart)
bool is_Clockwise(int iPart)
TSG_Intersection Intersects(CSG_Shape *pShape)
Definition shape.cpp:118
virtual double Get_MMax(void)
Definition shapes.h:210
virtual double Get_Distance(TSG_Point Point) const =0
virtual double Get_MMin(void)
Definition shapes.h:209
virtual double Get_ZMax(void)
Definition shapes.h:194
virtual double Get_ZMin(void)
Definition shapes.h:193
virtual bool Assign(CSG_Table_Record *pRecord)
Definition shape.cpp:195
virtual const CSG_Rect & Get_Extent(void)=0
virtual bool Assign(CSG_Data_Object *pObject, bool bProgress=false)
Definition shapes.cpp:351
bool Make_Clean(void)
Definition shapes.cpp:525
virtual bool is_Valid(void) const
Definition shapes.h:804
virtual CSG_Shape * Add_Shape(CSG_Table_Record *pCopy=NULL, TSG_ADD_Shape_Copy_Mode mCopy=SHAPE_COPY)
Definition shapes.cpp:402
double m_MMax
Definition shapes.h:841
virtual const CSG_Rect & Get_Extent(void)
Definition shapes.h:810
virtual TSG_Data_Object_Type Get_ObjectType(void) const
Returns the object type as defined by TSG_Data_Object_Type. Used for run time type checking.
Definition shapes.h:796
virtual CSG_Table_Record * _Get_New_Record(sLong Index)
Definition shapes.cpp:380
double Get_ZMax(void)
Definition shapes.h:813
bool Create(const CSG_Shapes &Shapes)
Definition shapes.cpp:209
CSG_Shapes(void)
Definition shapes.cpp:156
virtual CSG_Shape * Get_Shape(const CSG_Point &Point, double Epsilon=0.)
Definition shapes.cpp:483
virtual bool Del_Shape(sLong Index)
Definition shapes.cpp:420
double m_ZMax
Definition shapes.h:841
virtual void _On_Construction(void)
Definition shapes.cpp:193
TSG_Shape_Type m_Type
Definition shapes.h:843
virtual bool Destroy(void)
Definition shapes.cpp:331
double m_MMin
Definition shapes.h:841
TSG_Vertex_Type Get_Vertex_Type(void) const
Definition shapes.h:808
virtual TSG_Shape_Type Get_Type(void) const
Definition shapes.h:806
virtual ~CSG_Shapes(void)
Definition shapes.cpp:325
double m_ZMin
Definition shapes.h:841
TSG_Vertex_Type m_Vertex_Type
Definition shapes.h:845
friend class CSG_Shape
Definition shapes.h:773
virtual bool On_Update(void)
Definition shapes.cpp:433
double Get_MMax(void)
Definition shapes.h:815
CSG_String AfterFirst(char Character) const
int Cmp(const CSG_String &String) const
CSG_String BeforeFirst(char Character) const
static CSG_String Format(const char *Format,...)
const SG_Char * c_str(void) const
sLong Get_Index(void) const
Definition table.h:136
bool Create(void)
Definition table.cpp:153
sLong Get_Count(void) const
Definition table.h:400
virtual bool Destroy(void)
Definition table.cpp:314
CSG_Table(void)
Definition table.cpp:147
virtual bool On_Update(void)
Definition table.cpp:1332
friend class CSG_Table_Record
Definition table.h:286
virtual CSG_Table_Record * Add_Record(CSG_Table_Record *pCopy=NULL)
Definition table.cpp:823
int m_Encoding
Definition table.h:487
virtual bool Del_Record(sLong Index)
Definition table.cpp:897
CSG_Rect m_Extent
Definition table.h:493
virtual void Set_Modified(bool bModified=true)
Definition table.cpp:1141
virtual void _On_Construction(void)
Definition table.cpp:295
bool Delete_Tool(CSG_Tool *pTool) const
CSG_Tool * Create_Tool(const CSG_String &Library, int Index, bool bWithGUI=false, bool bWithCMD=true) const
bool Set_Manager(class CSG_Data_Manager *pManager)
Definition tool.cpp:570
bool Set_Parameter(const CSG_String &ID, CSG_Parameter *pValue)
Definition tool.cpp:1284
virtual bool On_Before_Execution(void)
Definition tool.h:229
bool Execute(bool bAddHistory=false)
Definition tool.cpp:258
@ SG_DATAOBJECT_TYPE_PointCloud
Definition dataobject.h:123
@ SG_DATAOBJECT_TYPE_Shapes
Definition dataobject.h:121
@ INTERSECTION_None
Definition geo_tools.h:102
CSG_PointCloud * SG_Create_PointCloud(void)
CSG_String SG_Get_ShapeType_Name(TSG_Shape_Type Type)
Definition shapes.cpp:65
CSG_Shapes * SG_Create_Shapes(void)
Definition shapes.cpp:85
TSG_Vertex_Type
Definition shapes.h:91
@ SG_VERTEX_TYPE_XYZM
Definition shapes.h:94
@ SG_VERTEX_TYPE_XY
Definition shapes.h:92
@ SG_VERTEX_TYPE_XYZ
Definition shapes.h:93
TSG_ADD_Shape_Copy_Mode
Definition shapes.h:124
@ SHAPE_NO_COPY
Definition shapes.h:125
TSG_Shape_Type
Definition shapes.h:100
@ SHAPE_TYPE_Polygon
Definition shapes.h:105
@ SHAPE_TYPE_Line
Definition shapes.h:104
@ SHAPE_TYPE_Points
Definition shapes.h:103
@ SHAPE_TYPE_Point
Definition shapes.h:102
@ SHAPE_TYPE_Undefined
Definition shapes.h:101
double x
Definition geo_tools.h:129
double y
Definition geo_tools.h:129
CSG_Tool_Library_Manager & SG_Get_Tool_Library_Manager(void)