1 /**********************************************************
2 * Version $Id$
3 *********************************************************/
4
5 ///////////////////////////////////////////////////////////
6 // //
7 // SAGA //
8 // //
9 // System for Automated Geoscientific Analyses //
10 // //
11 // Tool Library //
12 // shapes_points //
13 // //
14 //-------------------------------------------------------//
15 // //
16 // Clip_Points.cpp //
17 // //
18 // Copyright (C) 2008 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 #include "Clip_Points.h"
61
62
63 ///////////////////////////////////////////////////////////
64 // //
65 // //
66 // //
67 ///////////////////////////////////////////////////////////
68
69 //---------------------------------------------------------
CClip_Points(void)70 CClip_Points::CClip_Points(void)
71 {
72 Set_Name (_TL("Clip Points with Polygons"));
73
74 Set_Author (SG_T("(c) 2008 by O.Conrad"));
75
76 Set_Description (_TW(
77 ""
78 ));
79
80 //-----------------------------------------------------
81 Parameters.Add_Shapes(
82 NULL , "POINTS" , _TL("Points"),
83 _TL(""),
84 PARAMETER_INPUT, SHAPE_TYPE_Point
85 );
86
87 CSG_Parameter *pNode = Parameters.Add_Shapes(
88 NULL , "POLYGONS" , _TL("Polygons"),
89 _TL(""),
90 PARAMETER_INPUT, SHAPE_TYPE_Polygon
91 );
92
93 Parameters.Add_Table_Field(
94 pNode , "FIELD" , _TL("Add Attribute to Clipped Points"),
95 _TL(""),
96 true
97 );
98
99 Parameters.Add_Shapes_List(
100 NULL , "CLIPS" , _TL("Clipped Points"),
101 _TL(""),
102 PARAMETER_OUTPUT, SHAPE_TYPE_Point
103 );
104
105 Parameters.Add_Choice(
106 NULL , "METHOD" , _TL("Clipping Options"),
107 _TL(""),
108 CSG_String::Format(SG_T("%s|%s|"),
109 _TL("one layer for all points"),
110 _TL("separate layer for each polygon")
111 ), 0
112 );
113 }
114
115 //---------------------------------------------------------
~CClip_Points(void)116 CClip_Points::~CClip_Points(void)
117 {}
118
119
120 ///////////////////////////////////////////////////////////
121 // //
122 // //
123 // //
124 ///////////////////////////////////////////////////////////
125
126 //---------------------------------------------------------
On_Execute(void)127 bool CClip_Points::On_Execute(void)
128 {
129 int Method, iField;
130 CSG_Shapes *pPoints, *pPolygons, *pClip;
131 CSG_Parameter_Shapes_List *pClips;
132
133 //-----------------------------------------------------
134 pPoints = Parameters("POINTS") ->asShapes();
135 pPolygons = Parameters("POLYGONS") ->asShapes();
136 pClips = Parameters("CLIPS") ->asShapesList();
137 Method = Parameters("METHOD") ->asInt();
138 iField = Parameters("FIELD") ->asInt();
139
140 //-----------------------------------------------------
141 if( !pPoints->is_Valid() )
142 {
143 Message_Add(_TL("Invalid points layer."));
144
145 return( false );
146 }
147 else if( !pPolygons->is_Valid() )
148 {
149 Message_Add(_TL("Invalid polygon layer."));
150
151 return( false );
152 }
153
154 //-----------------------------------------------------
155 if( iField >= pPolygons->Get_Field_Count() )
156 {
157 iField = -1;
158 }
159
160 pClips->Del_Items();
161
162 if( Method == 0 )
163 {
164 pClip = SG_Create_Shapes(SHAPE_TYPE_Point, CSG_String::Format(SG_T("%s [%s]"), pPoints->Get_Name(), pPolygons->Get_Name()), pPoints);
165
166 if( iField >= 0 )
167 {
168 pClip->Add_Field(pPolygons->Get_Field_Name(iField), pPolygons->Get_Field_Type(iField));
169 }
170 }
171
172 //-----------------------------------------------------
173 for(int iPolygon=0; iPolygon<pPolygons->Get_Count() && Set_Progress(iPolygon, pPolygons->Get_Count()); iPolygon++)
174 {
175 CSG_Shape_Polygon *pPolygon = (CSG_Shape_Polygon *)pPolygons->Get_Shape(iPolygon);
176
177 if( Method == 1 )
178 {
179 CSG_String Name(pPoints->Get_Name());
180
181 Name += iField >= 0
182 ? CSG_String::Format(SG_T(" [%s]"), pPolygon->asString(iField))
183 : CSG_String::Format(SG_T(" [%00d]"), 1 + pClips->Get_Item_Count());
184
185 pClip = SG_Create_Shapes(SHAPE_TYPE_Point, Name, pPoints);
186
187 if( iField >= 0 )
188 {
189 pClip->Add_Field(pPolygons->Get_Field_Name(iField), pPolygons->Get_Field_Type(iField));
190 }
191 }
192
193 for(int iPoint=0; iPoint<pPoints->Get_Count() && Process_Get_Okay(false); iPoint++)
194 {
195 CSG_Shape *pPoint = pPoints->Get_Shape(iPoint);
196
197 if( pPolygon->Contains(pPoint->Get_Point(0)) )
198 {
199 pPoint = pClip->Add_Shape(pPoint, SHAPE_COPY);
200
201 if( iField >= 0 )
202 {
203 pPoint->Set_Value(pPoints->Get_Field_Count(), pPolygon->asString(iField));
204 }
205 }
206 }
207
208 if( Method == 1 )
209 {
210 if( pClip->Get_Count() > 0 )
211 {
212 pClips->Add_Item(pClip);
213 }
214 else
215 {
216 delete(pClip);
217 }
218 }
219 }
220
221 //-----------------------------------------------------
222 if( Method == 0 )
223 {
224 if( pClip->Get_Count() > 0 )
225 {
226 pClips->Add_Item(pClip);
227 }
228 else
229 {
230 delete(pClip);
231 }
232 }
233
234 return( pClips->Get_Item_Count() > 0 );
235 }
236
237
238 ///////////////////////////////////////////////////////////
239 // //
240 // //
241 // //
242 ///////////////////////////////////////////////////////////
243
244 //---------------------------------------------------------
245