1. Summary
  2. Files
  3. Support
  4. Report Spam
  5. Create account
  6. Log in

root/trunk/saga-gis/src/modules/shapes/shapes_tools/shapes_generate.cpp @ 1369

Revision 1369, 10.6 KB (checked in by reklov_w, 13 months ago)

new module "generate shapes" added to MLB Shapes-Tools: Allows to generate point, line or polygon shapes from a table with x and y coordinates and an identifier

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
Line 
1/**********************************************************
2 * Version $Id$
3 *********************************************************/
4
5///////////////////////////////////////////////////////////
6//                                                       //
7//                         SAGA                          //
8//                                                       //
9//      System for Automated Geoscientific Analyses      //
10//                                                       //
11//                    Module Library:                    //
12//                     shapes_tools                      //
13//                                                       //
14//-------------------------------------------------------//
15//                                                       //
16//                  shapes_generate.cpp                  //
17//                                                       //
18//                 Copyright (C) 2012 by                 //
19//                    Volker Wichmann                    //
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; version 2 of the License.   //
28//                                                       //
29// SAGA is distributed in the hope that it will be       //
30// useful, but WITHOUT ANY WARRANTY; without even the    //
31// implied warranty of MERCHANTABILITY or FITNESS FOR A  //
32// PARTICULAR PURPOSE. See the GNU General Public        //
33// License for more details.                             //
34//                                                       //
35// You should have received a copy of the GNU General    //
36// Public License along with this program; if not,       //
37// write to the Free Software Foundation, Inc.,          //
38// 59 Temple Place - Suite 330, Boston, MA 02111-1307,   //
39// USA.                                                  //
40//                                                       //
41//-------------------------------------------------------//
42//                                                       //
43//    e-mail:     wichmann@laserdata                     //
44//                                                       //
45//    contact:    Volker Wichmann                        //
46//                LASERDATA GmbH                         //
47//                Management and analysis of             //
48//                laserscanning data                     //
49//                Innsbruck, Austria                     //
50//                                                       //
51///////////////////////////////////////////////////////////
52
53//---------------------------------------------------------
54
55
56///////////////////////////////////////////////////////////
57//                                                                                                               //
58//                                                                                                               //
59//                                                                                                               //
60///////////////////////////////////////////////////////////
61
62//---------------------------------------------------------
63#include "shapes_generate.h"
64
65
66///////////////////////////////////////////////////////////
67//                                                                                                               //
68//                              Construction/Destruction                                 //
69//                                                                                                               //
70///////////////////////////////////////////////////////////
71
72//---------------------------------------------------------
73CShapes_Generate::CShapes_Generate(void)
74{
75
76        Set_Name(_TL("Generate Shapes"));
77
78        Set_Author(_TL("Volker Wichmann (c) 2012, LASERDATA GmbH"));
79
80        Set_Description (_TW(
81                "The module allows to generate point, line or polygon shapes from "
82                "a table with x and y coordinates and an identifier. The table must "
83                "be sorted in vertex order.\n\n"
84                "The identifier has different meanings:\n\n"
85                "* Point Shapes: The identifier is arbitrary\n\n"
86                "* Line Shapes: The identifier is unique for each line\n\n"
87                "* Polygon Shapes: The identifier is unique for each polygon; "
88                "the first polygon vertex may but must not be duplicated in "
89                "order to close the polygon\n\n")
90        );
91
92
93        //-----------------------------------------------------
94        Parameters.Add_Table(
95                NULL    , "INPUT"               ,_TL("Input"),
96                _TL("Table with coordinates."),
97                PARAMETER_INPUT
98        );
99
100        Parameters.Add_Table_Field(
101                Parameters("INPUT"), "FIELD_ID", _TL("ID"),
102                _TL("Field with identifier.")
103        );
104        Parameters.Add_Table_Field(
105                Parameters("INPUT"), "FIELD_X", _TL("X"),
106                _TL("Field with x-coordinate.")
107        );
108        Parameters.Add_Table_Field(
109                Parameters("INPUT"), "FIELD_Y", _TL("Y"),
110                _TL("Field with y-coordinate.")
111        );
112        /*Parameters.Add_Table_Field(
113                Parameters("INPUT"), "FIELD_Z", _TL("Z"),
114                _TL("Field with z-coordinate."),
115                true
116        );*/
117
118        Parameters.Add_Choice(
119                NULL    , "SHAPE_TYPE"  , _TL("Shape Type"),
120                _TL("Shape type to generate."),
121                CSG_String::Format(SG_T("%s|%s|%s|"),
122                        _TL("Point(s)"),
123                        _TL("Line(s)"),
124                        _TL("Polygon(s)")
125                ), 0
126        );
127
128        Parameters.Add_Shapes(
129                NULL    , "OUTPUT"              , _TL("Output"),
130                _TL("Generated Shapefile."),
131                PARAMETER_OUTPUT
132        );
133}
134
135//---------------------------------------------------------
136CShapes_Generate::~CShapes_Generate(void)
137{}
138
139
140///////////////////////////////////////////////////////////
141//                                                                                                               //
142//                                                                                                               //
143//                                                                                                               //
144///////////////////////////////////////////////////////////
145
146//---------------------------------------------------------
147bool CShapes_Generate::On_Execute(void)
148{
149        CSG_Table               *pInput;
150        CSG_Shapes              *pOutput;
151        int                             iFieldId, iFieldX, iFieldY; // iFieldZ
152        int                             iShapeType;
153        CSG_String              sName;
154
155        pInput          = Parameters("INPUT")->asTable();
156        iFieldId        = Parameters("FIELD_ID")->asInt();
157        iFieldX         = Parameters("FIELD_X")->asInt();
158        iFieldY         = Parameters("FIELD_Y")->asInt();
159        //iFieldZ       = Parameters("FIELD_Z")->asInt();
160        iShapeType      = Parameters("SHAPE_TYPE")->asInt();
161        pOutput         = Parameters("OUTPUT")->asShapes();
162       
163       
164        if (pInput->Get_Count() < 1)
165        {
166                SG_UI_Msg_Add_Error(_TL("Input table is empty!"));
167                return (false);
168        }
169
170
171        //-----------------------------------------------------
172        // Initialize ...
173        sName = CSG_String::Format(SG_T("Shapes_%s"), pInput->Get_Name());
174
175        pOutput->Destroy();
176
177        switch (iShapeType)
178        {
179        default:
180        case 0:                         // point
181                pOutput->Create(SHAPE_TYPE_Point, sName, (CSG_Table*)0, SG_VERTEX_TYPE_XY);
182                break;
183        case 1:                         // line
184                pOutput->Create(SHAPE_TYPE_Line, sName, (CSG_Table*)0, SG_VERTEX_TYPE_XY);
185                break;
186        case 2:                         // polygon
187                pOutput->Create(SHAPE_TYPE_Polygon, sName, (CSG_Table*)0, SG_VERTEX_TYPE_XY);
188                break;
189        }
190
191        pOutput->Add_Field(SG_T("ID"), SG_DATATYPE_Int);
192
193       
194        //-----------------------------------------------------
195        // Generate ...
196
197        switch (iShapeType)
198        {
199        default:
200        case 0:                         // point
201                if (!Generate_Point_Shapes(pInput, pOutput, iFieldId, iFieldX, iFieldY))
202                        return (false);
203                break;
204        case 1:                         // line
205                if (!Generate_Line_Shapes(pInput, pOutput, iFieldId, iFieldX, iFieldY))
206                        return (false);
207                break;
208        case 2:                         // polygon
209                if (!Generate_Polygon_Shapes(pInput, pOutput, iFieldId, iFieldX, iFieldY))
210                        return (false);
211                break;
212        }
213
214
215        //-----------------------------------------------------
216        return (true);
217}
218
219
220//---------------------------------------------------------
221bool CShapes_Generate::Generate_Point_Shapes(CSG_Table *pInput, CSG_Shapes *pOutput, int iFieldId, int iFieldX, int iFieldY)
222{
223        for (int i=0; i<pInput->Get_Count() && Set_Progress(i, pInput->Get_Count()); i++)
224        {
225                CSG_Table_Record        *pRecord = pInput->Get_Record(i);
226                CSG_Shape                       *pShape  = pOutput->Add_Shape();
227               
228                pShape->Add_Point(pRecord->asDouble(iFieldX), pRecord->asDouble(iFieldY));
229                pShape->Set_Value(0, pRecord->asInt(iFieldId));
230        }
231
232
233        return (true);
234}
235
236
237//---------------------------------------------------------
238bool CShapes_Generate::Generate_Line_Shapes(CSG_Table *pInput, CSG_Shapes *pOutput, int iFieldId, int iFieldX, int iFieldY)
239{
240        int                     iVertices       = 0;
241        int                     iID                     = pInput->Get_Record(0)->asInt(iFieldId);
242        CSG_Shape       *pShape         = pOutput->Add_Shape();
243
244        for (int i=0; i<pInput->Get_Count() && Set_Progress(i, pInput->Get_Count()); i++)
245        {
246                CSG_Table_Record        *pRecord = pInput->Get_Record(i);
247
248                if (pRecord->asInt(iFieldId) != iID)
249                {
250                        if (iVertices < 2)
251                        {
252                                pOutput->Del_Record(pOutput->Get_Record_Count() - 1);
253                                SG_UI_Msg_Add_Error(_TL("Line with less than 2 vertices encountered!"));
254                                return (false);
255                        }
256
257                        pShape          = pOutput->Add_Shape();
258                        iID                     = pRecord->asInt(iFieldId);
259
260                        pShape->Set_Value(0, iID);
261
262                        iVertices       = 0;
263                }
264
265                pShape->Add_Point(pRecord->asDouble(iFieldX), pRecord->asDouble(iFieldY));
266               
267                iVertices       += 1;
268                iID                     = pRecord->asInt(iFieldId);
269        }
270
271        if (iVertices < 2)
272        {
273                pOutput->Del_Record(pOutput->Get_Record_Count() - 1);
274                SG_UI_Msg_Add_Error(_TL("Line with less than 2 vertices encountered!"));
275                return (false);
276        }
277
278
279        return (true);
280}
281
282
283//---------------------------------------------------------
284bool CShapes_Generate::Generate_Polygon_Shapes(CSG_Table *pInput, CSG_Shapes *pOutput, int iFieldId, int iFieldX, int iFieldY)
285{
286        int                     iVertices       = 0;
287        int                     iID                     = pInput->Get_Record(0)->asInt(iFieldId);
288        CSG_Shape       *pShape         = pOutput->Add_Shape();
289        TSG_Point       pointA;
290        pointA.x        = pInput->Get_Record(0)->asDouble(iFieldX);
291        pointA.y        = pInput->Get_Record(0)->asDouble(iFieldY);
292
293        for (int i=0; i<pInput->Get_Count() && Set_Progress(i, pInput->Get_Count()); i++)
294        {
295                CSG_Table_Record        *pRecord = pInput->Get_Record(i);
296
297                if (pRecord->asInt(iFieldId) != iID)
298                {
299                        if (iVertices < 3)
300                        {
301                                pOutput->Del_Record(pOutput->Get_Record_Count() - 1);
302                                SG_UI_Msg_Add_Error(_TL("Polygon with less than 3 vertices encountered!"));
303                                return (false);
304                        }
305
306                        TSG_Point       pointB;
307                        pointB.x        = pInput->Get_Record(i-1)->asDouble(iFieldX);
308                        pointB.y        = pInput->Get_Record(i-1)->asDouble(iFieldY);
309
310                        if (pointA.x != pointB.x || pointA.y != pointB.y)
311                                pShape->Add_Point(pointA);
312
313                        pShape          = pOutput->Add_Shape();
314                        iID                     = pRecord->asInt(iFieldId);
315
316                        pShape->Set_Value(0, iID);
317
318                        pointA.x        = pInput->Get_Record(i)->asDouble(iFieldX);
319                        pointA.y        = pInput->Get_Record(i)->asDouble(iFieldY);
320                        iVertices       = 0;
321                }
322
323                pShape->Add_Point(pRecord->asDouble(iFieldX), pRecord->asDouble(iFieldY));
324               
325                iVertices       += 1;
326                iID                     = pRecord->asInt(iFieldId);
327        }
328
329        if (iVertices < 3)
330        {
331                pOutput->Del_Record(pOutput->Get_Record_Count() - 1);
332                SG_UI_Msg_Add_Error(_TL("Polygon with less than 3 vertices encountered!"));
333                return (false);
334        }
335
336
337        return (true);
338}
339
340
341///////////////////////////////////////////////////////////
342//                                                                                                               //
343//                                                                                                               //
344//                                                                                                               //
345///////////////////////////////////////////////////////////
346
347//---------------------------------------------------------
Note: See TracBrowser for help on using the browser.