OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimGpkgContentsRecord.cpp
Go to the documentation of this file.
1 //----------------------------------------------------------------------------
2 //
3 // File: ossimGpkgContentsRecord.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_contents table.
10 //
11 //----------------------------------------------------------------------------
12 // $Id$
13 
15 #include "ossimSqliteUtil.h"
16 #include <ossim/base/ossimCommon.h>
17 #include <ossim/base/ossimDpt.h>
19 #include <ossim/base/ossimNotify.h>
20 #include <sqlite3.h>
21 #include <iomanip>
22 
23 static const std::string TABLE_NAME = "gpkg_contents";
24 
26  :
28  m_table_name(),
29  m_data_type(),
30  m_identifier(),
31  m_description(),
32  m_last_change(),
33  m_min_x(ossim::nan()),
34  m_min_y(ossim::nan()),
35  m_max_x(ossim::nan()),
36  m_max_y(ossim::nan()),
37  m_srs_id(0)
38 {
39 }
40 
42  :
44  m_table_name(obj.m_table_name),
45  m_data_type(obj.m_data_type),
46  m_identifier(obj.m_identifier),
47  m_description(obj.m_description),
48  m_last_change(obj.m_last_change),
49  m_min_x(obj.m_min_x),
50  m_min_y(obj.m_min_y),
51  m_max_x(obj.m_max_x),
52  m_max_y(obj.m_max_y),
53  m_srs_id(obj.m_srs_id)
54 {
55 }
56 
58  const ossimGpkgContentsRecord& copy_this)
59 {
60  if ( this != &copy_this )
61  {
62  m_table_name = copy_this.m_table_name;
63  m_data_type = copy_this.m_data_type;
64  m_identifier = copy_this.m_identifier;
65  m_description = copy_this.m_description;
66  m_last_change = copy_this.m_last_change;
67  m_min_x = copy_this.m_min_x;
68  m_min_y = copy_this.m_min_y;
69  m_max_x = copy_this.m_max_x;
70  m_max_y = copy_this.m_max_y;
71  m_srs_id = copy_this.m_srs_id;
72  }
73  return *this;
74 }
75 
77 {
78 }
79 
81 {
82  return TABLE_NAME;
83 }
84 
85 bool ossimGpkgContentsRecord::init( sqlite3_stmt* pStmt )
86 {
87  static const char M[] = "ossimGpkgContentsRecord::init";
88 
89  bool status = false;
90 
91  if ( pStmt )
92  {
93  const ossim_int32 EXPECTED_COLUMNS = 10;
94  ossim_int32 nCol = sqlite3_column_count( pStmt );
95 
96  if ( nCol != EXPECTED_COLUMNS )
97  {
99  << M << " WARNING:\nUnexpected number of columns: " << nCol
100  << "Expected column count: " << EXPECTED_COLUMNS
101  << std::endl;
102  }
103 
104  if ( nCol >= EXPECTED_COLUMNS )
105  {
106  ossim_int32 columnsFound = 0;
107  std::string colName;
108  const char* c = 0; // To catch null so not to pass to string.
109 
110  for ( ossim_int32 i = 0; i < nCol; ++i )
111  {
112  colName = sqlite3_column_name(pStmt, i);
113  if ( colName.size() )
114  {
115  if ( colName == "table_name" )
116  {
117  c = (const char*)sqlite3_column_text(pStmt, i);
118  m_table_name = (c ? c : "");
119  ++columnsFound;
120  }
121  else if ( colName == "data_type" )
122  {
123  c = (const char*)sqlite3_column_text(pStmt, i);
124  m_data_type = (c ? c : "");
125  ++columnsFound;
126  }
127  else if ( colName == "identifier" )
128  {
129  c = (const char*)sqlite3_column_text(pStmt, i);
130  m_identifier = (c ? c : "");
131  ++columnsFound;
132  }
133  else if ( colName == "description" )
134  {
135  c = (const char*)sqlite3_column_text(pStmt, i);
136  m_description = (c ? c : "");
137  ++columnsFound;
138  }
139  else if ( colName == "last_change" )
140  {
141  c = (const char*)sqlite3_column_text(pStmt, i);
142  m_last_change = (c ? c : "");
143  ++columnsFound;
144  }
145  else if ( colName == "min_x" )
146  {
147  m_min_x = sqlite3_column_double(pStmt, i);
148  ++columnsFound;
149  }
150  else if ( colName == "min_y" )
151  {
152  m_min_y = sqlite3_column_double(pStmt, i);
153  ++columnsFound;
154  }
155  else if ( colName == "max_x" )
156  {
157  m_max_x = sqlite3_column_double(pStmt, i);
158  ++columnsFound;
159  }
160  else if ( colName == "max_y" )
161  {
162  m_max_y = sqlite3_column_double(pStmt, i);
163  ++columnsFound;
164  }
165  else if ( colName == "srs_id" )
166  {
167  m_srs_id = sqlite3_column_int(pStmt, i);
168  ++columnsFound;
169  }
170  else
171  {
173  << M << " Unhandled column name["
174  << i << "]: " << colName << std::endl;
175  }
176 
177  } // Matches: if ( colName.size() )
178 
179  if ( columnsFound == EXPECTED_COLUMNS )
180  {
181  status = true;
182  break;
183  }
184 
185  } // Matches: for ( ossim_int32 i = 0; i < nCol; ++i )
186  }
187 
188  } // Matches: if ( pStmt )
189 
190  return status;
191 
192 } // End: ossimGpkgContentsRecord::init( sqlite3_stmt* pStmt )
193 
195  const std::string& tableName, ossim_int32 srs_id,
196  const ossimDpt& minPt, const ossimDpt& maxPt )
197 {
198  bool status = false;
199  if ( (minPt.hasNans() == false) && (maxPt.hasNans() == false) )
200  {
201  m_table_name = tableName;
202  m_data_type = "tiles";
203  m_identifier = "Raster Tiles for ";
204  m_identifier += tableName;
205  m_description = "Create by ossim gpkg writer.";
206 
208 
209  m_min_x = minPt.x;
210  m_min_y = minPt.y;
211  m_max_x = maxPt.x;
212  m_max_y = maxPt.y;
213  m_srs_id = srs_id;
214  status = true;
215  }
216  return status;
217 }
218 
220 {
221  bool status = false;
222  if ( db )
223  {
224  status = ossim_sqlite::tableExists( db, TABLE_NAME );
225 
226  if ( !status )
227  {
228  std::ostringstream sql;
229  sql << "CREATE TABLE " << TABLE_NAME << " ( "
230  << "table_name TEXT NOT NULL PRIMARY KEY, "
231  << "data_type TEXT NOT NULL, "
232  << "identifier TEXT UNIQUE, "
233  << "description TEXT DEFAULT \"\", "
234 
235  //---
236  // Requirement 13:
237  // Note: Spec changed to %fZ. No %f on linux but sqlite library handles.
238  // << "last_change DATETIME NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%SZ','now')), "
239  //---
240  << "last_change DATETIME NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ','now')), "
241  << "min_x DOUBLE, "
242  << "min_y DOUBLE, "
243  << "max_x DOUBLE, "
244  << "max_y DOUBLE, "
245  << "srs_id INTEGER, "
246  << "CONSTRAINT fk_gc_r_srs_id FOREIGN KEY (srs_id) "
247  << "REFERENCES gpkg_spatial_ref_sys(srs_id)"
248  << ")";
249 
250  if ( ossim_sqlite::exec( db, sql.str() ) == SQLITE_DONE )
251  {
252  status = true;
253  }
254  }
255  }
256  return status;
257 }
258 
260 {
261  bool status = false;
262  if ( db )
263  {
264  std::ostringstream sql;
265  sql << "INSERT INTO gpkg_contents VALUES ( "
266  << "'" << m_table_name << "', "
267  << "'" << m_data_type << "', "
268  << "'" << m_identifier << "', "
269  << "'" << m_description << "', "
270  << "'" << m_last_change << "', "
271  << std::setprecision(16)
272  << m_min_x << ", "
273  << m_min_y << ", "
274  << m_max_x << ", "
275  << m_max_y << ", "
276  << m_srs_id
277  << " )";
278 
279  if ( ossim_sqlite::exec( db, sql.str() ) == SQLITE_DONE )
280  {
281  status = true;
282  }
283  }
284  return status;
285 }
286 
288  const std::string& prefix ) const
289 {
290  std::string myPref = prefix.size() ? prefix : std::string("gpkg_contents.");
291  std::string value;
292 
293  std::string key = "table_name";
294  kwl.addPair(myPref, key, m_table_name, true);
295 
296  key = "data_type";
297  kwl.addPair(myPref, key, m_data_type, true);
298 
299  key = "identifier";
300  kwl.addPair(myPref, key, m_identifier, true);
301 
302  key = "description";
303  kwl.addPair(myPref, key, m_description, true);
304 
305  key = "last_change";
306  kwl.addPair(myPref, key, m_last_change, true);
307 
308  key = "min_x";
309  value = ossimString::toString(m_min_x, 15).string();
310  kwl.addPair(myPref, key, value, true);
311 
312  key = "min_y";
313  value = ossimString::toString(m_min_y, 15).string();
314  kwl.addPair(myPref, key, value, true);
315 
316  key = "max_x";
317  value = ossimString::toString(m_max_x, 15).string();
318  kwl.addPair(myPref, key, value, true);
319 
320  key = "max_y";
321  value = ossimString::toString(m_max_y, 15).string();
322  kwl.addPair(myPref, key, value, true);
323 
324  key = "srs_id";
326  kwl.addPair(myPref, key, value, true);
327 }
virtual ~ossimGpkgContentsRecord()
destructor
std::basic_ostringstream< char > ostringstream
Class for char output memory streams.
Definition: ossimIosFwd.h:35
Represents serializable keyword/value map.
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
bool insert(sqlite3 *db)
Inserst this record into gpkg_spatial_ref_sys table.
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)
static bool createTable(sqlite3 *db)
Creates table in database.
const ossimGpkgContentsRecord & operator=(const ossimGpkgContentsRecord &copy_this)
virtual bool init(sqlite3_stmt *pStmt)
Initialize from database.
virtual void saveState(ossimKeywordlist &kwl, const std::string &prefix) const
Saves the state of object to keyword list.
bool hasNans() const
Definition: ossimDpt.h:67
return status
void getTime(std::string &result)
Gets time in the form of "%Y-%m-%dT%H:%M:%S.000Z".
double x
Definition: ossimDpt.h:164
ossimGpkgContentsRecord()
default constructor
static const std::string & getTableName()
Get the table name "gpkg_contents".
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)
int ossim_int32
const std::string & string() const
Definition: ossimString.h:414