Skip to content

Commit f1ef7db

Browse files
committed
Update routable points structure
1 parent 81494fa commit f1ef7db

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed

MapboxGeocoder/MBPlacemark.swift

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ open class Placemark: NSObject, Codable {
7878
case boundingBox = "bbox"
7979
}
8080

81-
8281
/**
8382
Creates a placemark from the given [Carmen GeoJSON](https://github.com/mapbox/carmen/blob/master/carmen-geojson.md) feature.
8483
*/
@@ -386,6 +385,18 @@ internal struct Properties: Codable {
386385
let category: String?
387386
}
388387

388+
// Used internally for flattening and transforming routable_points.points.coordinates
389+
internal struct RoutableLocation: Codable {
390+
internal let coordinates: [Double]
391+
392+
internal var coordinate: CLLocationCoordinate2D? {
393+
if coordinates.count >= 2 {
394+
return CLLocationCoordinate2D(latitude: coordinates[1], longitude: coordinates[0])
395+
}
396+
return nil
397+
}
398+
}
399+
389400
/**
390401
A concrete subclass of `Placemark` to represent results of geocoding requests.
391402
*/
@@ -396,8 +407,8 @@ open class GeocodedPlacemark: Placemark {
396407
case routableLocations = "routable_points"
397408
}
398409

