OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimGpkgInfo.cpp
Go to the documentation of this file.
1 //----------------------------------------------------------------------------
2 //
3 // License: LGPL
4 //
5 // See LICENSE.txt file in the top level directory for more details.
6 //
7 // Author: David Burken
8 //
9 // Description: GeoPackage Info class.
10 //
11 //----------------------------------------------------------------------------
12 // $Id$
13 
14 
15 #include "ossimGpkgInfo.h"
17 #include "ossimGpkgUtil.h"
18 
19 #include <ossim/base/ossimCommon.h>
22 #include <ossim/base/ossimNotify.h>
23 #include <ossim/base/ossimString.h>
24 #include <ossim/base/ossimTrace.h>
25 
26 #include <sqlite3.h>
27 
28 static ossimTrace traceDebug("ossimGpkgInfo:debug");
29 static ossimTrace traceTiles("ossimGpkgInfo:tiles");
30 
32  : ossimInfoBase(),
33  m_file()
34 {
35 }
36 
38 {
39 }
40 
42 {
43  bool result = false;
44 
45  if ( file.size() ) // Check for empty filename.
46  {
47  std::ifstream str;
48  str.open( file.c_str(), std::ios_base::in | std::ios_base::binary);
49 
50  if ( str.good() ) // Open good...
51  {
52  if ( ossim_gpkg::checkSignature( str ) ) // Has "SQLite format 3" signature.
53  {
54  m_file = file;
55  result = true;
56  }
57  }
58  }
59 
60  return result;
61 }
62 
64 {
65  static const char MODULE[] = "ossimGpkgInfo::open";
66 
67  if ( traceDebug() )
68  {
70  << MODULE << " entered..."
71  << "File: " << m_file.c_str()
72  << std::endl;
73  }
74 
75  // Check for empty filename.
76  if (m_file.size())
77  {
78  ossimKeywordlist kwl;
79 
80  std::ifstream fileStream;
81  fileStream.open(m_file.c_str(), std::ios_base::in | std::ios_base::binary);
82  char APP_ID[5];
83  fileStream.seekg( 68, std::ios_base::beg );
84  fileStream.read(APP_ID, 4);
85  APP_ID[4] = '\0';
86  std::string appId(APP_ID);
87  kwl.addPair("gpkg.", "version", appId, true);
88  fileStream.close();
89 
90  sqlite3* db;
91 
92  int rc = sqlite3_open_v2( m_file.c_str(), &db, SQLITE_OPEN_READONLY, 0);
93  if ( rc == SQLITE_OK )
94  {
95  // Get the gpkg_contents records:
96  std::string tableName = "gpkg_contents";
97  std::vector< ossimRefPtr<ossimGpkgDatabaseRecordBase> > records;
98  if ( ossim_gpkg::getTableRows( db, tableName, records ) )
99  {
100  std::vector< ossimRefPtr<ossimGpkgDatabaseRecordBase> >::const_iterator i =
101  records.begin();
102  ossim_int32 index = 0;
103  while ( i != records.end() )
104  {
105  std::string prefix = "gpkg.";
106  prefix += tableName;
107  prefix += ossimString::toString(index++).string();
108  prefix += ".";
109  (*i)->saveState( kwl, prefix );
110  ++i;
111  }
112  }
113 
114  records.clear();
115 
116  // Get the gpkg_spatial_ref_sys record:
117  tableName = "gpkg_spatial_ref_sys";
118  if ( ossim_gpkg::getTableRows( db, tableName, records ) )
119  {
120  std::vector< ossimRefPtr<ossimGpkgDatabaseRecordBase> >::const_iterator i =
121  records.begin();
122  ossim_int32 index = 0;
123  while ( i != records.end() )
124  {
125  std::string prefix = "gpkg.";
126  prefix += tableName;
127  prefix += ossimString::toString(index++).string();
128  prefix += ".";
129  (*i)->saveState( kwl, prefix );
130  ++i;
131  }
132  }
133 
134  records.clear();
135 
136  // Get the gpkg_tile_matrix_set records:
137  tableName = "gpkg_tile_matrix_set";
138  if ( ossim_gpkg::getTableRows( db, tableName, records ) )
139  {
140  std::vector< ossimRefPtr<ossimGpkgDatabaseRecordBase> >::const_iterator i =
141  records.begin();
142  ossim_int32 index = 0;
143  while ( i != records.end() )
144  {
145  std::string prefix = "gpkg.";
146  prefix += tableName;
147  prefix += ossimString::toString(index++).string();
148  prefix += ".";
149  (*i)->saveState( kwl, prefix );
150  ++i;
151 
152  if ( traceTiles() )
153  {
154  std::string key = "table_name";
155  std::string tileTableName = kwl.findKey( prefix, key );
156  if ( tileTableName.size() )
157  {
158  ossim_gpkg::printTiles( db, tileTableName,
160  }
161  }
162  }
163  }
164 
165  records.clear();
166 
167  // Get the gpkg_tile_matrix_set records:
168  tableName = "gpkg_tile_matrix";
169  if ( ossim_gpkg::getTableRows( db, tableName, records ) )
170  {
171  std::vector< ossimRefPtr<ossimGpkgDatabaseRecordBase> >::const_iterator i =
172  records.begin();
173  ossim_int32 index = 0;
174  while ( i != records.end() )
175  {
176  std::string prefix = "gpkg.";
177  prefix += tableName;
178  prefix += ossimString::toString(index++).string();
179  prefix += ".";
180  (*i)->saveState( kwl, prefix );
181  ++i;
182  }
183  }
184 
185  records.clear();
186 
187  // Get the nsg_tile_matrix_extent records:
188  tableName = "nsg_tile_matrix_extent";
189  if ( ossim_gpkg::getTableRows( db, tableName, records ) )
190  {
191  std::vector< ossimRefPtr<ossimGpkgDatabaseRecordBase> >::const_iterator i =
192  records.begin();
193  ossim_int32 index = 0;
194  while ( i != records.end() )
195  {
196  std::string prefix = "gpkg.";
197  prefix += tableName;
198  prefix += ossimString::toString(index++).string();
199  prefix += ".";
200  (*i)->saveState( kwl, prefix );
201  ++i;
202  }
203  }
204 
205 
206 
207  if ( kwl.getSize() )
208  {
209  out << kwl << std::endl;
210  }
211  }
212 
213  sqlite3_close(db);
214  db = 0;
215  }
216 
217  return out;
218 }
virtual bool open(const ossimFilename &file)
open method.
Represents serializable keyword/value map.
const std::string & findKey(const std::string &key) const
Find methods that take std::string(s).
std::basic_ifstream< char > ifstream
Class for char input file streams.
Definition: ossimIosFwd.h:44
static ossimString toString(bool aValue)
Numeric to string methods.
void addPair(const std::string &key, const std::string &value, bool overwrite=true)
ossimGpkgInfo()
default constructor
bool getTableRows(sqlite3 *db, const std::string &tableName, std::vector< ossimRefPtr< ossimGpkgDatabaseRecordBase > > &result)
Parse table rows.
bool checkSignature(std::istream &in)
Check signature method.
std::ostream & printTiles(sqlite3 *db, const std::string &tileTableName, std::ostream &out)
std::string::size_type size() const
Definition: ossimString.h:405
virtual std::ostream & print(std::ostream &out) const
Print method.
Info Base.
Definition: ossimInfoBase.h:32
virtual ~ossimGpkgInfo()
virtual destructor
ossim_uint32 getSize() const
const char * c_str() const
Returns a pointer to a null-terminated array of characters representing the string&#39;s contents...
Definition: ossimString.h:396
ossimFilename m_file
Definition: ossimGpkgInfo.h:56
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)
std::basic_ostream< char > ostream
Base class for char output streams.
Definition: ossimIosFwd.h:23
int ossim_int32
const std::string & string() const
Definition: ossimString.h:414