libosmscout  1.1.1
GeoBox.h
Go to the documentation of this file.
1 #ifndef OSMSCOUT_UTIL_GEOBOX_H
2 #define OSMSCOUT_UTIL_GEOBOX_H
3 
4 /*
5  This source is part of the libosmscout library
6  Copyright (C) 2015 Tim Teulings
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Lesser General Public
10  License as published by the Free Software Foundation; either
11  version 2.1 of the License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public
19  License along with this library; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22 
24 
25 #include <osmscout/GeoCoord.h>
26 
28 #include <osmscout/util/Distance.h>
29 
30 namespace osmscout {
31 
40  class OSMSCOUT_API GeoBox CLASS_FINAL
41  {
42  private:
43  GeoCoord minCoord=GeoCoord(0.0,0.0);
44  GeoCoord maxCoord=GeoCoord(0.0,0.0);
45  bool valid=false;
46 
47  public:
51  GeoBox() = default;
52 
56  GeoBox(const GeoBox& other) = default;
57 
62  GeoBox(const GeoCoord& coordA,
63  const GeoCoord& coordB);
64 
68  void Invalidate()
69  {
70  valid=false;
71  minCoord.Set(0.0,0.0);
72  maxCoord.Set(0.0,0.0);
73  }
74 
78  void Set(const GeoCoord& coordA,
79  const GeoCoord& coordB);
80 
84  void Include(const GeoBox& other);
85 
89  void Include(const GeoCoord& point);
90 
102  template<typename P> bool Includes(const P& coord,
103  bool openInterval=true) const
104  {
105  if (!valid){
106  return false;
107  }
108 
109  if (openInterval) {
110  return minCoord.GetLat()<=coord.GetLat() &&
111  maxCoord.GetLat()>coord.GetLat() &&
112  minCoord.GetLon()<=coord.GetLon() &&
113  maxCoord.GetLon()>coord.GetLon();
114  }
115 
116  return minCoord.GetLat()<=coord.GetLat() &&
117  maxCoord.GetLat()>=coord.GetLat() &&
118  minCoord.GetLon()<=coord.GetLon() &&
119  maxCoord.GetLon()>=coord.GetLon();
120  }
121 
133  bool Intersects(const GeoBox& other,
134  bool openInterval=true) const
135  {
136  if (!valid || !other.valid) {
137  return false;
138  }
139 
140  if (openInterval) {
141  return !(other.GetMaxLon()<minCoord.GetLon() ||
142  other.GetMinLon()>=maxCoord.GetLon() ||
143  other.GetMaxLat()<minCoord.GetLat() ||
144  other.GetMinLat()>=maxCoord.GetLat());
145  }
146 
147  return !(other.GetMaxLon()<minCoord.GetLon() ||
148  other.GetMinLon()>maxCoord.GetLon() ||
149  other.GetMaxLat()<minCoord.GetLat() ||
150  other.GetMinLat()>maxCoord.GetLat());
151  }
152 
158  GeoBox Intersection(const GeoBox& other) const;
159 
165  bool IsValid() const
166  {
167  return valid;
168  }
169 
173  GeoCoord GetMinCoord() const
174  {
175  return minCoord;
176  }
177 
181  GeoCoord GetMaxCoord() const
182  {
183  return maxCoord;
184  }
185 
186  GeoCoord GetCenter() const;
187 
191  double GetMinLat() const
192  {
193  return minCoord.GetLat();
194  }
195 
199  double GetMinLon() const
200  {
201  return minCoord.GetLon();
202  }
203 
207  double GetMaxLat() const
208  {
209  return maxCoord.GetLat();
210  }
211 
215  double GetMaxLon() const
216  {
217  return maxCoord.GetLon();
218  }
219 
223  double GetWidth() const
224  {
225  return maxCoord.GetLon()-minCoord.GetLon();
226  }
227 
231  double GetHeight() const
232  {
233  return maxCoord.GetLat()-minCoord.GetLat();
234  }
235 
241  double GetSize() const
242  {
243  return GetWidth()*GetHeight();
244  }
245 
249  GeoCoord GetBottomLeft() const
250  {
251  return minCoord;
252  }
253 
257  GeoCoord GetBottomRight() const
258  {
259  return GeoCoord(minCoord.GetLat(),
260  maxCoord.GetLon());
261  }
262 
266  GeoCoord GetTopLeft() const
267  {
268  return GeoCoord(maxCoord.GetLat(),
269  minCoord.GetLon());
270  }
271 
275  GeoCoord GetTopRight() const
276  {
277  return maxCoord;
278  }
279 
283  std::string GetDisplayText() const;
284 
288  GeoBox& operator=(const GeoBox& other) = default;
289 
294  static GeoBox BoxByCenterAndRadius(const GeoCoord& center,const Distance& radius);
295  };
296 }
297 
298 #endif
GeoCoord GetTopRight() const
Definition: GeoBox.h:275
GeoCoord GetBottomRight() const
Definition: GeoBox.h:257
GeoCoord GetMinCoord() const
Definition: GeoBox.h:173
double GetMinLat() const
Definition: GeoBox.h:191
double GetSize() const
Definition: GeoBox.h:241
double GetMaxLat() const
Definition: GeoBox.h:207
double GetMaxLon() const
Definition: GeoBox.h:215
double GetMinLon() const
Definition: GeoBox.h:199
double GetWidth() const
Definition: GeoBox.h:223
GeoCoord GetMaxCoord() const
Definition: GeoBox.h:181
Definition: Area.h:38
#define CLASS_FINAL
Definition: Compiler.h:26
bool Intersects(const GeoBox &other, bool openInterval=true) const
Definition: GeoBox.h:133
#define OSMSCOUT_API
Definition: CoreImportExport.h:45
double GetHeight() const
Definition: GeoBox.h:231
bool IsValid() const
Definition: GeoBox.h:165
GeoCoord GetBottomLeft() const
Definition: GeoBox.h:249
bool Includes(const P &coord, bool openInterval=true) const
Definition: GeoBox.h:102
void Invalidate()
Definition: GeoBox.h:68
GeoCoord GetTopLeft() const
Definition: GeoBox.h:266