OpenCPN Partial API docs
mbtiles.h
1 /******************************************************************************
2  *
3  * Project: OpenCPN
4  * Purpose: MBTiles Chart Support
5  * Author: David Register
6  *
7  ***************************************************************************
8  * Copyright (C) 2018 by David S. Register *
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  * This program is distributed in the hope that it will be useful, *
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18  * GNU General Public License for more details. *
19  * *
20  * You should have received a copy of the GNU General Public License *
21  * along with this program; if not, write to the *
22  * Free Software Foundation, Inc., *
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
24  ***************************************************************************
25  *
26  *
27  */
28 
29 #ifndef _CHARTMBTILES_H_
30 #define _CHARTMBTILES_H_
31 
32 #include <cstdint>
33 #include "chartbase.h"
34 #include "model/georef.h" // for GeoRef type
35 #include "OCPNRegion.h"
36 #include "viewport.h"
37 #include "TileDescriptor.hpp"
38 #include "WorkerThread.hpp"
39 #include "TileCache.hpp"
40 
41 enum class MBTilesType : std::int8_t { BASE, OVERLAY };
42 enum class MBTilesScheme : std::int8_t { XYZ, TMS };
43 
44 class WXDLLEXPORT ChartMbTiles;
45 
46 //-----------------------------------------------------------------------------
47 // Constants, etc.
48 //-----------------------------------------------------------------------------
49 
50 //-----------------------------------------------------------------------------
51 // Fwd Refs
52 //-----------------------------------------------------------------------------
53 
54 class ViewPort;
55 class PixelCache;
56 class ocpnBitmap;
57 class mbTileDescriptor;
58 
59 namespace SQLite {
60 class Database;
61 }
62 
63 class GLShaderProgram;
64 class MbtTilesThread;
65 //-----------------------------------------------------------------------------
66 // Helper classes
67 //-----------------------------------------------------------------------------
68 
69 // ----------------------------------------------------------------------------
70 // ChartMBTiles
71 // ----------------------------------------------------------------------------
72 
73 class ChartMBTiles : public ChartBase {
74 public:
75  // Public methods
76 
77  ChartMBTiles();
78  virtual ~ChartMBTiles();
79 
80  // Accessors
81  virtual ThumbData *GetThumbData(int tnx, int tny, float lat, float lon);
82  virtual ThumbData *GetThumbData();
83  virtual bool UpdateThumbData(double lat, double lon);
84 
85  virtual bool AdjustVP(ViewPort &vp_last, ViewPort &vp_proposed);
86 
87  int GetNativeScale() { return m_Chart_Scale; }
88  double GetNormalScaleMin(double canvas_scale_factor, bool b_allow_overzoom);
89  double GetNormalScaleMax(double canvas_scale_factor, int canvas_width);
90 
91  virtual InitReturn Init(const wxString &name, ChartInitFlag init_flags);
92 
93  bool RenderRegionViewOnDC(wxMemoryDC &dc, const ViewPort &VPoint,
94  const OCPNRegion &Region);
95 
96  virtual bool RenderRegionViewOnGL(const wxGLContext &glc,
97  const ViewPort &VPoint,
98  const OCPNRegion &RectRegion,
99  const LLRegion &Region);
100 
101  virtual double GetNearestPreferredScalePPM(double target_scale_ppm);
102 
103  virtual void GetValidCanvasRegion(const ViewPort &VPoint,
104  OCPNRegion *pValidRegion);
105  virtual LLRegion GetValidRegion();
106 
107  virtual bool GetChartExtent(Extent *pext);
108 
109  void SetColorScheme(ColorScheme cs, bool bApplyImmediate);
110 
111  double GetPPM() { return m_ppm_avg; }
112  double GetZoomFactor() { return m_zoomScaleFactor; }
113 
114 protected:
115  // Methods
116  bool RenderViewOnDC(wxMemoryDC &dc, const ViewPort &VPoint);
117  InitReturn PreInit(const wxString &name, ChartInitFlag init_flags,
118  ColorScheme cs);
119  InitReturn PostInit(void);
120 
121  void PrepareTiles();
122  void PrepareTilesForZoom(int zoomFactor, bool bset_geom);
123  bool getTileTexture(mbTileDescriptor *tile);
124  void FlushTiles(void);
125  bool RenderTile(mbTileDescriptor *tile, int zoomLevel,
126  const ViewPort &VPoint);
127 
128  // Protected Data
129 
130  float m_LonMax, m_LonMin, m_LatMax, m_LatMin;
131 
132  double m_ppm_avg; // Calculated true scale factor of the 1X chart,
133  // pixels per meter
134 
135  int m_b_cdebug;
136 
137  int m_minZoom, m_maxZoom;
138  TileCache *m_tileCache;
139  LLRegion m_minZoomRegion;
140  wxBitmapType m_imageType;
141 
142  double m_zoomScaleFactor;
143 
144  MBTilesType m_Type;
145  MBTilesScheme m_Scheme;
146 
147  SQLite::Database *m_pDB;
148  int m_nTiles;
149  std::string m_format;
150 
151  GLShaderProgram *m_tile_shader_program;
152  uint32_t m_tileCount;
153  MbtTilesThread *m_workerThread;
154  void StartThread();
155  void StopThread();
156 
157 private:
158  void InitFromTiles(const wxString &name);
159  wxPoint2DDouble GetDoublePixFromLL(ViewPort &vp, double lat, double lon);
160 };
161 
162 #endif