File: createLayer.cpp

package info (click to toggle)
libterralib 4.3.0%2Bdfsg.1-2
  • links: PTS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 70,928 kB
  • sloc: cpp: 254,330; ansic: 82,910; makefile: 814; sh: 80; xml: 37
file content (299 lines) | stat: -rw-r--r-- 7,876 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/************************************************************************************
TerraLib - a library for developing GIS applications.
Copyright  2001-2004 INPE and Tecgraf/PUC-Rio.

This code is part of the TerraLib library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

You should have received a copy of the GNU Lesser General Public
License along with this library.

The authors reassure the license terms regarding the warranties.
They specifically disclaim any warranties, including, but not limited to,
the implied warranties of merchantability and fitness for a particular purpose.
The library provided hereunder is on an "as is" basis, and the authors have no
obligation to provide maintenance, support, updates, enhancements, or modifications.
In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct,
indirect, special, incidental, or consequential damages arising out of the use
of this library and its documentation.
*************************************************************************************/

/* 
	This file shows an example of how to create a layer in a MySQL TerraLib database,
	and insert some vectorial data in this new layer. 
	
	Author: Lubia Vinhas  
*/

#include <TeMySQL.h>


int main()
{
	// Datatabase server parameters
	string host = "localhost";
	string dbname = "DB320RC1";
	string user = "root";
	string password = "vinhas";

	// Opens a connection to a database accessible though ADO
	TeDatabase* db = new TeMySQL();
	if (!db->connect(host, user, password, dbname))
	{
		cout << "Error: " << db->errorMessage() << endl << endl;
		cout << "Press Enter\n";
		getchar();
		return 1;
	}

	cout << "Connection successful to the database \"" << dbname << "\" on MySQL server \"" << host << "\" !\n";

	// Creates a projection
	TeDatum mDatum = TeDatumFactory::make("SAD69");
	TeProjection* pUTM = new TeUtm(mDatum,0.0);

	// Create a new layer called "TesteLayer"
	string layerName = "TesteLayer";
	if (db->layerExist(layerName))
	{
		cout << "There is already a layer named \"TesteLayer\" in the TerraLib database!\n\n";
		db->close();
		cout << "Press Enter\n";
		getchar();
		return 1;
	}

	TeLayer* layer = new TeLayer(layerName, db, pUTM);
	if (layer->id() <= 0)      // layer wasnt created properly
	{
		cout << "Error: " << db->errorMessage() << endl;
		db->close();
		cout << "Press Enter\n";
		getchar();
		return 1;
	}

	// Add the geometry representation LINES and store it in a table called "TesteLayerLines"
	if (!layer->addGeometry(TeLINES, "TesteLayerLines"))
	{
		cout << "Error: " << db->errorMessage() << endl;
		db->close();
		cout << "Press Enter\n";
		getchar();
		return 1;
	}

	// Create a set of lines
	TeLine2D reta;
	reta.add(TeCoord2D(500,500));
	reta.add(TeCoord2D(600,500));
	reta.add(TeCoord2D(700,500));
	reta.objectId("reta");

	TeLine2D ele;
	ele.add(TeCoord2D(500,600));
	ele.add(TeCoord2D(600,600));
	ele.add(TeCoord2D(700,700));
	ele.add(TeCoord2D(800,600));
	ele.add(TeCoord2D(900,600));
	ele.objectId("ele");

	TeLineSet ls;
	ls.add(reta);
	ls.add(ele);

	// Add the line set to the layer
	if (!layer->addLines(ls))
	{
		cout << "Error: " << db->errorMessage() << endl;
		db->close();
		cout << "Press Enter\n";
		getchar();
		return 1;
	}
	else
		cout << "Lines inserted to the new layer!\n";

	// Creates a set of  polygons
	// A simple polygon
	TeLine2D line;
	line.add(TeCoord2D(900,900));
	line.add(TeCoord2D(900,1000));
	line.add(TeCoord2D(1000,1000));
	line.add(TeCoord2D(1000,900));
	line.add(TeCoord2D(900,900));

	TeLinearRing r1(line);
	TePolygon poly1;
	poly1.add(r1);
	poly1.objectId("spoli");

	// A polygon with a hole
	TeLine2D line2;
	line2.add(TeCoord2D(200,200));
	line2.add(TeCoord2D(200,400));
	line2.add(TeCoord2D(400,400));
	line2.add(TeCoord2D(400,200));
	line2.add(TeCoord2D(200,200));

	TeLinearRing r2(line2);

	TeLine2D line3;
	line3.add(TeCoord2D(250,250));
	line3.add(TeCoord2D(250,300));
	line3.add(TeCoord2D(300,300));
	line3.add(TeCoord2D(300,250));
	line3.add(TeCoord2D(250,250));

	TeLinearRing r3(line3);

	TePolygon poly2;
	poly2.add(r2);
	poly2.add(r3);
	poly2.objectId("cpoli");

	TePolygonSet ps;
	ps.add(poly1);
	ps.add(poly2);

	// Add the polygon set to the layer
	// As addGeometry wasnt called before, the table of polygons will have a default name
	if (!layer->addPolygons(ps))
	{
		cout << "Error: " << db->errorMessage() << endl;
		db->close();
		cout << "Press Enter\n";
		getchar();
		return 1;
	}
	else
		cout << "Polygons inserted in the new layer!\n";

	// Create a set of points
	TePoint p1(40,40);
	p1.objectId("ponto1");
	TePoint p2(65,65);
	p2.objectId("ponto2");

	TePointSet pos;
	pos.add(p1);
	pos.add(p2);

	// Add the point set to the layer
	// As addGeometry wasnt called before, the table of points will have a default name
	if (!layer->addPoints(pos))
	{
		cout << "Error: " << db->errorMessage() << endl;
		db->close();
		cout << "Press Enter\n";
		getchar();
		return 1;
	}
	else
		cout << "Points inserted in the new layer!\n";

	// Create an attribute table
	// Define a list of attributes
	TeAttributeList attList;
	TeAttribute at;
	at.rep_.type_ = TeSTRING;
	at.rep_.numChar_ = 16;
	at.rep_.name_ = "object_id";
	at.rep_.isPrimaryKey_ = true;
	attList.push_back(at);

	at.rep_.type_ = TeSTRING;       // a string attribute
	at.rep_.numChar_ = 255;
	at.rep_.name_ = "nome";
	at.rep_.isPrimaryKey_ = false;
	attList.push_back(at);  

	at.rep_.type_ = TeREAL; // a float attribute
	at.rep_.name_ = "val1";
	at.rep_.decimals_ = 3;
	at.rep_.isPrimaryKey_ = false;
	attList.push_back(at);  

	at.rep_.type_ = TeINT;          // an integer attribute
	at.rep_.name_ = "val2";
	at.rep_.isPrimaryKey_ = false;
	attList.push_back(at);  

	// Create an attribute table associated to the new layer, called "TesteLayerAttr"
	TeTable attTable("TesteLayerAttr", attList, "object_id", "object_id");		// creates a table in memory
	if (!layer->createAttributeTable(attTable))
	{
		cout << "Error: " << db->errorMessage() << endl << endl;
		db->close();
		cout << "Press Enter\n";
		getchar();
		return 1;
	}

	// Each row is related to one of the geometries by its objetc_id
	TeTableRow row;
	row.push_back("reta");
	row.push_back("An straight line");
	row.push_back("11.1");
	row.push_back("11");
	attTable.add(row);
	row.clear();

	row.push_back("ele");
	row.push_back("A mountain shaped line");
	row.push_back("22.2");
	row.push_back("22");
	attTable.add(row);
	row.clear();

	row.push_back("spoli");
	row.push_back("A simple polygon");
	row.push_back("33.3");
	row.push_back("33");
	attTable.add(row);

	row.clear();
	row.push_back("ponto1");
	row.push_back("A point");
	row.push_back("55.5");
	row.push_back("55");
	attTable.add(row);
	row.clear();

	row.push_back("ponto2");
	row.push_back("Another point");
	row.push_back("66.6");
	row.push_back("66");
	attTable.add(row);
	row.clear();

	row.push_back("cpoli");
	row.push_back("A polygon with hole");
	row.push_back("44.4");
	row.push_back("44");
	attTable.add(row);
	row.clear();

	// Saves the table in the database
	if (!layer->saveAttributeTable( attTable ))
	{
		cout << "Error: " << db->errorMessage() << endl << endl;
		db->close();
		cout << "Press Enter\n";
		getchar();
		return 1;
	}
	else
		cout << "Objects inserted in the new layer!\n\n";

	// Close database
	db->close();
	cout << "Press Enter\n";
	cout.flush();
	getchar();
	return 0;
}