DisplayWindow.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 DisplayWindow.cpp
22 
23  \brief A simple main window to show example of TerraLib Graph.
24 */
25 
26 // Example
27 #include "DisplayWindow.h"
28 #include "GraphExamples.h"
29 
30 // TerraLib
31 #include <terralib/common.h>
32 #include <terralib/dataaccess.h>
33 #include <terralib/geometry.h>
38 #include <terralib/maptools.h>
39 #include <terralib/raster.h>
40 #include <terralib/se.h>
47 
48 // BOOST
49 #include <boost/algorithm/string.hpp>
50 #include <boost/filesystem.hpp>
51 #include <boost/lexical_cast.hpp>
52 #include <boost/uuid/random_generator.hpp>
53 #include <boost/uuid/uuid_io.hpp>
54 
55 // Qt
56 #include <QAction>
57 #include <QActionGroup>
58 #include <QStatusBar>
59 #include <QToolBar>
60 
61 // STL
62 #include <cassert>
63 
64 std::size_t DisplayWindow::ms_id = 0;
65 
66 DisplayWindow::DisplayWindow(QWidget* parent, Qt::WindowFlags f)
67  : QMainWindow(parent, f),
68  m_tool(0),
69  m_count(0),
70  m_pngPrefix("")
71 {
72  // Creates the tool bar
73  m_toolBar = addToolBar("Tools");
74 
75  // Creates the status bar
76  statusBar();
77 
78  // Setups the tool actions
79  setupActions();
80 
81  // Creates the Map Display
82  QSize size(600, 600);
83  m_display = new te::qt::widgets::MapDisplay(size, this);
85  setCentralWidget(m_display);
86 
87  // Install zoom wheel tool. It's always active!
88  m_display->installEventFilter(new te::qt::widgets::ZoomWheel(m_display));
89 
90  // Install coordinate tracking tool. It's always active!
92  connect(coordTracking, SIGNAL(coordTracked(QPointF&)), SLOT(onCoordTracked(QPointF&)));
93  m_display->installEventFilter(coordTracking);
94 
95  // Adjusting
96  setWindowTitle(tr("TerraLib Graph Example"));
97  setMinimumSize(60, 60);
98  resize(size);
99 }
100 
102 {
103 }
104 
106 {
107  // Pan
108  m_setPan = new QAction(tr("Pan"), this);
109  m_setPan->setCheckable(true);
110  connect(m_setPan, SIGNAL(triggered()), SLOT(onPanTriggered()));
111 
112  // Zoom Area
113  m_setZoomArea = new QAction(tr("Zoom Area"), this);
114  m_setZoomArea->setCheckable(true);
115  connect(m_setZoomArea, SIGNAL(triggered()), SLOT(onZoomAreaTriggered()));
116 
117  // Distance
118  m_png = new QAction(tr("PNG"), this);
119  m_png->setCheckable(false);
120  connect(m_png, SIGNAL(triggered()), SLOT(onPNGTriggered()));
121 
122  // Tools group
123  QActionGroup* toolsGroup = new QActionGroup(this);
124  toolsGroup->addAction(m_setPan);
125  toolsGroup->addAction(m_setZoomArea);
126 
127  // Add action on toolbar
128  m_toolBar->addAction(m_setPan);
129  m_toolBar->addAction(m_setZoomArea);
130  m_toolBar->addAction(m_png);
131 }
132 
134 {
135  //creates a layer
137 
138  layer->setGraph(graph);
140  layer->setStyle(s);
141 
142  // creates a layer renderer
144  layer->setRenderer(r);
145 
146  // Adding layer to layer list
147  m_layers.push_back(layer);
148 
149  // Updates MapDisplay layer list and extent
151  m_display->setSRID(layer->getSRID());
152  m_display->setExtent(extent);
153 }
154 
155 void DisplayWindow::addRasterLayer(std::string path, std::string name)
156 {
157  // std::string datasetName = path;
158  // // set input raster name
159  // std::map<std::string, std::string> rinfo;
160  // rinfo["URI"] = datasetName;
161 
162  // // open input raster
163  // std::unique_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("GDAL");
164  // ds->setConnectionInfo(rinfo);
165  // ds->open();
166 
167  // std::unique_ptr<te::da::DataSetType> dt = ds->getDataSetType(name);
168  // te::rst::RasterProperty* rstp = static_cast<te::rst::RasterProperty*>(dt->getProperties()[0]->clone());
169 
170  // te::da::DataSet* dataSet = tr->getDataSet(name);
171  // te::rst::Raster* raster = dataSet->getRaster();
172  // raster->getGrid()->setSRID(29193); //UTM SAD 69 zona 23
173  // //raster->getGrid()->setSRID(4326); //lat long wgs84
174 
175  // te::gm::Envelope* extent = raster->getExtent();
176 
177  //// Creates a Layer
178  // te::map::RasterLayer* layer = new te::map::RasterLayer(te::common::Convert2String(ms_id++), datasetName);
179  // layer->setDataSource(ds);
180  // layer->setDataSetName(name);
181  // layer->setVisibility(te::map::VISIBLE);
182  // layer->getRaster()->getGrid()->setSRID(29193); //UTM SAD 69 zona 23
183  // layer->setSRID(29193);
184  // //layer->getRaster()->getGrid()->setSRID(4326); //lat long wgs84
185  // //layer->setSRID(4326);
186 
187  // // Creates a Layer Renderer
188  // te::map::RasterLayerRenderer* r = new te::map::RasterLayerRenderer();
189  // layer->setRenderer(r);
190 
191  // // Adding layer to layer list
192  // m_layers.push_back(layer);
193 
194 
195  // // Updates MapDisplay layer list and extent
196  // m_display->setLayerList(m_layers);
197  // m_display->setSRID(raster->getSRID());
198  // m_display->setExtent(*extent);
199 }
200 
201 void DisplayWindow::addVectorialLayer(std::string path, int srid)
202 {
203  // Creates and connects data source
204  std::string connInfo ("file://" + path);
205 
206  std::unique_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("OGR", connInfo);
207  ds->open();
208 
209  // let's include the new datasource in the managers
210  boost::uuids::basic_random_generator<boost::mt19937> gen;
211  boost::uuids::uuid u = gen();
212  std::string id = boost::uuids::to_string(u);
213 
215  dsInfo->setConnInfo(connInfo);
216  dsInfo->setTitle("DS");
217  dsInfo->setAccessDriver("OGR");
218  dsInfo->setType("OGR");
219  dsInfo->setDescription("");
220  dsInfo->setId(id);
221 
223 
224 
225  std::vector<std::string> datasets = ds->getDataSetNames();
226 
227  // Gets DataSet Type
228  std::string dsName = datasets[0];
229  std::unique_ptr<te::da::DataSetType> dt = ds->getDataSetType(dsName);
230 
231  // Default geometry property
232  std::unique_ptr<te::da::DataSet> dataSet = ds->getDataSet(dsName);
233 
234  std::size_t geomPos = te::da::GetFirstSpatialPropertyPos(dataSet.get());
235 
236  //te::gm::GeometryProperty* geomProperty = te::da::GetFirstGeomProperty(dt.get());
237 
238  std::unique_ptr<te::gm::Envelope> extent = dataSet->getExtent(geomPos);
239 
240  // Creates a Layer
241  te::map::DataSetLayer* layer = new te::map::DataSetLayer(boost::lexical_cast<std::string>(ms_id++), dsName);
242  layer->setDataSetName(dsName);
244  layer->setSRID(srid);
245  layer->setExtent(*extent.get());
246  layer->setDataSourceId(id);
247 
248  // Creates a hard-coded style
250  symbolizer->setFill(te::se::CreateFill("#FFE4E1", "1.0"));
251  symbolizer->setStroke(te::se::CreateStroke("#000000", "2", "1.0"));
252 
253  te::se::Rule* rule = new te::se::Rule;
254  rule->push_back(symbolizer);
255 
257  style->push_back(rule);
258 
259  layer->setStyle(style);
260  layer->setRendererType("ABSTRACT_LAYER_RENDERER");
261 
262  // Adding layer to layer list
263  m_layers.push_back(layer);
264 
265  // Updates MapDisplay layer list and extent
267  m_display->setSRID(srid);
268  m_display->setExtent(*extent.get());
269 }
270 
271 void DisplayWindow::setPNGPrefix(std::string prefix)
272 {
273  m_pngPrefix = prefix;
274 }
275 
277 {
278  delete m_tool;
279  m_tool = new te::qt::widgets::Pan(m_display, Qt::OpenHandCursor, Qt::ClosedHandCursor);
280  m_display->installEventFilter(m_tool);
281 }
282 
284 {
285  delete m_tool;
286  m_tool = new te::qt::widgets::ZoomArea(m_display, Qt::BlankCursor);
287  m_display->installEventFilter(m_tool);
288 }
289 
291 {
292  QString fileName = m_pngPrefix.c_str();
293  QString count;
294  count.setNum(m_count);
295  fileName.append(count);
296  fileName.append(".png");
297 
298  m_display->getDisplayPixmap()->save(fileName);
299 
300  ++m_count;
301 }
302 
303 void DisplayWindow::onCoordTracked(QPointF& coordinate)
304 {
305  QString text = QString::fromUtf8("Coordinates: (") + QString::number(coordinate.x()) + " , " + QString::number(coordinate.y()) + ")";
306  QStatusBar* sb = statusBar();
307  sb->showMessage(text);
308 }
std::list< te::map::AbstractLayerPtr > m_layers
void setGraph(te::graph::AbstractGraph *ds)
It sets the graph associated to the layer.
QAction * m_setPan
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
virtual void setDataSourceId(const std::string &id)
This class implements a concrete tool to geographic zoom operation using the mouse wheel...
The Style defines the styling that is to be applied to a geographic dataset (vector geometries or cov...
Definition: Style.h:65
A PolygonSymbolizer is used to draw a polygon (or other area-type geometries), including filling its ...
void MapDisplay()
It retrieves data from a data source, create a set of layer and show map display. ...
This class implements a concrete tool to geographic zoom in operation using a boundary rectangle...
Definition: ZoomArea.h:49
void setDataSetName(const std::string &name)
void setSRID(int srid)
It sets the Spatial Reference System ID associated to the Layer.
This file contains include headers for TerraLib Symbology Encoding module.
void push_back(const std::string &semanticTypeIdentifier)
Definition: Style.cpp:75
A Qt4 widget to control the display of a set of layers.
static te::dt::Date ds(2010, 01, 01)
void push_back(Symbolizer *s)
Definition: Rule.cpp:138
This class implements a concrete tool to geographic zoom in operation using a boundary rectangle...
Class used to define the graph metadata informations.
This class implements a concrete tool to measure operation (distance, area, and angle).
It renders the objects associated to a Layer.
Abstract class used to define the main functions of graph struct. All graph implementations must used...
void setRendererType(const std::string &t)
std::string getName()
It returns the graph name.
virtual te::graph::GraphMetadata * getMetadata()=0
Function used to access the graph metadata.
virtual void setLayerList(const std::list< te::map::AbstractLayerPtr > &layers)
It sets the layer list to be showed in the Map Display.
This class implements a concrete tool to geographic pan operation.
Definition: Pan.h:49
TEDATAACCESSEXPORT std::size_t GetFirstSpatialPropertyPos(const te::da::DataSet *dataset)
It returns the first dataset spatial property or NULL if none is found.
~DisplayWindow()
Destructor.
virtual void setResizePolicy(const ResizePolicy &policy)
Sets the resize policy to this map display.
static DataSourceInfoManager & getInstance()
It returns a reference to the singleton instance.
An Envelope defines a 2D rectangular region.
The FeatureTypeStyle defines the styling that is to be applied to a dataset that can be viewed as a f...
This class implements a concrete tool to geographic coordinate tracking on mouse move operation...
Abstract class used to define the main functions of graph struct. All graph implementations must used...
Definition: AbstractGraph.h:55
void addGraph(te::graph::AbstractGraph *graph, te::gm::Envelope extent, te::se::Style *s=0)
static te::dt::TimeDuration dt(20, 30, 50, 11)
virtual void setVisibility(Visibility v)
It sets the layer visibility.
te::qt::widgets::AbstractTool * m_tool
virtual void setExtent(const te::gm::Envelope &mbr)
It sets the Layer extent (or minimum bounding box).
void setRenderer(LayerRenderer *renderer)
It sets the renderer used to paint this layer.
This class implements a concrete tool to geographic coordinate tracking on mouse move operation...
Definition: CoordTracking.h:52
QAction * m_setZoomArea
This class implements a concrete tool to geographic pan operation.
QToolBar * m_toolBar
void onZoomAreaTriggered()
void addVectorialLayer(std::string path, int srid)
TESEEXPORT Stroke * CreateStroke(const std::string &color, const std::string &width)
Creates a stroke.
static std::size_t ms_id
It renders the objects associated to a Layer.
Definition: LayerRenderer.h:57
DisplayWindow(QWidget *parent=0, Qt::WindowFlags f=0)
Constructor.
A Rule is used to attach property/scale conditions to and group the individual symbols used for rende...
Definition: Rule.h:76
This class implements a concrete tool to geographic zoom operation using the mouse wheel...
Definition: ZoomWheel.h:49
void onCoordTracked(QPointF &coordinate)
void setStroke(Stroke *stroke)
A Stroke specifies the appearance of a linear geometry.
virtual void setStyle(te::se::Style *style)
It sets the Style associated to the layer.
A class that represents a data source component.
This file contains include headers for the TerraLib Common Runtime module.
void addRasterLayer(std::string path, std::string name)
virtual int getSRID() const
It returns the Spatial Reference System ID associated to the Layer.
A simple main window to show example of TerraLib Graph.
A layer with reference to a dataset.
Definition: DataSetLayer.h:47
void setFill(Fill *f)
A Fill specifies the pattern for filling an area geometry.
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.
These routines show how to use the Graph module.
This file contains include headers for the Data Access module of TerraLib.
virtual void setSRID(const int &srid, bool doRefresh=true)
It sets a new Spatial Reference System to be used by the Map Display.
This file contains include headers for the Map Tools module of TerraLib.
QAction * m_png
virtual void setExtent(te::gm::Envelope &e, bool doRefresh=true)
It sets the world visible area and refreshes the contents in the map display.
te::qt::widgets::MapDisplay * m_display
A Layer is a reference to a virtual dataset with/without geometric attributes.
std::string m_pngPrefix
void setPNGPrefix(std::string prefix)
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
TESEEXPORT Fill * CreateFill(const std::string &color, const std::string &opacity)
Creates a fill.
virtual QPixmap * getDisplayPixmap() const
It returns the map display pixmap.