399-
private enum RoutableLocationsKeys: String, CodingKey {
400-
case points = "points"
410+
private enum PointsCodingKeys: String, CodingKey {
411+
case points
401412
}
402413

403414
/**
@@ -414,10 +425,13 @@ open class GeocodedPlacemark: Placemark {
414425

415426
let container = try decoder.container(keyedBy: CodingKeys.self)
416427

417-
if let pointsContainer = try? container.nestedContainer(keyedBy: RoutableLocationsKeys.self, forKey: .routableLocations),
418-
let coordinatePairs = try pointsContainer.decodeIfPresent([[CLLocationDegrees]].self, forKey: .points) {
419-
let locations = coordinatePairs.map { CLLocation(coordinate: CLLocationCoordinate2D(geoJSON: $0)) }
420-
routableLocations = locations
428+
if let pointsContainer = try? container.nestedContainer(keyedBy: PointsCodingKeys.self, forKey: .routableLocations),
429+
var coordinatesContainer = try? pointsContainer.nestedUnkeyedContainer(forKey: .points) {
430+
431+
if let routableLocation = try coordinatesContainer.decodeIfPresent(RoutableLocation.self),
432+
let coordinate = routableLocation.coordinate {
433+
routableLocations = [CLLocation(coordinate: coordinate)]
434+
}
421435
}
422436
}
423437

@@ -426,10 +440,13 @@ open class GeocodedPlacemark: Placemark {
426440

427441
var container = encoder.container(keyedBy: CodingKeys.self)
428442

429-
if let routableLocations = routableLocations {
430-
var pointsContainer = container.nestedContainer(keyedBy: RoutableLocationsKeys.self, forKey: .routableLocations)
431-
let coordinatePairs = routableLocations.map { $0.geojson() }
432-
try pointsContainer.encodeIfPresent(coordinatePairs, forKey: .points)
443+
if let routableLocations = routableLocations,
444+
!routableLocations.isEmpty {
445+
var pointsContainer = container.nestedContainer(keyedBy: PointsCodingKeys.self, forKey: .routableLocations)
446+
var coordinatesContainer = pointsContainer.nestedUnkeyedContainer(forKey: .points)
447+
let routableLocation = RoutableLocation(coordinates: [routableLocations[0].coordinate.longitude,
448+
routableLocations[0].coordinate.latitude])
449+
try coordinatesContainer.encode(routableLocation)
433450
}
434451
}
435452

@@ -554,6 +571,4 @@ open class GeocodedPlacemark: Placemark {
554571
A concrete subclass of `Placemark` to represent entries in a `GeocodedPlacemark` object’s `superiorPlacemarks` property. These entries are like top-level geocoding results, except that they lack location information and are flatter, with properties directly at the top level.
555572
*/
556573
@objc(MBQualifyingPlacemark)
557-
open class QualifyingPlacemark: Placemark {
558-
559-
}
574+
open class QualifyingPlacemark: Placemark {}

MapboxGeocoderTests/ForwardGeocodingTests.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@ class ForwardGeocodingTests: XCTestCase {
3939
XCTAssertEqual(task.state, URLSessionTask.State.completed)
4040
}
4141

42-
XCTAssertEqual(addressPlacemark.routableLocations![0].coordinate.longitude, CLLocationDegrees(0))
43-
XCTAssertEqual(addressPlacemark.routableLocations![0].coordinate.latitude, CLLocationDegrees(1))
44-
XCTAssertEqual(addressPlacemark.routableLocations![1].coordinate.longitude, CLLocationDegrees(2))
45-
XCTAssertEqual(addressPlacemark.routableLocations![1].coordinate.latitude, CLLocationDegrees(3))
42+
XCTAssertEqual(addressPlacemark.routableLocations![0].coordinate.longitude, CLLocationDegrees(138.995284))
43+
XCTAssertEqual(addressPlacemark.routableLocations![0].coordinate.latitude, CLLocationDegrees(-34.470403))
4644

4745
XCTAssertEqual(addressPlacemark.description, "Pennsylvania Ave", "forward geocode should populate description")
4846
XCTAssertEqual(addressPlacemark.debugDescription, "Pennsylvania Ave, Wasaga Beach, Ontario L9Z 3A8, Canada", "forward geocode should populate debug description")
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"type":"FeatureCollection","query":["1600","pennsylvania","ave"],"features":[{"id":"address.10133733172573100","type":"Feature","text":"Pennsylvania Ave","place_name":"Pennsylvania Ave, Wasaga Beach, Ontario L9Z 3A8, Canada","relevance":0.39,"properties":{},"center":[-79.9850737,44.5047077],"routable_points":{"points":[[0,1],[2,3]]},"geometry":{"type":"Point","coordinates":[-79.9850737,44.5047077]},"context":[{"id":"place.655624","text":"Wasaga Beach","wikidata":"Q3047234"},{"id":"postcode.17609609551414490","text":"L9Z 3A8"},{"id":"region.13373639426376420","text":"Ontario","short_code":"CA-ON","wikidata":"Q1904"},{"id":"country.15589894856372040","text":"Canada","short_code":"ca","wikidata":null}]},{"id":"address.6485281316573100","type":"Feature","text":"Pennsylvania Ave","place_name":"Pennsylvania Ave, Vaughan, Ontario L4K 3X6, Canada","relevance":0.39,"properties":{},"center":[-79.532497,43.802232],"geometry":{"type":"Point","coordinates":[-79.532497,43.802232]},"context":[{"id":"neighborhood.5043724752221980","text":"Vellore Woods"},{"id":"place.646758","text":"Vaughan","wikidata":"Q44013"},{"id":"postcode.6385804433309570","text":"L4K 3X6"},{"id":"region.13373639426376420","text":"Ontario","short_code":"CA-ON","wikidata":"Q1904"},{"id":"country.15589894856372040","text":"Canada","short_code":"ca","wikidata":"Q16"}]},{"id":"address.12769812476573100","type":"Feature","text":"Pennsylvania Ave","place_name":"Pennsylvania Ave, Stellarton, Nova Scotia B0K 1S0, Canada","relevance":0.39,"properties":{},"center":[-62.661376,45.556068],"geometry":{"type":"Point","coordinates":[-62.661376,45.556068]},"context":[{"id":"place.593196","text":"Stellarton","wikidata":"Q3498168"},{"id":"postcode.13310912212063190","text":"B0K 1S0"},{"id":"region.10539239752558240","text":"Nova Scotia","short_code":"CA-NS","wikidata":" Q10116"},{"id":"country.15589894856372040","text":"Canada","short_code":"ca","wikidata":null}]},{"id":"address.6456604709573100","type":"Feature","text":"Pennsylvania Ave","place_name":"Pennsylvania Ave, Vaughan, Ontario L4K 3X8, Canada","relevance":0.39,"properties":{},"center":[-79.531684,43.802706],"geometry":{"type":"Point","coordinates":[-79.531684,43.802706]},"context":[{"id":"neighborhood.5043724752221980","text":"Vellore Woods"},{"id":"place.646758","text":"Vaughan","wikidata":"Q44013"},{"id":"postcode.6367605302663760","text":"L4K 3X8"},{"id":"region.13373639426376420","text":"Ontario","short_code":"CA-ON","wikidata":null},{"id":"country.15589894856372040","text":"Canada","short_code":"ca","wikidata":"Q16"}]}],"attribution":"NOTICE: © 2016 Mapbox and its suppliers. All rights reserved. Use of this data is subject to the Mapbox Terms of Service (https://www.mapbox.com/about/maps/). This response and the information it contains may not be retained."}
1+
{"type":"FeatureCollection","query":["1600","pennsylvania","ave"],"features":[{"id":"address.10133733172573100","type":"Feature","text":"Pennsylvania Ave","place_name":"Pennsylvania Ave, Wasaga Beach, Ontario L9Z 3A8, Canada","relevance":0.39,"properties":{},"center":[-79.9850737,44.5047077],"routable_points":{"points":[{"coordinates":[138.995284,-34.470403]}]},"geometry":{"type":"Point","coordinates":[-79.9850737,44.5047077]},"context":[{"id":"place.655624","text":"Wasaga Beach","wikidata":"Q3047234"},{"id":"postcode.17609609551414490","text":"L9Z 3A8"},{"id":"region.13373639426376420","text":"Ontario","short_code":"CA-ON","wikidata":"Q1904"},{"id":"country.15589894856372040","text":"Canada","short_code":"ca","wikidata":null}]},{"id":"address.6485281316573100","type":"Feature","text":"Pennsylvania Ave","place_name":"Pennsylvania Ave, Vaughan, Ontario L4K 3X6, Canada","relevance":0.39,"properties":{},"center":[-79.532497,43.802232],"geometry":{"type":"Point","coordinates":[-79.532497,43.802232]},"context":[{"id":"neighborhood.5043724752221980","text":"Vellore Woods"},{"id":"place.646758","text":"Vaughan","wikidata":"Q44013"},{"id":"postcode.6385804433309570","text":"L4K 3X6"},{"id":"region.13373639426376420","text":"Ontario","short_code":"CA-ON","wikidata":"Q1904"},{"id":"country.15589894856372040","text":"Canada","short_code":"ca","wikidata":"Q16"}]},{"id":"address.12769812476573100","type":"Feature","text":"Pennsylvania Ave","place_name":"Pennsylvania Ave, Stellarton, Nova Scotia B0K 1S0, Canada","relevance":0.39,"properties":{},"center":[-62.661376,45.556068],"geometry":{"type":"Point","coordinates":[-62.661376,45.556068]},"context":[{"id":"place.593196","text":"Stellarton","wikidata":"Q3498168"},{"id":"postcode.13310912212063190","text":"B0K 1S0"},{"id":"region.10539239752558240","text":"Nova Scotia","short_code":"CA-NS","wikidata":" Q10116"},{"id":"country.15589894856372040","text":"Canada","short_code":"ca","wikidata":null}]},{"id":"address.6456604709573100","type":"Feature","text":"Pennsylvania Ave","place_name":"Pennsylvania Ave, Vaughan, Ontario L4K 3X8, Canada","relevance":0.39,"properties":{},"center":[-79.531684,43.802706],"geometry":{"type":"Point","coordinates":[-79.531684,43.802706]},"context":[{"id":"neighborhood.5043724752221980","text":"Vellore Woods"},{"id":"place.646758","text":"Vaughan","wikidata":"Q44013"},{"id":"postcode.6367605302663760","text":"L4K 3X8"},{"id":"region.13373639426376420","text":"Ontario","short_code":"CA-ON","wikidata":null},{"id":"country.15589894856372040","text":"Canada","short_code":"ca","wikidata":"Q16"}]}],"attribution":"NOTICE: © 2016 Mapbox and its suppliers. All rights reserved. Use of this data is subject to the Mapbox Terms of Service (https://www.mapbox.com/about/maps/). This response and the information it contains may not be retained."}

0 commit comments

Comments
 (0)