Skip to content

Commit

Permalink
Implement and migrate to NominatimGeocoder
Browse files Browse the repository at this point in the history
Fixes #159
  • Loading branch information
fwcd committed Jul 2, 2024
1 parent c8a4876 commit 51535ae
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Sources/D2Commands/CAU/UnivIS/CampusCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class CampusCommand: StringCommand {
presented: true,
requiredPermissionLevel: .basic
)
let geocoder = MapQuestGeocoder()
let geocoder = NominatimGeocoder()

public init() {}

Expand Down
2 changes: 1 addition & 1 deletion Sources/D2Commands/Misc/GeocodeCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class GeocodeCommand: StringCommand {
requiredPermissionLevel: .basic
)
public let outputValueType: RichValueType = .geoCoordinates
private let geocoder = MapQuestGeocoder()
private let geocoder = NominatimGeocoder()

public init() {}

Expand Down
29 changes: 29 additions & 0 deletions Sources/D2NetAPIs/Nominatim/NominatimGeocoder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Foundation
import Utils

public struct NominatimGeocoder {
public init() {}

public func geocode(location: String) async throws -> GeoCoordinates {
let request = try HTTPRequest(
scheme: "https",
host: "nominatim.openstreetmap.org",
path: "/search",
query: [
"q": location,
"format": "jsonv2",
],
headers: [
"User-Agent": "D2",
]
)
let results = try await request.fetchJSON(as: [NominatimGeocodingResult].self)
guard let result = results.first else {
throw NominatimGeocodingError.noResults
}
guard let coords = result.geoCoordinates else {
throw NominatimGeocodingError.noGeoCoordinates
}
return coords
}
}
4 changes: 4 additions & 0 deletions Sources/D2NetAPIs/Nominatim/NominatimGeocodingError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public enum NominatimGeocodingError: Error {
case noResults
case noGeoCoordinates
}
44 changes: 44 additions & 0 deletions Sources/D2NetAPIs/Nominatim/NominatimGeocodingResult.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import Utils

struct NominatimGeocodingResult: Codable {
enum CodingKeys: String, CodingKey {
case placeId = "place_id"
case license
case osmType = "osm_type"
case osmId = "osm_id"
case lat
case lon
case category
case type
case placeRank = "place_rank"
case importance
case addressType = "addresstype"
case name
case displayName = "display_name"
case boundingBox = "boundingbox"
}

var placeId: Int?
var license: String?
var osmType: String?
var osmId: Int?
var lat: String?
var lon: String?
var category: String?
var type: String?
var placeRank: Int?
var importance: Double?
var addressType: String?
var name: String?
var displayName: String?
var boundingBox: [String]?

var geoCoordinates: GeoCoordinates? {
guard let lat = lat.flatMap(Double.init),
let lon = lon.flatMap(Double.init) else { return nil }
return .init(
latitude: lat,
longitude: lon
)
}
}

0 comments on commit 51535ae

Please sign in to comment.