All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
MapModel.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2001-2014 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 MapModel.cpp
22 
23  \brief
24 
25  \ingroup layout
26 */
27 
28 // TerraLib
29 #include "MapModel.h"
30 #include "ContextItem.h"
31 #include "Context.h"
32 #include "../../../maptools/Canvas.h"
33 #include "terralib4/terralib/kernel/TeDefines.h"
34 #include "../../../srs/SpatialReferenceSystemManager.h"
35 #include "../../../common/StringUtils.h"
36 
37 // STL
38 #include <vector>
39 #include <string>
40 #include <sstream> // std::stringstream
41 
43 {
44  m_box = te::gm::Envelope(0., 0., 150., 120.);
45 }
46 
48 {
49 
50 }
51 
53 {
54  te::color::RGBAColor** pixmap = 0;
55 
56  ContextItem contextNotify;
57  contextNotify.setPixmap(pixmap);
58  notifyAll(contextNotify);
59 }
60 
62 {
64 
65  updateVisitors();
66 }
67 
69 {
70  if(m_layer)
71  {
72  if(m_layer->getId() == layer->getId())
73  return false;
74  }
75 
76  m_layer = layer;
77 
78  updateVisitors();
79 
80  return true;
81 }
82 
84 {
85  return m_layer;
86 }
87 
89 {
90  if(!m_layer)
91  return 0;
92 
93  if(m_layer.get() == 0)
94  return 0;
95 
96  // World box: coordinates in the same SRS as the layer
97  te::gm::Envelope worldBox = m_layer->getExtent();
98  int srid = m_layer->getSRID();
99 
100  //About units names (SI): terralib5\resources\json\uom.json
101  te::common::UnitOfMeasurePtr unitPtr = unitMeasureLayer();
102 
103  if(!unitPtr)
104  return 0;
105 
106  std::string nameUnit = unitPtr->getName();
107  nameUnit = te::common::Convert2UCase(nameUnit);
108 
109  double fx;
110  double fy;
111  double wMM;
112  double factor;
113  double area;
114 
115  wMM = m_box.getWidth();
116  fx = m_box.getWidth()/worldBox.getWidth();
117  fy = m_box.getHeight()/worldBox.getHeight();
118 
119  if (fx > fy)
120  {
121  factor = fy;
122  area = (int)(factor * worldBox.getWidth() + .5);
123  }
124  else
125  {
126  factor = fx;
127  area = (int)(factor * worldBox.getHeight() + .5);
128  }
129 
130  if (nameUnit.compare("METRE") == 0)
131  wMM /= 1000.;
132  else if (nameUnit.compare("KILOMETRE") == 0)
133  wMM /= 1000000.;
134  else if (nameUnit.compare("FOOT") == 0)
135  wMM /= (12. * 25.4);
136  else if (nameUnit.compare("DEGREE") == 0)
137  wMM /= 110000000.;
138 
139  return (1. / factor ) /(wMM / area);
140 }
141 
143 {
144  te::gm::Envelope worldBox;
145 
146  if(!m_layer)
147  return worldBox;
148 
149  if(m_layer.get() == 0)
150  return worldBox;
151 
152  // World box: coordinates in the same SRS as the layer
153  worldBox = m_layer->getExtent();
154  int srid = m_layer->getSRID();
155 
156  //About units names (SI): terralib5\resources\json\uom.json
157  te::common::UnitOfMeasurePtr unitPtr = unitMeasureLayer();
158 
159  if(!unitPtr)
160  return worldBox;
161 
162  std::string unitPtrStr = unitPtr->getName();
163  unitPtrStr = te::common::Convert2UCase(unitPtrStr);
164 
165  if(unitPtrStr.compare("DEGREE") == 0)
166  {
167  double longitude = worldBox.getCenter().x;
168  int meridiano = (int)(longitude / 6);
169  meridiano = meridiano * 6;
170  meridiano = abs(meridiano) + 3;
171 
172  double long0 = -meridiano * TeCDR;
173 
174  // TeUTM T4
175  int zone = ((int)((long0*TeCRD+183.0)/6.0));
176 
177  std::string proj4 = proj4DescToPlanar(zone);
178 
179  // Get the id of the projection of destination
180  std::pair<std::string, unsigned int> projMeters = te::srs::SpatialReferenceSystemManager::getInstance().getIdFromP4Txt(proj4);
181 
182  // Remapping
183  worldBox.transform(srid, projMeters.second);
184 
185  }
186 
187  return worldBox;
188 }
189 
191 {
193 
194  if(!m_layer)
195  return unitPtr;
196 
197  //About units names (SI): terralib5\resources\json\uom.json
198 
199  int srid = m_layer->getSRID();
200 
201  // Checks if is Planar Geographic
202  std::string authName = "EPSG"; // Now: So far it is the only one supported by TerraLib 5. Future: Review this line!
203  te::srs::SpatialReferenceSystemManager::getInstance().isGeographic(srid, authName);
204  unitPtr = te::srs::SpatialReferenceSystemManager::getInstance().getUnit(srid, authName);
205 
206  return unitPtr;
207 }
208 
210 {
211  te::gm::Envelope worldBox;
212 
213  if(!m_layer)
214  return worldBox;
215 
216  if(m_layer.get() == 0)
217  return worldBox;
218 
219  // World box: coordinates in the same SRS as the layer
220  worldBox = m_layer->getExtent();
221  int srid = m_layer->getSRID();
222 
223  //About units names (SI): terralib5\resources\json\uom.json
224  te::common::UnitOfMeasurePtr unitPtr = unitMeasureLayer();
225 
226  if(!unitPtr)
227  return worldBox;
228 
229  std::string unitPtrStr = unitPtr->getName();
230  unitPtrStr = te::common::Convert2UCase(unitPtrStr);
231 
232  if(unitPtrStr.compare("DEGREE") != 0)
233  {
234  std::string proj4 = proj4DescToGeodesic();
235 
236  // Get the id of the projection of destination
237  std::pair<std::string, unsigned int> projMeters = te::srs::SpatialReferenceSystemManager::getInstance().getIdFromP4Txt(proj4);
238 
239  // Remapping
240  worldBox.transform(srid, projMeters.second);
241  }
242 
243  return worldBox;
244 }
245 
247 {
248  /*
249  PROJ4
250  +proj Projection name
251  +datum Datum name
252  +lat_0 Latitude of origin
253  +lon_0 Central meridian
254  +x_0 False easting
255  +y_0 False northing
256  +lat_1 Latitude of first standard parallel
257  +lat_2 Latitude of second standard parallel
258  +units meters, US survey feet, etc.
259  +lat_ts Latitude of true scale
260  +south Denotes southern hemisphere UTM zone
261  +no_defs Don't use the /usr/share/proj/proj_def.dat defaults file
262  */
263 
264  std::stringstream szone;
265  szone << zone;
266 
267  std::string proj4 = "+proj=utm";
268  proj4+= " +zone="+ szone.str();
269  proj4+= " +south"; // pode ser +noth?
270  proj4+= " +ellps=intl";
271  proj4+= " +towgs84=-206,172,-6,0,0,0,0";
272  proj4+= " +units=m";
273  proj4+= " +no_defs ";
274 
275  return proj4;
276 }
277 
279 {
280  std::string proj4;
281  proj4 += "+proj=longlat";
282  proj4 += " +ellps=aust_SA";
283  proj4 += " +towgs84=-57,1,-41,0,0,0,0";
284  proj4 += " +no_defs ";
285 
286  return proj4;
287 }
virtual te::gm::Envelope getWorldInMeters()
Definition: MapModel.cpp:142
virtual double getScale()
Definition: MapModel.cpp:88
void setPixmap(te::color::RGBAColor **pixmap)
Definition: ContextItem.cpp:77
virtual te::gm::Envelope getWorldInDegrees()
Definition: MapModel.cpp:209
double x
x-coordinate.
Definition: Coord2D.h:86
std::string Convert2UCase(const std::string &value)
It converts a string to upper case.
Definition: StringUtils.h:163
double getWidth() const
It returns the envelope width.
Definition: Envelope.h:443
virtual void updateProperties(te::layout::Properties *properties)
virtual void updateProperties(te::layout::Properties *properties)
Definition: MapModel.cpp:61
Coord2D getCenter() const
It returns the rectangle's center coordinate.
Definition: Envelope.cpp:49
static SpatialReferenceSystemManager & getInstance()
It returns a reference to the singleton instance.
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
virtual te::map::AbstractLayerPtr getLayer()
Definition: MapModel.cpp:83
virtual ~MapModel()
Definition: MapModel.cpp:47
boost::shared_ptr< UnitOfMeasure > UnitOfMeasurePtr
virtual void draw(ContextItem context)
Definition: MapModel.cpp:52
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
virtual bool refreshLayer(te::map::AbstractLayerPtr layer)
Definition: MapModel.cpp:68
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
void transform(int oldsrid, int newsrid)
It will transform the coordinates of the Envelope from the old SRS to the new one.
Definition: Envelope.cpp:90
virtual te::common::UnitOfMeasurePtr unitMeasureLayer()
Definition: MapModel.cpp:190
double getHeight() const
It returns the envelope height.
Definition: Envelope.h:448
std::string proj4DescToPlanar(int zone)
Definition: MapModel.cpp:246
std::string proj4DescToGeodesic()
Definition: MapModel.cpp:278