DrawPNG.cpp
Go to the documentation of this file.
1 #include "CanvasExamples.h"
2 
3 // TerraLib
4 #include "../../Config.h"
5 #include <terralib/geometry.h>
6 #include <terralib/dataaccess.h>
8 
9 // STL
10 #include <iostream>
11 #include <exception>
12 
13 // Qt
14 #include <QApplication>
15 
16 void DrawPNG()
17 {
18 // If you want to use any Qt resource you must initialize a QApplication in some place!!!
19  int argc = 0;
20  QApplication app(argc, 0);
21 
22  try
23  {
24  std::string data_dir = TERRALIB_DATA_DIR;
25 
26  std::string connInfo ("file://");
27  connInfo += data_dir + "/shape/munic_2001.shp";
28  std::unique_ptr<te::da::DataSource> dsOGR = te::da::DataSourceFactory::make("OGR", connInfo);
29 
30  dsOGR->open();
31 
32  std::unique_ptr<te::da::DataSourceTransactor> t = dsOGR->getTransactor();
33 
34  std::unique_ptr<te::da::DataSetType> dt = (dsOGR->getDataSetType("munic_2001"));
35  std::unique_ptr<te::da::DataSet> dataset = t->getDataSet("munic_2001");
36  std::size_t pos = te::da::GetFirstPropertyPos(dataset.get(), te::dt::GEOMETRY_TYPE);
37  std::unique_ptr<te::gm::Envelope> extent = dataset->getExtent(pos);
39 
40  if(extent.get() == 0)
41  throw("Extent not loaded!");
42 
43 
44  te::qt::widgets::Canvas canvas(800, 600);
45 
46  double llx = extent->m_llx;
47  double lly = extent->m_lly;
48  double urx = extent->m_urx;
49  double ury = extent->m_ury;
50 
51  canvas.calcAspectRatio(llx,lly,urx,ury);
52  canvas.setWindow(llx,lly,urx,ury);
53 
56  {
57  canvas.setPolygonFillColor(te::color::RGBAColor(255, 255, 0, 255));
58  canvas.setPolygonContourColor(te::color::RGBAColor(0, 0, 0, 255));
59  }
61  {
62  canvas.setLineColor(te::color::RGBAColor(0, 255, 0, 255));
63  }
64  else
65  {
66  canvas.setPointColor(te::color::RGBAColor(255, 0, 0, 255));
67  }
68 
69  std::string geomName = (te::da::GetFirstGeomProperty(dt.get()))->getName();
70 
71  while(dataset->moveNext())
72  {
73  std::unique_ptr<te::gm::Geometry> g = dataset->getGeometry(geomName);
74  canvas.draw(g.get());
75  }
76 
77  std::string fileName = dt->getName() + ".png";
78  canvas.save(fileName.c_str(), te::map::PNG);
79  }
80  catch(const std::exception& e)
81  {
82  std::cout << std::endl << "An exception has occurred in drawPNG example: " << e.what() << std::endl;
83  }
84  catch(...)
85  {
86  std::cout << std::endl << "An unexpected exception has occurred in drawPNG example!" << std::endl;
87  }
88 }
89 
A canvas built on top of Qt.
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
Geometric property.
void setPolygonFillColor(const te::color::RGBAColor &color)
It sets the color used to fill the draw of polygon geometries.
void setPolygonContourColor(const te::color::RGBAColor &color)
It sets the pen color used to draw the boundary of polygon geometries.
void draw(const te::gm::Geometry *geom)
It draws the geometry on canvas.
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).
Several examples declarations.
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.
GeomType getGeometryType() const
It returns the geometry subtype allowed for the property.
static te::dt::TimeDuration dt(20, 30, 50, 11)
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)
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.
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
This file contains include headers for the Data Access module of TerraLib.
void DrawPNG()
It retrieves data from a Shape-file and prints it to a png graphics file.
Definition: DrawPNG.cpp:16