OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimGpkgTileMatrixSetRecord.cpp
Go to the documentation of this file.
1 //----------------------------------------------------------------------------
2 //
3 // File: ossimGpkgTileMatrixSetRecord.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_set 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>
20 #include <ossim/base/ossimNotify.h>
21 #include <sqlite3.h>
22 #include <iomanip>
23 
24 static const std::string TABLE_NAME = "gpkg_tile_matrix_set";
25 
27  :
29  m_table_name(),
30  m_srs_id(0),
31  m_min_x(ossim::nan()),
32  m_min_y(ossim::nan()),
33  m_max_x(ossim::nan()),
34  m_max_y(ossim::nan())
35 {
36 }
37 
39  :
41  m_table_name(obj.m_table_name),
42  m_srs_id(obj.m_srs_id),
43  m_min_x(obj.m_min_x),
44  m_min_y(obj.m_min_y),
45  m_max_x(obj.m_max_x),
46  m_max_y(obj.m_max_y)
47 {
48 }
49 
52 {
53  if ( this != &obj )
54  {
56  m_srs_id = obj.m_srs_id;
57  m_min_x = obj.m_min_x;
58  m_min_y = obj.m_min_y;
59  m_max_x = obj.m_max_x;
60  m_max_y = obj.m_max_y;
61  }
62  return *this;
63 }
64 
66 {
67 }
68 
70 {
71  return TABLE_NAME;
72 }
73 
74 bool ossimGpkgTileMatrixSetRecord::init( sqlite3_stmt* pStmt )
75 {
76  static const char M[] = "ossimGpkgTileMatrixSetRecord::init";
77 
78  bool status = false;
79 
80  if ( pStmt )
81  {
82  const ossim_int32 EXPECTED_COLUMNS = 6;
83  ossim_int32 nCol = sqlite3_column_count( pStmt );
84 
85  if ( nCol != EXPECTED_COLUMNS )
86  {
88  << M << " WARNING:\nUnexpected number of columns: " << nCol
89  << "Expected column count: " << EXPECTED_COLUMNS
90  << std::endl;
91  }
92 
93  if ( nCol >= EXPECTED_COLUMNS )
94  {
95  ossim_int32 columnsFound = 0;
96  std::string colName;
97  const char* c = 0; // To catch null so not to pass to string.
98 
99  for ( ossim_int32 i = 0; i < nCol; ++i )
100  {
101  colName = sqlite3_column_name(pStmt, i);
102  if ( colName.size() )
103  {
104  if ( colName == "table_name" )
105  {
106  c = (const char*)sqlite3_column_text(pStmt, i);
107  m_table_name = (c ? c : "");
108  ++columnsFound;
109  }
110  else if ( colName == "srs_id" )
111  {
112  m_srs_id = sqlite3_column_int(pStmt, i);
113  ++columnsFound;
114  }
115  else if ( colName == "min_x" )
116  {
117  m_min_x = sqlite3_column_double(pStmt, i);
118  ++columnsFound;
119  }
120  else if ( colName == "min_y" )
121  {
122  m_min_y = sqlite3_column_double(pStmt, i);
123  ++columnsFound;
124  }
125  else if ( colName == "max_x" )
126  {
127  m_max_x = sqlite3_column_double(pStmt, i);
128  ++columnsFound;
129  }
130  else if ( colName == "max_y" )
131  {
132  m_max_y = sqlite3_column_double(pStmt, i);
133  ++columnsFound;
134  }
135  else
136  {
138  << M << " Unhandled column name["
139  << i << "]: " << colName << std::endl;
140  }
141 
142  } // Matches: if ( colName.size() )
143 
144  if ( columnsFound == EXPECTED_COLUMNS )
145  {
146  status = true;
147  break;
148  }
149 
150  } // Matches: for ( int i = 0; i < nCol; ++i )
151  }
152 
153  } // Matches: if ( pStmt )
154 
155  return status;
156 
157 } // End: ossimGpkgTileMatrixSetRecord::init( pStmt )
158 
160  const std::string& tableName,ossim_int32 srs_id,
161  const ossimDpt& minPt, const ossimDpt& maxPt )
162 {
163  bool status = false;
164  if ( (minPt.hasNans() == false) && (maxPt.hasNans() == false) )
165  {
166  m_table_name = tableName;
167  m_srs_id = srs_id;
168  m_min_x = minPt.x;
169  m_min_y = minPt.y;
170  m_max_x = maxPt.x;
171  m_max_y = maxPt.y;
172  status = true;
173  }
174  return status;
175 }
176 
178 {
179  bool status = false;
180  if ( db )
181  {
182  status = ossim_sqlite::tableExists( db, TABLE_NAME );
183  if ( !status )
184  {
185  std::ostringstream sql;
186  sql << "CREATE TABLE " << TABLE_NAME << " ( "
187  << "table_name TEXT NOT NULL PRIMARY KEY, "
188  << "srs_id INTEGER NOT NULL, "
189  << "min_x DOUBLE NOT NULL, "
190  << "min_y DOUBLE NOT NULL, "
191  << "max_x DOUBLE NOT NULL, "
192  << "max_y DOUBLE NOT NULL, "
193  << "CONSTRAINT fk_gtms_table_name FOREIGN KEY (table_name) REFERENCES gpkg_contents(table_name), "
194  << "CONSTRAINT fk_gtms_srs FOREIGN KEY (srs_id) REFERENCES gpkg_spatial_ref_sys (srs_id) "
195  << ")";
196 
197  if ( ossim_sqlite::exec( db, sql.str() ) == SQLITE_DONE )
198  {
199  status = true;
200  }
201  }
202  }
203  return status;
204 }
205 
207 {
208  bool status = false;
209  if ( db )
210  {
211  std::ostringstream sql;
212  sql << "INSERT INTO gpkg_tile_matrix_set VALUES ( "
213  << "'" << m_table_name << "', "
214  << m_srs_id << ", "
215  << std::setprecision(16)
216  << m_min_x << ", "
217  << m_min_y << ", "
218  << m_max_x << ", "
219  << m_max_y
220  << " )";
221 
222  if ( ossim_sqlite::exec( db, sql.str() ) == SQLITE_DONE )
223  {
224  status = true;
225  }
226  }
227  return status;
228 }
229 
231 {
233 }
234 
236 {
237  return m_max_x - m_min_x;
238 }
239 
241 {
242  return m_max_y - m_min_y;
243 }
244 
246  const std::string& prefix ) const
247 {
248  std::string myPref = prefix.size() ? prefix : std::string("gpkg_tile_matrix_set.");
249  std::string value;
250 
251  std::string key = "table_name";
252  kwl.addPair(myPref, key, m_table_name, true);
253 
254  key = "srs_id";
256  kwl.addPair(myPref, key, value, true);
257 
258  key = "min_x";
259  value = ossimString::toString(m_min_x, 15).string();
260  kwl.addPair(myPref, key, value, true);
261 
262  key = "min_y";
263  value = ossimString::toString(m_min_y, 15).string();
264  kwl.addPair(myPref, key, value, true);
265 
266  key = "max_x";
267  value = ossimString::toString(m_max_x, 15).string();
268  kwl.addPair(myPref, key, value, true);
269 
270  key = "max_y";
271  value = ossimString::toString(m_max_y, 15).string();
272  kwl.addPair(myPref, key, value, true);
273 }
std::basic_ostringstream< char > ostringstream
Class for char output memory streams.
Definition: ossimIosFwd.h:35
Represents serializable keyword/value map.
static const std::string & getTableName()
Get the table name "gpkg_tile_matrix_set".
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)
const ossimGpkgTileMatrixSetRecord & operator=(const ossimGpkgTileMatrixSetRecord &obj)
virtual void saveState(ossimKeywordlist &kwl, const std::string &prefix) const
Saves the state of object.
static bool createTable(sqlite3 *db)
Creates table in database.
double ossim_float64
ossimGpkgTileMatrixSetRecord()
default constructor
bool hasNans() const
Definition: ossimDpt.h:67
return status
void getRect(ossimDrect &rect) const
Gets the rectangle from bounds.
double x
Definition: ossimDpt.h:164
bool insert(sqlite3 *db)
Inserst this record into gpkg_spatial_ref_sys table.
virtual bool init(sqlite3_stmt *pStmt)
Initialize from database.
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)
int ossim_int32
const std::string & string() const
Definition: ossimString.h:414