leaflet-geopackage icon indicating copy to clipboard operation
leaflet-geopackage copied to clipboard

load event needed

Open ghost opened this issue 2 years ago • 2 comments

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.

ghost avatar Sep 13 '23 12:09 ghost

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

danielbarela avatar Sep 13 '23 20:09 danielbarela

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/[email protected]/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);
  }

ghost avatar Sep 19 '23 12:09 ghost