Skip to content

Commit

Permalink
Improve gdal initialization
Browse files Browse the repository at this point in the history
    - Fix BaseUrl issue with lab/lite
    - Load gdal at startup and only once
  • Loading branch information
martinRenou committed Sep 23, 2024
1 parent 1a17911 commit 9fb9a0f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 68 deletions.
7 changes: 4 additions & 3 deletions packages/base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"name": "JupyterGIS contributors"
},
"files": [
"lib/**/*.{d.ts,eot,gif,html,jpg,js,js.map,json,png,svg,woff2,ttf}",
"lib/**/*.{d.ts,eot,gif,html,jpg,js,js.map,json,png,svg,woff2,ttf,data,wasm}",
"style/**/*.{css,js,eot,gif,html,jpg,json,png,svg,woff2,ttf}"
],
"main": "lib/index.js",
Expand All @@ -27,10 +27,11 @@
"url": "https://github.com/geojupyter/jupytergis.git"
},
"scripts": {
"build": "jlpm run build:gallery && tsc -b",
"build": "jlpm run build:gallery && tsc -b && jlpm run cp:gdal",
"build:gallery": "python rasterlayer_gallery_generator.py",
"cp:gdal": "cp ../../node_modules/gdal3.js/dist/package/gdal3WebAssembly.data lib && cp ../../node_modules/gdal3.js/dist/package/gdal3WebAssembly.wasm lib",
"build:prod": "jlpm run clean && jlpm run build",
"build:dev": "tsc -b",
"build:dev": "tsc -b && jlpm run cp:gdal",
"clean": "rimraf tsconfig.tsbuildinfo",
"clean:lib": "rimraf lib tsconfig.tsbuildinfo",
"clean:all": "jlpm run clean:lib",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { faSpinner } from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { IDict } from '@jupytergis/schema';
import { PageConfig } from '@jupyterlab/coreutils';
import { Button } from '@jupyterlab/ui-components';
import initGdalJs from 'gdal3.js';
import { ReadonlyPartialJSONObject } from '@lumino/coreutils';
import { ExpressionValue } from 'ol/expr/expression';
import React, { useEffect, useRef, useState } from 'react';
import { ISymbologyDialogProps } from '../../symbologyDialog';
import BandRow from './BandRow';
import StopRow from './StopRow';
import { getGdal } from '../../../gdal';

export interface IStopRow {
value: number;
Expand Down Expand Up @@ -116,17 +116,13 @@ const SingleBandPseudoColor = ({

let tifData;

const baseUrl = PageConfig.getBaseUrl();

const tifDataState = (await state.fetch(layerId)) as string;
if (tifDataState) {
tifData = JSON.parse(tifDataState);
const layerState = await state.fetch(`jupytergis:${layerId}`);
if (layerState) {
tifData = JSON.parse(
(layerState as ReadonlyPartialJSONObject).tifData as string
);
} else {
//! This takes so long, maybe do when adding source instead
const Gdal = await initGdalJs({
path: baseUrl + '/extensions/@jupytergis/jupytergis-core/static',
useWorker: false
});
const Gdal = await getGdal();

const fileData = await fetch(sourceInfo.url);
const file = new File([await fileData.blob()], 'loaded.tif');
Expand Down
18 changes: 18 additions & 0 deletions packages/base/src/gdal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import initGdalJs from 'gdal3.js';

export async function getGdal() {
const dataurl = new URL('./gdal3WebAssembly.data', import.meta.url);
const wasmurl = new URL('./gdal3WebAssembly.wasm', import.meta.url);

// TODO Pass gdal JS too and run gdal in a worker?
return await initGdalJs({
paths: {
wasm: wasmurl.href,
data: dataurl.href
},
useWorker: false
});
}

// Early load gdal
getGdal();
52 changes: 0 additions & 52 deletions python/jupytergis_core/extension.webpack.config.js

This file was deleted.

1 change: 0 additions & 1 deletion python/jupytergis_core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@
},
"extension": true,
"outputDir": "jupytergis_core/labextension",
"webpackConfig": "./extension.webpack.config.js",
"sharedPackages": {
"@jupytergis/base": {
"singleton": true,
Expand Down

0 comments on commit 9fb9a0f

Please sign in to comment.