ol-load-geopackage 2.0.0 → 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 +19 -14
- 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
|
@@ -10,11 +10,11 @@ The current version was tested with OpenLayers 10.7, but should work with OpenLa
|
|
|
10
10
|
|
|
11
11
|
Each example (in the [GitHub repository](https://github.com/richard-thomas/ol-load-geopackage)) is presented as HTML/JavaScript source code and as directly viewable web pages (built using both [Vite](https://vite.dev/) and the [Webpack](https://webpack.js.org/) module bundler).
|
|
12
12
|
|
|
13
|
-
- Basic Example: web page: [Vite](https://richard-thomas.github.io/ol-load-geopackage/examples-vite/basic_example.html), [Webpack](https://richard-thomas.github.io/ol-load-geopackage/examples/dist/basic_example.html) (sources: [Vite HTML](https://github.com/richard-thomas/ol-load-geopackage/tree/master/examples-vite/basic_example.html), [Webpack HTML](https://github.com/richard-thomas/ol-load-geopackage/tree/master/examples/dist/basic_example.html),
|
|
13
|
+
- Basic Example: web page: [Vite](https://richard-thomas.github.io/ol-load-geopackage/examples-vite/dist/basic_example.html), [Webpack](https://richard-thomas.github.io/ol-load-geopackage/examples/dist/basic_example.html) (sources: [Vite HTML](https://github.com/richard-thomas/ol-load-geopackage/tree/master/examples-vite/basic_example.html), [Webpack HTML](https://github.com/richard-thomas/ol-load-geopackage/tree/master/examples/dist/basic_example.html),
|
|
14
14
|
[JavaScript (common)](https://github.com/richard-thomas/ol-load-geopackage/tree/master/examples/src/basic_example.js))
|
|
15
15
|
- Loads vector tables and associated QGIS "layer_styles" SLD XML strings from an OGC GeoPackage and render all tables as layers on an OpenLayers map. Displays details of package contents.
|
|
16
16
|
|
|
17
|
-
- Proj4 Example: web page: [Vite](https://richard-thomas.github.io/ol-load-geopackage/examples-vite/proj4_example.html), [Webpack](https://richard-thomas.github.io/ol-load-geopackage/examples/dist/proj4_example.html) (sources: [Vite HTML](https://github.com/richard-thomas/ol-load-geopackage/tree/master/examples-vite/proj4_example.html), [Webpack HTML](https://github.com/richard-thomas/ol-load-geopackage/tree/master/examples/dist/proj4_example.html),
|
|
17
|
+
- Proj4 Example: web page: [Vite](https://richard-thomas.github.io/ol-load-geopackage/examples-vite/dist/proj4_example.html), [Webpack](https://richard-thomas.github.io/ol-load-geopackage/examples/dist/proj4_example.html) (sources: [Vite HTML](https://github.com/richard-thomas/ol-load-geopackage/tree/master/examples-vite/proj4_example.html), [Webpack HTML](https://github.com/richard-thomas/ol-load-geopackage/tree/master/examples/dist/proj4_example.html),
|
|
18
18
|
[JavaScript (common)](https://github.com/richard-thomas/ol-load-geopackage/tree/master/examples/src/proj4_example.js))
|
|
19
19
|
- Used in conjunction with Proj4js module to enable additional projections to those built in to OpenLayers. These other projections can be for the input source data and/or the output display projection. Also demonstrates loading required sql.js WebAssembly binary (WASM) from an external Content Delivery Network (CDN) site.
|
|
20
20
|
|
|
@@ -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
|
|
|
@@ -46,8 +46,13 @@ This package must be imported as a module - it is not designed to be loaded dire
|
|
|
46
46
|
```javascript
|
|
47
47
|
import { initSqlJsWasm, loadGpkg } from 'ol-load-geopackage';
|
|
48
48
|
|
|
49
|
-
initSqlJsWasm();
|
|
50
|
-
var gpkgPromise
|
|
49
|
+
initSqlJsWasm('.');
|
|
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,10 +64,10 @@ 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
|
-
Note that the _initSqlJsWasm()_ statement will start the asynchronous loading of the required sql.js WebAssembly binary file sql-wasm.wasm (from the
|
|
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.
|
|
66
71
|
|
|
67
72
|
### Building with Webpack
|
|
68
73
|
|
|
@@ -99,13 +104,13 @@ npm install
|
|
|
99
104
|
npm run-script sql-install
|
|
100
105
|
npm run-script build
|
|
101
106
|
```
|
|
102
|
-
|
|
107
|
+
Although you can then test the output code placed in the dist folder using the python HTTP server method (as in the Webpack example), it is easier to use Vite's built in HTTP server with:
|
|
103
108
|
|
|
104
109
|
```bash
|
|
105
110
|
npm run-script preview
|
|
106
111
|
```
|
|
107
112
|
|
|
108
|
-
The Vite dev
|
|
113
|
+
The Vite dev server can be used to automatically re-build, act as a webhost and trigger the browser to reload every time the code changes. The following script commands (defined in [package.json](https://github.com/richard-thomas/ol-load-geopackage/tree/master/examples-vite/package.json)) will start the dev server at a top level _index.html_ file which has links to the 2 examples:
|
|
109
114
|
|
|
110
115
|
```bash
|
|
111
116
|
npm run-script dev
|
|
@@ -115,9 +120,9 @@ npm run-script dev
|
|
|
115
120
|
|
|
116
121
|
The JavaScript module has 3 exported functions/constants which are described in the separate [API Specification](API.md):
|
|
117
122
|
|
|
118
|
-
- [initSqlJsWasm()](#initsqljswasmsqljswasmdir) - Initialisation: start loading of required sql.js WASM file
|
|
119
|
-
- [loadGpkg()](#loadgpkggpkgfile-displayprojection) - start loading and data extraction of GeoPackage
|
|
120
|
-
- [sql_js_version](#sql_js_version) - NPM version number of underlying sql.js module
|
|
123
|
+
- [initSqlJsWasm()](API.md#initsqljswasmsqljswasmdir) - Initialisation: start loading of required sql.js WASM file
|
|
124
|
+
- [loadGpkg()](API.md#loadgpkggpkgfile-displayprojection) - start loading and data extraction of GeoPackage
|
|
125
|
+
- [sql_js_version](API.md#sql_js_version) - NPM version number of underlying sql.js module
|
|
121
126
|
|
|
122
127
|
## Migrating from ol-load-geopackage v1.x.x
|
|
123
128
|
|
|
@@ -126,11 +131,11 @@ In v1.x.x the sql.js WASM file (sql-wasm.wasm) was implicitly loaded from the cu
|
|
|
126
131
|
```javascript
|
|
127
132
|
import loadGpkg from 'ol-load-geopackage';
|
|
128
133
|
```
|
|
129
|
-
From v2.0.0 the WASM must be explicitly loaded by invoking the new [initSqlJsWasm()](#initsqljswasmsqljswasmdir) function, with the sql-wasm.wasm file placed in the root folder if not specified as a parameter:
|
|
134
|
+
From v2.0.0 the WASM must be explicitly loaded by invoking the new [initSqlJsWasm()](API.md#initsqljswasmsqljswasmdir) function, with the sql-wasm.wasm file placed in the root folder if not specified as a parameter. In this example, it is placed in the current folder to mimic v1.x.x behaviour:
|
|
130
135
|
|
|
131
136
|
```javascript
|
|
132
137
|
import { initSqlJsWasm, loadGpkg } from 'ol-load-geopackage';
|
|
133
|
-
initSqlJsWasm();
|
|
138
|
+
initSqlJsWasm('.');
|
|
134
139
|
```
|
|
135
140
|
|
|
136
141
|
## Contributions
|
|
@@ -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.
|
|
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",
|