OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimGpkgTileMatrixRecord.cpp
Go to the documentation of this file.
1 //----------------------------------------------------------------------------
2 //
3 // File: ossimGpkgTileMatrixRecord.cpp
4 //
5 // License: LGPL
6 //
7 // See LICENSE.txt file in the top level directory for more details.
8 //
9 // Description: Container class for gpkg_tile_matrix table.
10 //
11 //----------------------------------------------------------------------------
12 // $Id$
13 
15 #include "ossimSqliteUtil.h"
16 #include <ossim/base/ossimCommon.h>
17 #include <ossim/base/ossimDpt.h>
18 #include <ossim/base/ossimIpt.h>
20 #include <ossim/base/ossimNotify.h>
21 #include <sqlite3.h>
22 #include <iomanip>
23 
24 static const std::string TABLE_NAME = "gpkg_tile_matrix";
25 
27  :
29  m_table_name(),
30  m_zoom_level(0),
31  m_matrix_width(0),
32  m_matrix_height(0),
33  m_tile_width(0),
34  m_tile_height(0),
35  m_pixel_x_size(ossim::nan()),
36  m_pixel_y_size(ossim::nan())
37 {
38 }
39 
41  :
43  m_table_name(obj.m_table_name),
44  m_zoom_level(obj.m_zoom_level),
45  m_matrix_width(obj.m_matrix_width),
46  m_matrix_height(obj.m_matrix_height),
47  m_tile_width(obj.m_tile_width),
48  m_tile_height(obj.m_tile_height),
49  m_pixel_x_size(obj.m_pixel_x_size),
50  m_pixel_y_size(obj.m_pixel_y_size)
51 {
52 }
53 
55  const ossimGpkgTileMatrixRecord& obj)
56 {
57  if ( this != &obj )
58  {
67  }
68  return *this;
69 }
70 
71 bool ossimGpkgTileMatrixRecord::init( const std::string& tableName,
72  ossim_int32 zoom_level,
73  const ossimIpt& matrixSize,
74  const ossimIpt& tileSize,
75  const ossimDpt& gsd )
76 {
77  bool status = false;
78 
79  if ( (matrixSize.hasNans() == false) && (tileSize.hasNans() == false) &&
80  (gsd.hasNans() == false) )
81  {
82  m_table_name = tableName;
83  m_zoom_level = zoom_level;
84  m_matrix_width = matrixSize.x;
85  m_matrix_height = matrixSize.y;
86  m_tile_width = tileSize.x;
87  m_tile_height = tileSize.y;
88  m_pixel_x_size = gsd.x;
89  m_pixel_y_size = gsd.y;
90  status = true;
91  }
92 
93  return status;
94 
95 } // End: ossimGpkgTileMatrixRecord::init( zoom_level, ... )
96 
98 {
99  bool status = false;
100  if ( db )
101  {
102  status = ossim_sqlite::tableExists( db, TABLE_NAME );
103  if ( !status )
104  {
105  std::ostringstream sql;
106  sql << "CREATE TABLE " << TABLE_NAME << " ( "
107  << "table_name TEXT NOT NULL, "
108  << "zoom_level INTEGER NOT NULL, "
109  << "matrix_width INTEGER NOT NULL, "
110  << "matrix_height INTEGER NOT NULL, "
111  << "tile_width INTEGER NOT NULL, "
112  << "tile_height INTEGER NOT NULL, "
113  << "pixel_x_size DOUBLE NOT NULL, "
114  << "pixel_y_size DOUBLE NOT NULL, "
115  << "CONSTRAINT pk_ttm PRIMARY KEY (table_name, zoom_level), "
116  << "CONSTRAINT fk_tmm_table_name FOREIGN KEY (table_name) REFERENCES gpkg_contents(table_name) "
117  << ")";
118 
119  if ( ossim_sqlite::exec( db, sql.str() ) == SQLITE_DONE )
120  {
121  status = true;
122  }
123  }
124  }
125  return status;
126 }
127 
129 {
130  bool status = false;
131  if ( db )
132  {
133  std::ostringstream sql;
134  sql << "INSERT INTO gpkg_tile_matrix VALUES ( "
135  << "'" << m_table_name << "', "
136  << m_zoom_level << ", "
137  << m_matrix_width << ", "
138  << m_matrix_height << ", "
139  << m_tile_width << ", "
140  << m_tile_height << ", "
141  << std::setiosflags(std::ios::fixed) << std::setprecision(16)
142  << m_pixel_x_size << ", "
143  << m_pixel_y_size
144  << " )";
145 
146  if ( ossim_sqlite::exec( db, sql.str() ) == SQLITE_DONE )
147  {
148  status = true;
149  }
150  }
151  return status;
152 }
153 
155 {
156 }
157 
159 {
160  return TABLE_NAME;
161 }
162 
163 bool ossimGpkgTileMatrixRecord::init( sqlite3_stmt* pStmt )
164 {
165  static const char M[] = "ossimGpkgTileMatrixRecord::init";
166 
167  bool status = false;
168 
169  if ( pStmt )
170  {
171  const ossim_int32 EXPECTED_COLUMNS = 8;
172  ossim_int32 nCol = sqlite3_column_count( pStmt );
173 
174  if ( nCol != EXPECTED_COLUMNS )
175  {
177  << M << " WARNING:\nUnexpected number of columns: " << nCol
178  << "Expected column count: " << EXPECTED_COLUMNS
179  << std::endl;
180  }
181 
182  if ( nCol >= EXPECTED_COLUMNS )
183  {
184  ossim_int32 columnsFound = 0;
185  std::string colName;
186  const char* c = 0; // To catch null so not to pass to string.
187 
188  for ( ossim_int32 i = 0; i < nCol; ++i )
189  {
190  colName = sqlite3_column_name(pStmt, i);
191  if ( colName.size() )
192  {
193  if ( colName == "table_name" )
194  {
195  c = (const char*)sqlite3_column_text(pStmt, i);
196  m_table_name = (c ? c : "");
197  ++columnsFound;
198  }
199  else if ( colName == "zoom_level" )
200  {
201  m_zoom_level = sqlite3_column_int(pStmt, i);
202  ++columnsFound;
203  }
204  else if ( colName == "matrix_width" )
205  {
206  m_matrix_width = sqlite3_column_int(pStmt, i);
207  ++columnsFound;
208  }
209  else if ( colName == "matrix_height" )
210  {
211  m_matrix_height = sqlite3_column_int(pStmt, i);
212  ++columnsFound;
213  }
214  else if ( colName == "tile_width" )
215  {
216  m_tile_width = sqlite3_column_int(pStmt, i);
217  ++columnsFound;
218  }
219  else if ( colName == "tile_height" )
220  {
221  m_tile_height = sqlite3_column_int(pStmt, i);
222  ++columnsFound;
223  }
224  else if ( colName == "pixel_x_size" )
225  {
226  m_pixel_x_size = sqlite3_column_double(pStmt, i);
227  ++columnsFound;
228  }
229  else if ( colName == "pixel_y_size" )
230  {
231  m_pixel_y_size = sqlite3_column_double(pStmt, i);
232  ++columnsFound;
233  }
234  else
235  {
237  << M << " Unhandled column name["
238  << i << "]: " << colName << std::endl;
239  }
240 
241  } // Matches: if ( colName.size() )
242 
243  if ( columnsFound == EXPECTED_COLUMNS )
244  {
245  status = true;
246  break;
247  }
248 
249  } // Matches: for ( int i = 0; i < nCol; ++i )
250  }
251 
252  } // Matches: if ( pStmt )
253 
254  return status;
255 
256 } // End: ossimGpkgTileMatrixRecord::init( sqlite3_stmt* pStmt )
257 
259  const std::string& prefix ) const
260 {
261  std::string myPref = prefix.size() ? prefix : std::string("gpkg_tile_matrix.");
262  std::string value;
263 
264  std::string key = "table_name";
265  kwl.addPair(myPref, key, m_table_name, true);
266 
267  key = "zoom_level";
269  kwl.addPair(myPref, key, value, true);
270 
271  key = "matrix_width";
273  kwl.addPair(myPref, key, value, true);
274 
275  key = "matrix_height";
277  kwl.addPair(myPref, key, value, true);
278 
279  key = "tile_width";
281  kwl.addPair(myPref, key, value, true);
282 
283  key = "tile_height";
285  kwl.addPair(myPref, key, value, true);
286 
287  key = "pixel_x_size";
288  value = ossimString::toString(m_pixel_x_size, 15, true).string();
289  kwl.addPair(myPref, key, value, true);
290 
291  key = "pixel_y_size";
292  value = ossimString::toString(m_pixel_y_size, 15, true).string();
293  kwl.addPair(myPref, key, value, true);
294 }
295 
297 {
298  size.x = m_matrix_width;
299  size.y = m_matrix_height;
300 }
301 
303 {
304  size.x = m_tile_width;
305  size.y = m_tile_height;
306 }
307 
309 {
310  gsd.x = m_pixel_x_size;
311  gsd.y = m_pixel_y_size;
312 }
std::basic_ostringstream< char > ostringstream
Class for char output memory streams.
Definition: ossimIosFwd.h:35
virtual void saveState(ossimKeywordlist &kwl, const std::string &prefix) const
Saves the state of object.
Represents serializable keyword/value map.
static const std::string & getTableName()
Get the table name "gpkg_tile_matrix".
bool insert(sqlite3 *db)
Inserst this record into gpkg_spatial_ref_sys table.
int exec(sqlite3 *db, const std::string &sql)
Preforms sqlite3_prepare_v2, sqlite3_step and sqlite3_finalize.
double nan()
Method to return ieee floating point double precision NAN.
Definition: ossimCommon.h:135
This code was derived from https://gist.github.com/mshockwave.
Definition: Barrier.h:8
double y
Definition: ossimDpt.h:165
bool tableExists(sqlite3 *db, const std::string &tableName)
Checks for existance of table.
static ossimString toString(bool aValue)
Numeric to string methods.
void addPair(const std::string &key, const std::string &value, bool overwrite=true)
void getGsd(ossimDpt &gsd) const
Get gsd.
ossimGpkgTileMatrixRecord()
default constructor
virtual bool init(sqlite3_stmt *pStmt)
Initialize from database.
void getMatrixSize(ossimIpt &size) const
Get matrix size.
yy_size_t size
virtual ~ossimGpkgTileMatrixRecord()
destructor
bool hasNans() const
Definition: ossimDpt.h:67
const ossimGpkgTileMatrixRecord & operator=(const ossimGpkgTileMatrixRecord &obj)
void getTileSize(ossimIpt &size) const
Get tile size.
return status
ossim_int32 y
Definition: ossimIpt.h:142
double x
Definition: ossimDpt.h:164
ossim_int32 x
Definition: ossimIpt.h:141
bool hasNans() const
Definition: ossimIpt.h:58
static bool createTable(sqlite3 *db)
Creates table in database.
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)
int ossim_int32
const std::string & string() const
Definition: ossimString.h:414