OpenCPN Partial API docs
Loading...
Searching...
No Matches
gshhs.h
Go to the documentation of this file.
1/**************************************************************************
2 * Copyright (C) 2008 Jacques Zaninetti - http://www.zygrib.org *
3 * Copyright (C) 2012 Jesper Weissglass *
4 * Copyright (C) 2012 David S. Register *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
20 **************************************************************************/
21
31#ifndef GSHHS_H
32#define GSHHS_H
33
34#include <string>
35#include <vector>
36
37#include "viewport.h"
38#include "poly_math.h"
39#include "color_types.h"
40
41#include "ocpndc.h"
42
43#ifdef __MSVC__
44#pragma warning(disable : 4251) // relates to std::string fpath
45#endif
46
47//-------------------------------------------------------------------------
48// Subset of original Projection, only whats needed for GSHHS.
49
50#ifndef M_PI_2
51#define M_PI_2 1.57079632679489661923
52#endif
53#ifndef M_PI_4
54#define M_PI_4 0.785398163397448309616
55#endif
56
57//-------------------------------------------------------------------------
58class wxLineF {
59public:
60 wxLineF(double x1, double y1, double x2, double y2) {
61 m_p1 = wxRealPoint(x1, y1);
62 m_p2 = wxRealPoint(x2, y2);
63 }
64 wxRealPoint &p1() { return m_p1; }
65 wxRealPoint &p2() { return m_p2; }
66 wxRealPoint m_p1, m_p2;
67};
68
70 int version; // Version number of the GSHHS data format
71 int pasx; // Longitude step size for cells (1/10th degree)
72 int pasy; // Latitude step size for cells (1/10th degree)
73 // xmin, ymin, xmax, ymax define the bounding box of the entire grid in 1/10th
74 // of a degree. This allows for quick determination of whether a given point
75 // is within the covered area.
76 int xmin; // Minimum longitude of the grid (1/10th degree)
77 int ymin; // Minimum latitude of the grid (1/10th degree)
78 int xmax; // Maximum longitude of the grid (1/10th degree)
79 int ymax; // Maximum latitude of the grid (1/10th degree)
80 int p1; // Spare (reserved for future use)
81 int p2; // Spare (reserved for future use)
82 int p3; // Spare (reserved for future use)
83 int p4; // Spare (reserved for future use)
84 int p5; // Spare (reserved for future use)
85};
86
87typedef std::vector<wxRealPoint> contour;
88typedef std::vector<contour> contour_list;
89#define GSSH_SUBM 16 // divide each cell to 16x16 sub cells
90
91//==========================================================================
92
94public:
95 GshhsPolyCell(FILE *fpoly, int x0, int y0, PolygonFileHeader *header);
97
98 void ClearPolyV();
99
100 void drawMapPlain(ocpnDC &pnt, double dx, ViewPort &vp, wxColor seaColor,
101 wxColor landColor, bool idl);
102
103 void drawSeaBorderLines(ocpnDC &pnt, double dx, ViewPort &vp);
104 std::vector<wxLineF> *getCoasts() { return &coasts; }
105 contour_list &getPoly1() { return poly1; }
106
107 /* we remap the segments into a high resolution map to
108 greatly reduce intersection testing time */
109 std::vector<wxLineF> *high_res_map[GSSH_SUBM * GSSH_SUBM];
110
111private:
112 int nbpoints;
113 int x0cell, y0cell;
114
115 FILE *fpoly;
116
117 std::vector<wxLineF> coasts;
118 PolygonFileHeader *header;
119 contour_list poly1, poly2, poly3, poly4, poly5;
120
121 // used for opengl vertex cache
122 float_2Dpt *polyv[6];
123 int polyc[6];
124
125 void DrawPolygonFilled(ocpnDC &pnt, contour_list *poly, double dx,
126 ViewPort &vp, wxColor const &color);
127#ifdef ocpnUSE_GL
128 void DrawPolygonFilledGL(ocpnDC &pnt, contour_list *p, float_2Dpt **pv,
129 int *pvc, ViewPort &vp, wxColor const &color,
130 bool idl);
131#endif
132 void DrawPolygonContour(ocpnDC &pnt, contour_list *poly, double dx,
133 ViewPort &vp);
134
135 void ReadPoly(contour_list &poly);
136 void ReadPolygonFile();
137};
138
140public:
141 GshhsPolyReader(int quality);
143
144 void drawGshhsPolyMapPlain(ocpnDC &pnt, ViewPort &vp, wxColor const &seaColor,
145 wxColor const &landColor);
146
147 void drawGshhsPolyMapSeaBorders(ocpnDC &pnt, ViewPort &vp);
148
149 void InitializeLoadQuality(int quality); // 5 levels: 0=low ... 4=full
150 bool crossing1(wxLineF trajectWorld);
151 int currentQuality;
152 int ReadPolyVersion();
153 int GetPolyVersion() { return polyHeader.version; }
154
155private:
156 FILE *fpoly;
157 GshhsPolyCell *allCells[360][180];
158
159 PolygonFileHeader polyHeader;
160 void readPolygonFileHeader(FILE *polyfile, PolygonFileHeader *header);
161
162 wxMutex mutex1, mutex2;
163
164 ViewPort last_rendered_vp;
165};
166
167// GSHHS file format:
168//
169// int id; /* Unique polygon id number, starting at 0 */
170// int n; /* Number of points in this polygon */
171// int flag; /* level + version << 8 + greenwich << 16 + source <<
172// 24 */ int west, east, south, north; /* min/max extent in micro-degrees */ int
173// area; /* Area of polygon in 1/10 km^2 */
174//
175// Here, level, version, greenwhich, and source are
176// level: 1 land, 2 lake, 3 island_in_lake, 4 pond_in_island_in_lake
177// version: Set to 4 for GSHHS version 1.4
178// greenwich: 1 if Greenwich is crossed
179// source: 0 = CIA WDBII, 1 = WVS
180
181//==========================================================
183public:
184 double lon, lat; // longitude, latitude
185 GshhsPoint(double lo, double la) {
186 lon = lo;
187 lat = la;
188 }
189};
190
191//==========================================================
192
194public:
195 GshhsPolygon(FILE *file);
196
197 virtual ~GshhsPolygon();
198
199 int getLevel() { return flag & 255; }
200 int isGreenwich() { return greenwich; }
201 int isAntarctic() { return antarctic; }
202 bool isOk() { return ok; }
203 int readInt4();
204 int readInt2();
205
206 int id; /* Unique polygon id number, starting at 0 */
207 int n; /* Number of points in this polygon */
208 int flag; /* level + version << 8 + greenwich << 16 + source << 24 */
209 double west, east, south, north; /* min/max extent in DEGREES */
210 int area; /* Area of polygon in 1/10 km^2 */
211 int areaFull, container, ancestor;
212
213 std::vector<GshhsPoint *> lsPoints;
214
215protected:
216 FILE *file;
217 bool ok;
218 bool greenwich, antarctic;
219};
220
221//==========================================================
222
224public:
225 GshhsReader();
226 ~GshhsReader();
227
228 void drawContinents(ocpnDC &pnt, ViewPort &vp, wxColor const &seaColor,
229 wxColor const &landColor);
230
231 void drawSeaBorders(ocpnDC &pnt, ViewPort &vp);
232 void drawBoundaries(ocpnDC &pnt, ViewPort &vp);
233 void drawRivers(ocpnDC &pnt, ViewPort &vp);
234
235 int GetPolyVersion() { return gshhsPoly_reader->GetPolyVersion(); }
236
237 static wxString getNameExtension(int quality);
238 static wxString getFileName_boundaries(int quality);
239 static wxString getFileName_rivers(int quality);
240 static wxString getFileName_Land(int quality);
241 static bool gshhsFilesExists(int quality);
242
243 int getQuality() { return quality; }
244
245 // bool crossing( wxLineF traject, wxLineF trajectWorld ) const;
246 bool crossing1(wxLineF trajectWorld);
247 // Open the polygon file, read and return the version from the header.
248 int ReadPolyVersion();
249 bool qualityAvailable[6];
250
251 void LoadQuality(int quality);
252 int GetMinAvailableQuality() { return minQualityAvailable; }
253 int GetMaxAvailableQuality() { return maxQualityAvailable; }
254
255private:
256 int quality; // 5 levels: 0=low ... 4=full
257 int selectBestQuality(void);
272 int selectBestQuality(ViewPort &vp);
273
274 int maxQualityAvailable;
275 int minQualityAvailable;
276
277 std::string fpath; // directory containing gshhs files
278
279 GshhsPolyReader *gshhsPoly_reader;
280
281 std::vector<GshhsPolygon *> *lsPoly_boundaries[5];
282 std::vector<GshhsPolygon *> *lsPoly_rivers[5];
283
284 std::vector<GshhsPolygon *> &getList_boundaries();
285 std::vector<GshhsPolygon *> &getList_rivers();
286 //-----------------------------------------------------
287
288 int GSHHS_scaledPoints(GshhsPolygon *pol, wxPoint *pts, double decx,
289 ViewPort &vp);
290
291 void GsshDrawLines(ocpnDC &pnt, std::vector<GshhsPolygon *> &lst,
292 ViewPort &vp, bool isClosed);
293 void clearLists();
294};
295
296inline bool GshhsReader::crossing1(wxLineF trajectWorld) {
297 return this->gshhsPoly_reader->crossing1(trajectWorld);
298}
299#define GSHHS_SCL 1.0e-6 /* Convert micro-degrees to degrees */
300
301//-------------------------------------------------------------------------------
302
304public:
305 GSHHSChart();
306 ~GSHHSChart();
307 void SetColorScheme(ColorScheme scheme);
308 void RenderViewOnDC(ocpnDC &dc, ViewPort &VPoint);
309 void Reset();
310 void SetColorsDirect(wxColour newLand, wxColour newWater);
311
312 wxColor land;
313 wxColor water;
314 int GetMinAvailableQuality();
315 int GetMaxAvailableQuality();
316
317private:
318 GshhsReader *reader;
319};
320
321/*
322 * Create a GSHHS singleton for detecting land crossing and load the data
323 * from GSHHS files on disk.
324 */
325void gshhsCrossesLandInit();
326/*
327 * Reset the GSHHS singleton and reload the data from the GSHHS files on disk.
328 * For example, this should be invoked when new GSHHS files have been installed.
329 */
330void gshhsCrossesLandReset();
331/*
332 * Detects if the rectangle specified with the coordinates crosses land.
333 * The best available quality is used, regardless of the chart scale.
334 */
335bool gshhsCrossesLand(double lat1, double lon1, double lat2, double lon2);
336
337#endif
ViewPort - Core geographic projection and coordinate transformation engine.
Definition viewport.h:56
Device context class that can use either wxDC or OpenGL for drawing.
Definition ocpndc.h:60
Layer to use wxDC or opengl.
Geographic projection and coordinate transformations.