Skip to content

Commit

Permalink
Sporting hospitals by distance
Browse files Browse the repository at this point in the history
  • Loading branch information
fboes committed Nov 14, 2024
1 parent bca08f7 commit 75b8529
Show file tree
Hide file tree
Showing 8 changed files with 252 additions and 205 deletions.
100 changes: 50 additions & 50 deletions data/HEMS-lueneburg-ec135/custom_missions_user.tmc

Large diffs are not rendered by default.

112 changes: 56 additions & 56 deletions data/HEMS-lueneburg-uh60/custom_missions_user.tmc

Large diffs are not rendered by default.

88 changes: 44 additions & 44 deletions data/HEMS-san_francisco-ec135/custom_missions_user.tmc

Large diffs are not rendered by default.

100 changes: 50 additions & 50 deletions data/HEMS-san_francisco-uh60/custom_missions_user.tmc

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/lib/general/AeroflyMissionDescription.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export default class AeroflyMissionDescription {

for (const cp of this.#mission.checkpoints) {
if (lastCp !== null) {
distance += this.getDistanceBetweenCheckpoints(lastCp, cp);
distance += AeroflyMissionDescription.getDistanceBetweenCheckpoints(lastCp, cp);
}

lastCp = cp;
Expand Down Expand Up @@ -181,7 +181,7 @@ export default class AeroflyMissionDescription {
* @param {AeroflyMissionCheckpoint} cp
* @returns {number} distance in meters
*/
getDistanceBetweenCheckpoints(lastCp, cp) {
static getDistanceBetweenCheckpoints(lastCp, cp) {
const lat1 = (lastCp.latitude / 180) * Math.PI;
const lon1 = (lastCp.longitude / 180) * Math.PI;
const lat2 = (cp.latitude / 180) * Math.PI;
Expand Down
3 changes: 2 additions & 1 deletion dist/lib/hems/Configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ export class Configuration {
* @type {string}
*/
this.geoJsonFile =
positionals[0] ?? path.join(path.dirname(fileURLToPath(import.meta.url)), "../../data/hems/san_francisco.geojson");
positionals[0] ??
path.join(path.dirname(fileURLToPath(import.meta.url)), "../../data/hems/san_francisco.geojson");
if (!path.isAbsolute(this.geoJsonFile)) {
this.geoJsonFile = path.join(process.cwd(), this.geoJsonFile);
}
Expand Down
1 change: 0 additions & 1 deletion dist/lib/hems/GeoJsonLocations.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export class GeoJsonLocations {
throw Error("Missing FeatureCollection with features in GeoJSON file");
}


const pointFeatures = featureCollection.features.filter((f) => {
return f.type === "Feature" && f.geometry.type === "Point";
});
Expand Down
49 changes: 48 additions & 1 deletion dist/lib/hems/Scenario.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ export class Scenario {

const isTransfer = this.configuration.canTransfer && locations.hospitals.length > 1 && Math.random() <= 0.1;
const waypoint1 = this.#getRandLocation(isTransfer ? locations.hospitals : locations.other);
const waypoint2 = this.#getRandLocation(locations.hospitals, waypoint1);
const waypoint2 = isTransfer
? this.#getRandLocation(locations.hospitals, waypoint1)
: this.#getNearestLocation(locations.hospitals, waypoint1);

const conditions = new AeroflyMissionConditions({
time,
Expand Down Expand Up @@ -135,6 +137,51 @@ export class Scenario {
return location;
}

/**
* @param {import('./GeoJsonLocations.js').GeoJsonFeature[]} locations
* @param {import('./GeoJsonLocations.js').GeoJsonFeature} location
* @returns {import('./GeoJsonLocations.js').GeoJsonFeature}
*/
#getNearestLocation(locations, location) {
let distance = 9999;
let nearestLocation = locations[0];
for (const testLocation of locations) {
const testDistance = this.#getDistanceBetweenLocations(testLocation, location);
if (testDistance < distance) {
nearestLocation = testLocation;
distance = testDistance;
}
}

return nearestLocation;
}

/**
*
* @param {import('./GeoJsonLocations.js').GeoJsonFeature} lastCp
* @param {import('./GeoJsonLocations.js').GeoJsonFeature} cp
* @returns {number} distance in meters
*/
#getDistanceBetweenLocations(lastCp, cp) {
const lat1 = (lastCp.geometry.coordinates[1] / 180) * Math.PI;
const lon1 = (lastCp.geometry.coordinates[0] / 180) * Math.PI;
const lat2 = (cp.geometry.coordinates[1] / 180) * Math.PI;
const lon2 = (cp.geometry.coordinates[0] / 180) * Math.PI;

const dLon = lon2 - lon1;
const dLat = lat2 - lat1;

//const y = Math.sin(dLon) * Math.cos(lat2);
//const x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(dLon);
//const bearing = ((Math.atan2(y, x) * 180) / Math.PI + 360) % 360;

const a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return 6_371_000 * c;
}

/**
*
* @param {import('./GeoJsonLocations.js').GeoJsonFeature} location
Expand Down

0 comments on commit 75b8529

Please sign in to comment.