Skip to content

Commit

Permalink
update level filterting
Browse files Browse the repository at this point in the history
  • Loading branch information
djfhe committed Jul 18, 2024
1 parent 7538969 commit 25d2194
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 82 deletions.
27 changes: 0 additions & 27 deletions exe/backend/src/http_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,6 @@ struct http_server::impl {
void handle_graph(web_server::http_req_t const& req,
web_server::http_res_cb_t const& cb) {
auto const query = boost::json::parse(req.body()).as_object();
auto const level = query.contains("level")
? to_level(query.at("level").to_number<float>())
: level_t::invalid();
auto const waypoints = query.at("waypoints").as_array();
auto const profile = get_search_profile_from_request(query);
auto const min = point::from_latlng(
Expand All @@ -231,31 +228,7 @@ struct http_server::impl {

auto gj = geojson_writer{.w_ = w_};
l_.find(min, max, [&](way_idx_t const w) {
if (level == level_t::invalid()) {
gj.write_way(w);
return;
}

auto const way_prop = w_.r_->way_properties_[w];
if (way_prop.is_elevator()) {
auto const n = w_.r_->way_nodes_[w][0];
auto const np = w_.r_->node_properties_[n];
if (np.is_multi_level()) {
auto has_level = false;
for_each_set_bit(
foot<true>::get_elevator_multi_levels(*w_.r_, n),
[&](auto&& bit) { has_level |= (level == level_t{bit}); });
if (has_level) {
gj.write_way(w);
return;
}
}
}

if (way_prop.from_level() == level || way_prop.to_level() == level) {
gj.write_way(w);
return;
}
});

switch (profile) {
Expand Down
8 changes: 4 additions & 4 deletions include/osr/geojson.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct geojson_writer {
{"oneway_car", p.is_oneway_car()},
{"oneway_bike", p.is_oneway_bike()},
{"max_speed", p.max_speed_km_per_h()},
{"level", to_float(level_t{p.from_level()})},
{"from_level", to_float(level_t{p.from_level()})},
{"to_level", to_float(level_t{p.to_level()})},
{"is_elevator", p.is_elevator()},
{"is_steps", p.is_steps()},
Expand Down Expand Up @@ -101,9 +101,9 @@ struct geojson_writer {
}
});

auto levels = std::stringstream{};
auto levels = std::vector<float>();
foot<true>::for_each_elevator_level(
*w_.r_, n, [&](auto&& l) { levels << to_float(level_t{l}) << " "; });
*w_.r_, n, [&](auto&& l) { levels.push_back(to_float(level_t{l})); });

auto properties = boost::json::object{
{"osm_node_id", to_idx(w_.node_to_osm_[n])},
Expand All @@ -116,7 +116,7 @@ struct geojson_writer {
{"is_elevator", p.is_elevator()},
{"is_parking", p.is_parking()},
{"multi_level", p.is_multi_level()},
{"levels", levels.str()},
{"levels", boost::json::array(levels.begin(), levels.end())},
{"ways", fmt::format("{}", w_.r_->node_ways_[n] |
std::views::transform([&](auto&& w) {
return w_.way_osm_idx_[w];
Expand Down
83 changes: 43 additions & 40 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@
</div>
<div id="journey-info"></div>
<script type="module">
import {style} from "./style.js";
import { style, setStyleLayerFilters } from "./style.js";

const start = [8.663205312233744, 50.106847864540164];
const destination = [8.665205312233744, 50.106847864540164];
Expand Down Expand Up @@ -552,7 +552,6 @@
'Content-Type': 'application/json'
},
body: JSON.stringify({
level: currentLevel,
waypoints: bounds.toArray().flat(),
profile: query.profile,
})
Expand Down Expand Up @@ -580,12 +579,49 @@

const levelsDom = document.getElementById('levels');

const updateRenderedMapLevels = () => {
setStyleLayerFilters(map, currentLevel);
const filter = [
'any',
['==', 'level', currentLevel],
]
map.setFilter('path-outline', [
'any',
['==', 'level', currentLevel],
])
map.setFilter('path', [
'any',
['==', 'level', currentLevel],
])
map.setFilter('graph-node', [
'all',
['in', currentLevel, ['get', 'levels']]
])
map.setFilter('graph-geometry', [
'all',
['==', 'type', 'geometry'],
[
'any',
['==', 'from_level', currentLevel],
['==', 'to_level', currentLevel],
],
])
map.setFilter('graph-edge', [
'all',
['==', 'type', 'edge'],
[
'any',
['==', 'from_level', currentLevel],
['==', 'to_level', currentLevel],
],
])

}

const setLevels = async (levels) => {
if (levels.length === 0 && currentLevel !== 0) {
currentLevel = 0;
style(map, 0);
await addRouting();
await toggleGraph(false);
updateRenderedMapLevels();
}

levelsDom.innerHTML = '';
Expand All @@ -606,9 +642,7 @@
currentLeveLButton?.classList.remove('active');
currentLeveLButton = levelButton;
levelButton.classList.add('active');
style(map, l);
await addRouting();
await toggleGraph(false);
updateRenderedMapLevels();
};

levelsDom.appendChild(levelButton);
Expand All @@ -631,11 +665,6 @@
'line-join': 'round',
'line-cap': 'round',
},
'filter': [
'any',
['!has', 'level'],
['==', 'level', currentLevel]
],
'paint': {
'line-color': ['interpolate-lab', ['linear'], ['/', ['number', ['get', 'cost'], 0], ['max', ['number', ['get', 'distance'], 1], 1]], 0, 'green', 0.25, 'darkorange', 0.5, 'chocolate', 1, 'darkred'],
'line-width': 12,
Expand All @@ -650,11 +679,6 @@
'line-join': 'round',
'line-cap': 'round',
},
'filter': [
'any',
['!has', 'level'],
['==', 'level', currentLevel]
],
'paint': {
'line-color': ['match', ['get', 'type', ['get', 'to_node']], 'foot', 'lightcyan', 'bike', 'cyan', 'car', 'skyblue', 'lightcyan'],
'line-width': 5,
Expand All @@ -675,15 +699,6 @@
'id': 'graph-geometry',
'type': 'line',
'source': 'graph',
'filter': [
'all',
['==', 'type', 'geometry'],
[
'any',
['!has', 'level'],
['==', 'level', currentLevel]
]
],
'layout': {
'line-join': 'round',
'line-cap': 'round',
Expand All @@ -697,15 +712,6 @@
'id': 'graph-edge',
'type': 'line',
'source': 'graph',
'filter': [
'all',
['==', 'type', 'edge'],
[
'any',
['!has', 'level'],
['==', 'level', currentLevel]
]
],
'layout': {
'line-join': 'round',
'line-cap': 'round',
Expand All @@ -718,10 +724,6 @@
'id': 'graph-node',
'type': 'circle',
'source': 'graph',
'filter': [
'all',
['==', '$type', 'Point']
],
'paint': {
'circle-radius': 6,
}
Expand Down Expand Up @@ -838,6 +840,7 @@
addGraph();
addRouting();
await updateRoute();
updateRenderedMapLevels();
setGraphNodeHighlighting();
setGraphEdgeHiglighting();
});
Expand Down
84 changes: 73 additions & 11 deletions web/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,15 @@ const sport_outline = "#b3e998";
const building = "#ded7d3";
const building_outline = "#cfc8c4";

export const style = (map, level) => {
const matchLevel = (expression) => {
return ["all",
["any",
["!has", "level"],
["==", ["get", "level"], level]
],
expression
];
}

const style = (map, level) => {
const demSource = new mlcontour.DemSource({
url: 'https://s3.amazonaws.com/elevation-tiles-prod/terrarium/{z}/{x}/{y}.png',
encoding: 'terrarium',
maxzoom: 14,
// offload contour line computation to a web worker
worker: true
});

demSource.setupMaplibre(maplibregl);

map.setStyle({
Expand Down Expand Up @@ -749,3 +740,74 @@ export const style = (map, level) => {
);
}
};

function setStyleLayerFilters(map, level) {
const layersWithLevelFilters = [
{
id: "indoor-corridor",
filter: ["all", ["==", "indoor", "corridor"], ["==", "level", level]]
},
{
id: "indoor",
filter: ["all", ["!in", "indoor", "corridor", "wall", "elevator"], ["==", "level", level]]
},
{
id: "indoor-outline",
filter: ["all", ["!in", "indoor", "corridor", "wall", "elevator"], ["==", "level", level]]
},
{
id: "indoor-names",
filter: ["any", ["!has", "level"], ["==", "level", level]]
},
{
id: "landuse-public-transport",
filter: ["all", ["==", "landuse", "public_transport"], ["any", ["!has", "level"], ["==", "level", level]]]
},
{
id: "footway",
filter: [
"all",
["in", "highway", "footway", "track", "cycleway", "path", "unclassified", "service"],
level === 0
? ["any", ["!has", "level"], ["==", "level", level]]
: ["==", "level", level]
]
},
{
id: "steps",
filter: [
"all",
["==", "highway", "steps"],
level === 0
? ["any", ["!has", "from_level"], ["any", ["==", "from_level", level], ["==", "to_level", level]]]
: ["any", ["==", "from_level", level], ["==", "to_level", level]]
]
},
{
id: "indoor-elevator-outline",
filter: ["all", ["==", "indoor", "elevator"], ["<=", "from_level", level], [">=", "to_level", level]]
},
{
id: "indoor-elevator",
filter: ["all", ["==", "indoor", "elevator"], ["<=", "from_level", level], [">=", "to_level", level]]
},
{
id: "indoor-elevator-icon",
filter: ["all", ["==", "indoor", "elevator"], ["<=", "from_level", level], [">=", "to_level", level]]
},
{
id: "rail_secondary",
filter: ["all", ["==", "rail", "secondary"], ["any", ["!has", "level"], ["==", "level", level]]]
},
{
id: "rail_primary",
filter: ["all", ["==", "rail", "primary"], ["any", ["!has", "level"], ["==", "level", level]]]
}
];

layersWithLevelFilters.forEach(layer => {
map.setFilter(layer.id, layer.filter);
});
}

export { style, setStyleLayerFilters }

0 comments on commit 25d2194

Please sign in to comment.