Skip to content

Commit

Permalink
Fix parsing of parking capcity for wheelchair users
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed Nov 5, 2024
1 parent 39c0aac commit ed33630
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class DebugStyleSpec {
private static final String MAGENTA = "#f21d52";
private static final String BRIGHT_GREEN = "#22DD9E";
private static final String DARK_GREEN = "#136b04";
private static final String RED = "#fc0f2a";
private static final String PURPLE = "#BC55F2";
private static final String BLACK = "#140d0e";

Expand All @@ -71,6 +72,7 @@ public class DebugStyleSpec {
private static final String STOPS_GROUP = "Stops";
private static final String VERTICES_GROUP = "Vertices";
private static final String TRAVERSAL_PERMISSIONS_GROUP = "Traversal permissions";
private static final String WHEELCHAIR_GROUP = "Wheelchair accessibility";

static StyleSpec build(
VectorSourceLayer regularStops,
Expand All @@ -90,6 +92,7 @@ static StyleSpec build(
allSources,
ListUtils.combine(
List.of(StyleBuilder.ofId("background").typeRaster().source(BACKGROUND_SOURCE).minZoom(0)),
wheelchair(edges),
traversalPermissions(edges),
edges(edges),
vertices(vertices),
Expand Down Expand Up @@ -252,6 +255,35 @@ private static List<StyleBuilder> traversalPermissions(VectorSourceLayer edges)
return ListUtils.combine(permissionStyles, List.of(textStyle));
}

private static List<StyleBuilder> wheelchair(VectorSourceLayer edges) {
return List.of(
StyleBuilder
.ofId("wheelchair-accessible")
.vectorSourceLayer(edges)
.group(WHEELCHAIR_GROUP)
.typeLine()
.lineColor(DARK_GREEN)
.booleanFilter("wheelchairAccessible", true)
.lineWidth(LINE_WIDTH)
.lineOffset(LINE_OFFSET)
.minZoom(6)
.maxZoom(MAX_ZOOM)
.intiallyHidden(),
StyleBuilder
.ofId("wheelchair-inaccessible")
.vectorSourceLayer(edges)
.group(WHEELCHAIR_GROUP)
.typeLine()
.lineColor(RED)
.booleanFilter("wheelchairAccessible", false)
.lineWidth(LINE_WIDTH)
.lineOffset(LINE_OFFSET)
.minZoom(6)
.maxZoom(MAX_ZOOM)
.intiallyHidden()
);
}

private static String permissionColor(StreetTraversalPermission p) {
return switch (p) {
case NONE -> "#000";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -29,7 +30,7 @@ public class StyleBuilder {
private final Map<String, Object> layout = new LinkedHashMap<>();
private final Map<String, Object> metadata = new LinkedHashMap<>();
private final Map<String, Object> line = new LinkedHashMap<>();
private List<String> filter = List.of();
private List<Object> filter = List.of();

public static StyleBuilder ofId(String id) {
return new StyleBuilder(id);
Expand Down Expand Up @@ -227,6 +228,11 @@ public final StyleBuilder permissionsFilter(StreetTraversalPermission p) {
return this;
}

public final StyleBuilder booleanFilter(String propertyName, boolean value) {
filter = List.of("==", propertyName, value);
return this;
}

/**
* Only apply the style to the given vertices.
*/
Expand Down Expand Up @@ -257,7 +263,7 @@ public JsonNode toJson() {

private StyleBuilder filterClasses(Class... classToFilter) {
var clazzes = Arrays.stream(classToFilter).map(Class::getSimpleName).toList();
filter = ListUtils.combine(List.of("in", "class"), clazzes);
filter = new ArrayList<>(ListUtils.combine(List.of("in", "class"), clazzes));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ private OptionalInt parseCapacity(OsmWithTags element) {
}

private OptionalInt parseCapacity(OsmWithTags element, String capacityTag) {
return element.getTagAsInt(
return element.parseIntOrBoolean(
capacityTag,
v -> issueStore.add(new InvalidVehicleParkingCapacity(element, v))
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ protected Collection<KeyValue> map(Edge input) {
private static List<KeyValue> mapStreetEdge(StreetEdge se) {
var props = Lists.newArrayList(
kv("permission", se.getPermission().toString()),
kv("bicycleSafetyFactor", roundTo2Decimals(se.getBicycleSafetyFactor()))
kv("bicycleSafetyFactor", roundTo2Decimals(se.getBicycleSafetyFactor())),
kv("wheelchairAccessible", se.isWheelchairAccessible())
);
if (se.hasBogusName()) {
props.addFirst(kv("name", "%s (generated)".formatted(se.getName().toString())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class OsmWithTags {

private static final Set<String> LEVEL_TAGS = Set.of("level", "layer");
private static final Set<String> DEFAULT_LEVEL = Set.of("0");
private static final Consumer<String> NO_OP = x -> {};

/* To save memory this is only created when an entity actually has tags. */
private Map<String, String> tags;
Expand Down Expand Up @@ -220,6 +221,27 @@ public OptionalInt getTagAsInt(String tag, Consumer<String> errorHandler) {
return OptionalInt.empty();
}

/**
* Some tags are allowed to have values like 55, "true" or "false".
*/
public OptionalInt parseIntOrBoolean(String tag, Consumer<String> errorHandler) {
var maybeInt = getTagAsInt(tag, NO_OP);
if (maybeInt.isPresent()) {
return maybeInt;
} else {
if (isTagTrue(tag)) {
return OptionalInt.of(1);
} else if (isTagFalse(tag)) {
return OptionalInt.of(0);
} else if (hasTag(tag)) {
errorHandler.accept(getTag(tag));
return OptionalInt.empty();
} else {
return OptionalInt.empty();
}
}
}

/**
* Checks is a tag contains the specified value.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.OptionalInt;
import java.util.Set;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.opentripplanner.osm.wayproperty.specifier.WayTestData;

public class OsmWithTagsTest {
Expand Down Expand Up @@ -272,4 +276,26 @@ void fallbackName() {
var namedTunnel = WayTestData.carTunnel();
assertFalse(namedTunnel.hasNoName());
}

private static List<Arguments> parseIntOrBooleanCases() {
return List.of(
Arguments.of("true", OptionalInt.of(1)),
Arguments.of("yes", OptionalInt.of(1)),
Arguments.of("no", OptionalInt.of(0)),
Arguments.of("false", OptionalInt.of(0)),
Arguments.of("0", OptionalInt.of(0)),
Arguments.of("12", OptionalInt.of(12)),
Arguments.of("", OptionalInt.empty())
);
}

@ParameterizedTest
@MethodSource("parseIntOrBooleanCases")
void parseIntOrBoolean(String value, OptionalInt expected) {
var way = new OsmWithTags();
var key = "capacity:disabled";
way.addTag(key, value);
var maybeInt = way.parseIntOrBoolean(key, i -> {});
assertEquals(expected, maybeInt);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,104 @@
"group" : "Other"
}
},
{
"id" : "wheelchair-accessible",
"source" : "vectorSource",
"source-layer" : "edges",
"type" : "line",
"minzoom" : 6,
"maxzoom" : 23,
"paint" : {
"line-color" : "#136b04",
"line-width" : [
"interpolate",
[
"linear"
],
[
"zoom"
],
13,
0.2,
23,
8.0
],
"line-offset" : [
"interpolate",
[
"linear"
],
[
"zoom"
],
13,
0.3,
23,
6.0
]
},
"filter" : [
"==",
"wheelchairAccessible",
true
],
"layout" : {
"line-cap" : "round",
"visibility" : "none"
},
"metadata" : {
"group" : "Wheelchair accessibility"
}
},
{
"id" : "wheelchair-inaccessible",
"source" : "vectorSource",
"source-layer" : "edges",
"type" : "line",
"minzoom" : 6,
"maxzoom" : 23,
"paint" : {
"line-color" : "#fc0f2a",
"line-width" : [
"interpolate",
[
"linear"
],
[
"zoom"
],
13,
0.2,
23,
8.0
],
"line-offset" : [
"interpolate",
[
"linear"
],
[
"zoom"
],
13,
0.3,
23,
6.0
]
},
"filter" : [
"==",
"wheelchairAccessible",
false
],
"layout" : {
"line-cap" : "round",
"visibility" : "none"
},
"metadata" : {
"group" : "Wheelchair accessibility"
}
},
{
"id" : "NONE",
"source" : "vectorSource",
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/MapView/GeometryPropertyPopup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function GeometryPropertyPopup({
{Object.entries(properties).map(([key, value]) => (
<tr key={key}>
<th scope="row">{key}</th>
<td>{value}</td>
<td>{String(value)}</td>
</tr>
))}
</tbody>
Expand Down

0 comments on commit ed33630

Please sign in to comment.