Skip to content

Commit

Permalink
switched to the new level schema
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Aug 28, 2024
1 parent 87f1bef commit 8cb0f33
Show file tree
Hide file tree
Showing 8 changed files with 332 additions and 154 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"dist/*",
"src/"
],
"version": "0.0.14",
"version": "0.0.15",
"scripts": {
"test": "jest .",
"lint": "eslint src --fix && tsc --noEmit && prettier --write .",
Expand Down
3 changes: 2 additions & 1 deletion src/IndoorLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { default as turfDistance } from "@turf/distance";

import type { Level } from "./Types";

import { bboxCenter, overlap } from "./bbox";
import IndoorMap from "./IndoorMap";
import { filterWithLevel } from "./levelFilter";
import { bboxCenter, overlap } from "./bbox";

type SavedFilter = {
filter: ExpressionSpecification;
Expand Down Expand Up @@ -144,6 +144,7 @@ export default class IndoorLayer {
this._savedFilters.forEach(({ filter, layerId }) => {
this._map.setFilter(layerId, filterFn(filter));
});
console.log(structuredClone(this._map.getStyle()));
}

_updateSelectedMap(indoorMap: IndoorMap | null) {
Expand Down
2 changes: 1 addition & 1 deletion src/MapServerHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { default as turfDistance } from "@turf/distance";
import type { IndoorMapOptions, MapGLWithIndoor } from "./Types";

import addIndoorTo from "./addIndoorTo";
import IndoorMap from "./IndoorMap";
import { bboxContains } from "./bbox";
import IndoorMap from "./IndoorMap";

type RemoteMap = {
indoorMap?: IndoorMap;
Expand Down
18 changes: 8 additions & 10 deletions src/levelFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,32 @@ import type { ExpressionSpecification } from "maplibre-gl";

import type { Level } from "./Types";

export function levelFilters(level: Level): ExpressionSpecification[] {
const levelBetween: ExpressionSpecification = [
export function levelFilters(level: Level): ExpressionSpecification {
return [
"all",
["in", ";", ["get", "level"]],
[
// level=LEVEL;... => if LEVEL <= current_level, we can display
// level=MIN~... => if LEVEL <= current_level, we can display
"<=",
[
"to-number",
["slice", ["get", "level"], 0, ["index-of", ";", ["get", "level"]]],
["slice", ["get", "level"], 0, ["index-of", "~", ["get", "level"]]],
],
level,
],
[
// level=...;LEVEL => if LEVEL >= current_level, we can display
// level=...~MAX => if LEVEL >= current_level, we can display
">=",
[
"to-number",
[
"slice",
["get", "level"],
["+", ["index-of", ";", ["get", "level"]], 1],
["+", ["index-of", "~", ["get", "level"]], 1],
],
],
level,
],
];
return [levelBetween, ["==", ["get", "level"], level.toString()]];
}

export function filterWithLevel(
Expand All @@ -41,9 +39,9 @@ export function filterWithLevel(
return [
"all",
initialFilter,
["any", ["!", ["has", "level"]], ...levelFilters(level)],
["any", ["!", ["has", "level"]], levelFilters(level)],
];
} else {
return ["all", initialFilter, ["any", ...levelFilters(level)]];
return ["all", initialFilter, levelFilters(level)];
}
}
31 changes: 9 additions & 22 deletions src/levelFromGeojson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,18 @@ import type { LevelsRange } from "./Types";
* Extract level from feature
*
* @returns {LevelsRange | null} the level or the range of level.
* @param range e.g. "-1--2","1-2","1-2"
* @param range e.g. "-1~-2"
*/
export function parseLevelRange(range: string): LevelsRange | null {
range = range.replaceAll(" ", "");
const firstIsNegative = range.startsWith("-");
let firstEnd = firstIsNegative
? range.substring(1).indexOf("-")
: range.indexOf("-");
if (firstEnd === -1) {
// e.g "1"
const rangeFloat = parseFloat(range);
if (isNaN(rangeFloat)) return null;
return { max: rangeFloat, min: rangeFloat };
}
if (firstIsNegative) {
firstEnd += 1; // if we add this before, we could not differentiate "-100" from "100-"
}
const secondStart = firstEnd + 1;
const firstFloat = parseFloat(range.substring(0, firstEnd));
const secondFloat = parseFloat(range.substring(secondStart));
if (isNaN(firstFloat) || isNaN(secondFloat)) return null;
return {
max: Math.max(firstFloat, secondFloat),
min: Math.min(firstFloat, secondFloat),
const splitRange = range.split("~");
const parsedRange = {
max: parseFloat(splitRange[1]),
min: parseFloat(splitRange[0]),
};
if (isNaN(parsedRange.min) || isNaN(parsedRange.max)) {
return null;
}
return parsedRange;
}

/**
Expand Down
Loading

0 comments on commit 8cb0f33

Please sign in to comment.