Skip to content

Commit

Permalink
Dedup code
Browse files Browse the repository at this point in the history
  • Loading branch information
conbrad committed Feb 3, 2025
1 parent 7e82ca6 commit 28741aa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 32 deletions.
2 changes: 1 addition & 1 deletion mobile/asa-go/src/FBAMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { fireZoneExtentsMap } from "@/fireZoneUnitExtents";
import { CENTER_OF_BC } from "@/utils/constants";
import { extentsMap } from "@/fireCentreExtents";
import { PMTilesFileVectorSource } from "@/utils/pmtilesVectorSource";
import { PMTilesCache } from "@/utils/PMTilesCache";
import { PMTilesCache } from "@/utils/pmtilesCache";
import { Filesystem } from "@capacitor/filesystem";
export const MapContext = React.createContext<Map | null>(null);

Expand Down
52 changes: 21 additions & 31 deletions mobile/asa-go/src/utils/pmtilesVectorSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { createXYZ } from "ol/tilegrid";
import TileState from "ol/TileState";
import { PMTiles } from "pmtiles";
import { MVT } from "ol/format";
import { PMTilesCache } from "@/utils/PMTilesCache";
import { PMTilesCache } from "@/utils/pmtilesCache";
import { DateTime } from "luxon";
import { RunType } from "@/api/fbaAPI";
import { isUndefined } from "lodash";
Expand Down Expand Up @@ -115,27 +115,31 @@ export class PMTilesFileVectorSource extends VectorTileSource {

const pmtiles = await pmtilesCache.loadPMTiles(options.filename);

if (!isUndefined(pmtiles)) {
this.pmtiles_ = pmtiles;
} else {
throw Error("Unable to initialize pmtiles");
}
const header = await this.pmtiles_.getHeader();

this.tileGrid = createXYZ({
maxZoom: header.maxZoom,
minZoom: header.minZoom,
tileSize: 512,
});

this.setTileLoadFunction(this.tileLoadFunction);
this.setState("ready");
await this.initTileGrid(pmtiles);
} catch (error) {
console.error("Error loading PMTiles file:", error);
this.setState("error");
}
}

async initTileGrid(pmtiles?: PMTiles) {
if (!isUndefined(pmtiles)) {
this.pmtiles_ = pmtiles;
} else {
throw Error("Unable to initialize pmtiles");
}
const header = await this.pmtiles_.getHeader();

this.tileGrid = createXYZ({
maxZoom: header.maxZoom,
minZoom: header.minZoom,
tileSize: 512,
});

this.setTileLoadFunction(this.tileLoadFunction);
this.setState("ready");
}

static async createHFILayer(
pmtilesCache: PMTilesCache,
options: HFIPMTilesFileVectorOptions
Expand All @@ -160,21 +164,7 @@ export class PMTilesFileVectorSource extends VectorTileSource {
options.run_date,
options.filename
);
if (!isUndefined(pmtiles)) {
this.pmtiles_ = pmtiles;
} else {
throw Error("Unable to initialize pmtiles");
}
const header = await this.pmtiles_.getHeader();

this.tileGrid = createXYZ({
maxZoom: header.maxZoom,
minZoom: header.minZoom,
tileSize: 512,
});

this.setTileLoadFunction(this.tileLoadFunction);
this.setState("ready");
await this.initTileGrid(pmtiles);
} catch (error) {
console.error("Error loading PMTiles file:", error);
this.setState("error");
Expand Down

0 comments on commit 28741aa

Please sign in to comment.