@loaders.gl/geopackage 3.4.11 → 3.4.12

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