ol-load-geopackage 2.0.1 → 2.1.0
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/API.md +4 -1
- package/README.md +8 -3
- package/dist/ol-load-geopackage.js +33 -20
- package/package.json +2 -3
package/API.md
CHANGED
|
@@ -14,7 +14,10 @@ Parameters:
|
|
|
14
14
|
|
|
15
15
|
- string `sqlJsWasmDir` (optional): URL or path relative to root of folder containing sql-wasm.wasm file. Default value is root folder.
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
Returns a Promise which delivers:
|
|
18
|
+
- WebAssembly: sql.js SQLITE database access library
|
|
19
|
+
|
|
20
|
+
Note that unless you require access to sql.js outside ol-load-geopackage, then the returned promise can be ignored; [loadGpkg()](#loadgpkggpkgfile-displayprojection) will wait if necessary and handle any errors loading the WASM file. The only other reason to monitor the returned promise is if you are unlikely to invoke loadGpkg() for a long time and want to ensure the WASM has been successfully loaded.
|
|
18
21
|
|
|
19
22
|
If you want to load the associated WebAssembly binary (sql-wasm.wasm) from an external Content Delivery Network (CDN) then it is important that you load the WASM file that matches the version of the sql.js module being imported by ol-load-geopackage. Thus you will need to incorporate the _sql_js_version_ constant in the calling parameter, for example (from the proj4_example):
|
|
20
23
|
|
package/README.md
CHANGED
|
@@ -37,7 +37,7 @@ Use Node.js to install the NPM package: [ol-load-geopackage](https://www.npmjs.c
|
|
|
37
37
|
npm install --save ol-load-geopackage
|
|
38
38
|
```
|
|
39
39
|
|
|
40
|
-
After running npm install, the sql.js WebAssembly file (sql-wasm.wasm) will need to be copied from folder _node_modules/sql.js/dist/_ to a folder where the web page can load it from.
|
|
40
|
+
After running npm install, the sql.js WebAssembly file (sql-wasm.wasm) will need to be copied from folder _node_modules/sql.js/dist/_ to a folder where the web page can load it from (unless you plan to load it from a CDN).
|
|
41
41
|
|
|
42
42
|
## Basic usage
|
|
43
43
|
|
|
@@ -47,7 +47,12 @@ This package must be imported as a module - it is not designed to be loaded dire
|
|
|
47
47
|
import { initSqlJsWasm, loadGpkg } from 'ol-load-geopackage';
|
|
48
48
|
|
|
49
49
|
initSqlJsWasm('.');
|
|
50
|
-
var gpkgPromise
|
|
50
|
+
var gpkgPromise;
|
|
51
|
+
try {
|
|
52
|
+
gpkgPromise = loadGpkg(<gpkgFile>, <displayProjection>);
|
|
53
|
+
} catch (error) {
|
|
54
|
+
alert('loadGpkg() failed before Promise set-up:\n' + error);
|
|
55
|
+
}
|
|
51
56
|
gpkgPromise
|
|
52
57
|
.then(([dataFromGpkg, sldsFromGpkg]) => {
|
|
53
58
|
for (var table in dataFromGpkg) {
|
|
@@ -59,7 +64,7 @@ gpkgPromise
|
|
|
59
64
|
// sldsFromGpkg[layerName]
|
|
60
65
|
}
|
|
61
66
|
})
|
|
62
|
-
.catch(error => alert('ol-load-geopackage error
|
|
67
|
+
.catch(error => alert('ol-load-geopackage error:\n' + error));
|
|
63
68
|
```
|
|
64
69
|
|
|
65
70
|
Note that the _initSqlJsWasm()_ statement will start the asynchronous loading of the required sql.js WebAssembly binary file sql-wasm.wasm (from the current folder in this case), so is best placed early in the code.
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* Both Sources and SLDs are returned in objects with table names used as keys.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
import initSqlJs from
|
|
10
|
+
import initSqlJs from 'sql.js';
|
|
11
11
|
import {get as ol_proj_get} from 'ol/proj.js';
|
|
12
12
|
import ol_source_Vector from 'ol/source/Vector.js';
|
|
13
13
|
import ol_format_WKB from 'ol/format/WKB.js';
|
|
@@ -25,13 +25,18 @@ var promiseSqlWasmLoaded;
|
|
|
25
25
|
export { initSqlJsWasm, loadGpkg, sql_js_version };
|
|
26
26
|
|
|
27
27
|
/**
|
|
28
|
-
* Initialisation: start asynchronous loading of WASM file required by sql.js
|
|
28
|
+
* Initialisation: start asynchronous loading of WASM file required by sql.js.
|
|
29
|
+
* Note that unless you require access to sql.js outside ol-load-geopackage,
|
|
30
|
+
* then the returned promise can be ignored; loadGpkg() will wait if necessary
|
|
31
|
+
* and handle any errors loading the WASM file.
|
|
29
32
|
* @param {string} sqlJsWasmDir - URL of folder containing sql-wasm.wasm file to load for sql.js
|
|
33
|
+
* @returns {Promise} Promise which delivers:
|
|
34
|
+
* {WebAssembly} sql.js SQLITE database access library
|
|
30
35
|
*/
|
|
31
36
|
function initSqlJsWasm(sqlJsWasmDir) {
|
|
32
37
|
// If the WASM file location isn't specified look for it in the root folder
|
|
33
38
|
if (sqlJsWasmDir === undefined) {
|
|
34
|
-
sqlJsWasmDir =
|
|
39
|
+
sqlJsWasmDir = '';
|
|
35
40
|
}
|
|
36
41
|
|
|
37
42
|
// For reading OGC GeoPackage files, use the sql.js SQLite reader;
|
|
@@ -46,6 +51,8 @@ function initSqlJsWasm(sqlJsWasmDir) {
|
|
|
46
51
|
`${sqlJsWasmDir}/\n` +
|
|
47
52
|
`[sql.js error message]: ${error}`);
|
|
48
53
|
});
|
|
54
|
+
|
|
55
|
+
return promiseSqlWasmLoaded;
|
|
49
56
|
}
|
|
50
57
|
|
|
51
58
|
/**
|
|
@@ -58,16 +65,22 @@ function initSqlJsWasm(sqlJsWasmDir) {
|
|
|
58
65
|
*/
|
|
59
66
|
function loadGpkg(gpkgFile, displayProjection) {
|
|
60
67
|
|
|
61
|
-
//
|
|
62
|
-
|
|
68
|
+
// Check SQL WASM loading was initiated
|
|
69
|
+
if (promiseSqlWasmLoaded === undefined) {
|
|
70
|
+
throw new Error('SQL WASM loading has not started; ' +
|
|
71
|
+
'did you forget to run initSqlJsWasm() first?');
|
|
72
|
+
}
|
|
63
73
|
|
|
64
74
|
// Check if we have a definition for the display projection (SRS)
|
|
65
75
|
if (!ol_proj_get(displayProjection)) {
|
|
66
|
-
throw new Error(
|
|
76
|
+
throw new Error('Missing requested display projection [' +
|
|
67
77
|
displayProjection +
|
|
68
78
|
'] - can be added beforehand with ol/proj/proj4');
|
|
69
79
|
}
|
|
70
80
|
|
|
81
|
+
// Start OGC GeoPackage load and processing to extract data/SLDs
|
|
82
|
+
var gpkgReadPromise = readRawGpkg(gpkgFile);
|
|
83
|
+
|
|
71
84
|
return Promise.allSettled([promiseSqlWasmLoaded, gpkgReadPromise])
|
|
72
85
|
.then((results) => {
|
|
73
86
|
if (results[0].status === 'rejected') {
|
|
@@ -93,7 +106,7 @@ function loadGpkg(gpkgFile, displayProjection) {
|
|
|
93
106
|
function readRawGpkg(gpkgFile) {
|
|
94
107
|
return new Promise(function(succeed, fail) {
|
|
95
108
|
var oReq = new XMLHttpRequest();
|
|
96
|
-
oReq.responseType =
|
|
109
|
+
oReq.responseType = 'arraybuffer';
|
|
97
110
|
oReq.onreadystatechange = function() {
|
|
98
111
|
|
|
99
112
|
// When request finished and response is ready
|
|
@@ -108,7 +121,7 @@ function readRawGpkg(gpkgFile) {
|
|
|
108
121
|
}
|
|
109
122
|
}
|
|
110
123
|
};
|
|
111
|
-
oReq.open(
|
|
124
|
+
oReq.open('GET', gpkgFile);
|
|
112
125
|
oReq.send();
|
|
113
126
|
});
|
|
114
127
|
}
|
|
@@ -158,9 +171,9 @@ function processGpkgData(loadedGpkgFile, gpkgArrayBuffer, sqlWasm,
|
|
|
158
171
|
while (stmt.step()) {
|
|
159
172
|
let row = stmt.get();
|
|
160
173
|
featureTableNames.push({
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
174
|
+
'table_name': row[0],
|
|
175
|
+
'srs_id': row[1].toString(),
|
|
176
|
+
'geometry_column_name': row[2]
|
|
164
177
|
});
|
|
165
178
|
}
|
|
166
179
|
}
|
|
@@ -177,7 +190,7 @@ function processGpkgData(loadedGpkgFile, gpkgArrayBuffer, sqlWasm,
|
|
|
177
190
|
WHERE gpkg_contents.table_name='layer_styles'
|
|
178
191
|
`);
|
|
179
192
|
if (stmt.step()) {
|
|
180
|
-
stmt = db.prepare(
|
|
193
|
+
stmt = db.prepare('SELECT f_table_name,styleSLD FROM layer_styles');
|
|
181
194
|
while (stmt.step()) {
|
|
182
195
|
let row = stmt.get();
|
|
183
196
|
sldsFromGpkg[row[0]] = row[1];
|
|
@@ -194,7 +207,7 @@ function processGpkgData(loadedGpkgFile, gpkgArrayBuffer, sqlWasm,
|
|
|
194
207
|
|
|
195
208
|
// Check if we have a definition for the data projection (SRS)
|
|
196
209
|
if (!ol_proj_get(tableDataProjection)) {
|
|
197
|
-
throw new Error(
|
|
210
|
+
throw new Error('Missing data projection [' +
|
|
198
211
|
tableDataProjection + '] for table "' + table_name +
|
|
199
212
|
'" - can be added beforehand with ol/proj/proj4');
|
|
200
213
|
}
|
|
@@ -226,7 +239,7 @@ function processGpkgData(loadedGpkgFile, gpkgArrayBuffer, sqlWasm,
|
|
|
226
239
|
}
|
|
227
240
|
|
|
228
241
|
// For information only, save details of original projection (SRS)
|
|
229
|
-
vectorSource.setProperties({
|
|
242
|
+
vectorSource.setProperties({'origProjection': tableDataProjection});
|
|
230
243
|
dataFromGpkg[table_name] = vectorSource;
|
|
231
244
|
}
|
|
232
245
|
/*
|
|
@@ -263,7 +276,7 @@ function parseGpkgGeom(gpkgBinGeom) {
|
|
|
263
276
|
envelopeSize = 64;
|
|
264
277
|
break;
|
|
265
278
|
default:
|
|
266
|
-
throw new Error(
|
|
279
|
+
throw new Error('Invalid geometry envelope size flag in GeoPackage');
|
|
267
280
|
}
|
|
268
281
|
/*
|
|
269
282
|
// Extract SRS (EPSG code)
|
|
@@ -281,11 +294,11 @@ function parseGpkgGeom(gpkgBinGeom) {
|
|
|
281
294
|
// DEBUG: display other properties of the feature
|
|
282
295
|
console.log('gpkgBinGeom Header: ' + (littleEndian ? 'Little' : 'Big')
|
|
283
296
|
+ ' Endian');
|
|
284
|
-
console.log(
|
|
285
|
-
console.log(
|
|
286
|
-
console.log(
|
|
287
|
-
console.log(
|
|
288
|
-
console.log(
|
|
297
|
+
console.log('gpkgBinGeom Magic: 0x${gpkgBinGeom[0].toString(16)}${gpkgBinGeom[1].toString(16)}');
|
|
298
|
+
console.log('gpkgBinGeom Version:', gpkgBinGeom[2]);
|
|
299
|
+
console.log('gpkgBinGeom Flags:', flags);
|
|
300
|
+
console.log('gpkgBinGeom srs_id:', srsId);
|
|
301
|
+
console.log('gpkgBinGeom envelope size (bytes):', envelopeSize);
|
|
289
302
|
*/
|
|
290
303
|
// Extract WKB which starts after variable-size "envelope" field
|
|
291
304
|
var wkbOffset = envelopeSize + 8;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ol-load-geopackage",
|
|
3
|
-
"version": "2.0
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "Loads OGC GeoPackage vector data tables into OpenLayers Vector Sources",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist/",
|
|
@@ -24,8 +24,7 @@
|
|
|
24
24
|
"license": "ISC",
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"ol": ">=6.7.0",
|
|
27
|
-
"sql.js": "^1.6.1"
|
|
28
|
-
"util": "^0.12.4"
|
|
27
|
+
"sql.js": "^1.6.1"
|
|
29
28
|
},
|
|
30
29
|
"repository": {
|
|
31
30
|
"type": "git",
|