<commit_before>/**
 * GeoPackage module.
 * @module geoPackage
 */

var SpatialReferenceSystemDao = require('./core/srs').SpatialReferenceSystemDao
  , GeometryColumnsDao = require('./features/columns').GeometryColumnsDao
  , FeatureDao = require('./features/user/featureDao')
  , FeatureTableReader = require('./features/user/featureTableReader')
  , ContentsDao = require('./core/contents').ContentsDao
  , Contents = require('./core/contents').Contents
  , TileMatrixSetDao = require('./tiles/matrixset').TileMatrixSetDao
  , TileMatrixSet = require('./tiles/matrixset').TileMatrixSet
  , TileMatrixDao = require('./tiles/matrix').TileMatrixDao
  , TileMatrix = require('./tiles/matrix').TileMatrix
  , TileTableReader = require('./tiles/user/tileTableReader')
  , TileDao = require('./tiles/user/tileDao')
  , TileTable = require('./tiles/user/tileTable')
  , TileBoundingBoxUtils = require('./tiles/tileBoundingBoxUtils')
  , TableCreator = require('./db/tableCreator')
  , UserTable = require('./user/userTable')
  , FeatureTable = require('./features/user/featureTable')
  , DataColumnsDao = require('./dataColumns').DataColumnsDao
  , DataColumnConstraintsDao = require('./dataColumnConstraints').DataColumnConstraintsDao
  , MetadataDao = require('./metadata').MetadataDao
  , MetadataReferenceDao = require('./metadata/reference').MetadataReferenceDao
  , ExtensionDao = require('./extension').ExtensionDao
  , TableIndexDao = require('./extension/index/tableIndex').TableIndexDao
  , GeometryIndexDao = require('./extension/index/geometryIndex').GeometryIndexDao
  , DataTypes = require('./db/dataTypes');

var async = require('async')
  , proj4 = require('proj4')
  , toArray = require('stream-to-array');

proj4 = 'default' in proj4 ? proj4['default'] : proj4; // Module loading hack

var defs = require('./proj4Defs');
for (var name in defs) {
  if (defs[name]) {
    proj4.defs(name, defs[name]);
  }
}

/**
 * GeoPackage database
 * @class GeoPackage
 */
var GeoPackage = function(name, path, connection) {
  this.name = name;
  this.path = path;
  this.connection = connection;
  this.tableCreator = new TableCreator(this);
}

GeoPackage.loadProjections = function(items) {
  if (!(items instanceof Array)) throw new Error('Invalid array of projections');
  for (var i = 0; i < items.length; i++) {
    if (!defs[items[i]]) throw new Error('Projection not found');
    this.addProjection(items[i], defs[items[i]]);
  }
}

GeoPackage.addProjection = function(name, definition) {
  if (!name || ! definition) throw new Error('Invalid projection name/definition');
  proj4.defs(''+name, ''+definition);
}

GeoPackage.hasProjection = function(name) {
  return proj4.defs(''+name);
}

GeoPackage.prototype.close = function() {
  this.connection.close();
}

GeoPackage.prototype.getDatabase = function() {
  return this.connection;
}

GeoPackage.prototype.getPath = function() {
  return this.path;
}

GeoPackage.prototype.export = function(callback) {
  this.connection.export(callback);
}

/**
 * Get the GeoPackage name
 * @return {String} the GeoPackage name
 */
GeoPackage.prototype.getName = function() {
  return this.name;
}

GeoPackage.prototype.getSpatialReferenceSystemDao = function() {
  return new SpatialReferenceSystemDao(this.connection);
}

GeoPackage.prototype.getContentsDao = function() {
  return new ContentsDao(this.connection);
}

GeoPackage.prototype.getTileMatrixSetDao = function () {
  return new TileMatrixSetDao(this.connection);
};

GeoPackage.prototype.getTileMatrixDao = function() {
  return new TileMatrixDao(this.connection);
}

GeoPackage.prototype.getDataColumnsDao = function() {
  return new DataColumnsDao(this.connection);
}

GeoPackage.prototype.getExtensionDao = function() {
  return new ExtensionDao(this.connection);
}

GeoPackage.prototype.getTableIndexDao = function() {
  return new TableIndexDao(this.connection);
}

GeoPackage.prototype.getGeometryIndexDao = function() {
  return new GeometryIndexDao(this.connection);
}

GeoPackage.prototype.getSrs = function(srsId, callback) {
  var dao = this.getSpatialReferenceSystemDao();
  dao.queryForIdObject(srsId, callback);
}

