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 //                  line_properties.cpp                  //
17 //                                                       //
18 //                 Copyright (C) 2009 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 Hamburg                  //
47 //                Germany                                //
48 //                                                       //
49 ///////////////////////////////////////////////////////////
50 
51 //---------------------------------------------------------
52 
53 
54 ///////////////////////////////////////////////////////////
55 //														 //
56 //														 //
57 //														 //
58 ///////////////////////////////////////////////////////////
59 
60 //---------------------------------------------------------
61 #include "line_properties.h"
62 
63 
64 ///////////////////////////////////////////////////////////
65 //														 //
66 //														 //
67 //														 //
68 ///////////////////////////////////////////////////////////
69 
70 //---------------------------------------------------------
CLine_Properties(void)71 CLine_Properties::CLine_Properties(void)
72 {
73 	//-----------------------------------------------------
74 	Set_Name		(_TL("Line Properties"));
75 
76 	Set_Author		(SG_T("O. Conrad (c) 2009"));
77 
78 	Set_Description	(_TW(
79 		"Line properties: length, number of vertices."
80 	));
81 
82 	//-----------------------------------------------------
83 	Parameters.Add_Shapes(
84 		NULL	, "LINES"		, _TL("Lines"),
85 		_TL(""),
86 		PARAMETER_INPUT, SHAPE_TYPE_Line
87 	);
88 
89 	Parameters.Add_Shapes(
90 		NULL	, "OUTPUT"		, _TL("Lines with Property Attributes"),
91 		_TL("If not set property attributes will be added to the orignal layer."),
92 		PARAMETER_OUTPUT_OPTIONAL, SHAPE_TYPE_Line
93 	);
94 
95 	Parameters.Add_Value(
96 		NULL	, "BPARTS"		, _TL("Number of Parts"),
97 		_TL(""),
98 		PARAMETER_TYPE_Bool, false
99 	);
100 
101 	Parameters.Add_Value(
102 		NULL	, "BPOINTS"		, _TL("Number of Vertices"),
103 		_TL(""),
104 		PARAMETER_TYPE_Bool, false
105 	);
106 
107 	Parameters.Add_Value(
108 		NULL	, "BLENGTH"		, _TL("Length"),
109 		_TL(""),
110 		PARAMETER_TYPE_Bool, true
111 	);
112 }
113 
114 
115 ///////////////////////////////////////////////////////////
116 //														 //
117 //														 //
118 //														 //
119 ///////////////////////////////////////////////////////////
120 
121 //---------------------------------------------------------
On_Execute(void)122 bool CLine_Properties::On_Execute(void)
123 {
124 	//-------------------------------------------------
125 	int	bParts	= Parameters("BPARTS")	->asBool() ? 0 : -1;
126 	int	bPoints	= Parameters("BPOINTS")	->asBool() ? 0 : -1;
127 	int	bLength	= Parameters("BLENGTH")	->asBool() ? 0 : -1;
128 
129 	if( bParts && bPoints && bLength )
130 	{
131 		Error_Set(_TL("no properties selected"));
132 
133 		return( false );
134 	}
135 
136 	//-------------------------------------------------
137 	CSG_Shapes	*pLines	= Parameters("LINES")->asShapes();
138 
139 	if(	!pLines->is_Valid() || pLines->Get_Count() <= 0 )
140 	{
141 		Error_Set(_TL("invalid lines layer"));
142 
143 		return( false );
144 	}
145 
146 	if( Parameters("OUTPUT")->asShapes() && Parameters("OUTPUT")->asShapes() != pLines )
147 	{
148 		pLines	= Parameters("OUTPUT")->asShapes();
149 		pLines->Create(*Parameters("LINES")->asShapes());
150 	}
151 
152 	//-------------------------------------------------
153 	if( !bParts )	{	bParts	= pLines->Get_Field_Count();	pLines->Add_Field(SG_T("NPARTS") , SG_DATATYPE_Int   );	}
154 	if( !bPoints )	{	bPoints	= pLines->Get_Field_Count();	pLines->Add_Field(SG_T("NPOINTS"), SG_DATATYPE_Int   );	}
155 	if( !bLength )	{	bLength	= pLines->Get_Field_Count();	pLines->Add_Field(SG_T("LENGTH") , SG_DATATYPE_Double);	}
156 
157 	//-------------------------------------------------
158 	for(int i=0; i<pLines->Get_Count() && Set_Progress(i, pLines->Get_Count()); i++)
159 	{
160 		CSG_Shape	*pLine	= pLines->Get_Shape(i);
161 
162 		if( bParts  >= 0 )	pLine->Set_Value(bParts , pLine->Get_Part_Count());
163 		if( bPoints >= 0 )	pLine->Set_Value(bPoints, pLine->Get_Point_Count());
164 		if( bLength >= 0 )	pLine->Set_Value(bLength, ((CSG_Shape_Line *)pLine)->Get_Length());
165 	}
166 
167 	//-------------------------------------------------
168 	if( pLines == Parameters("LINES")->asShapes() )
169 	{
170 		DataObject_Update(pLines);
171 	}
172 
173 	return( true );
174 }
175 
176 
177 ///////////////////////////////////////////////////////////
178 //														 //
179 //														 //
180 //														 //
181 ///////////////////////////////////////////////////////////
182 
183 //---------------------------------------------------------
184