Skip to content

Commit

Permalink
Merge pull request #142 from kieler/133-remove-getvehicletrackdistanc…
Browse files Browse the repository at this point in the history
…ekm-and-move-getvehicletrackdistancepercentage

133 remove getvehicletrackdistancekm and move getvehicletrackdistancepercentage
  • Loading branch information
Kebslock authored Sep 8, 2023
2 parents d092441 + bac391e commit 50f1cd6
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 60 deletions.
11 changes: 7 additions & 4 deletions Server/src/routes/track.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,16 +217,19 @@ export class TrackRoute {
vehicles.map(async (vehicle: Vehicle) => {
// get the current position of the vehicle
const geo_pos = await VehicleService.getVehiclePosition(vehicle)
const trackKm = geo_pos ? GeoJSONUtils.getTrackKm(geo_pos) : undefined
// If we know that, convert it in the API format.
const pos: Position | undefined = geo_pos
? {
lat: GeoJSONUtils.getLatitude(geo_pos),
lng: GeoJSONUtils.getLongitude(geo_pos)
}
lat: GeoJSONUtils.getLatitude(geo_pos),
lng: GeoJSONUtils.getLongitude(geo_pos)
}
: undefined
// Also acquire the percentage position. It might happen that a percentage position is known, while the position is not.
// This might not make much sense.
const percentagePosition = (await VehicleService.getVehicleTrackDistancePercentage(vehicle)) ?? undefined
const percentagePosition = trackKm
? (await TrackService.getTrackKmAsPercentage(trackKm, track)) ?? undefined
: undefined
const heading = await VehicleService.getVehicleHeading(vehicle)
return {
id: vehicle.uid,
Expand Down
14 changes: 9 additions & 5 deletions Server/src/routes/vehicle.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Feature, GeoJsonProperties, Point } from "geojson"
import please_dont_crash from "../utils/please_dont_crash"
import database from "../services/database.service"
import GeoJSONUtils from "../utils/geojsonUtils"
import TrackService from "../services/track.service"

/**
* The router class for the routing of the vehicle data to app and website.
Expand Down Expand Up @@ -126,7 +127,7 @@ export class VehicleRoute {
res.sendStatus(500)
return
}
const userVehicleTrackKm: number | null = await VehicleService.getVehicleTrackDistanceKm(userVehicle)
const userVehicleTrackKm: number | null = GeoJSONUtils.getTrackKm(pos)
if (!userVehicleTrackKm) {
logger.error(`Could not compute track kilometer for vehicle with id ${userVehicle.uid}
at track wit id ${userVehicle.trackId}`)
Expand All @@ -149,7 +150,7 @@ export class VehicleRoute {
allVehiclesOnTrack.map(async v => {
const pos = await VehicleService.getVehiclePosition(v)
const trackers = await database.trackers.getByVehicleId(v.uid)
const nearbyVehicleTrackKm: number | null = await VehicleService.getVehicleTrackDistanceKm(v)
const nearbyVehicleTrackKm: number | null = pos ? GeoJSONUtils.getTrackKm(pos) : null
if (!nearbyVehicleTrackKm) {
logger.error(`Could not compute track kilometer for vehicle with id ${v.uid}
at track wit id ${v.trackId}`)
Expand All @@ -160,7 +161,7 @@ export class VehicleRoute {
type: v.typeId,
trackerIds: trackers.map(t => t.uid),
pos: pos ? { lat: GeoJSONUtils.getLatitude(pos), lng: GeoJSONUtils.getLongitude(pos) } : undefined,
percentagePosition: (await VehicleService.getVehicleTrackDistancePercentage(v)) ?? -1,
percentagePosition: -1,
heading: await VehicleService.getVehicleHeading(v),
headingTowardsUser: undefined
}
Expand All @@ -176,7 +177,7 @@ export class VehicleRoute {
type: v.typeId,
trackerIds: trackers.map(t => t.uid),
pos: pos ? { lat: GeoJSONUtils.getLatitude(pos), lng: GeoJSONUtils.getLongitude(pos) } : undefined,
percentagePosition: (await VehicleService.getVehicleTrackDistancePercentage(v)) ?? -1,
percentagePosition: (await TrackService.getTrackKmAsPercentage(nearbyVehicleTrackKm, track)) ?? -1,
heading: await VehicleService.getVehicleHeading(v),
headingTowardsUser:
userVehicleSimplifiedHeading !== 0 && nearbySimplifiedVehicleHeading !== 0
Expand All @@ -187,7 +188,10 @@ export class VehicleRoute {
)
).filter(v => v.id !== userVehicle.uid && v.track === track.uid && v.percentagePosition !== -1)

const percentagePositionOnTrack: number | null = await VehicleService.getVehicleTrackDistancePercentage(userVehicle)
const percentagePositionOnTrack: number | null = await TrackService.getTrackKmAsPercentage(
userVehicleTrackKm,
track
)
if (!percentagePositionOnTrack) {
logger.error(`Could not determine percentage position on track for user with vehicle ${userVehicle.uid}`)
res.sendStatus(500)
Expand Down
26 changes: 25 additions & 1 deletion Server/src/services/track.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default class TrackService {
track: GeoJSON.FeatureCollection<GeoJSON.Point>
): GeoJSON.FeatureCollection<GeoJSON.Point> {
// iterate over all features
turfMeta.featureEach(track, function(feature, featureIndex) {
turfMeta.featureEach(track, function (feature, featureIndex) {
// compute track kilometer for each point
if (featureIndex > 0) {
const prevFeature = track.features[featureIndex - 1]
Expand Down Expand Up @@ -99,6 +99,30 @@ export default class TrackService {
return GeoJSONUtils.getTrackKm(projectedPoint)
}

/**
* Calculate percentage value for given track kilometer of given track
* @param trackKm track kilometer value to convert to percentage
* @param track `Track` to use for calculation as reference
* @returns percentage value of `trackKm` regarding `track`, `null` if an error occurs
*/
public static async getTrackKmAsPercentage(trackKm: number, track: Track): Promise<number | null> {
// get total track length in kilometers
const trackLength = this.getTrackLength(track)
if (trackLength == null) {
logger.error(`Could not compute track length from track with id ${track.uid} to convert track kilometer value.`)
return null
}

// check if track kilometer is within bounds
if (trackKm < 0 || trackKm > trackLength) {
logger.error(`Expected track kilometer to be between 0 and ${trackLength}, but got ${trackKm}.`)
return null
}

// convert to percentage
return (trackKm / trackLength) * 100
}

/**
* Project a position onto a track
* @param position position to project onto the track
Expand Down
50 changes: 0 additions & 50 deletions Server/src/services/vehicle.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,56 +378,6 @@ export default class VehicleService {
return TrackService.getProjectedPointOnTrack(geoJSONPoint, track)
}

/**
* Just a wrapper for getting position of a vehicle to get its distance along the track.
* @param vehicle `Vehicle` to get the distance for
* @returns distance of `vehicle` as kilometers along the track, `null` if not possible
*/
public static async getVehicleTrackDistanceKm(vehicle: Vehicle): Promise<number | null> {
// get track point of vehicle
const vehicleTrackPoint = await this.getVehiclePosition(vehicle)
if (vehicleTrackPoint == null) {
logger.error(`Could not compute position of vehicle with id ${vehicle.uid}.`)
return null
}

// get track kilometer for vehicle position
const vehicleTrackKm = GeoJSONUtils.getTrackKm(vehicleTrackPoint)
if (vehicleTrackKm == null) {
logger.error(`Could not read track kilometer value from position ${JSON.stringify(vehicleTrackPoint)}.`)
return null
}

return vehicleTrackKm
}

/**
* Get distance for vehicle along the track as percentage.
* @param vehicle `Vehicle` to get the distance for
* @returns distance of `vehicle` as percentage along the track, `null` if not possible
*/
public static async getVehicleTrackDistancePercentage(vehicle: Vehicle): Promise<number | null> {
// get track
const track = await database.tracks.getById(vehicle.trackId)
if (track == null) {
logger.error(`Track with id ${vehicle.trackId} was not found.`)
return null
}

// get distance of vehicle and length of track and check for success
const trackLength = TrackService.getTrackLength(track)
const vehicleDistance = await this.getVehicleTrackDistanceKm(vehicle)
if (trackLength == null || vehicleDistance == null) {
logger.error(
`Distance of track with id ${track.uid} or distance of vehicle with id ${vehicle.uid} on that track could not be computed.`
)
return null
}

// return percentage
return (vehicleDistance / trackLength) * 100
}

/**
* Compute average heading of all trackers assigned to a specified vehicle.
* No headings from app will be used here due to mobility.
Expand Down

0 comments on commit 50f1cd6

Please sign in to comment.