GeoPackage.prototype.getTileDaoWithTileMatrixSet = function (tileMatrixSet, callback) {
  var tileMatrices = [];
  var tileMatrixDao = this.getTileMatrixDao();
  tileMatrixDao.queryForEqWithField(TileMatrixDao.COLUMN_TABLE_NAME, tileMatrixSet.table_name, null, null, TileMatrixDao.COLUMN_ZOOM_LEVEL + ' ASC, ' + TileMatrixDao.COLUMN_PIXEL_X_SIZE + ' DESC, ' + TileMatrixDao.COLUMN_PIXEL_Y_SIZE + ' DESC', function(err, results) {
    async.eachSeries(results, function(result, callback) {
      var tm = new TileMatrix();
      tileMatrixDao.populateObjectFromResult(tm, result);
      tileMatrices.push(tm);
      callback();
    }, function(err) {
      var tableReader = new TileTableReader(tileMatrixSet);
      tableReader.readTileTable(this.connection, function(err, tileTable) {
        new TileDao(this.connection, tileTable, tileMatrixSet, tileMatrices, function(err, tileDao){
          callback(err, tileDao);
        });
      }.bind(this));
    }.bind(this));
  }.bind(this));
};

GeoPackage.prototype.getTileDaoWithContents = function (contents, callback) {
  var dao = this.getContentsDao();
  dao.getTileMatrixSet(contents, function(err, columns) {
    this.getTileDaoWithTileMatrixSet(columns, callback);
  }.bind(this));
};

GeoPackage.prototype.getTileDaoWithTableName = function (tableName, callback) {
  var tms = this.getTileMatrixSetDao();
  tms.queryForEqWithFieldAndValue(TileMatrixSetDao.COLUMN_TABLE_NAME, tableName, function(err, results) {
    if (results.length > 1) {
      return callback(new Error('Unexpected state. More than one Tile Matrix Set matched for table name: ' + tableName + ', count: ' + results.length));
    } else if (results.length === 0) {
      return callback(new Error('No Tile Matrix found for table name: ' + tableName));
    }
    var tileMatrixSet = new TileMatrixSet();
    tms.populateObjectFromResult(tileMatrixSet, results[0]);
    this.getTileDaoWithTileMatrixSet(tileMatrixSet, callback);
  }.bind(this));
};<commit_msg>getTables method added to the GeoPackage object<commit_after>/**
 * GeoPackage module.
 * @module geoPackage
 */

var SpatialReferenceSystemDao = require('./core/srs').SpatialReferenceSystemDao
  , GeometryColumnsDao = require('./features/columns').GeometryColumnsDao
  , FeatureDao = require('./features/user/featureDao')
  , FeatureTableReader = require('./features/user/featureTableReader')
  , ContentsDao = require('./core/contents').ContentsDao
  , Contents = require('./core/contents').Contents
  , TileMatrixSetDao = require('./tiles/matrixset').TileMatrixSetDao
  , TileMatrixSet = require('./tiles/matrixset').TileMatrixSet
  , TileMatrixDao = require('./tiles/matrix').TileMatrixDao
  , TileMatrix = require('./tiles/matrix').TileMatrix
  , TileTableReader = require('./tiles/user/tileTableReader')
  , TileDao = require('./tiles/user/tileDao')
  , TileTable = require('./tiles/user/tileTable')
  , TileBoundingBoxUtils = require('./tiles/tileBoundingBoxUtils')
  , TableCreator = require('./db/tableCreator')
  , UserTable = require('./user/userTable')
  , FeatureTable = require('./features/user/featureTable')
  , DataColumnsDao = require('./dataColumns').DataColumnsDao
  , DataColumnConstraintsDao = require('./dataColumnConstraints').DataColumnConstraintsDao
  , MetadataDao = require('./metadata').MetadataDao
  , MetadataReferenceDao = require('./metadata/reference').MetadataReferenceDao
  , ExtensionDao = require('./extension').ExtensionDao
  , TableIndexDao = require('./extension/index/tableIndex').TableIndexDao
  , GeometryIndexDao = require('./extension/index/geometryIndex').GeometryIndexDao
  , DataTypes = require('./db/dataTypes');

var async = require('async')
  , proj4 = require('proj4')
  , toArray = require('stream-to-array');

proj4 = 'default' in proj4 ? proj4['default'] : proj4; // Module loading hack

var defs = require('./proj4Defs');
for (var name in defs) {
  if (defs[name]) {
    proj4.defs(name, defs[name]);
  }
}

/**
 * GeoPackage database
 * @class GeoPackage
 */
var GeoPackage = function(name, path, connection) {
  this.name = name;
  this.path = path;
  this.connection = connection;
  this.tableCreator = new TableCreator(this);
}

GeoPackage.loadProjections = function(items) {
  if (!(items instanceof Array)) throw new Error('Invalid array of projections');
  for (var i = 0; i < items.length; i++) {
    if (!defs[items[i]]) throw new Error('Projection not found');
    this.addProjection(items[i], defs[items[i]]);
  }
}

GeoPackage.addProjection = function(name, definition) {
  if (!name || ! definition) throw new Error('Invalid projection name/definition');
  proj4.defs(''+name, ''+definition);
}

