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