OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimGpkgNsgTileMatrixExtentRecord.cpp
Go to the documentation of this file.
1 //----------------------------------------------------------------------------
2 //
3 // File: ossimGpkgNsgTileMatrixExtentRecord.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 nsg_tile_matrix_extent 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/ossimDrect.h>
19 #include <ossim/base/ossimIrect.h>
21 #include <ossim/base/ossimNotify.h>
22 #include <sqlite3.h>
23 #include <iomanip>
24 
25 static const std::string TABLE_NAME = "nsg_tile_matrix_extent";
26 
28  :
30  m_table_name(),
31  m_zoom_level(0),
32  m_extent_type(),
33  m_min_column(0),
34  m_min_row(0),
35  m_max_column(0),
36  m_max_row(0),
37  m_min_x(ossim::nan()),
38  m_min_y(ossim::nan()),
39  m_max_x(ossim::nan()),
40  m_max_y(ossim::nan())
41 {
42 }
43 
46  :
48  m_table_name(obj.m_table_name),
49  m_zoom_level(obj.m_zoom_level),
50  m_extent_type(obj.m_extent_type),
51  m_min_column(obj.m_min_column),
52  m_min_row(obj.m_min_row),
53  m_max_column(obj.m_max_column),
54  m_max_row(obj.m_max_row),
55  m_min_x(obj.m_min_x),
56  m_min_y(obj.m_min_y),
57  m_max_x(obj.m_max_x),
58  m_max_y(obj.m_max_y)
59 {
60 }
61 
64 {
65  if ( this != &obj )
66  {
71  m_min_row = obj.m_min_row;
73  m_max_row = obj.m_max_row;
74  m_min_x = obj.m_min_x;
75  m_min_y = obj.m_min_y;
76  m_max_x = obj.m_max_x;
77  m_max_y = obj.m_max_y;
78  }
79  return *this;
80 }
81 
83 {
84 }
85 
87 {
88  return TABLE_NAME;
89 }
90 
91 bool ossimGpkgNsgTileMatrixExtentRecord::init( sqlite3_stmt* pStmt )
92 {
93  static const char M[] = "ossimGpkgNsgTileMatrixExtentRecord::init";
94 
95  bool status = false;
96 
97  if ( pStmt )
98  {
99  const ossim_int32 EXPECTED_COLUMNS = 11;
100  ossim_int32 nCol = sqlite3_column_count( pStmt );
101 
102  if ( nCol != EXPECTED_COLUMNS )
103  {
105  << M << " WARNING:\nUnexpected number of columns: " << nCol
106  << "Expected column count: " << EXPECTED_COLUMNS
107  << std::endl;
108  }
109 
110  if ( nCol >= EXPECTED_COLUMNS )
111  {
112  ossim_int32 columnsFound = 0;
113  std::string colName;
114  ossim_int32 type;
115 
116  const char* c = 0; // To catch null so not to pass to string.
117 
118  for ( ossim_int32 i = 0; i < nCol; ++i )
119  {
120  colName = sqlite3_column_name(pStmt, i);
121  type = sqlite3_column_type(pStmt, i);
122  if ( colName.size() )
123  {
124  if ( colName == "table_name" )
125  {
126  c = (const char*)sqlite3_column_text(pStmt, i);
127  m_table_name = (c ? c : "");
128  ++columnsFound;
129  }
130  else if ( ( colName == "zoom_level" ) && ( type == SQLITE_INTEGER ) )
131  {
132  m_zoom_level = sqlite3_column_int(pStmt, i);
133  ++columnsFound;
134  }
135  else if ( colName == "extent_type" )
136  {
137  c = (const char*)sqlite3_column_text(pStmt, i);
138  m_extent_type = (c ? c : "");
139  ++columnsFound;
140  }
141  else if ( ( colName == "min_column" ) && ( type == SQLITE_INTEGER ) )
142  {
143  m_min_column = sqlite3_column_int(pStmt, i);
144  ++columnsFound;
145  }
146  else if ( ( colName == "min_row" ) && ( type == SQLITE_INTEGER ) )
147  {
148  m_min_row = sqlite3_column_int(pStmt, i);
149  ++columnsFound;
150  }
151  else if ( ( colName == "max_column" ) && ( type == SQLITE_INTEGER ) )
152  {
153  m_max_column = sqlite3_column_int(pStmt, i);
154  ++columnsFound;
155  }
156  else if ( ( colName == "max_row" ) && ( type == SQLITE_INTEGER ) )
157  {
158  m_max_row = sqlite3_column_int(pStmt, i);
159  ++columnsFound;
160  }
161  else if ( ( colName == "min_x" ) && ( type == SQLITE_FLOAT ) )
162  {
163  m_min_x = sqlite3_column_double(pStmt, i);
164  ++columnsFound;
165  }
166  else if ( ( colName == "min_y" ) && ( type == SQLITE_FLOAT ) )
167  {
168  m_min_y = sqlite3_column_double(pStmt, i);
169  ++columnsFound;
170  }
171  else if ( ( colName == "max_x" ) && ( type == SQLITE_FLOAT ) )
172  {
173  m_max_x = sqlite3_column_double(pStmt, i);
174  ++columnsFound;
175  }
176  else if ( colName == "max_y" )
177  {
178  m_max_y = sqlite3_column_double(pStmt, i);
179  ++columnsFound;
180  }
181  else
182  {
184  << M << " Unhandled column name["
185  << i << "]: " << colName << std::endl;
186  }
187 
188  } // Matches: if ( colName.size() )
189 
190  if ( columnsFound == EXPECTED_COLUMNS )
191  {
192  status = true;
193  break;
194  }
195 
196  } // Matches: for ( int i = 0; i < nCol; ++i )
197  }
198 
199  } // Matches: if ( pStmt )
200 
201  return status;
202 
203 } // End: ossimGpkgNsgTileMatrixExtentRecord::init( pStmt )
204 
205 bool ossimGpkgNsgTileMatrixExtentRecord::init( const std::string& tableName,
206  ossim_int32 zoom_level,
207  const ossimIrect& imageRect,
208  const ossimDrect& projectionRect )
209 {
210  bool status = false;
211  if ( (imageRect.hasNans() == false) && (projectionRect.hasNans() == false) )
212  {
213  m_table_name = tableName;
214  m_zoom_level = zoom_level;
215  m_extent_type = "complete";
216  m_min_column = imageRect.ul().x;
217  m_min_row = imageRect.ul().y;
218  m_max_column = imageRect.lr().x;
219  m_max_row = imageRect.lr().y;
220  m_min_x = projectionRect.ll().x;
221  m_min_y = projectionRect.ll().y;
222  m_max_x = projectionRect.ur().x;
223  m_max_y = projectionRect.ur().y;
224 
225  status = true;
226  }
227  return status;
228 }
229 
231 {
232  bool status = false;
233  if ( db )
234  {
235  status = ossim_sqlite::tableExists( db, TABLE_NAME );
236  if ( !status )
237  {
238  std::ostringstream sql;
239  sql << "CREATE TABLE " << TABLE_NAME << " ( "
240  << "table_name TEXT NOT NULL, "
241  << "zoom_level INTEGER NOT NULL, "
242  << "extent_type TEXT NOT NULL, "
243  << "min_column INTEGER NOT NULL, "
244  << "min_row INTEGER NOT NULL, "
245  << "max_column INTEGER NOT NULL, "
246  << "max_row INTEGER NOT NULL, "
247  << "min_x DOUBLE NOT NULL, "
248  << "min_y DOUBLE NOT NULL, "
249  << "max_x DOUBLE NOT NULL, "
250  << "max_y DOUBLE NOT NULL, "
251  << "CONSTRAINT pk_ntme PRIMARY KEY (table_name, zoom_level, extent_type, min_column, min_row, max_column, max_row), "
252  << "CONSTRAINT fk_ntme FOREIGN KEY (table_name, zoom_level) "
253  << "REFERENCES gpkg_tile_matrix(table_name, zoom_level)"
254  << ")";
255 
256  if ( ossim_sqlite::exec( db, sql.str() ) == SQLITE_DONE )
257  {
258  status = true;
259  }
260  }
261  }
262  return status;
263 }
264 
266 {
267  bool status = false;
268  if ( db )
269  {
270  std::ostringstream sql;
271  sql << "INSERT INTO nsg_tile_matrix_extent VALUES ( "
272  << "'" << m_table_name << "', "
273  << m_zoom_level << ", "
274  << "'" << m_extent_type << "', "
275  << m_min_column << ", "
276  << m_min_row << ", "
277  << m_max_column << ", "
278  << m_max_row << ", "
279  << std::setprecision(16)
280  << m_min_x << ", "
281  << m_min_y << ", "
282  << m_max_x << ", "
283  << m_max_y
284  << " )";
285 
286  if ( ossim_sqlite::exec( db, sql.str() ) == SQLITE_DONE )
287  {
288  status = true;
289  }
290  }
291  return status;
292 }
293 
295  const std::string& prefix ) const
296 {
297  std::string myPref = prefix.size() ? prefix : std::string("nsg_tile_matrix_extent.");
298  std::string value;
299 
300  std::string key = "table_name";
301  kwl.addPair(myPref, key, m_table_name, true);
302 
303  key = "zoom_level";
305  kwl.addPair(myPref, key, value, true);
306 
307  key = "extent_type";
308  kwl.addPair(myPref, key, m_extent_type, true);
309 
310  key = "min_column";
312  kwl.addPair(myPref, key, value, true);
313 
314  key = "min_row";
316  kwl.addPair(myPref, key, value, true);
317 
318  key = "max_column";
320  kwl.addPair(myPref, key, value, true);
321 
322  key = "max_row";
324  kwl.addPair(myPref, key, value, true);
325 
326  key = "min_x";
327  value = ossimString::toString(m_min_x, 20).string();
328  kwl.addPair(myPref, key, value, true);
329 
330  key = "min_y";
331  value = ossimString::toString(m_min_y, 20).string();
332  kwl.addPair(myPref, key, value, true);
333 
334  key = "max_x";
335  value = ossimString::toString(m_max_x, 20).string();
336  kwl.addPair(myPref, key, value, true);
337 
338  key = "max_y";
339  value = ossimString::toString(m_max_y, 20).string();
340  kwl.addPair(myPref, key, value, true);
341 }
std::basic_ostringstream< char > ostringstream
Class for char output memory streams.
Definition: ossimIosFwd.h:35
Represents serializable keyword/value map.
const ossimGpkgNsgTileMatrixExtentRecord & operator=(const ossimGpkgNsgTileMatrixExtentRecord &obj)
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 bool createTable(sqlite3 *db)
Creates table in database.
static ossimString toString(bool aValue)
Numeric to string methods.
virtual void saveState(ossimKeywordlist &kwl, const std::string &prefix) const
Saves the state of object.
const ossimIpt & ul() const
Definition: ossimIrect.h:274
void addPair(const std::string &key, const std::string &value, bool overwrite=true)
virtual bool init(sqlite3_stmt *pStmt)
Initialize from database.
std::string m_extent_type
“complete”,”missing”,“present”,”absent”,”mixed”
const ossimIpt & lr() const
Definition: ossimIrect.h:276
bool hasNans() const
Definition: ossimDrect.h:396
return status
bool insert(sqlite3 *db)
Inserst this record into gpkg_spatial_ref_sys table.
static const std::string & getTableName()
Get the table name "nsg_tile_matrix_extent".
ossim_int32 y
Definition: ossimIpt.h:142
const ossimDpt & ur() const
Definition: ossimDrect.h:340
double x
Definition: ossimDpt.h:164
bool hasNans() const
Definition: ossimIrect.h:337
ossim_int32 x
Definition: ossimIpt.h:141
const ossimDpt & ll() const
Definition: ossimDrect.h:342
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)
int ossim_int32
const std::string & string() const
Definition: ossimString.h:414