Skip to content

Commit

Permalink
Address initial PR commits
Browse files Browse the repository at this point in the history
  • Loading branch information
Pete-Y-CS committed Jul 28, 2023
1 parent 97c970e commit a95d291
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/lib/browse/InterventionColorSelector.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { get return x as DataDrivenPropertyValueSpecification } from "../../maplibre_helpers";
import { constructMatchExpression } from "../../maplibre_helpers";
import { colorInterventionsBySchema, schemaLegend } from "../../schemas";
import { map } from "../../stores";
import { Legend } from "../common";
Expand All @@ -15,7 +15,7 @@
color = colorInterventionsBySchema("v1");
legendRows = schemaLegend("v1");
} else {
color = get return x as DataDrivenPropertyValueSpecification(
color = constructMatchExpression(
["get", "funding_programme"],
{ ATF2: colors.atf2, ATF3: colors.atf3, ATF4: colors.atf4 },
"grey"
Expand Down
6 changes: 3 additions & 3 deletions src/lib/draw/route/route_tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
isLine,
isPoint,
isPolygon,
getDataDrivenPropertyValueSpecification,
constructMatchExpression,
overwriteCircleLayer,
overwriteLineLayer,
overwritePolygonLayer,
Expand Down Expand Up @@ -53,15 +53,15 @@ export class RouteTool {
id: "route-points",
source,
filter: isPoint,
color: getDataDrivenPropertyValueSpecification(
color: constructMatchExpression(
["get", "type"],
{
hovered: "green",
important: "red",
},
"black"
),
radius: getDataDrivenPropertyValueSpecification(
radius: constructMatchExpression(
["get", "type"],
{ unimportant: circleRadiusPixels / 2.0 },
circleRadiusPixels
Expand Down
4 changes: 2 additions & 2 deletions src/lib/layers/lane_details/IntersectionMarkings.svelte
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<script lang="ts">
import type { GeoJSON } from "geojson";
import { get return x as DataDrivenPropertyValueSpecification } from "../../../maplibre_helpers";
import { constructMatchExpression } from "../../../maplibre_helpers";
import Layer from "./Layer.svelte";
export let gj: GeoJSON;
let style = {
type: "fill",
paint: {
"fill-color": get return x as DataDrivenPropertyValueSpecification(
"fill-color": constructMatchExpression(
["get", "type"],
{
"sidewalk corner": "#CCCCCC",
Expand Down
4 changes: 2 additions & 2 deletions src/lib/layers/lane_details/IntersectionPolygons.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import type { GeoJSON } from "geojson";
import { get return x as DataDrivenPropertyValueSpecification } from "../../../maplibre_helpers";
import { constructMatchExpression } from "../../../maplibre_helpers";
import Layer from "./Layer.svelte";
export let gj: GeoJSON;
Expand All @@ -9,7 +9,7 @@
type: "fill",
filter: ["==", ["get", "type"], "intersection"],
paint: {
"fill-color": get return x as DataDrivenPropertyValueSpecification(
"fill-color": constructMatchExpression(
["get", "intersection_kind"],
{
MapEdge: "#696",
Expand Down
4 changes: 2 additions & 2 deletions src/lib/layers/lane_details/LaneMarkings.svelte
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<script lang="ts">
import type { GeoJSON } from "geojson";
import { get return x as DataDrivenPropertyValueSpecification } from "../../../maplibre_helpers";
import { constructMatchExpression } from "../../../maplibre_helpers";
import Layer from "./Layer.svelte";
export let gj: GeoJSON;
let style = {
type: "fill",
paint: {
"fill-color": get return x as DataDrivenPropertyValueSpecification(
"fill-color": constructMatchExpression(
["get", "type"],
{
"center line": "white",
Expand Down
4 changes: 2 additions & 2 deletions src/lib/layers/lane_details/LanePolygons.svelte
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<script lang="ts">
import type { GeoJSON } from "geojson";
import { get return x as DataDrivenPropertyValueSpecification } from "../../../maplibre_helpers";
import { constructMatchExpression } from "../../../maplibre_helpers";
import Layer from "./Layer.svelte";
export let gj: GeoJSON;
let style = {
type: "fill",
paint: {
"fill-color": get return x as DataDrivenPropertyValueSpecification(
"fill-color": constructMatchExpression(
["get", "type"],
// TODO Generate TS types from osm2streets
{
Expand Down
10 changes: 1 addition & 9 deletions src/maplibre_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,19 +229,11 @@ export function setPrecision(pt: Position): Position {
// Helper for https://maplibre.org/maplibre-style-spec/expressions/#match.
// Gets one feature property, uses a map to match a key to a value, and
// includes a fallback if no keys match.
export function getDataDrivenPropertyValueSpecification<OutputType>(
export function constructMatchExpression<OutputType>(
getter: any[],
map: { [name: string]: OutputType },
fallback: OutputType
): DataDrivenPropertyValueSpecification<OutputType> {
// This looks weird because it is weird. I hate to add a comment but here we are.
// MapLibre's DataDrivenPropertyValueSpecification allows you to have a value which depends on properties
// of an object you are drawing, so you could see for a route which funding round it belonged to, for an
// ATIP example. Their specification is an array containing ordered elements of any kind. The first is how
// you want to match, then, an array defining the route to property you want to look at in your object.
// Key, value pairs are added directly to the array, where key is what you want the property in the object being tested
// to equal or not, and value is what you want to return. So in our case it might be ATF3 (a funding round)
// and red (a color to use for drawing the object). Finally there is a fallback value (e.g. grey) if nothing matches. Nonsense.
let x: any[] = ["match", getter];
for (let [key, value] of Object.entries(map)) {
x.push(key);
Expand Down
6 changes: 3 additions & 3 deletions src/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { DataDrivenPropertyValueSpecification } from "maplibre-gl";
import { colors } from "./colors";
import { getDataDrivenPropertyValueSpecification } from "./maplibre_helpers";
import { constructMatchExpression } from "./maplibre_helpers";
import type { Schema } from "./types";

export function schemaTitle(schema: Schema): string {
Expand Down Expand Up @@ -39,7 +39,7 @@ export function colorInterventionsBySchema(
schema: Schema
): DataDrivenPropertyValueSpecification<string> {
if (schema == "planning") {
return getDataDrivenPropertyValueSpecification(
return constructMatchExpression(
["get", "reference_type", ["get", "planning"]],
{
preapp: colors.preapp,
Expand All @@ -53,7 +53,7 @@ export function colorInterventionsBySchema(
}

// All other schemas go by intervention type
return getDataDrivenPropertyValueSpecification(
return constructMatchExpression(
["get", "intervention_type"],
{
area: colors.area,
Expand Down

0 comments on commit a95d291

Please sign in to comment.