Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

Commit

Permalink
Added length parameter in Roads
Browse files Browse the repository at this point in the history
  • Loading branch information
zoi23333 authored and Seb-sti1 committed Dec 18, 2023
1 parent 35443d5 commit e651e53
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 65 deletions.
1 change: 1 addition & 0 deletions backend/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export interface Road {
way_name: string;
branches: OSMWayId[][];
geometries: Record<OSMWayId, LatLng[]>;
length: number;
}

/**
Expand Down
15 changes: 4 additions & 11 deletions backend/src/roads/road.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
13 changes: 12 additions & 1 deletion backend/src/roads/road.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}

/**
Expand Down
1 change: 1 addition & 0 deletions frontend/src/models/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface IRoad {
branches: WayId[][];
// the geometry of each way
geometries: Record<WayId, LatLng[]>;
length: number;
}

export enum ImageType {
Expand Down
43 changes: 5 additions & 38 deletions frontend/src/pages/Main.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<number>(13); // Default zoom level
const [wayLength, setWayLength] = useState<number | null>(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<number>((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 (
<>
Expand Down Expand Up @@ -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(
Expand Down
15 changes: 0 additions & 15 deletions frontend/src/queries/road.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
};

0 comments on commit e651e53

Please sign in to comment.