From 294c2003880d335bb5944cfc1fb8b546f50a36cd Mon Sep 17 00:00:00 2001 From: Walter Jenkins Date: Mon, 29 Jan 2024 09:41:51 -0600 Subject: [PATCH] routes and stops on map --- app/lib/utils.ts | 5 +- app/routes/__root.routes.$routeShortName.tsx | 85 ++++++++++++++++++-- package-lock.json | 1 + package.json | 1 + 4 files changed, 84 insertions(+), 8 deletions(-) diff --git a/app/lib/utils.ts b/app/lib/utils.ts index f6db104..c8b6891 100644 --- a/app/lib/utils.ts +++ b/app/lib/utils.ts @@ -1,6 +1,7 @@ import fs from "fs"; import { type ClassValue, clsx } from "clsx"; +import { FeatureCollection, Point } from "geojson"; import { twMerge } from "tailwind-merge"; import { region, timezone } from "~/config"; @@ -95,7 +96,9 @@ export const getGeojson = (routeShortName: string) => { .readdirSync(`geojson/${agency}`) .filter((el) => el.includes(`_${routeShortName}_`)); return files.map((el) => { - const contents = readFileToString(`${`geojson/${agency}`}/${el}`); + const contents = JSON.parse( + readFileToString(`${`geojson/${agency}`}/${el}`), + ) as FeatureCollection; const [, routeShortName, direction] = el.split("_"); const obj = { fileName: el, diff --git a/app/routes/__root.routes.$routeShortName.tsx b/app/routes/__root.routes.$routeShortName.tsx index 05e046f..e1a5592 100644 --- a/app/routes/__root.routes.$routeShortName.tsx +++ b/app/routes/__root.routes.$routeShortName.tsx @@ -5,12 +5,15 @@ import { redirect, } from "@remix-run/node"; import { useLoaderData } from "@remix-run/react"; +import bbox from "@turf/bbox"; +import { lineString } from "@turf/helpers"; import clsx from "clsx"; import { eq } from "drizzle-orm"; +import concat from "lodash/concat"; import groupBy from "lodash/groupBy"; import mapboxCss from "mapbox-gl/dist/mapbox-gl.css"; import { useState } from "react"; -import Map from "react-map-gl"; //eslint-disable-line +import Map, { CircleLayer, Layer, LineLayer, Source } from "react-map-gl"; //eslint-disable-line import { db } from "drizzle/config"; import { routes } from "drizzle/schema"; @@ -35,6 +38,18 @@ export async function loader({ params }: LoaderFunctionArgs) { const files = getFiles(formattedDate, _routes[0].routeShortName); const geojson = getGeojson(routeShortName); + const bounds = bbox( + lineString( + concat( + ...geojson.map((el) => + el.contents.features + .filter((el) => el.properties?.stop_id) + .map((feat) => feat.geometry.coordinates), + ), + ), + ), + ); + const routeIds = _routes.map((el) => el.routeId); const [directions] = await Promise.all([getDirections(routeIds)]); @@ -47,6 +62,7 @@ export async function loader({ params }: LoaderFunctionArgs) { return json({ routes: _routes, dow, + bounds, service, files, geojson, @@ -55,6 +71,44 @@ export async function loader({ params }: LoaderFunctionArgs) { }); } +const stopsStyle = (fileName: string): CircleLayer => { + return { + id: `${fileName}-stops`, + type: "circle", + paint: { + "circle-radius": 5, + "circle-color": "#007cbf", + }, + }; +}; + +const routesStyle = (fileName: string): LineLayer => { + return { + id: `${fileName}-routes`, + type: "line", + paint: { + "line-color": [ + "case", + ["has", "route_color"], + ["get", "route_color"], + "#ffffff", + ], + "line-width": 3, + "line-opacity": [ + "interpolate", + ["linear"], + ["zoom"], + // zoom is 5 (or less) -> circle radius will be 0px (hidden) + 5, + 0, + // zoom is 10 (or greater) -> circle radius will be 5px + 10, + 1, + ], + }, + }; +}; + export default function RootRouteTemplate() { const loaderData = useLoaderData(); const [dow, setDow] = useState(loaderData.service); @@ -62,7 +116,7 @@ export default function RootRouteTemplate() { Object.keys(loaderData.directions)[0], ); - // console.log(loaderData); + console.log(loaderData); return (

@@ -71,13 +125,30 @@ export default function RootRouteTemplate() { + > + {loaderData.geojson.map((el) => ( + + + + + ))} + {loaderData.files.length === 0 ? (
No files found for this route
) : ( diff --git a/package-lock.json b/package-lock.json index 2a806b7..4907fe3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,6 +17,7 @@ "@remix-run/react": "*", "@remix-run/serve": "*", "@turf/bbox": "^6.5.0", + "@turf/helpers": "^6.5.0", "@types/geojson": "^7946.0.13", "@types/lodash": "^4.14.202", "@types/mapbox-gl": "^2.7.19", diff --git a/package.json b/package.json index 738c92e..8d66aa9 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "@remix-run/react": "*", "@remix-run/serve": "*", "@turf/bbox": "^6.5.0", + "@turf/helpers": "^6.5.0", "@types/geojson": "^7946.0.13", "@types/lodash": "^4.14.202", "@types/mapbox-gl": "^2.7.19",