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 df3c3d96..cc78227f 100644 --- a/backend/src/roads/road.controller.ts +++ b/backend/src/roads/road.controller.ts @@ -16,18 +16,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 3bb846eb..48eda425 100644 --- a/backend/src/roads/road.service.ts +++ b/backend/src/roads/road.service.ts @@ -216,7 +216,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); - }); -};