OpenCPN Partial API docs
Loading...
Searching...
No Matches
shapefile_basemap.h
Go to the documentation of this file.
1/**************************************************************************
2 * Copyright (C) 2012-2023 by David S. Register *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, see <https://www.gnu.org/licenses/>. *
16 **************************************************************************/
17
24#ifndef SHAPEFILE_BASEMAP_H
25#define SHAPEFILE_BASEMAP_H
26
27#include <functional>
28#include <future>
29#include <map>
30#include <thread>
31#include <vector>
32
33#include "gl_headers.h"
34
35#include <wx/gdicmn.h>
36
37#include "ShapefileReader.hpp"
38#include "poly_math.h"
39#include "ocpndc.h"
40#include "std_filesystem.h"
41
42typedef std::vector<wxRealPoint> contour;
43typedef std::vector<contour> contour_list;
44
45class ShapeBaseChartSet; // forward
47
60class LatLonKey {
61public:
69 LatLonKey(int lat, int lon) {
70 this->lat = lat;
71 this->lon = lon;
72 }
78 int lat;
84 int lon;
85
86 bool operator<(const LatLonKey &k) const {
87 if (this->lat < k.lat) {
88 return this->lon < k.lon;
89 }
90 return this->lat < k.lat;
91 }
92
93 bool operator==(const LatLonKey &other) const {
94 return (lat == other.lat && lon == other.lon);
95 }
96};
97
98template <>
99struct std::hash<LatLonKey> {
100 std::size_t operator()(const LatLonKey &k) const {
101 return 360 * k.lat + k.lon;
102 }
103};
104
119
129public:
130 ShapeBaseChart() = delete;
131 ShapeBaseChart(const std::string &filename, const size_t &min_scale,
132 const wxColor &color = *wxBLACK)
133 : _dmod(1),
134 _loading(false),
135 _is_usable(false),
136 _is_tiled(false),
137 _min_scale(min_scale),
138 _filename(filename),
139 _reader(nullptr),
140 _color(color) {
141 _is_usable = fs::exists(filename);
142 }
143
145 this->_filename = t._filename;
146 this->_is_usable = t._is_usable;
147 this->_is_tiled = t._is_tiled;
148 this->_min_scale = t._min_scale;
149 this->_reader = nullptr;
150 this->_color = t._color;
151 this->_dmod = t._dmod;
152 this->_loading = t._loading;
153 }
155 CancelLoading(); // Ensure async operation is done before cleanup.
156 delete _reader;
157 }
158
159 void SetColor(wxColor color) { _color = color; }
160
167 int _dmod;
168
179 bool LoadSHP();
186 bool IsUsable() { return _is_usable && !_loading; }
187 size_t MinScale() { return _min_scale; }
188 void RenderViewOnDC(ocpnDC &dc, ViewPort &vp) { DrawPolygonFilled(dc, vp); }
189 static const std::string ConstructPath(const std::string &dir,
190 const std::string &quality_suffix) {
191 return std::string(dir + fs::path::preferred_separator + "basemap_" +
192 quality_suffix + ".shp");
193 }
194
212 bool CrossesLand(double &lat1, double &lon1, double &lat2, double &lon2);
213
215 void CancelLoading();
216
217private:
218 std::future<bool> _loaded;
219 bool _loading;
220 bool _is_usable;
226 bool _is_tiled;
232 size_t _min_scale;
233 void DoDrawPolygonFilled(ocpnDC &pnt, ViewPort &vp,
234 const shp::Feature &feature);
235 void DoDrawPolygonFilledGL(ocpnDC &pnt, ViewPort &vp,
236 const shp::Feature &feature);
237 void DrawPolygonFilled(ocpnDC &pnt, ViewPort &vp);
238#ifdef ocpnUSE_GL
239 void AddPointToTessList(shp::Point &point, ViewPort &vp, GLUtesselator *tobj,
240 bool idl);
241#endif
242
247 std::string _filename;
254 shp::ShapefileReader *_reader;
261 std::unordered_map<LatLonKey, std::vector<size_t>> _tiles;
268 wxColor _color;
269
292 bool LineLineIntersect(const std::pair<double, double> &A,
293 const std::pair<double, double> &B,
294 const std::pair<double, double> &C,
295 const std::pair<double, double> &D);
296
315 bool PolygonLineIntersect(const shp::Feature &feature,
316 const std::pair<double, double> &A,
317 const std::pair<double, double> &B);
318};
319
326public:
328 ~ShapeBaseChartSet() { Cleanup(); }
329 static wxPoint2DDouble GetDoublePixFromLL(ViewPort &vp, double lat,
330 double lon);
331
332 void SetBasemapLandColor(wxColor color);
333 wxColor GetBasemapLandColor();
334
335 void RenderViewOnDC(ocpnDC &dc, ViewPort &vp);
336
337 ShapeBaseChart &SelectBaseMap(const size_t &scale);
343 bool IsUsable() {
344 return _basemap_map.size() > 0 && LowestQualityBaseMap().IsUsable();
345 }
346
358 bool CrossesLand(double lat1, double lon1, double lat2, double lon2) {
359 if (IsUsable()) {
360 return HighestQualityBaseMap().CrossesLand(lat1, lon1, lat2, lon2);
361 }
362 return false;
363 }
364
365 void Cleanup() {
366 for (auto &pair : _basemap_map) {
367 pair.second.CancelLoading();
368 }
369 _basemap_map.clear();
370 _loaded = false;
371 }
372 void Reset();
373
374private:
375 void LoadBasemaps(const std::string &dir);
376 void DrawPolygonFilled(ocpnDC &pnt, ViewPort &vp, wxColor const &color);
377 void DrawPolygonFilledGL(ocpnDC &pnt, int *pvc, ViewPort &vp,
378 wxColor const &color, bool idl);
386 ShapeBaseChart &LowestQualityBaseMap();
394 ShapeBaseChart &HighestQualityBaseMap();
395
396 bool _loaded;
402 wxColor land_color;
403
404 std::map<Quality, ShapeBaseChart> _basemap_map;
405};
406
408#endif
A latitude/longitude key for 1x1 or 10x10 degree grid tiles.
int lat
Integer latitude value representing the northern (top) boundary of a latitude band.
LatLonKey(int lat, int lon)
Constructor for creating a LatLonKey.
int lon
Integer longitude value representing the western (left) boundary of a longitude band.
Manages a set of ShapeBaseChart objects at different resolutions.
bool CrossesLand(double lat1, double lon1, double lat2, double lon2)
Determines if a line segment between two geographical points crosses any land mass.
bool IsUsable()
Checks if the chart set contains at least one usable chart.
Represents a basemap chart based on shapefile data.
bool CrossesLand(double &lat1, double &lon1, double &lat2, double &lon2)
Determines if a line segment between two geographical points intersects any land mass represented in ...
void CancelLoading()
Cancel the chart loading operation.
int _dmod
Tile size in degrees.
bool LoadSHP()
Loads the shapefile data into memory.
bool IsUsable()
Determines if the chart is ready to be used for rendering or spatial queries.
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
Platform independent GL includes.
Layer to use wxDC or opengl.
ShapeBaseChartSet gShapeBasemap
Global instance.
Quality
Basemap quality.
@ full
Full resolution of the OSM dataset.
@ low
Low resolution.
@ crude
Planetary scale dataset.
@ high
High resolution.
@ medium
Medium resolution.