TsCanvas.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file TsCanvas.cpp
22 
23  \brief Test suite for the Qt Canvas implementation.
24  */
25 
26 
27 // Unit-Test TerraLib
28 #include "TsCanvas.h"
29 
30 // TerraLib
31 #include <terralib/color.h>
32 #include <terralib/common.h>
34 #include <terralib/dataaccess.h>
35 #include <terralib/geometry.h>
36 #include <terralib/maptools.h>
37 #include <terralib/postgis.h>
38 #include <terralib/qt/widgets.h>
39 #include <terralib/annotationtext/Text.h>
40 #include <terralib/annotationtext/Element.h>
41 #include <terralib/annotationtext/Attributes.h>
42 
43 // Qt
44 #include <QtGui/QApplication>
45 #include <QtGui/QPrinter>
46 #include <QtGui/QPrintDialog>
47 
48 // STL
49 #include <ctime>
50 
52 
54 {
55 }
56 
58 {
59 }
60 
62 {
63 #ifdef TE_COMPILE_ALL
64  int argc = 0;
65  QApplication app(argc, 0);
66 
67 // create the canvas and adjust the world-device transformation parameters
68  te::qt::widgets::Canvas canvas(800, 600);
69 
70  canvas.setWindow(-180.0, -90.0, +180.0, +90.0);
71 
72 // adjust line pen
73  canvas.setLineColor(te::color::RGBAColor(255, 0, 0, 255));
74 
75 // draw a line
77  l.setPoint(0, -90.0, -45.0);
78  l.setPoint(1, +90.0, +45.0);
79 
80  canvas.draw(&l);
81 
82 // draw a polygon
84  shell->setPoint(0, -90.0, -45.0);
85  shell->setPoint(1, +90.0, -45.0);
86  shell->setPoint(2, 0.0, +45.0);
87  shell->setPoint(3, -90.0, -45.0);
88 
90  hole1->setPoint(0, -50.0, -30.0);
91  hole1->setPoint(1, -40.0, -30.0);
92  hole1->setPoint(2, -45.0, -10.0);
93  hole1->setPoint(3, -50.0, -30.0);
94 
96  hole2->setPoint(0, -30.0, -30.0);
97  hole2->setPoint(1, -20.0, -30.0);
98  hole2->setPoint(2, -25.0, -10.0);
99  hole2->setPoint(3, -30.0, -30.0);
100 
102  p.setRingN(0, shell);
103  p.setRingN(1, hole1);
104  p.setRingN(2, hole2);
105 
106  canvas.setPolygonContourColor(te::color::RGBAColor(0, 0, 255, 255));
107  canvas.setPolygonFillColor(te::color::RGBAColor(0, 255, 0, 100));
108 
109  canvas.draw(&p);
110 
111 
112 // save a PNG to disk
113  canvas.save("canvas.png", te::map::Canvas::PNG);
114 
115  return;
116 #endif
117 }
118 
120 {
121 //#ifdef TE_COMPILE_ALL
122  int argc = 0;
123  QApplication app(argc, 0);
124 
125 // connecting to a PostGIS database
126  std::map<std::string, std::string> connInfo;
127  connInfo["PG_HOST"] = "atlas.dpi.inpe.br" ; // or "localhost";
128  connInfo["PG_USER"] = "postgres";
129  //connInfo["PG_PASSWORD"] = "sitim110";
130  connInfo["PG_DB_NAME"] = "terralib4";
131  connInfo["PG_CONNECT_TIMEOUT"] = "4";
132 
133  te::da::DataSource* ds = 0;
134 
135  CPPUNIT_ASSERT_NO_THROW_MESSAGE("Could not instantiate the PostGIS data source!",
136  ds = te::da::DataSourceFactory::make("POSTGIS"));
137 
138  CPPUNIT_ASSERT_NO_THROW_MESSAGE("Could not open the PostGIS data source!",
139  ds->open(connInfo));
140 
141  te::da::DataSourceTransactor* transactor = ds->getTransactor();
142  te::da::DataSourceCatalogLoader* cl = transactor->getCatalogLoader();
143 
144  CPPUNIT_ASSERT_NO_THROW_MESSAGE("Could not load the PostGIS data source catalog!",
145  cl->loadCatalog(true));
146 
147 
148  clock_t begin = 0;
149  clock_t end = 0;
150 
151  te::da::DataSourceCatalog* catalog = ds->getCatalog();
152 
153  size_t nLayers = catalog->getNumberOfDataSets();
154 
155  for(size_t i = 0; i < nLayers; ++i)
156  {
157 
158 // get DataSetType information: geometry-column, feature-name, feature extent
160 
161  //if(!(ft->getName() == "public.goias_geologia" || dt->getName() == "public.br_uf_2010" || dt->getName() == "public.sp_setores_censitarios"))
162  // continue;
163 
164  if(dt->hasGeom() == false)
165  continue;
166 
167  //int geomCol = dt->getDefaultGeomPropertyPos();
168 
169  te::gm::GeometryProperty* gcol = static_cast<te::gm::GeometryProperty*>(dt->findFirstPropertyOfType(te::dt::GEOMETRY_TYPE));
170  int geomCol = dt->getPropertyPosition(gcol->getName());
171 
172  const te::gm::Envelope* extent = cl->getExtent(gcol);
173  if(extent == 0)
174  continue;
175 
176  te::qt::widgets::Canvas canvas(800, 600);
177 
178  //QPrinter* printer = new QPrinter(QPrinter::HighResolution);
179  //QPrintDialog pd(printer);
180  //if (pd.exec() != QDialog::Accepted)
181  // continue;
182  //canvas.setDevice(printer);
183 
185  ls->setPoint(0, extent->m_llx, extent->m_lly);
186  ls->setPoint(1, extent->m_urx, extent->m_lly);
187  ls->setPoint(2, extent->m_urx, extent->m_ury);
188  ls->setPoint(3, extent->m_llx, extent->m_ury);
189  ls->setPoint(4, extent->m_llx, extent->m_lly);
190 
191 // set canvas definitions
192  double wwi = extent->m_llx;
193  double ww = extent->m_urx - extent->m_llx;
194  double whi = extent->m_lly;
195  double wh = extent->m_ury - extent->m_lly;
196 
197  double llx = wwi;
198  double urx = wwi + ww;
199  double lly = whi;
200  double ury = whi + wh;
201  canvas.calcAspectRatio(llx, lly, urx, ury);
202  wwi = llx;
203  whi = lly;
204  ww = urx - llx;
205  wh = ury - lly;
206  canvas.setWindow(llx-ww/10., lly-wh/10., urx+ww/10, ury+wh/10.);
207 
208  if(gcol->getType() /*getGeometryType()*/ == te::gm::MultiPolygonType ||
210  {
211  canvas.setPolygonFillColor(te::color::RGBAColor(255, 255, 0, 255));
212  canvas.setPolygonContourColor(te::color::RGBAColor(0, 0, 0, 255));
213  }
214  else if(gcol->getGeometryType() == te::gm::MultiLineStringType)
215  {
216  canvas.setLineColor(te::color::RGBAColor(0, 255, 0, 255));
217  }
218  else
219  {
220  canvas.setPointColor(te::color::RGBAColor(255, 0, 0, 255));
221  }
222 
223 // retrieve the data from the database
224  begin = clock();
225 
226  te::da::DataSet* dataset = transactor->getDataSet(dt->getName());
227 
228  end = clock();
229 
230  std::cout << "Testing " << dt->getName() << std::endl;
231  std::cout << "retrieving time: " << end - begin << " miliseconds" << std::endl;
232 
233 // check if there is data
234  if(dataset->isEmpty())
235  {
236  delete dataset;
237  continue;
238  }
239 
240 // draw data
241  begin = clock();
242 
243  while(dataset->moveNext())
244  {
245  te::gm::Geometry* g = dataset->getGeometry(geomCol);
246  canvas.draw(g);
247  delete g;
248  }
249  canvas.setLineColor(te::color::RGBAColor(255, 0, 0, 255));
250  canvas.draw(ls);
251 
252  end = clock();
253 
254  std::cout << "drawing time: " << end - begin << " miliseconds" << std::endl << std::endl;
255 
256  double ix = extent->m_llx;
257  double iy = extent->m_lly;
258  double w = extent->m_urx - extent->m_llx;
259  double h = extent->m_ury - extent->m_lly;
260 
261  te::gm::Envelope* bb = new te::gm::Envelope(0, 0, ix+w/2., iy + h/2.);
262  te::gm::Point* pt = new te::gm::Point(0, 0, 0, bb);
263  te::at::Text text;
264  te::at::Element* textElement = new te::at::Element("Instituto Nacional de Pesquisas espaciais", pt);
265  te::at::Attributes* attr = new te::at::Attributes("Verdana", 12.);
266  textElement->setAttributes(attr);
267  text.add(textElement);
268 
269  bb->init(0, 0, ix+w/2., iy+h/2. - h/10);
270  te::gm::Point* pt2 = new te::gm::Point(0, 0, 0, bb);
271 
272  std::string value = "Multi line text\ncenter justification\nteste\ncom fonte\nVerdana\npointSize 12\nResolution = ";
273  value += te::common::Convert2String(canvas.getResolution()) + "dpi";
274  te::at::Element* textElement2 = new te::at::Element(value, pt2);
275  text.add(textElement2);
276 
277  bb->init(0, 0, ix + w/20., iy+h/2.-h/10);
278  te::gm::Point* pt3 = new te::gm::Point(0, 0, 0, bb);
279  value = "Multi line text\nLeft justification\nteste\ncom fonte\nArial\npointSize 10\nResolution = ";
280  value += te::common::Convert2String(canvas.getResolution()) + "dpi";
281  te::at::Element* textElement3 = new te::at::Element(value, pt3);
283  attr2->setTextColor("green");
284  textElement3->setAttributes(attr2);
285  text.add(textElement3);
286 
287  bb->init(0, 0, ix+w-w/20., iy+h/2.-h/10.);
288  te::gm::Point* pt4 = new te::gm::Point(0, 0, 0, bb);
289  value = "Multi line text\nRight justification\nteste\ncom fonte\nScript MT Bold\npointSize 10\nResolution = ";
290  value += te::common::Convert2String(canvas.getResolution()) + "dpi";
291  te::at::Element* textElement4 = new te::at::Element(value, pt4);
293  attr3->setTextColor("red");
294  textElement4->setAttributes(attr3);
295  text.add(textElement4);
296 
297  canvas.draw(&text);
298 
299  std::string fileName = dt->getName() + ".png";
300  canvas.save(fileName.c_str(), te::map::PNG);
301 
302  delete dataset;
303  }
304 
305  delete transactor;
306 
307  CPPUNIT_ASSERT_NO_THROW_MESSAGE("Could not close the PostGIS data source!",
308  ds->close());
309 
310  delete ds;
311 //#endif
312 }
313 
virtual std::unique_ptr< te::gm::Geometry > getGeometry(std::size_t i) const =0
Method for retrieving a geometric attribute value.
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
Geometric property.
void init(const double &llx, const double &lly, const double &urx, const double &ury)
It initializes (sets) the envelope bounds.
void tcPerformance()
Test Case: it will draw several layers in order to make a benchmark.
Definition: TsCanvas.cpp:119
virtual std::unique_ptr< DataSourceTransactor > getTransactor()=0
It returns the set of parameters used to set up the access channel to the underlying repository...
virtual void open()=0
It opens the data source and makes it ready for using.
boost::shared_ptr< DataSetType > DataSetTypePtr
Definition: DataSetType.h:653
void setTextColor(const std::string &color)
It sets the text color.
Definition: Attributes.cpp:189
CPPUNIT_TEST_SUITE_REGISTRATION(TsCanvas)
It represents the system catalog of a DataSource.
void setUp()
Definition: TsCanvas.cpp:53
double m_urx
Upper right corner x-coordinate.
void setPolygonFillColor(const te::color::RGBAColor &color)
It sets the color used to fill the draw of polygon geometries.
A Text may contain 1 or more Text Elements.
Definition: Text.h:51
std::size_t getNumberOfDataSets() const
It returns the number of datasets in the catalog.
void setPolygonContourColor(const te::color::RGBAColor &color)
It sets the pen color used to draw the boundary of polygon geometries.
static te::dt::Date ds(2010, 01, 01)
void tearDown()
Definition: TsCanvas.cpp:57
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
void draw(const te::gm::Geometry *geom)
It draws the geometry on canvas.
te::gm::Point * pt2
const DataSetTypePtr & getDataSetType(std::size_t i) const
It returns the i-th dataset schema.
virtual bool moveNext()=0
It moves the internal pointer to the next item of the collection.
A LinearRing is a LineString that is both closed and simple.
Definition: LinearRing.h:53
void setWindow(const double &llx, const double &lly, const double &urx, const double &ury)
It sets the world (or window) coordinates area (supposing a cartesian reference system).
double m_llx
Lower left corner x-coordinate.
virtual bool isEmpty() const =0
It returns true if the collection is empty.
LineString is a curve with linear interpolation between points.
Definition: LineString.h:62
A point with x and y coordinate values.
Definition: Point.h:50
void setPoint(std::size_t i, const double &x, const double &y)
It sets the value of the specified point.
An Envelope defines a 2D rectangular region.
void setPointColor(const te::color::RGBAColor &color)
It sets the point drawing color.
void calcAspectRatio(double &llx, double &lly, double &urx, double &ury, const te::map::AlignType hAlign=te::map::Center, const te::map::AlignType vAlign=te::map::Center)
It calculates the best aspect ratio for world (or window) coordinates area (supposing a cartesian ref...
void setLineColor(const te::color::RGBAColor &color)
It sets the pen color used to draw line geometries.
This file contains include headers for the TerraLib Qt widgets.
GeomType getGeometryType() const
It returns the geometry subtype allowed for the property.
static te::dt::TimeDuration dt(20, 30, 50, 11)
void setAttributes(Attributes *attribute)
it sets the text attributes.
te::gm::Polygon * p
A DataSourceTransactor can be viewed as a connection to the data source for reading/writing things in...
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
void tcTransformation()
Test Case: it will test if the transformation from/to device and world is ok.
Definition: TsCanvas.cpp:61
double m_lly
Lower left corner y-coordinate.
This file contains include headers for the TerraLib PostGIS driver.
This file contains include headers for the Color Model of TerraLib.
int getType() const
It returns the property data type.
Definition: Property.h:161
A dataset is the unit of information manipulated by the data access module of TerraLib.
Test suite for the Qt canvas widget.
Definition: TsCanvas.h:44
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
Test suite for the Qt Canvas implementation.
double m_ury
Upper right corner y-coordinate.
virtual void close()=0
It closes the data source and clears all the resources used by its internal communication channel...
te::gm::Point * pt3
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
Utility functions for dealing with strings.
This file contains include headers for the TerraLib Common Runtime module.
std::string Convert2String(boost::int16_t value)
It converts a short integer value to a string.
Definition: StringUtils.h:56
This file contains include headers for the Vector Geometry model of TerraLib.
void save(const char *fileName, te::map::ImageType t, int quality=75, int fg=0) const
It saves the canvas content to a file image in the specified format type.
This file contains include headers for the Data Access module of TerraLib.
This file contains include headers for the Map Tools module of TerraLib.
void add(Element *element)
It adds the element.
Definition: Text.cpp:80
void setRingN(std::size_t i, Curve *r)
It sets the informed position ring to the new one.
const std::string & getName() const
It returns the property name.
Definition: Property.h:127
int getResolution()
It returns the device resolution.