All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GridPlanarModel.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 GridPlanarModel.cpp
22 
23  \brief
24 
25  \ingroup layout
26 */
27 
28 // TerraLib
29 #include "GridPlanarModel.h"
30 #include "../core/ContextItem.h"
31 #include "../core/pattern/singleton/Context.h"
32 #include "../../geometry/Envelope.h"
33 #include "../../geometry/Coord2D.h"
34 #include "../core/property/Property.h"
35 #include "../core/property/PlanarGridSettingsConfigProperties.h"
36 #include "../core/enum/Enums.h"
37 
38 // STL
39 #include <string>
40 #include <sstream>
41 
44  m_unit(StyleKilometer)
45 {
46  m_type = Enums::getInstance().getEnumObjectType()->getGridPlanarItem();
47 
50 }
51 
53 {
54  if(m_settingsConfig)
55  {
56  delete m_settingsConfig;
57  m_settingsConfig = 0;
58  }
59 }
60 
62 {
63  if((!canvas) || (!utils))
64  return;
65 
66  if(!box.isValid())
67  return;
68 
69  calculateGaps(box);
70 
71  if(!m_visible)
72  return;
73 
74  m_srid = srid;
75 
76  if(srid <= 0)
77  return;
78 
79  te::color::RGBAColor color = te::color::RGBAColor(0, 0, 0, 255);
80  canvas->setLineColor(color);
81  canvas->setPolygonFillColor(color);
82  canvas->setPolygonContourColor(color);
83 
84  canvas->setTextPointSize(m_pointTextSize);
85  canvas->setFontFamily(m_fontText);
86 
87  gridTextFreeMemory();
88 
89  drawVerticalLines(canvas, utils, box);
90  drawHorizontalLines(canvas, utils, box);
91 }
92 
94 {
95  // Draw a horizontal line and the y coordinate change(vertical)
96 
97  double y1;
98  double yInit;
99 
100  WorldTransformer transf = utils->getTransformGeo(box, m_boxMapMM);
101  transf.setMirroring(false);
102 
103  yInit = m_initialGridPointY;
104  if(yInit < box.getLowerLeftY())
105  {
106  double dify = box.getLowerLeftY() - yInit;
107  int nParts = (int)(dify/m_lneVrtGap);
108  if(nParts == 0)
109  {
110  yInit = m_initialGridPointY;
111  }
112  else
113  {
114  yInit = yInit + (nParts * m_lneVrtGap);
115  }
116  }
117 
118  utils = Context::getInstance().getUtils();
119 
120  double wtxt = 0;
121  double htxt = 0;
122 
123  y1 = yInit;
124  for( ; y1 < box.getUpperRightY() ; y1 += m_lneVrtGap)
125  {
126  if(y1 < box.getLowerLeftY())
127  continue;
128 
129  double llx = 0;
130  double urx = 0;
131  double y = 0;
132  transf.system1Tosystem2(box.getLowerLeftX(), y1, llx, y);
133  transf.system1Tosystem2(box.getUpperRightX(), y1, urx, y);
134 
135  te::gm::Envelope newBox(llx, y, urx, y);
136 
137  te::gm::LinearRing* line = 0;
138  line = utils->createSimpleLine(newBox);
139  utils->drawLineW(line);
140 
141  std::ostringstream convert;
142  convert.precision(10);
143  double number = y1 / (double)m_unit;
144  convert << number;
145 
146  utils->textBoundingBox(wtxt, htxt, convert.str());
147 
148  if(m_visibleAllTexts)
149  {
150  if(m_leftText)
151  {
152  canvas->drawText(llx - m_lneHrzDisplacement - wtxt, y, convert.str(), 0);
153  te::gm::Point* coordLeft = new te::gm::Point(m_mapDisplacementX + llx - m_lneHrzDisplacement - wtxt, m_mapDisplacementY + y);
154  m_gridTexts[coordLeft] = convert.str();
155  }
156 
157  if(m_rightText)
158  {
159  canvas->drawText(urx + m_lneHrzDisplacement, y, convert.str(), 0);
160  te::gm::Point* coordRight = new te::gm::Point(m_mapDisplacementX + urx + m_lneHrzDisplacement, m_mapDisplacementY + y);
161  m_gridTexts[coordRight] = convert.str();
162  }
163  }
164 
165  if(line)
166  {
167  delete line;
168  line = 0;
169  }
170  }
171 }
172 
174 {
175  // Draw a vertical line and the x coordinate change(horizontal)
176 
177  double x1;
178  double xInit;
179 
180  WorldTransformer transf = utils->getTransformGeo(box, m_boxMapMM);
181  transf.setMirroring(false);
182 
183  xInit = m_initialGridPointX;
184  if(xInit < box.getLowerLeftX())
185  {
186  double difx = box.getLowerLeftX() - xInit;
187  int nParts = (int)(difx/m_lneHrzGap);
188  if(nParts == 0)
189  {
190  xInit = m_initialGridPointX;
191  }
192  else
193  {
194  xInit = xInit + (nParts * m_lneHrzGap);
195  }
196  }
197 
198  utils = Context::getInstance().getUtils();
199 
200  double wtxt = 0;
201  double htxt = 0;
202 
203  x1 = xInit;
204  for( ; x1 < box.getUpperRightX() ; x1 += m_lneHrzGap)
205  {
206  if(x1 < box.getLowerLeftX())
207  continue;
208 
209  double lly = 0;
210  double ury = 0;
211  double x = 0;
212  transf.system1Tosystem2(x1, box.getLowerLeftY(), x, lly);
213  transf.system1Tosystem2(x1, box.getUpperRightY(), x, ury);
214 
215  te::gm::Envelope newBox(x, lly, x, ury);
216 
217  if(lly > ury)
218  {
219  double ycopy = lly;
220  lly = ury;
221  ury = ycopy;
222  }
223 
224  te::gm::LinearRing* line = 0;
225  line = utils->createSimpleLine(newBox);
226  utils->drawLineW(line);
227 
228  std::ostringstream convert;
229  convert.precision(10);
230  double number = x1 / (double)m_unit;
231  convert << number;
232 
233  utils->textBoundingBox(wtxt, htxt, convert.str());
234 
235  if(m_visibleAllTexts)
236  {
237  if(m_bottomText)
238  {
239  canvas->drawText(x - (wtxt/2.), lly - m_lneVrtDisplacement - htxt, convert.str(), 0);
240  te::gm::Point* coordBottom = new te::gm::Point(m_mapDisplacementX + x - (wtxt/2.), m_mapDisplacementY + lly - m_lneVrtDisplacement - htxt);
241  m_gridTexts[coordBottom] = convert.str();
242  }
243 
244  if(m_topText)
245  {
246  canvas->drawText(x - (wtxt/2.), ury + m_lneVrtDisplacement, convert.str(), 0);
247  te::gm::Point* coordTop = new te::gm::Point(m_mapDisplacementX + x - (wtxt/2.), m_mapDisplacementY + ury + m_lneVrtDisplacement);
248  m_gridTexts[coordTop] = convert.str();
249  }
250  }
251 
252  if(line)
253  {
254  delete line;
255  line = 0;
256  }
257  }
258 }
259 
261 {
262  if(m_lneHrzGap <= 0)
263  {
264  m_lneHrzGap = m_mapScale * 0.05;
265  }
266 
267  if(m_lneVrtGap <= 0)
268  {
269  m_lneVrtGap = m_lneHrzGap;
270  }
271 
272  if(m_initialGridPointX <= 0)
273  {
274  m_initialGridPointX = box.getLowerLeftX();
275  }
276  if(m_initialGridPointY <= 0)
277  {
278  m_initialGridPointY = box.getLowerLeftY();
279  }
280 }
281 
282 
283 
void system1Tosystem2(double &wx, double &wy) const
It transforms the coordinate wx and wy from world coordinates to other(system) coordinates without us...
virtual void drawHorizontalLines(te::map::Canvas *canvas, Utils *utils, te::gm::Envelope box)
Class that represents a "Model" part of GridMap MVC component. Its coordinate system is the same of s...
Definition: GridMapModel.h:67
virtual void setFontFamily(const std::string &family)=0
It sets the text font family.
virtual void textBoundingBox(double &w, double &h, std::string txt)
A method that calculates the height and width of a text.
Definition: Utils.cpp:271
te::layout::WorldTransformer getTransformGeo(te::gm::Envelope boxgeo, te::gm::Envelope boxmm)
Returns a WorldTransformer object to transformations between geo coordinates and millimeter coordinat...
Definition: Utils.cpp:298
virtual void setPolygonFillColor(const te::color::RGBAColor &color)=0
It sets the color used to fill the draw of polygon geometries.
virtual void draw(te::map::Canvas *canvas, Utils *utils, te::gm::Envelope box, int srid)
const double & getUpperRightX() const
It returns a constant refernce to the x coordinate of the upper right corner.
Definition: Envelope.h:410
virtual void drawText(int x, int y, const std::string &txt, float angle=0.0, te::at::HorizontalAlignment hAlign=te::at::Start, te::at::VerticalAlignment vAlign=te::at::Baseline)=0
It draws a text.
const double & getLowerLeftY() const
It returns a constant refernce to the y coordinate of the lower left corner.
Definition: Envelope.h:400
const double & getUpperRightY() const
It returns a constant refernce to the x coordinate of the upper right corner.
Definition: Envelope.h:420
EnumType * m_type
type of the MVC component
virtual te::gm::LinearRing * createSimpleLine(te::gm::Envelope box)
Creates a LinearRing (line) pointer from a box in world coordinates (mm)
Definition: Utils.cpp:99
A LinearRing is a LineString that is both closed and simple.
Definition: LinearRing.h:53
static Enums & getInstance()
It returns a reference to the singleton instance.
A point with x and y coordinate values.
Definition: Point.h:50
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
Utils * getUtils()
Returns pointer with functions to manipulate the canvas and conversion between projections.
Definition: Context.cpp:153
virtual void drawLineW(te::gm::LinearRing *line)
Draw a line in world coordinates (mm).
Definition: Utils.cpp:87
virtual void drawVerticalLines(te::map::Canvas *canvas, Utils *utils, te::gm::Envelope box)
This class implements the logic for transforming from System 1 coordinate to other type of coordinate...
virtual void calculateGaps(te::gm::Envelope box)
A canvas is an abstraction of a drawing area.
Definition: Canvas.h:91
const double & getLowerLeftX() const
It returns a constant reference to the x coordinate of the lower left corner.
Definition: Envelope.h:390
virtual void setPolygonContourColor(const te::color::RGBAColor &color)=0
It sets the pen color used to draw the boundary of polygon geometries.
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
std::string convert(const path &v)
URI path to string.
Definition: path.cpp:219
virtual void setLineColor(const te::color::RGBAColor &color)=0
It sets the pen color used to draw line geometries.
Utility class with functions to manipulate the canvas and conversion between projections.
Definition: Utils.h:61
bool isValid() const
It tells if the rectangle is valid or not.
Definition: Envelope.h:438
virtual void setTextPointSize(double size)=0
It sets the text point Size.