GeoPackage.hasProjection = function(name) {
  return proj4.defs(''+name);
}

GeoPackage.prototype.close = function() {
  this.connection.close();
}

GeoPackage.prototype.getDatabase = function() {
  return this.connection;
}

GeoPackage.prototype.getPath = function() {
  return this.path;
}

GeoPackage.prototype.export = function(callback) {
  this.connection.export(callback);
}

/**
 * Get the GeoPackage name
 * @return {String} the GeoPackage name
 */
GeoPackage.prototype.getName = function() {
  return this.name;
}

GeoPackage.prototype.getSpatialReferenceSystemDao = function() {
  return new SpatialReferenceSystemDao(this.connection);
}

GeoPackage.prototype.getContentsDao = function() {
  return new ContentsDao(this.connection);
}

GeoPackage.prototype.getTileMatrixSetDao = function () {
  return new TileMatrixSetDao(this.connection);
};

GeoPackage.prototype.getTileMatrixDao = function() {
  return new TileMatrixDao(this.connection);
}

GeoPackage.prototype.getDataColumnsDao = function() {
  return new DataColumnsDao(this.connection);
}

GeoPackage.prototype.getExtensionDao = function() {
  return new ExtensionDao(this.connection);
}

GeoPackage.prototype.getTableIndexDao = function() {
  return new TableIndexDao(this.connection);
}

GeoPackage.prototype.getGeometryIndexDao = function() {
  return new GeometryIndexDao(this.connection);
}

GeoPackage.prototype.getSrs = function(srsId, callback) {
  var dao = this.getSpatialReferenceSystemDao();
  dao.queryForIdObject(srsId, callback);
}

GeoPackage.prototype.getTileDaoWithTileMatrixSet = function (tileMatrixSet, callback) {
  var tileMatrices = [];
  var tileMatrixDao = this.getTileMatrixDao();
  tileMatrixDao.queryForEqWithField(TileMatrixDao.COLUMN_TABLE_NAME, tileMatrixSet.table_name, null, null, TileMatrixDao.COLUMN_ZOOM_LEVEL + ' ASC, ' + TileMatrixDao.COLUMN_PIXEL_X_SIZE + ' DESC, ' + TileMatrixDao.COLUMN_PIXEL_Y_SIZE + ' DESC', function(err, results) {
    async.eachSeries(results, function(result, callback) {
      var tm = new TileMatrix();
      tileMatrixDao.populateObjectFromResult(tm, result);
      tileMatrices.push(tm);
      callback();
    }, function(err) {
      var tableReader = new TileTableReader(tileMatrixSet);
      tableReader.readTileTable(this.connection, function(err, tileTable) {
        new TileDao(this.connection, tileTable, tileMatrixSet, tileMatrices, function(err, tileDao){
          callback(err, tileDao);
        });
      }.bind(this));
    }.bind(this));
  }.bind(this));
};

GeoPackage.prototype.getTileDaoWithContents = function (contents, callback) {
  var dao = this.getContentsDao();
  dao.getTileMatrixSet(contents, function(err, columns) {
    this.getTileDaoWithTileMatrixSet(columns, callback);
  }.bind(this));
};

GeoPackage.prototype.getTileDaoWithTableName = function (tableName, callback) {
  var tms = this.getTileMatrixSetDao();
  tms.queryForEqWithFieldAndValue(TileMatrixSetDao.COLUMN_TABLE_NAME, tableName, function(err, results) {
    if (results.length > 1) {
      return callback(new Error('Unexpected state. More than one Tile Matrix Set matched for table name: ' + tableName + ', count: ' + results.length));
    } else if (results.length === 0) {
      return callback(new Error('No Tile Matrix found for table name: ' + tableName));
    }
    var tileMatrixSet = new TileMatrixSet();
    tms.populateObjectFromResult(tileMatrixSet, results[0]);
    this.getTileDaoWithTileMatrixSet(tileMatrixSet, callback);
  }.bind(this));
};

/**
 * Gets the tables from the GeoPackage
 * @param  {GeoPackage}   geopackage open GeoPackage object
 * @param  {Function} callback   called with an error if one occurred and an object containing a 'features' property which is an array of feature table names and a 'tiles' property which is an array of tile table names
 */
GeoPackage.prototype.getTables = function(callback) {
  var tables = {};
  this.getFeatureTables(function(err, featureTables) {
    tables.features = featureTables;
    this.getTileTables(function(err, tileTables) {
      tables.tiles = tileTables;
      callback(null, tables);
    });
  }.bind(this));
};
{'fraction_non_alphanumeric': 0.1351456009913259, 'fraction_numerical': 0.0021685254027261464, 'mean_word_length': 4.923394495412844, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3577232', 'n_tokens_mistral': 3945, 'n_tokens_neox': 4021, 'n_words': 752}