-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement and migrate to NominatimGeocoder
Fixes #159
- Loading branch information
Showing
5 changed files
with
79 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
44
Sources/D2NetAPIs/Nominatim/NominatimGeocodingResult.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
} | ||
} |