Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add combined stops/stations layer #789

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 39 additions & 4 deletions packages/otp2-tile-overlay/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { generateLayerPaint, ROUTE_COLOR_EXPRESSION } from "./util"

const SOURCE_ID = "otp2-tiles"
const AREA_TYPES = ["areaStops"]
const STOPS_AND_STATIONS_TYPE = "OTP-UI-stopsAndStations"

const OTP2TileLayerWithPopup = ({
color,
Expand Down Expand Up @@ -130,25 +131,34 @@ const OTP2TileLayerWithPopup = ({
map?.on("mouseleave", id, onLayerLeave)
map?.on("click", id, onMapClick || defaultClickHandler)

map?.on("mouseenter", `${id}-secondary`, onLayerEnter)
map?.on("mouseleave", `${id}-secondary`, onLayerLeave)
map?.on("click", `${id}-secondary`, onMapClick || defaultClickHandler)

return () => {
map?.off("mouseenter", id, onLayerEnter);
map?.off("mouseleave", id, onLayerLeave);
map?.off("click", id, onMapClick || defaultClickHandler);

map?.off("mouseenter", `${id}-secondary`, onLayerEnter);
map?.off("mouseleave", `${id}-secondary`, onLayerLeave);
map?.off("click", `${id}-secondary`, onMapClick || defaultClickHandler)
};
}, [id, map])

let filter: any[] = ["all"]
if (network) {
filter = ["all", ["==", "network", network]]
}
if (type === "stops" || type === "areaStops") {
if (type === "stops" || type === "areaStops" || type === STOPS_AND_STATIONS_TYPE) {
filter = ["all", ["!", ["has", "parentStation"]], ["!=", ["get", "routes"], ["literal", "[]"]]]
}
if (stopsWhitelist) {
filter = ["in", ["get", "gtfsId"], ["literal", stopsWhitelist]]
}

const isArea = AREA_TYPES.includes(type)
const isStopsAndStations = type === STOPS_AND_STATIONS_TYPE
return (
<>
{isArea && <Layer
Expand Down Expand Up @@ -177,7 +187,27 @@ const OTP2TileLayerWithPopup = ({
source={SOURCE_ID}
type="line"
/>}
{!isArea && <Layer
{isStopsAndStations && <Layer
filter={filter}
id={id}
key={`${id}-stops`}
paint={generateLayerPaint(color).stops}
source={SOURCE_ID}
minzoom={stopsWhitelist ? 2 : 14}
source-layer="stops"
type="circle"
/>}
{isStopsAndStations && <Layer
filter={filter}
id={`${id}-secondary`}
key={`${id}-stations`}
paint={generateLayerPaint(color).stops}
source={SOURCE_ID}
minzoom={stopsWhitelist ? 2 : 14}
source-layer="stations"
type="circle"
/>}
{!isArea && !isStopsAndStations && <Layer
filter={filter}
id={id}
key={id}
Expand Down Expand Up @@ -224,14 +254,19 @@ const OTP2TileLayerWithPopup = ({
* @returns Array of <Source> and <OTP2TileLayerWithPopup> components
*/
const generateOTP2TileLayers = (
layers: { color?: string; name?: string; network?: string; type: string, initiallyVisible?: boolean }[],
layers: { color?: string; name?: string; network?: string; type: string, initiallyVisible?: boolean, overrideType?: string }[],
endpoint: string,
setLocation?: (location: MapLocationActionArg) => void,
setViewedStop?: (stop: Stop) => void,
stopsWhitelist?: string[],
configCompanies?: ConfiguredCompany[],
getEntityPrefix?: (entity: Stop | Station) => JSX.Element
): JSX.Element[] => {
const fakeOtpUiLayerIndex = layers.findIndex(l=>l.type === STOPS_AND_STATIONS_TYPE)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment here explaining why we're calling this fakeOtpUiLayerIndex

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a real layer! The others are all linked to actual maplibre layers

if (fakeOtpUiLayerIndex > -1) {
layers[fakeOtpUiLayerIndex].overrideType = "stops,stations"
}

return [
<Source
// @ts-expect-error we use a nonstandard prop
Expand All @@ -240,7 +275,7 @@ const generateOTP2TileLayers = (
key={SOURCE_ID}
type="vector"
// Only grab the data we need based on layers defined
url={`${endpoint}/${layers.map((l) => l.type).join(",")}/tilejson.json`}
url={`${endpoint}/${layers.map((l) => l.overrideType || l.type).join(",")}/tilejson.json`}
/>,
...layers.map((layer) => {
const { color, name, network, type, initiallyVisible } = layer
Expand Down
Loading