All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GridGeodesicModel.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 GridGeodesicModel.cpp
22 
23  \brief
24 
25  \ingroup layout
26 */
27 
28 // TerraLib
29 #include "GridGeodesicModel.h"
30 #include "../core/ContextItem.h"
31 #include "../../geometry/Envelope.h"
32 #include "../../geometry/Coord2D.h"
33 #include "../core/property/Property.h"
34 #include "../core/property/GeodesicGridSettingsConfigProperties.h"
35 #include "../../srs/Config.h"
36 #include "../core/WorldTransformer.h"
37 #include "../core/enum/Enums.h"
38 
39 // STL
40 #include <string>
41 #include <sstream>
42 #include <cmath>
43 
46  m_pointTextSizeCorner(12),
47  m_degreesText(true),
48  m_minutesText(false),
49  m_secondsText(false),
50  m_lneCornerHrzDisplacement(10),
51  m_lneCornerVrtDisplacement(10),
52  m_lowerRightCornerText(false),
53  m_upperRightCornerText(false),
54  m_lowerLeftCornerText(false),
55  m_upperLeftCornerText(false),
56  m_visibleCornerTextsText(false),
57 
58  /* Geodesic: Topographic Map */
59 
60  m_defineScale(false),
61  m_clip(false),
62  m_scale(0.),
63  m_lneX1(0.),
64  m_lneX2(0.),
65  m_lneY1(0.),
66  m_lneY2(0.),
67  m_lneX3(0.),
68  m_lneX4(0.),
69  m_lneY3(0.),
70  m_lneY4(0.)
71 {
72  m_type = Enums::getInstance().getEnumObjectType()->getGridGeodesicItem();
73 
76 }
77 
79 {
80  if(m_settingsConfig)
81  {
82  delete m_settingsConfig;
83  m_settingsConfig = 0;
84  }
85 }
86 
88 {
89  if((!canvas) || (!utils))
90  return;
91 
92  if(!box.isValid())
93  return;
94 
95  m_srid = srid;
96 
97  calculateGaps(box);
98 
99  if(!m_visible)
100  return;
101 
102  if(srid <= 0)
103  return;
104 
105  te::color::RGBAColor color = te::color::RGBAColor(0, 0, 0, 255);
106  canvas->setLineColor(color);
107 
108  canvas->setTextPointSize(m_pointTextSize);
109  canvas->setFontFamily(m_fontText);
110 
111  gridTextFreeMemory();
112 
113  drawVerticalLines(canvas, utils, box);
114  drawHorizontalLines(canvas, utils, box);
115 }
116 
118 {
119  // Draw a horizontal line and the y coordinate change(vertical)
120 
121  double y1;
122  double yInit;
123 
124  WorldTransformer transf = utils->getTransformGeo(m_planarBox, m_boxMapMM);
125  transf.setMirroring(false);
126 
127  int zone = utils->calculatePlanarZone(box);
128 
129  yInit = m_initialGridPointY;
130  if(yInit < box.getLowerLeftY())
131  {
132  double dify = box.getLowerLeftY() - yInit;
133  int nParts = (int)(dify/m_lneVrtGap);
134  if(nParts == 0)
135  {
136  yInit = m_initialGridPointY;
137  }
138  else
139  {
140  yInit = yInit + (nParts * m_lneVrtGap);
141  }
142  }
143 
144  y1 = yInit;
145  for( ; y1 < box.getUpperRightY() ; y1 += m_lneVrtGap)
146  {
147  if(y1 < box.getLowerLeftY())
148  continue;
149 
150  te::gm::Envelope env(box.getLowerLeftX(), y1, box.getUpperRightX(), y1);
151 
152  te::gm::LinearRing* line = 0;
153  line = utils->addCoordsInX(env, y1, m_lneVrtGap);
154 
155  // Curvatura da linha: de latlong para planar;
156  // Desenhar linha: de planar para milimetro
157 
158  utils->remapToPlanar(line, zone);
159  utils->convertToMillimeter(transf, line);
160 
161  utils->drawLineW(line);
162 
163  std::string text = utils->convertDecimalToDegree(y1, m_degreesText, m_minutesText, m_secondsText);
164 
165  te::gm::Envelope* ev = const_cast<te::gm::Envelope*>(line->getMBR());
166 
167  if(m_visibleAllTexts)
168  {
169  if(m_leftText)
170  {
171  canvas->drawText(ev->getLowerLeftX() - m_lneHrzDisplacement, ev->getLowerLeftY(), text, 0);
172  te::gm::Point* coordLeft = new te::gm::Point(ev->getLowerLeftX() - m_lneHrzDisplacement, ev->getLowerLeftY());
173  m_gridTexts[coordLeft] = text;
174  }
175 
176  if(m_rightText)
177  {
178  canvas->drawText(ev->getUpperRightX() + m_lneHrzDisplacement, ev->getUpperRightY(), text, 0);
179  te::gm::Point* coordRight = new te::gm::Point(ev->getUpperRightX() + m_lneHrzDisplacement, ev->getUpperRightY());
180  m_gridTexts[coordRight] = text;
181  }
182  }
183 
184  if(line)
185  {
186  delete line;
187  line = 0;
188  }
189  }
190 }
191 
193 {
194  // Draw a vertical line and the x coordinate change(horizontal)
195 
196  double x1;
197  double xInit;
198 
199  WorldTransformer transf = utils->getTransformGeo(m_planarBox, m_boxMapMM);
200  transf.setMirroring(false);
201 
202  int zone = utils->calculatePlanarZone(box);
203 
204  xInit = m_initialGridPointX;
205  if(xInit < box.getLowerLeftX())
206  {
207  double difx = box.getLowerLeftX() - xInit;
208  int nParts = (int)(difx/m_lneHrzGap);
209  if(nParts == 0)
210  {
211  xInit = m_initialGridPointX;
212  }
213  else
214  {
215  xInit = xInit + (nParts * m_lneHrzGap);
216  }
217  }
218 
219  x1 = xInit;
220 
221  for( ; x1 < box.getUpperRightX() ; x1 += m_lneHrzGap)
222  {
223  if(x1 < box.getLowerLeftX())
224  continue;
225 
226  te::gm::Envelope env(x1, box.getLowerLeftY(), x1, box.getUpperRightY());
227  te::gm::LinearRing* line = 0;
228  line = utils->addCoordsInY(env, x1, m_lneHrzGap);
229 
230  // Curvatura da linha: de latlong para planar;
231  // Desenhar linha: de planar para milimetro
232  utils->remapToPlanar(line, zone);
233  utils->convertToMillimeter(transf, line);
234 
235  utils->drawLineW(line);
236 
237  std::string text = utils->convertDecimalToDegree(x1, m_degreesText, m_minutesText, m_secondsText);
238 
239  te::gm::Envelope* ev = const_cast<te::gm::Envelope*>(line->getMBR());
240 
241  if(m_visibleAllTexts)
242  {
243  if(m_bottomText)
244  {
245  canvas->drawText(ev->getLowerLeftX(), ev->getLowerLeftX() - m_lneVrtDisplacement, text, 0);
246  te::gm::Point* coordRight = new te::gm::Point(ev->getLowerLeftX(), ev->getLowerLeftX() - m_lneVrtDisplacement);
247  m_gridTexts[coordRight] = text;
248  }
249 
250  if(m_topText)
251  {
252  canvas->drawText(ev->getUpperRightX(), ev->getUpperRightY() + m_lneVrtDisplacement, text, 0);
253  te::gm::Point* coordRight = new te::gm::Point(ev->getUpperRightX(), ev->getUpperRightY() + m_lneVrtDisplacement);
254  m_gridTexts[coordRight] = text;
255  }
256  }
257 
258  if(line)
259  {
260  delete line;
261  line = 0;
262  }
263  }
264 }
265 
267 {
268  te::gm::Coord2D init = box.getLowerLeft();
269  te::gm::Coord2D end = box.getUpperRight();
270 
271  if(m_initialGridPointX <= 0)
272  {
273  m_initialGridPointX = init.x;
274  }
275  if(m_initialGridPointY <= 0)
276  {
277  m_initialGridPointY = init.y;
278  }
279 
280  if(m_srid != TE_UNKNOWN_SRS)
281  {
282  if(m_lneHrzGap <= 0)
283  {
284  m_lneHrzGap = std::fabs(init.x - end.x);
285  m_lneHrzGap /= 4;
286  }
287  if(m_lneVrtGap <= 0)
288  m_lneVrtGap = m_lneHrzGap;
289  }
290  else
291  {
292  m_lneVrtGap = m_lneHrzGap = 0.001;
293  }
294 }
295 
297 {
298  m_planarBox = box;
299 }
300 
302 {
303  setVisibleCornerTextsText(visible);
304 }
305 
307 {
308  m_visibleCornerTextsText = visible;
309 
310  m_lowerRightCornerText = visible;
311  m_upperRightCornerText = visible;
312  m_lowerLeftCornerText = visible;
313  m_upperLeftCornerText = visible;
314 }
315 
317 {
318  return m_visibleCornerTextsText;
319 }
320 
virtual void convertToMillimeter(WorldTransformer transf, te::gm::LinearRing *line)
Convert LinearRing from one coordinate system to mm.
Definition: Utils.cpp:497
virtual std::string convertDecimalToDegree(const double &value, bool bDegrees, bool bMinutes, bool bSeconds)
Converts decimal geo coordinates to degrees.
Definition: Utils.cpp:314
Class that represents a "Model" part of GridMap MVC component. Its coordinate system is the same of s...
Definition: GridMapModel.h:67
double y
y-coordinate.
Definition: Coord2D.h:114
virtual void setFontFamily(const std::string &family)=0
It sets the text font family.
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
double x
x-coordinate.
Definition: Coord2D.h:113
virtual void drawHorizontalLines(te::map::Canvas *canvas, Utils *utils, te::gm::Envelope box)
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
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
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 void setVisibleCornerTextsText(bool visible)
virtual void drawVerticalLines(te::map::Canvas *canvas, Utils *utils, te::gm::Envelope box)
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
int calculatePlanarZone(te::gm::Envelope latLongBox)
Calculates the area from a box in coordinated latlong.
Definition: Utils.cpp:419
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
#define TE_UNKNOWN_SRS
A numeric value to represent a unknown SRS identification in TerraLib.
Definition: Config.h:44
virtual te::gm::LinearRing * addCoordsInX(te::gm::Envelope box, double axisCoord, double gap)
Creates a line with n points in x axis. Method used to create the grid lines on a map...
Definition: Utils.cpp:221
virtual void calculateGaps(te::gm::Envelope box)
virtual void drawLineW(te::gm::LinearRing *line)
Draw a line in world coordinates (mm).
Definition: Utils.cpp:87
virtual void remapToPlanar(te::gm::Envelope *latLongBox, int zone)
Map latlong to UTM zone.
Definition: Utils.cpp:447
virtual void setPlanarBox(te::gm::Envelope box)
This class implements the logic for transforming from System 1 coordinate to other type of coordinate...
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
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
Coord2D getUpperRight() const
It returns the upper right coordinate of the envelope.
Definition: Envelope.cpp:46
virtual void setLineColor(const te::color::RGBAColor &color)=0
It sets the pen color used to draw line geometries.
virtual te::gm::LinearRing * addCoordsInY(te::gm::Envelope box, double axisCoord, double gap)
Creates a line with n points in y axis. Method used to create the grid lines on a map...
Definition: Utils.cpp:246
Coord2D getLowerLeft() const
It returns the lower left coordinate of the envelope.
Definition: Envelope.cpp:41
Utility class with functions to manipulate the canvas and conversion between projections.
Definition: Utils.h:61
virtual void setVisibleAllTexts(bool visible)
bool isValid() const
It tells if the rectangle is valid or not.
Definition: Envelope.h:438
virtual void draw(te::map::Canvas *canvas, Utils *utils, te::gm::Envelope box, int srid)
virtual void setTextPointSize(double size)=0
It sets the text point Size.