@loaders.gl/geopackage 4.0.0-alpha.23 → 4.0.0-alpha.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@loaders.gl/geopackage",
3
3
  "description": "GeoPackage data loaders",
4
- "version": "4.0.0-alpha.23",
4
+ "version": "4.0.0-alpha.24",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
7
7
  "access": "public"
@@ -25,12 +25,12 @@
25
25
  ],
26
26
  "dependencies": {
27
27
  "@babel/runtime": "^7.3.1",
28
- "@loaders.gl/gis": "4.0.0-alpha.23",
29
- "@loaders.gl/schema": "4.0.0-alpha.23",
30
- "@loaders.gl/wkt": "4.0.0-alpha.23",
28
+ "@loaders.gl/gis": "4.0.0-alpha.24",
29
+ "@loaders.gl/schema": "4.0.0-alpha.24",
30
+ "@loaders.gl/wkt": "4.0.0-alpha.24",
31
31
  "@math.gl/proj4": "^3.5.1",
32
32
  "@types/sql.js": "^1.4.2",
33
33
  "sql.js": "1.5.0"
34
34
  },
35
- "gitHead": "e212f2a0c0e342f7cb65ce84fa2ff39f64b7d94b"
35
+ "gitHead": "97a8990595c132fb14e3445a8768d9f4cb98ff05"
36
36
  }
