From 40a46b9c04da05932fe39190428317c66eebddf2 Mon Sep 17 00:00:00 2001 From: Krystof Date: Fri, 17 May 2024 12:13:46 +0200 Subject: [PATCH] feat(app): get closest metro station --- .../metro-now-tests/metroUtilsTests.swift | 115 ++++++++++++++++-- .../metro-now-utils/metroUtils.swift | 44 +++++++ 2 files changed, 152 insertions(+), 7 deletions(-) diff --git a/app/metro-now/metro-now-tests/metroUtilsTests.swift b/app/metro-now/metro-now-tests/metroUtilsTests.swift index fae7af47..c75d352d 100644 --- a/app/metro-now/metro-now-tests/metroUtilsTests.swift +++ b/app/metro-now/metro-now-tests/metroUtilsTests.swift @@ -5,6 +5,7 @@ // Created by Kryštof Krátký on 15.05.2024. // +import CoreLocation @testable import metro_now import SwiftUI import XCTest @@ -53,27 +54,23 @@ final class metroUtilsTests: XCTestCase { func testsGetMetroLineColorOutputEnum() { /// init variables for testing - var lineLetter: String var result: Color var expectedResult: Color /// test 1 - lineLetter = MetroLine.A.rawValue - result = getMetroLineColor(lineLetter) + result = getMetroLineColor(.A) expectedResult = Color.green XCTAssertEqual(result, expectedResult) /// test 2 - lineLetter = MetroLine.B.rawValue - result = getMetroLineColor(lineLetter) + result = getMetroLineColor(.B) expectedResult = Color.yellow XCTAssertEqual(result, expectedResult) /// test 3 - lineLetter = MetroLine.C.rawValue - result = getMetroLineColor(lineLetter) + result = getMetroLineColor(.C) expectedResult = Color.red XCTAssertEqual(result, expectedResult) @@ -106,4 +103,108 @@ final class metroUtilsTests: XCTestCase { XCTAssertEqual(result, expectedResult) } + + func testsGetClosestStationFromGeoJSON() { + /// init variables for testing + var location: CLLocation + var result: MetroStationsGeoJSONFeature? + + /// test 1 + location = CLLocation( + latitude: 50.078453, + longitude: 14.430676 + ) + result = getClosestStationFromGeoJSON( + location: location + ) + + XCTAssertEqual( + result?.properties.name, + "Muzeum" + ) + + /// test 2 + location = CLLocation( + latitude: 50.079591, + longitude: 14.430883 + ) + result = getClosestStationFromGeoJSON( + location: location + ) + + XCTAssertEqual( + result?.properties.name, + "Muzeum" + ) + + /// test 3 + location = CLLocation( + latitude: 50.080094, + longitude: 14.429663 + ) + result = getClosestStationFromGeoJSON( + location: location + ) + + XCTAssertEqual( + result?.properties.name, + "Muzeum" + ) + + /// test 4 + location = CLLocation( + latitude: 50.074929, + longitude: 14.430177 + ) + result = getClosestStationFromGeoJSON( + location: location + ) + + XCTAssertEqual( + result?.properties.name, + "I. P. Pavlova" + ) + + /// test 5 + location = CLLocation( + latitude: 50.075499, + longitude: 14.322204 + ) + result = getClosestStationFromGeoJSON( + location: location + ) + + XCTAssertEqual( + result?.properties.name, + "Nemocnice Motol" + ) + + /// test 6 + location = CLLocation( + latitude: 50.047485, + longitude: 14.460385 + ) + result = getClosestStationFromGeoJSON( + location: location + ) + + XCTAssertEqual( + result?.properties.name, + "Kačerov" + ) + + /// test 7 + location = CLLocation( + latitude: 50.040648, + longitude: 14.456518 + ) + result = getClosestStationFromGeoJSON( + location: location + ) + + XCTAssertEqual( + result?.properties.name, + "Kačerov" + ) + } } diff --git a/app/metro-now/metro-now-utils/metroUtils.swift b/app/metro-now/metro-now-utils/metroUtils.swift index c0f38bf0..2d5b5cd1 100644 --- a/app/metro-now/metro-now-utils/metroUtils.swift +++ b/app/metro-now/metro-now-utils/metroUtils.swift @@ -5,7 +5,9 @@ // Created by Kryštof Krátký on 15.05.2024. // +import CoreLocation import Foundation +import MapKit import SwiftUI enum MetroLine: String { @@ -26,3 +28,45 @@ func getMetroLineColor(_ letter: String) -> Color { .purple } } + +func getMetroLineColor(_ letter: MetroLine) -> Color { + switch letter { + case .A: + .green + case .B: + .yellow + case .C: + .red + } +} + +func getClosestStationFromGeoJSON(location: CLLocation) -> MetroStationsGeoJSONFeature? { + let stations: MetroStationsGeoJSON? = getParsedJSONFile(.METRO_STATIONS_FILE) + + guard let stations else { + return nil + } + guard stations.features.count > 0 else { + return nil + } + + var closestStationIndex = 0 + var closestStationDistance: Double = location.distance(from: CLLocation( + latitude: stations.features[0].geometry.coordinates[1], longitude: stations.features[0].geometry.coordinates[0] + )) + + for (index, station) in stations.features.enumerated() { + let distance = location.distance(from: CLLocation( + latitude: station.geometry.coordinates[1], longitude: station.geometry.coordinates[0] + )) + + if closestStationDistance > distance { + closestStationIndex = index + closestStationDistance = distance + } + } + + return stations.features[closestStationIndex] +} + +func getSortedStationsByDistance() {}