-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
111 lines (103 loc) · 4.77 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
var L = require('leaflet');
const Promise = require('promise-polyfill');
if (!window.Promise) {
window.Promise = Promise;
}
var Mustache = require('mustache')
var JQueryStatic = require('jquery')
let geoPackage
let tableLayers
let featureLayers
let imageOverlay
let tableInfos
//const { GeoPackageManager, setSqljsWasmLocateFile } = gp.GeoPackage;
const { GeoPackageManager, setSqljsWasmLocateFile } = window.GeoPackage;
const geopackageMap = L.map('map').setView([38.6258, -90.189933], 14);
L.tileLayer('http://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}.png', {
attribution:
'Source: Esri, DigitalGlobe, GeoEye, Earthstar Geographics, CNES/Airbus DS, USDA, USGS, AeroGRID, IGN, and the GIS User Community. OpenStreetMap.',
}).addTo(geopackageMap);
const gp = require('@ngageoint/geopackage/dist/geopackage.min')
gp.setSqljsWasmLocateFile("https://unpkg.com/@ngageoint/[email protected]/dist/sql-wasm.wasm")
window.GeoPackage.setSqljsWasmLocateFile("https://unpkg.com/@ngageoint/[email protected]/dist/sql-wasm.wasm")
function loadByteArray(array) {
GeoPackageManager.open(array).then(geoPackage => {
// Now you can operate on the GeoPackage
// Get the tile table names
const tileTableNames = geoPackage.getTileTables();
const tileDao = geoPackage.getTileDao(tileTableNames[0]); // We know we have one tile layer, loop if you have more.
const maxZoom = tileDao.maxWebMapZoom;
const minZoom = tileDao.minWebMapZoom;
const tableLayer = new L.GridLayer({ noWrap: true, minZoom: minZoom, maxZoom: maxZoom });
tableLayer.createTile = function (tilePoint, done) {
const canvas = L.DomUtil.create('canvas', 'leaflet-tile');
const size = this.getTileSize();
canvas.width = size.x;
canvas.height = size.y;
let error = null;
setTimeout(function () {
console.time('Draw tile ' + tilePoint.x + ', ' + tilePoint.y + ' zoom: ' + tilePoint.z);
geoPackage
.xyzTile(tileTableNames[0], tilePoint.x, tilePoint.y, tilePoint.z, size.x, size.y, canvas)
.catch(err => {
error = err;
})
.finally(() => {
console.timeEnd('Draw tile ' + tilePoint.x + ', ' + tilePoint.y + ' zoom: ' + tilePoint.z);
done(error, canvas);
});
}, 0);
return canvas;
};
geopackageMap.addLayer(tableLayer);
tableLayer.bringToFront();
const featureTableNames = geoPackage.getFeatureTables();
featureTableNames.forEach(featureTable => {
console.log('featureTable: ' + featureTable);
const featureDao = geoPackage.getFeatureDao(featureTable);
const info = geoPackage.getInfoForTable(featureDao);
// query for all features
const iterator = featureDao.queryForEach();
for (const row of iterator) {
const feature = featureDao.getRow(row);
const geometry = feature.geometry;
if (geometry) {
// Make the information into something we can display on the map with leaflet
const geom = geometry.geometry;
const geoJson = geom.toGeoJSON();
geoJson.properties = {};
geoJson.properties['table_name'] = featureTable;
// map the values from the feature table into GeoJSON properties we can use to style the map and show a popup
for (const key in feature.values) {
if (feature.values.hasOwnProperty(key) && key != feature.geometryColumn.name) {
const column = info.columnMap[key];
geoJson.properties[column.displayName] = feature.values[key];
}
}
// eslint-disable-next-line @typescript-eslint/no-use-before-define
geojsonLayer.addData(geoJson);
}
}
});
});
const geojsonLayer = L.geoJson([], {
style: function (feature) {
// Style the polygons
return {
color: '#000',
weight: 2,
opacity: 1,
fillColor: '#093',
};
}
}).addTo(geopackageMap);
}
function loadGeoPackage(url) {
console.log('In loadGeoPackage')
fetch(url).then(res => res.arrayBuffer()).then(arrayBuffer => {
var uint = new Uint8Array(arrayBuffer)
console.log(uint)
loadByteArray(uint)
})
}
loadGeoPackage('https://github.com/ngageoint/GeoPackage/blob/master/docs/examples/rivers.gpkg')