LayerRenderer.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 LayerRenderer.cpp
22 
23  \brief It renders the objects associated to a Layer.
24  */
25 
26 // TerraLib
27 #include "../../common/STLUtils.h"
28 #include "../../datatype/Enums.h"
29 #include "../../geometry/LineString.h"
30 #include "../../geometry/Point.h"
31 #include "../../geometry/Utils.h"
32 #include "../../maptools/AbstractLayer.h"
33 #include "../../maptools/Canvas.h"
34 #include "../../maptools/CanvasConfigurer.h"
35 #include "../../maptools/MarkRendererManager.h"
36 #include "../../se/Mark.h"
37 #include "../../se/Style.h"
38 #include "../../se/Rule.h"
39 #include "../../se/Utils.h"
40 #include "../core/AbstractGraph.h"
41 #include "../core/Edge.h"
42 #include "../core/GraphMetadata.h"
43 #include "../core/Vertex.h"
44 #include "../iterator/BoxIterator.h"
45 #include "../iterator/MemoryIterator.h"
46 #include "../iterator/SequenceIterator.h"
47 #include "Layer.h"
48 #include "LayerRenderer.h"
49 
51 
53 
55  te::map::Canvas* canvas,
56  const te::gm::Envelope& bbox, int srid,
57  const double& /*scale*/, bool* /*cancel*/)
58 {
59  // Is our business?
60  te::graph::Layer* l = dynamic_cast<Layer*>(layer);
61  assert(l);
62 
63  if(!bbox.isValid())
64  return;
65 
66  // The canvas configurer
67  te::map::CanvasConfigurer cc(canvas);
68 
69  // Gets the associated layer style
70  te::se::Style* style = l->getStyle();
71  if(style == nullptr)
72  {
73  configDefaultLine(canvas);
74  configDefaultPoint(canvas);
75  }
76  else
77  {
78  if(style->getRules().empty())
79  {
80  throw;
81  }
82  const te::se::Rule* rule = style->getRule(0);
83 
84  const std::vector<te::se::Symbolizer*> symbolizers = rule->getSymbolizers();
85 
86  for(size_t t= 0; t < symbolizers.size(); ++t)
87  {
88  te::se::Symbolizer* symb = symbolizers[t];
89 
90  cc.config(symb);
91  }
92  }
93 
95 
96  if(g->getMetadata()->getDataSource())
97  drawDataSourceGraph(g, canvas, bbox);
98  else
99  drawMemoryGraph(g, canvas, bbox, srid);
100 }
101 
103 {
104  int idx = -1;
105 
106  for(int i = 0; i < g->getVertexPropertySize(); ++i)
107  {
109  if(p && p->getType() == te::dt::GEOMETRY_TYPE)
110  {
111  idx = i;
112  break;
113  }
114  }
115 
116  return idx;
117 }
118 
120 {
121  int idx = -1;
122 
123  for(int i = 0; i < g->getEdgePropertySize(); ++i)
124  {
126  if(p && p->getType() == te::dt::GEOMETRY_TYPE)
127  {
128  idx = i;
129  break;
130  }
131  }
132 
133  return idx;
134 }
135 
137 {
143 }
144 
146 {
147  te::se::Mark* mark = te::se::CreateMark("circle", te::se::CreateStroke("#000000", "2"), te::se::CreateFill("#FFFF00", "1.0"));
148  std::size_t size = 12;
151  canvas->setPointPattern(rgba, static_cast<int>(size), static_cast<int>(size));
152 
153  te::common::Free(rgba, size);
154  delete mark;
155 }
156 
158 {
159  te::se::Mark* mark = te::se::CreateMark("star", te::se::CreateStroke("#000000", "2"), te::se::CreateFill("#FFFF00", "1.0"));
160  std::size_t size = 12;
163  canvas->setPointPattern(rgba, static_cast<int>(size), static_cast<int>(size));
164 
165  te::common::Free(rgba, size);
166  delete mark;
167 }
168 
170 {
171 //set iterator
172  te::gm::Envelope* box = new te::gm::Envelope(bbox);
173 
175 
176  te::graph::Edge* edge = it->getFirstEdge();
177 
178  int vertexGeomPropIdx = checkVertexGeometryProperty(g);
179  int edgeGeomPropIdx = checkEdgeGeometryProperty(g);
180 
181  if(vertexGeomPropIdx == -1 && edgeGeomPropIdx == -1)
182  return;
183 
184  while(it->isEdgeIteratorAfterEnd() == false)
185  {
186  if(edge)
187  {
188  //draw vertex from and vertex to
189  te::graph::Vertex* vFrom = g->getVertex(edge->getIdFrom());
190  te::graph::Vertex* vTo = g->getVertex(edge->getIdTo());
191 
192  if(vFrom && vTo)
193  {
194  te::gm::Geometry* geomVFrom = (te::gm::Geometry*)vFrom->getAttributes()[vertexGeomPropIdx];
195  te::gm::Geometry* geomVTo = (te::gm::Geometry*)vTo->getAttributes()[vertexGeomPropIdx];
196 
197  //check if exist a geometry associated a edge... if not draw a simple line
198  if(edgeGeomPropIdx != -1)
199  {
200  te::gm::Geometry* geomEdge = (te::gm::Geometry*)edge->getAttributes()[edgeGeomPropIdx];
201 
202  canvas->draw(geomEdge);
203 
204  if(vFrom->getId() == vTo->getId())
205  {
206  canvas->draw(geomVFrom);
207  }
208  else
209  {
210  canvas->draw(geomVFrom);
211  canvas->draw(geomVTo);
212  }
213  }
214  else
215  {
216  if(vFrom->getId() == vTo->getId())
217  {
218  canvas->draw(geomVFrom);
219  }
220  else
221  {
222  te::gm::Point* pFrom = dynamic_cast<te::gm::Point*>(geomVFrom);
223  te::gm::Point* pTo = dynamic_cast<te::gm::Point*>(geomVTo);
224 
225  if(pFrom && pTo)
226  {
228  line->setPoint(0, pFrom->getX(), pFrom->getY());
229  line->setPoint(1, pTo->getX(), pTo->getY());
230 
231  canvas->draw(line);
232 
233  delete line;
234 
235  }
236 
237  canvas->draw(geomVFrom);
238  canvas->draw(geomVTo);
239  }
240  }
241  }
242  }
243  edge = it->getNextEdge();
244  }
245 
246  delete it;
247 }
248 
250 {
251 //set iterator
253 
254  te::graph::Edge* edge = it->getFirstEdge();
255 
256  int vertexGeomPropIdx = checkVertexGeometryProperty(g);
257  int edgeGeomPropIdx = checkEdgeGeometryProperty(g);
258 
259  if(vertexGeomPropIdx == -1 && edgeGeomPropIdx == -1)
260  return;
261 
262  te::gm::Geometry* geomBox = te::gm::GetGeomFromEnvelope(&bbox, srid);
263 
264  while(it->isEdgeIteratorAfterEnd() == false)
265  {
266  if(edge)
267  {
268  //draw vertex from and vertex to
269  te::graph::Vertex* vFrom = g->getVertex(edge->getIdFrom());
270  te::graph::Vertex* vTo = g->getVertex(edge->getIdTo());
271 
272  if(vFrom && vTo)
273  {
274  te::gm::Geometry* geomVFrom = (te::gm::Geometry*)vFrom->getAttributes()[vertexGeomPropIdx];
275  te::gm::Geometry* geomVTo = (te::gm::Geometry*)vTo->getAttributes()[vertexGeomPropIdx];
276 
277  //if(geomBox->contains(geomVFrom) && geomBox->contains(geomVTo))
278  if(geomVFrom->within(geomBox) && geomVTo->within(geomBox))
279  {
280  //check if exist a geometry associated a edge... if not draw a simple line
281  if(edgeGeomPropIdx != -1)
282  {
283  te::gm::Geometry* geomEdge = (te::gm::Geometry*)edge->getAttributes()[edgeGeomPropIdx];
284 
285  canvas->draw(geomEdge);
286 
287  if(vFrom->getId() == vTo->getId())
288  {
289  canvas->draw(geomVFrom);
290  }
291  else
292  {
293  canvas->draw(geomVFrom);
294  canvas->draw(geomVTo);
295  }
296  }
297  else
298  {
299  if(vFrom->getId() == vTo->getId())
300  {
301  canvas->draw(geomVFrom);
302  }
303  else
304  {
305  te::gm::Point* pFrom = dynamic_cast<te::gm::Point*>(geomVFrom);
306  te::gm::Point* pTo = dynamic_cast<te::gm::Point*>(geomVTo);
307 
308  if(pFrom && pTo)
309  {
311  line->setPoint(0, pFrom->getX(), pFrom->getY());
312  line->setPoint(1, pTo->getX(), pTo->getY());
313 
314  canvas->draw(line);
315 
316  delete line;
317 
318  }
319 
320  canvas->draw(geomVFrom);
321  canvas->draw(geomVTo);
322  }
323  }
324  }
325  }
326  }
327 
328  edge = it->getNextEdge();
329  }
330 
331  delete geomBox;
332 
333  delete it;
334 }
virtual te::graph::Edge * getNextEdge()
It returns a pointer to the next edge element of a graph.
virtual void setLineCapStyle(LineCapStyle style)=0
It sets the line cap style.
te::da::DataSource * getDataSource()
It returns the data source associated with this graph.
void drawMemoryGraph(te::graph::AbstractGraph *g, te::map::Canvas *canvas, const te::gm::Envelope &bbox, int srid)
The Style defines the styling that is to be applied to a geographic dataset (vector geometries or cov...
Definition: Style.h:65
A Mark specifies a geometric shape and applies coloring to it.
Definition: Mark.h:84
This is the base class for layers.
Definition: AbstractLayer.h:77
#define TE_SE_DEFAULT_STROKE_BASIC_WIDTH
It specifies the default width used by stroke basic (solid colors).
void configDefaultLine(te::map::Canvas *canvas)
Configs the canvas with default values for line styles.
te::graph::AbstractGraph * getGraph() const
It returns the graph associated to the layer.
void configLoopPoint(te::map::Canvas *canvas)
Configs the canvas with default values for point styles.
A Symbolizer describes how a feature is to appear on a map.
Definition: Symbolizer.h:80
virtual void setPointPattern(te::color::RGBAColor **pattern, int ncols, int nrows)=0
It sets the point pattern.
virtual void setLineWidth(int w)=0
It sets the line width.
#define TE_OPAQUE
For an RGBA color this is the value of the alpha-channel for totally opaque.
virtual te::graph::Edge * getNextEdge()
It returns a pointer to the next edge element of a graph.
std::vector< te::dt::AbstractData * > & getAttributes()
It returns the vector of attributes associated with this element.
Definition: Vertex.cpp:74
int checkVertexGeometryProperty(te::graph::AbstractGraph *g)
Verify if the graph layer has a geometry to define the vertexs elements.
virtual te::dt::Property * getVertexProperty(int idx)=0
Get a vertex property given a index.
It models a property definition.
Definition: Property.h:59
From the point of view of graph theory, vertices are treated as featureless and indivisible objects...
Definition: Vertex.h:68
virtual void setLineJoinStyle(LineJoinStyle style)=0
It sets the line join style.
It renders the objects associated to a Layer.
unsigned int line
Rule * getRule(std::size_t i) const
Definition: Style.cpp:105
const std::vector< Rule * > & getRules() const
Definition: Style.cpp:94
virtual te::graph::GraphMetadata * getMetadata()=0
Function used to access the graph metadata.
Class used to define the edge struct of a graph. Its compose with a identifier, the vertex origin and...
Definition: Edge.h:58
void Free(std::vector< T * > *v)
This function can be applied to a pointer to a vector of pointers.
Definition: STLUtils.h:131
LineString is a curve with linear interpolation between points.
Definition: LineString.h:62
const double & getY() const
It returns the Point y-coordinate value.
Definition: Point.h:152
static MarkRendererManager & getInstance()
It returns a reference to the singleton instance.
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.
This class defines a commun interface to represents a graph iterator class. The main diferency to ano...
Definition: BoxIterator.h:71
Abstract class used to define the main functions of graph struct. All graph implementations must used...
Definition: AbstractGraph.h:55
virtual bool isEdgeIteratorAfterEnd()
Used to check the iterator position.
virtual te::se::Style * getStyle() const
It returns the Style associated to the layer.
virtual bool within(const Geometry *const rhs) const _NOEXCEPT_OP(false)
It returns true if the geometry object is spatially within rhs geometry.
virtual void setLineDashStyle(LineDashStyle style)=0
It sets the line dash style.
void drawDataSourceGraph(te::graph::AbstractGraph *g, te::map::Canvas *canvas, const te::gm::Envelope &bbox)
te::gm::Polygon * p
~LayerRenderer()
Destructor.
int getIdFrom()
It returns the vertex origin identification.
Definition: Edge.cpp:71
TESEEXPORT Mark * CreateMark(const std::string &wellKnownName, Stroke *stroke, Fill *fill)
Creates a mark.
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
const std::vector< Symbolizer * > & getSymbolizers() const
Definition: Rule.cpp:158
virtual te::graph::Edge * getFirstEdge()
It returns a pointer to the first edge element of a graph.
virtual int getEdgePropertySize()=0
Used to verify the number of properties associated to edge elements.
TESEEXPORT Stroke * CreateStroke(const std::string &color, const std::string &width)
Creates a stroke.
virtual te::graph::Edge * getFirstEdge()
It returns a pointer to the first edge element of a graph.
virtual int getVertexPropertySize()=0
Used to verify the number of properties associated to vertex elements.
int getType() const
It returns the property data type.
Definition: Property.h:161
A canvas is an abstraction of a drawing area.
virtual void draw(te::map::AbstractLayer *layer, te::map::Canvas *canvas, const te::gm::Envelope &bbox, int srid, const double &scale, bool *cancel)
It draws the layer geographic objects in the given canvas using the informed SRS. ...
virtual void setPointColor(const te::color::RGBAColor &color)=0
It sets the point drawing color.
A Rule is used to attach property/scale conditions to and group the individual symbols used for rende...
Definition: Rule.h:76
virtual te::dt::Property * getEdgeProperty(int idx)=0
Get a edge property given a index.
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
#define TE_SE_DEFAULT_STROKE_BASIC_COLOR
It specifies the default color used by stroke basic (solid colors).
void configDefaultPoint(te::map::Canvas *canvas)
Configs the canvas with default values for point styles.
virtual void setLineColor(const te::color::RGBAColor &color)=0
It sets the pen color used to draw line geometries.
int getId()
It returns the vertex id.
Definition: Vertex.cpp:69
int checkEdgeGeometryProperty(te::graph::AbstractGraph *g)
Verify if the graph layer has a geometry to define the edges elements.
void config(const te::se::Symbolizer *symbolizer)
It configs the canvas based on given symbolizer.
#define TE_TRANSPARENT
For an RGBA color this is the value of the alpha-channel for totally transparent. ...
virtual te::graph::Vertex * getVertex(int id)=0
It returns the vertex element if it&#39;s exist.
virtual void draw(const te::gm::Geometry *geom)=0
It draws the geometry on canvas.
std::vector< te::dt::AbstractData * > & getAttributes()
It returns the vector of attributes associated with this element.
Definition: Edge.cpp:81
A Layer is a reference to a virtual dataset with/without geometric attributes.
const double & getX() const
It returns the Point x-coordinate value.
Definition: Point.h:138
bool isValid() const
It tells if the rectangle is valid or not.
TEGEOMEXPORT Geometry * GetGeomFromEnvelope(const Envelope *const e, int srid)
It creates a Geometry (a polygon) from the given envelope.
TESEEXPORT Fill * CreateFill(const std::string &color, const std::string &opacity)
Creates a fill.
int getIdTo()
It returns the vertex destiny identification.
Definition: Edge.cpp:76
virtual bool isEdgeIteratorAfterEnd()
Used to check the iterator position.
A Symbology Enconding visitor that configures a given canvas based on symbolizers elements...