package/dist/bundle.js DELETED
@@ -1,5 +0,0 @@
1
- "use strict";
2
- // @ts-nocheck
3
- const moduleExports = require('./index');
4
- globalThis.loaders = globalThis.loaders || {};
5
- module.exports = Object.assign(globalThis.loaders, moduleExports);
@@ -1,43 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.GeoPackageLoader = void 0;
4
- const parse_geopackage_1 = require("./lib/parse-geopackage");
5
- // __VERSION__ is injected by babel-plugin-version-inline
6
- // @ts-ignore TS2304: Cannot find name '__VERSION__'.
7
- // const VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest';
8
- const VERSION = 'latest';
9
- exports.GeoPackageLoader = {
10
- id: 'geopackage',
11
- name: 'GeoPackage',
12
- module: 'geopackage',
13
- version: VERSION,
14
- extensions: ['gpkg'],
15
- mimeTypes: ['application/geopackage+sqlite3'],
16
- category: 'geometry',
17
- parse: parse_geopackage_1.parseGeoPackage,
18
- options: {
19
- geopackage: {
20
- sqlJsCDN: parse_geopackage_1.DEFAULT_SQLJS_CDN
21
- },
22
- gis: {}
23
- }
24
- };
25
- /** Geopackage loader *
26
- export const GeoPackageTableLoader: LoaderWithParser<Record<string, Feature[]>, never, GeoPackageLoaderOptions> = {
27
- id: 'geopackage',
28
- name: 'GeoPackage',
29
- module: 'geopackage',
30
- version: VERSION,
31
- extensions: ['gpkg'],
32
- mimeTypes: ['application/geopackage+sqlite3'],
33
- category: 'geometry',
34
- parse: parseGeoPackage,
35
- options: {
36
- geopackage: {
37
- sqlJsCDN: DEFAULT_SQLJS_CDN,
38
- },
39
- gis: {
40
- }
41
- }
42
- };
43
- */
package/dist/index.js DELETED
@@ -1,5 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.GeoPackageLoader = void 0;
4
- var geopackage_loader_1 = require("./geopackage-loader");
5
- Object.defineProperty(exports, "GeoPackageLoader", { enumerable: true, get: function () { return geopackage_loader_1.GeoPackageLoader; } });
@@ -1,384 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.parseGeoPackage = exports.DEFAULT_SQLJS_CDN = void 0;
7
- const sql_js_1 = __importDefault(require("sql.js"));
8
- const wkt_1 = require("@loaders.gl/wkt");
9
- const gis_1 = require("@loaders.gl/gis");
10
- const proj4_1 = require("@math.gl/proj4");
11
- // We pin to the same version as sql.js that we use.
12
- // As of March 2022, versions 1.6.0, 1.6.1, and 1.6.2 of sql.js appeared not to work.
13
- exports.DEFAULT_SQLJS_CDN = 'https://cdnjs.cloudflare.com/ajax/libs/sql.js/1.5.0/';
14
- // https://www.geopackage.org/spec121/#flags_layout
15
- const ENVELOPE_BYTE_LENGTHS = {
16
- 0: 0,
17
- 1: 32,
18
- 2: 48,
19
- 3: 48,
20
- 4: 64,
21
- // values 5-7 are invalid and _should_ never show up
22
- 5: 0,
23
- 6: 0,
24
- 7: 0
25
- };
26
- // Documentation: https://www.geopackage.org/spec130/index.html#table_column_data_types
27
- const SQL_TYPE_MAPPING = {
28
- BOOLEAN: 'bool',
29
- TINYINT: 'int8',
30
- SMALLINT: 'int16',
31
- MEDIUMINT: 'int32',
32
- INT: 'int32',
33
- INTEGER: 'int32',
34
- FLOAT: 'float32',
35
- DOUBLE: 'float64',
36
- REAL: 'float64',
37
- TEXT: 'utf8',
38
- BLOB: 'binary',
39
- DATE: 'utf8',
40
- DATETIME: 'utf8',
41
- GEOMETRY: 'binary',
42
- POINT: 'binary',
43
- LINESTRING: 'binary',
44
- POLYGON: 'binary',
45
- MULTIPOINT: 'binary',
46
- MULTILINESTRING: 'binary',
47
- MULTIPOLYGON: 'binary',
48
- GEOMETRYCOLLECTION: 'binary'
49
- };
50
- async function parseGeoPackage(arrayBuffer, options) {
51
- const { sqlJsCDN = exports.DEFAULT_SQLJS_CDN } = options?.geopackage || {};
52
- const { reproject = false, _targetCrs = 'WGS84' } = options?.gis || {};
53
- const db = await loadDatabase(arrayBuffer, sqlJsCDN);
54
- const tables = listVectorTables(db);
55
- const projections = getProjections(db);
56
- // Mapping from tableName to geojson feature collection
57
- const outputTables = {
58
- shape: 'tables',
59
- tables: []
60
- };
61
- for (const table of tables) {
62
- const { table_name: tableName } = table;
63
- outputTables.tables.push({
64
- name: tableName,
65
- table: getVectorTable(db, tableName, projections, {
66
- reproject,
67
- _targetCrs
68
- })
69
- });
70
- }
71
- return outputTables;
72
- }
73
- exports.parseGeoPackage = parseGeoPackage;
74
- /**
75
- * Initialize SQL.js and create database
76
- *
77
- * @param arrayBuffer input bytes
78
- * @return SQL.js database object
79
- */
80
- async function loadDatabase(arrayBuffer, sqlJsCDN) {
81
- // In Node, `locateFile` must not be passed
82
- let SQL;
83
- if (sqlJsCDN) {
84
- SQL = await (0, sql_js_1.default)({
85
- locateFile: (file) => `${sqlJsCDN}${file}`
86
- });
87
- }
88
- else {
89
- SQL = await (0, sql_js_1.default)();
90
- }
91
- return new SQL.Database(new Uint8Array(arrayBuffer));
92
- }
93
- /**
94
- * Find all vector tables in GeoPackage
95
- * This queries the `gpkg_contents` table to find a list of vector tables
96
- *
97
- * @param db GeoPackage to query
98
- * @return list of table references
99
- */
100
- function listVectorTables(db) {
101
- // The gpkg_contents table can have at least three categorical values for
102
- // data_type.
103
- // - 'features' refers to a vector geometry table
104
- // (https://www.geopackage.org/spec121/#_contents_2)
105
- // - 'tiles' refers to a raster table
106
- // (https://www.geopackage.org/spec121/#_contents_3)
107
- // - 'attributes' refers to a data table with no geometry
108
- // (https://www.geopackage.org/spec121/#_contents_4).
109
- // We hard code 'features' because for now we don't support raster data or pure attribute data
110
- // eslint-disable-next-line quotes
111
- const stmt = db.prepare("SELECT * FROM gpkg_contents WHERE data_type='features';");
112
- const vectorTablesInfo = [];
113
- while (stmt.step()) {
114
- const vectorTableInfo = stmt.getAsObject();
115
- vectorTablesInfo.push(vectorTableInfo);
116
- }
117
- return vectorTablesInfo;
118
- }
119
- /**
120
- * Load geometries from vector table
121
- *
122
- * @param db GeoPackage object
123
- * @param tableName name of vector table to query
124
- * @param projections keys are srs_id values, values are WKT strings
125
- * @returns Array of GeoJSON Feature objects
126
- */
127
- function getVectorTable(db, tableName, projections, { reproject, _targetCrs }) {
128
- const dataColumns = getDataColumns(db, tableName);
129
- const geomColumn = getGeometryColumn(db, tableName);
130
- const featureIdColumn = getFeatureIdName(db, tableName);
131
- // Get vector features from table
132
- // Don't think it's possible to parameterize the table name in SQLite?
133
- const { columns, values } = db.exec(`SELECT * FROM \`${tableName}\`;`)[0];
134
- let projection;
135
- if (reproject) {
136
- const geomColumnProjStr = projections[geomColumn.srs_id];
137
- projection = new proj4_1.Proj4Projection({
138
- from: geomColumnProjStr,
139
- to: _targetCrs
140
- });
141
- }
142
- const geojsonFeatures = [];
143
- for (const row of values) {
144
- const geojsonFeature = constructGeoJsonFeature(columns, row, geomColumn,
145
- // @ts-ignore
146
- dataColumns, featureIdColumn);
147
- geojsonFeatures.push(geojsonFeature);
148
- }
149
- const schema = getSchema(db, tableName);
150
- if (projection) {
151
- return {
152
- shape: 'object-row-table',
153
- // @ts-expect-error TODO - null geometries causing problems...
154
- data: (0, gis_1.transformGeoJsonCoords)(geojsonFeatures, projection.project),
155
- schema
156
- };
157
- }
158
- return { data: geojsonFeatures, schema, shape: 'object-row-table' };
159
- }
160
- /**
161
- * Find all projections defined in GeoPackage
162
- * This queries the gpkg_spatial_ref_sys table
163
- * @param db GeoPackage object
164
- * @returns mapping from srid to WKT projection string
165
- */
166
- function getProjections(db) {
167
- // Query gpkg_spatial_ref_sys to get srid: srtext mappings
168
- const stmt = db.prepare('SELECT * FROM gpkg_spatial_ref_sys;');
169
- const projectionMapping = {};
170
- while (stmt.step()) {
171
- const srsInfo = stmt.getAsObject();
172
- const { srs_id, definition } = srsInfo;
173
- projectionMapping[srs_id] = definition;
174
- }
175
- return projectionMapping;
176
- }
177
- /**
178
- * Construct single GeoJSON feature given row's data
179
- * @param columns array of ordered column identifiers
180
- * @param row array of ordered values representing row's data
181
- * @param geomColumn geometry column metadata
182
- * @param dataColumns mapping from table column names to property name
183
- * @returns GeoJSON Feature object
184
- */
185
- function constructGeoJsonFeature(columns, row, geomColumn, dataColumns, featureIdColumn) {
186
- // Find feature id
187
- const idIdx = columns.indexOf(featureIdColumn);
188
- const id = row[idIdx];
189
- // Parse geometry columns to geojson
190
- const geomColumnIdx = columns.indexOf(geomColumn.column_name);
191
- const geometry = parseGeometry(row[geomColumnIdx].buffer);
192
- const properties = {};
193
- if (dataColumns) {
194
- for (const [key, value] of Object.entries(dataColumns)) {
195
- const idx = columns.indexOf(key);
196
- // @ts-ignore TODO - Check what happens if null?
197
- properties[value] = row[idx];
198
- }
199
- }
200
- else {
201
- // Put all columns except for the feature id and geometry in properties
202
- for (let i = 0; i < columns.length; i++) {
203
- if (i === idIdx || i === geomColumnIdx) {
204
- // eslint-disable-next-line no-continue
205
- continue;
206
- }
207
- const columnName = columns[i];
208
- properties[columnName] = row[i];
209
- }
210
- }
211
- return {
212
- id,
213
- type: 'Feature',
214
- geometry,
215
- properties
216
- };
217
- }
218
- /**
219
- * Get GeoPackage version from database
220
- * @param db database
221
- * @returns version string. One of '1.0', '1.1', '1.2'
222
- */
223
- // @ts-ignore
224
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
225
- function getGeopackageVersion(db) {
226
- const textDecoder = new TextDecoder();
227
- // Read application id from SQLite metadata
228
- const applicationIdQuery = db.exec('PRAGMA application_id;')[0];
229
- const applicationId = applicationIdQuery.values[0][0];
230
- // Convert 4-byte signed int32 application id to text
231
- const buffer = new ArrayBuffer(4);
232
- const view = new DataView(buffer);
233
- view.setInt32(0, Number(applicationId));
234
- const versionString = textDecoder.decode(buffer);
235
- if (versionString === 'GP10') {
236
- return '1.0';
237
- }
238
- if (versionString === 'GP11') {
239
- return '1.1';
240
- }
241
- // If versionString is GPKG, then read user_version
242
- const userVersionQuery = db.exec('PRAGMA user_version;')[0];
243
- const userVersionInt = userVersionQuery.values[0][0];
244
- if (userVersionInt && typeof userVersionInt === 'number' && userVersionInt < 10300) {
245
- return '1.2';
246
- }
247
- return null;
248
- }
249
- /**
250
- * Find name of feature id column in table
251
- * The feature ID is the primary key of the table.
252
- * http://www.geopackage.org/spec121/#feature_user_tables
253
- *
254
- * @param db database
255
- * @param tableName name of table
256
- * @return name of feature id column
257
- */
258
- function getFeatureIdName(db, tableName) {
259
- // Again, not possible to parameterize table name?
260
- const stmt = db.prepare(`PRAGMA table_info(\`${tableName}\`)`);
261
- while (stmt.step()) {
262
- const pragmaTableInfo = stmt.getAsObject();
263
- const { name, pk } = pragmaTableInfo;
264
- if (pk) {
265
- return name;
266
- }
267
- }
268
- // Is it guaranteed for there always to be at least one primary key column in the table?
269
- return null;
270
- }
271
- /**
272
- * Parse geometry buffer
273
- * GeoPackage vector geometries are slightly extended past the WKB standard
274
- * See: https://www.geopackage.org/spec121/#gpb_format
275
- *
276
- * @param arrayBuffer geometry buffer
277
- * @return GeoJSON geometry (in original CRS)
278
- */
279
- function parseGeometry(arrayBuffer) {
280
- const view = new DataView(arrayBuffer);
281
- const { envelopeLength, emptyGeometry } = parseGeometryBitFlags(view.getUint8(3));
282
- // A Feature object has a member with the name "geometry". The value of the
283
- // geometry member SHALL be either a Geometry object as defined above or, in
284
- // the case that the Feature is unlocated, a JSON null value.
285
- /** @see https://tools.ietf.org/html/rfc7946#section-3.2 */
286
- if (emptyGeometry) {
287
- return null;
288
- }
289
- // Do I need to find the srid here? Is it necessarily the same for every
290
- // geometry in a table?
291
- // const srid = view.getInt32(4, littleEndian);
292
- // 2 byte magic, 1 byte version, 1 byte flags, 4 byte int32 srid
293
- const wkbOffset = 8 + envelopeLength;
294
- // Loaders should not depend on `core` and the context passed to the main loader doesn't include a
295
- // `parseSync` option, so instead we call parseSync directly on WKBLoader
296
- const binaryGeometry = wkt_1.WKBLoader.parseSync?.(arrayBuffer.slice(wkbOffset));
297
- // @ts-expect-error
298
- return (0, gis_1.binaryToGeometry)(binaryGeometry);
299
- }
300
- /**
301
- * Parse geometry header flags
302
- * https://www.geopackage.org/spec121/#flags_layout
303
- *
304
- * @param byte uint8 number representing flags
305
- * @return object representing information from bit flags
306
- */
307
- function parseGeometryBitFlags(byte) {
308
- // Are header values little endian?
309
- const envelopeValue = (byte & 0b00001110) / 2;
310
- // TODO: Not sure the best way to handle this. Throw an error if envelopeValue outside 0-7?
311
- const envelopeLength = ENVELOPE_BYTE_LENGTHS[envelopeValue];
312
- return {
313
- littleEndian: Boolean(byte & 0b00000001),
314
- envelopeLength,
315
- emptyGeometry: Boolean(byte & 0b00010000),
316
- extendedGeometryType: Boolean(byte & 0b00100000)
317
- };
318
- }
319
- /**
320
- * Find geometry column in given vector table
321
- *
322
- * @param db GeoPackage object
323
- * @param tableName Name of vector table
324
- * @returns Array of geometry column definitions
325
- */
326
- function getGeometryColumn(db, tableName) {
327
- const stmt = db.prepare('SELECT * FROM gpkg_geometry_columns WHERE table_name=:tableName;');
328
- stmt.bind({ ':tableName': tableName });
329
- // > Requirement 30
330
- // > A feature table SHALL have only one geometry column.
331
- // https://www.geopackage.org/spec121/#feature_user_tables
332
- // So we should need one and only one step, given that we use the WHERE clause in the SQL query
333
- // above
334
- stmt.step();
335
- const geometryColumn = stmt.getAsObject();
336
- return geometryColumn;
337
- }
338
- /**
339
- * Find property columns in given vector table
340
- * @param db GeoPackage object
341
- * @param tableName Name of vector table
342
- * @returns Mapping from table column names to property name
343
- */
344
- function getDataColumns(db, tableName) {
345
- // gpkg_data_columns is not required to exist
346
- // https://www.geopackage.org/spec121/#extension_schema
347
- let stmt;
348
- try {
349
- stmt = db.prepare('SELECT * FROM gpkg_data_columns WHERE table_name=:tableName;');
350
- }
351
- catch (error) {
352
- if (error.message.includes('no such table')) {
353
- return null;
354
- }
355
- throw error;
356
- }
357
- stmt.bind({ ':tableName': tableName });
358
- // Convert DataColumnsRow object this to a key-value {column_name: name}
359
- const result = {};
360
- while (stmt.step()) {
361
- const column = stmt.getAsObject();
362
- const { column_name, name } = column;
363
- result[column_name] = name || null;
364
- }
365
- return result;
366
- }
367
- /**
368
- * Get arrow schema
369
- * @param db GeoPackage object
370
- * @param tableName table name
371
- * @returns Arrow-like Schema
372
- */
373
- function getSchema(db, tableName) {
374
- const stmt = db.prepare(`PRAGMA table_info(\`${tableName}\`)`);
375
- const fields = [];
376
- while (stmt.step()) {
377
- const pragmaTableInfo = stmt.getAsObject();
378
- const { name, type: sqlType, notnull } = pragmaTableInfo;
379
- const type = SQL_TYPE_MAPPING[sqlType];
380
- const field = { name, type, nullable: !notnull };
381
- fields.push(field);
382
- }
383
- return { fields, metadata: {} };
384
- }
package/dist/lib/types.js DELETED
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });