ngageoint / leaflet-geopackage

Leaflet GeoPackage
http://ngageoint.github.io/leaflet-geopackage/
19 stars 7 forks source link

load event needed #3

Open ghost opened 1 year ago

ghost commented 1 year ago

How about a 'load' event like in other async leaflet layers like TileLayer or ImageOverlay? I need to know when the GeoPackage has loaded completely so I can read the SQLite DB to read styles from a table.

danielbarela commented 1 year ago

We would be happy to accept a pull request from you.

ghost commented 1 year ago

I solved my problem by preloading the GeoPackage, caching it in geoPackages and then reading the style before creating the layer.

function loadGeoPackage(url: string, then_func: (gpkg: GeoPackage)=>void): void {
  let gpkg: GeoPackage;
  if (!(gpkg = geoPackages[url])) {
    setSqljsWasmLocateFile(((filename: string) => 'https://unpkg.com/@ngageoint/geopackage@4.2.3/dist/' + filename));
    const xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.responseType = 'arraybuffer';
    xhr.onload = function() {
      GeoPackageAPI.open(new Uint8Array(this.response)).then(function(gp) {
        geoPackages[url] = gp;
        then_func(gp);
      });
    };
    xhr.send();
    }
    else
      then_func(gpkg);
  }