From e651e535c84b9464da3eaf07f134d3966185986d Mon Sep 17 00:00:00 2001 From: zoi23333 <110458414+zoi23333@users.noreply.github.com> Date: Mon, 18 Dec 2023 10:55:20 +0100 Subject: [PATCH] Added length parameter in Roads --- backend/src/models.ts | 1 + backend/src/roads/road.controller.ts | 15 +++------- backend/src/roads/road.service.ts | 13 ++++++++- frontend/src/models/path.ts | 1 + frontend/src/pages/Main.tsx | 43 ++++------------------------ frontend/src/queries/road.ts | 15 ---------- 6 files changed, 23 insertions(+), 65 deletions(-) diff --git a/backend/src/models.ts b/backend/src/models.ts index fd53db21..19abf400 100644 --- a/backend/src/models.ts +++ b/backend/src/models.ts @@ -66,6 +66,7 @@ export interface Road { way_name: string; branches: OSMWayId[][]; geometries: Record; + length: number; } /** diff --git a/backend/src/roads/road.controller.ts b/backend/src/roads/road.controller.ts index b87e2aab..4fc095b8 100644 --- a/backend/src/roads/road.controller.ts +++ b/backend/src/roads/road.controller.ts @@ -9,18 +9,11 @@ export class RoadController { /** * Get all the roads in the database. * - * @author Kerbourc'h, Chen + * @author Kerbourc'h */ - @Get('paths/:wayId?') - async getRoadsInfo(@Param('wayId') wayId?: OSMWayId) { - if (wayId) { - // Get the length of the specific way - const wayData = await this.service.getWayData(wayId); - return { length: wayData.length }; - } else { - // Get all roads if no specific wayId is provided - return await this.service.getRoadsPaths(); - } + @Get('paths') + async getRoadsPaths() { + return await this.service.getRoadsPaths(); } /** diff --git a/backend/src/roads/road.service.ts b/backend/src/roads/road.service.ts index f233ccdf..0db81bbd 100644 --- a/backend/src/roads/road.service.ts +++ b/backend/src/roads/road.service.ts @@ -97,7 +97,18 @@ export class RoadService { Object.values(ways).forEach((way) => { geometries[way.osm_id] = way.section_geom; }); - return { way_name: wayName, branches: branches, geometries: geometries }; + + const sumLength = longestBranches.reduce( + (total, branch) => total + branch.length, + 0, + ); + + return { + way_name: wayName, + branches: branches, + geometries: geometries, + length: sumLength, + }; } /** diff --git a/frontend/src/models/path.ts b/frontend/src/models/path.ts index cb8127dc..ecf6ad20 100644 --- a/frontend/src/models/path.ts +++ b/frontend/src/models/path.ts @@ -47,6 +47,7 @@ export interface IRoad { branches: WayId[][]; // the geometry of each way geometries: Record; + length: number; } export enum ImageType { diff --git a/frontend/src/pages/Main.tsx b/frontend/src/pages/Main.tsx index a35d2e51..78fa3117 100644 --- a/frontend/src/pages/Main.tsx +++ b/frontend/src/pages/Main.tsx @@ -1,7 +1,7 @@ import { FC, useCallback, useEffect, useState } from 'react'; import Hamburger from '../Components/Map/Inputs/Hamburger'; -import { IRoad, WayId } from '../models/path'; -import { getRoadsPaths, getWayLength } from '../queries/road'; +import { IRoad } from '../models/path'; +import { getRoadsPaths } from '../queries/road'; import { LatLng, SurveyListItem } from '../models/models'; import { FeatureCollection } from 'geojson'; import { getAllConditions } from '../queries/conditions'; @@ -231,40 +231,6 @@ const Main: FC = () => { // Control Zoom level by the length of roads when selecting a road on the map const [zoomLevel, setZoomLevel] = useState(13); // Default zoom level - const [wayLength, setWayLength] = useState(null); - - /** - /* Function to calculate the total length of roads - /* - /* @author Chen - */ - const fetchAndSumWayLengths = useCallback( - async (branches: WayId[][]) => { - try { - const promises = branches.flat().map( - (wayId) => - new Promise((resolve) => { - getWayLength(wayId, resolve); - }), - ); - const lengths = await Promise.all(promises); - const totalLength = lengths.reduce((sum, length) => sum + length, 0); - setWayLength(totalLength); - } catch (error) { - console.error('Error fetching way lengths:', error); - setWayLength(0); - } - }, - [getWayLength, setWayLength], - ); - - // Zoom in when calling onSelectedPath - useEffect(() => { - if (wayLength !== null) { - const zoom = calculateZoomLevel(wayLength); - setZoomLevel(zoom); - } - }, [wayLength]); return ( <> @@ -385,8 +351,9 @@ const Main: FC = () => { setSelectedRoadIdx(index); // Get information of road branches for Zoom in const selectedRoad = roads[index]; - const Branches = selectedRoad.branches; - fetchAndSumWayLengths(Branches); // Call fetchAndSumWayLengths to calculate parameters and zoom in + const sumLength = selectedRoad.length; + const zoomParam = calculateZoomLevel(sumLength); + setZoomLevel(zoomParam); } else if (selectedRoadIdx != -1) { // if a road is selected, go to the inspect page for the clicked road branch navigate( diff --git a/frontend/src/queries/road.ts b/frontend/src/queries/road.ts index 98b5f217..c576df50 100644 --- a/frontend/src/queries/road.ts +++ b/frontend/src/queries/road.ts @@ -23,18 +23,3 @@ export const getRoadsData = ( ) => { post('/roads/data', wayIds, callback); }; - -/** - * Fetch the length of a specific way by its OSMWayId. - * @param wayId the OSMWayId of the way - * - * @author Chen - */ -export const getWayLength = ( - wayId: string, - callback: (length: number) => void, -) => { - get(`/roads/paths/${wayId}`, (data: { length: number }) => { - callback(data.length); - }); -};