1 /**********************************************************
2  * Version $Id$
3  *********************************************************/
4 
5 ///////////////////////////////////////////////////////////
6 //                                                       //
7 //                         SAGA                          //
8 //                                                       //
9 //      System for Automated Geoscientific Analyses      //
10 //                                                       //
11 //                     Tool Library                      //
12 //                     shapes_lines                      //
13 //                                                       //
14 //-------------------------------------------------------//
15 //                                                       //
16 //                Lines_From_Polygons.cpp                //
17 //                                                       //
18 //                 Copyright (C) 2005 by                 //
19 //                      Olaf Conrad                      //
20 //                                                       //
21 //-------------------------------------------------------//
22 //                                                       //
23 // This file is part of 'SAGA - System for Automated     //
24 // Geoscientific Analyses'. SAGA is free software; you   //
25 // can redistribute it and/or modify it under the terms  //
26 // of the GNU General Public License as published by the //
27 // Free Software Foundation, either version 2 of the     //
28 // License, or (at your option) any later version.       //
29 //                                                       //
30 // SAGA is distributed in the hope that it will be       //
31 // useful, but WITHOUT ANY WARRANTY; without even the    //
32 // implied warranty of MERCHANTABILITY or FITNESS FOR A  //
33 // PARTICULAR PURPOSE. See the GNU General Public        //
34 // License for more details.                             //
35 //                                                       //
36 // You should have received a copy of the GNU General    //
37 // Public License along with this program; if not, see   //
38 // <http://www.gnu.org/licenses/>.                       //
39 //                                                       //
40 //-------------------------------------------------------//
41 //                                                       //
42 //    e-mail:     oconrad@saga-gis.org                   //
43 //                                                       //
44 //    contact:    Olaf Conrad                            //
45 //                Institute of Geography                 //
46 //                University of Goettingen               //
47 //                Goldschmidtstr. 5                      //
48 //                37077 Goettingen                       //
49 //                Germany                                //
50 //                                                       //
51 ///////////////////////////////////////////////////////////
52 
53 //---------------------------------------------------------
54 
55 
56 ///////////////////////////////////////////////////////////
57 //														 //
58 //														 //
59 //														 //
60 ///////////////////////////////////////////////////////////
61 
62 //---------------------------------------------------------
63 #include "Lines_From_Polygons.h"
64 
65 
66 ///////////////////////////////////////////////////////////
67 //														 //
68 //														 //
69 //														 //
70 ///////////////////////////////////////////////////////////
71 
72 //---------------------------------------------------------
CLines_From_Polygons(void)73 CLines_From_Polygons::CLines_From_Polygons(void)
74 {
75 
76 	Set_Name		(_TL("Convert Polygons to Lines"));
77 
78 	Set_Author		(SG_T("O.Conrad (c) 2005"));
79 
80 	Set_Description	(_TW(
81 		"Convert polygons to lines."
82 	));
83 
84 	//-----------------------------------------------------
85 	Parameters.Add_Shapes(
86 		NULL	, "POLYGONS"	, _TL("Polygons"),
87 		_TL(""),
88 		PARAMETER_INPUT, SHAPE_TYPE_Polygon
89 	);
90 
91 	Parameters.Add_Shapes(
92 		NULL	, "LINES"		, _TL("Lines"),
93 		_TL(""),
94 		PARAMETER_OUTPUT, SHAPE_TYPE_Line
95 	);
96 }
97 
98 
99 ///////////////////////////////////////////////////////////
100 //														 //
101 //														 //
102 //														 //
103 ///////////////////////////////////////////////////////////
104 
105 //---------------------------------------------------------
On_Execute(void)106 bool CLines_From_Polygons::On_Execute(void)
107 {
108 	CSG_Shapes	*pLines, *pPolygons;
109 
110 	pPolygons	= Parameters("POLYGONS")	->asShapes();
111 	pLines		= Parameters("LINES")		->asShapes();
112 
113 	//-----------------------------------------------------
114 	if(	pPolygons->Get_Count() <= 0 )
115 	{
116 		Error_Set(_TL("no polygons in input"));
117 
118 		return( false );
119 	}
120 
121 	//-----------------------------------------------------
122 	pLines->Create(SHAPE_TYPE_Line, pPolygons->Get_Name(), pPolygons, pPolygons->Get_Vertex_Type());
123 
124 	for(int iPolygon=0; iPolygon<pPolygons->Get_Count(); iPolygon++)
125 	{
126 		CSG_Shape	*pPolygon	= pPolygons	->Get_Shape(iPolygon);
127 		CSG_Shape	*pLine		= pLines	->Add_Shape(pPolygon, SHAPE_COPY_ATTR);
128 
129 		for(int iPart=0; iPart<pPolygon->Get_Part_Count(); iPart++)
130 		{
131 			for(int iPoint=0; iPoint<pPolygon->Get_Point_Count(iPart); iPoint++)
132 			{
133 				pLine->Add_Point(pPolygon->Get_Point(iPoint, iPart), iPart);
134 
135 				if( pPolygons->Get_Vertex_Type() != SG_VERTEX_TYPE_XY )
136 				{
137 					pLine->Set_Z(pPolygon->Get_Z(iPoint, iPart), iPoint, iPart);
138 
139 					if( pPolygons->Get_Vertex_Type() == SG_VERTEX_TYPE_XYZM )
140 					{
141 						pLine->Set_M(pPolygon->Get_M(iPoint, iPart), iPoint, iPart);
142 					}
143 				}
144 			}
145 
146 			if( !CSG_Point(pPolygon->Get_Point(0, iPart)).is_Equal(pPolygon->Get_Point(pPolygon->Get_Point_Count(iPart) - 1, iPart)) )
147 			{
148 				pLine->Add_Point(pPolygon->Get_Point(0, iPart), iPart);
149 
150 				if( pPolygons->Get_Vertex_Type() != SG_VERTEX_TYPE_XY )
151 				{
152 					pLine->Set_Z(pPolygon->Get_Z(0, iPart), pLine->Get_Point_Count(iPart) - 1, iPart);
153 
154 					if( pPolygons->Get_Vertex_Type() == SG_VERTEX_TYPE_XYZM )
155 					{
156 						pLine->Set_M(pPolygon->Get_M(0, iPart), pLine->Get_Point_Count(iPart) - 1, iPart);
157 					}
158 				}
159 			}
160 		}
161 	}
162 
163 	return( true );
164 }
165 
166 
167 ///////////////////////////////////////////////////////////
168 //														 //
169 //														 //
170 //														 //
171 ///////////////////////////////////////////////////////////
172 
173 //---------------------------------------------------------
174