Skip to content

Commit df6e9d2

Browse files
committed
Merge pull request #21 from raphaelmor/v2.0.0
V2.0.0
2 parents bf722d6 + 00d5f8f commit df6e9d2

File tree

5 files changed

+348
-346
lines changed

5 files changed

+348
-346
lines changed

Polyline.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Pod::Spec.new do |s|
1616
#
1717

1818
s.name = "Polyline"
19-
s.version = "1.1.0"
19+
s.version = "2.0.0"
2020
s.summary = "Polyline encoder / decoder in swift"
2121

2222
s.description = <<-DESC
@@ -68,7 +68,7 @@ Pod::Spec.new do |s|
6868
# Supports git, hg, bzr, svn and HTTP.
6969
#
7070

71-
s.source = { :git => "https://github.com/raphaelmor/Polyline.git", :tag => "v1.1.0" }
71+
s.source = { :git => "https://github.com/raphaelmor/Polyline.git", :tag => "v2.0.0" }
7272

7373

7474
# ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #

Polyline/Polyline.swift

Lines changed: 64 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -38,78 +38,72 @@ import CoreLocation
3838
///
3939
/// :see: https://developers.google.com/maps/documentation/utilities/polylinealgorithm
4040
public struct Polyline {
41-
42-
/// The array of coordinates
43-
public let coordinates: [CLLocationCoordinate2D]
44-
/// The encoded polyline
45-
public let encodedPolyline: String = ""
46-
47-
/// The array of levels
48-
public let levels: [UInt32]?
49-
/// The encoded levels
50-
public let encodedLevels: String = ""
51-
52-
/// The array of location (computed from coordinates)
53-
public var locations: [CLLocation] {
54-
return toLocations(coordinates)
55-
}
56-
41+
42+
/// The array of coordinates (nil if polyline cannot be decoded)
43+
public let coordinates: [CLLocationCoordinate2D]?
44+
/// The encoded polyline
45+
public let encodedPolyline: String
46+
47+
/// The array of levels (nil if cannot be decoded, or is not provided)
48+
public let levels: [UInt32]?
49+
/// The encoded levels (nil if cannot be encoded, or is not provided)
50+
public let encodedLevels: String?
51+
52+
/// The array of location (computed from coordinates)
53+
public var locations: [CLLocation]? {
54+
return self.coordinates.map(toLocations)
55+
}
56+
5757
// MARK: - Public Methods -
5858

59-
/// This designated init encodes an [CLLocationCoordinate2D] to a String
60-
///
61-
/// :param: coordinates The array of CLLocationCoordinate2D that you want to encode
62-
/// :param: levels The optional array of levels that you want to encode
59+
/// This designated initializer encodes a [CLLocationCoordinate2D]
60+
///
61+
/// :param: coordinates The array of CLLocationCoordinate2D that you want to encode
62+
/// :param: levels The optional array of levels that you want to encode (default: nil)
63+
/// :param: precision The precision used for encoding (default: 1e5)
6364
public init(coordinates: [CLLocationCoordinate2D], levels: [UInt32]? = nil, precision: Double = 1e5) {
64-
65-
self.coordinates = coordinates
66-
self.levels = levels
67-
68-
encodedPolyline = encodeCoordinates(coordinates, precision: precision)
69-
70-
if let levelsToEncode = levels {
71-
encodedLevels = encodeLevels(levelsToEncode)
72-
}
73-
}
74-
75-
/// This designated init decodes a polyline String to an [CLLocation]
76-
///
77-
/// :param: encodedPolyline The polyline that you want to decode
78-
/// :param: encodedLevels The levels that you want to decode
79-
public init(encodedPolyline: String, encodedLevels: String? = nil, precision: Double = 1e5) {
80-
81-
self.encodedPolyline = encodedPolyline
82-
coordinates = []
83-
84-
if let decodedCoordinates: [CLLocationCoordinate2D] = decodePolyline(encodedPolyline, precision: precision) {
85-
coordinates = decodedCoordinates
86-
}
87-
88-
if let levelsToDecode = encodedLevels {
89-
self.encodedLevels = levelsToDecode
90-
91-
if let decodedLevels = decodeLevels(levelsToDecode) {
92-
levels = decodedLevels
93-
}
94-
}
95-
}
96-
97-
/// This init encodes an [CLLocation] to a String
98-
///
99-
/// :param: locations The array of CLLocation that you want to encode
100-
/// :param: levels The optional array of levels that you want to encode
101-
public init(locations: [CLLocation], levels: [UInt32]? = nil, precision: Double = 1e5) {
102-
103-
let coordinates = toCoordinates(locations)
65+
66+
self.coordinates = coordinates
67+
self.levels = levels
68+
69+
encodedPolyline = encodeCoordinates(coordinates, precision: precision)
70+
71+
encodedLevels = levels.map(encodeLevels)
72+
}
73+
74+
/// This designated initializer decodes a polyline String
75+
///
76+
/// :param: encodedPolyline The polyline that you want to decode
77+
/// :param: encodedLevels The levels that you want to decode (default: nil)
78+
/// :param: precision The precision used for decoding (default: 1e5)
79+
public init(encodedPolyline: String, encodedLevels: String? = nil, precision: Double = 1e5) {
80+
81+
self.encodedPolyline = encodedPolyline
82+
self.encodedLevels = encodedLevels
83+
84+
coordinates = decodePolyline(encodedPolyline, precision: precision)
85+
86+
levels = self.encodedLevels.flatMap(decodeLevels)
87+
}
88+
89+
/// This init encodes a [CLLocation]
90+
///
91+
/// :param: locations The array of CLLocation that you want to encode
92+
/// :param: levels The optional array of levels that you want to encode (default: nil)
93+
/// :param: precision The precision used for encoding (default: 1e5)
94+
public init(locations: [CLLocation], levels: [UInt32]? = nil, precision: Double = 1e5) {
95+
96+
let coordinates = toCoordinates(locations)
10497
self.init(coordinates: coordinates, levels: levels, precision:precision)
105-
}
98+
}
10699
}
107100

108101
// MARK: - Public Functions -
109102

110103
/// This function encodes an [CLLocationCoordinate2D] to a String
111104
///
112105
/// :param: coordinates The array of CLLocationCoordinate2D that you want to encode
106+
/// :param: precision The precision used to encode coordinates (default: 1e5)
113107
///
114108
/// :returns: A String representing the encoded Polyline
115109
public func encodeCoordinates(coordinates: [CLLocationCoordinate2D], precision: Double = 1e5) -> String {
@@ -131,9 +125,10 @@ public func encodeCoordinates(coordinates: [CLLocationCoordinate2D], precision:
131125
return encodedPolyline
132126
}
133127

134-
/// This function encodes an [CLLocationCoordinate2D] to a String
128+
/// This function encodes an [CLLocation] to a String
135129
///
136-
/// :param: coordinates The array of CLLocationCoordinate2D that you want to encode
130+
/// :param: coordinates The array of CLLocation that you want to encode
131+
/// :param: precision The precision used to encode locations (default: 1e5)
137132
///
138133
/// :returns: A String representing the encoded Polyline
139134
public func encodeLocations(locations: [CLLocation], precision: Double = 1e5) -> String {
@@ -152,9 +147,10 @@ public func encodeLevels(levels: [UInt32]) -> String {
152147
}
153148
}
154149

155-
/// This function decodes a String to an [CLLocationCoordinate2D]
150+
/// This function decodes a String to a [CLLocationCoordinate2D]?
156151
///
157152
/// :param: encodedPolyline String representing the encoded Polyline
153+
/// :param: precision The precision used to decode coordinates (default: 1e5)
158154
///
159155
/// :returns: A [CLLocationCoordinate2D] representing the decoded polyline if valid, nil otherwise
160156
public func decodePolyline(encodedPolyline: String, precision: Double = 1e5) -> [CLLocationCoordinate2D]? {
@@ -186,9 +182,10 @@ public func decodePolyline(encodedPolyline: String, precision: Double = 1e5) ->
186182
return decodedCoordinates
187183
}
188184

189-
/// This function decodes a String to an [CLLocation]
185+
/// This function decodes a String to a [CLLocation]?
190186
///
191187
/// :param: encodedPolyline String representing the encoded Polyline
188+
/// :param: precision The precision used to decode locations (default: 1e5)
192189
///
193190
/// :returns: A [CLLocation] representing the decoded polyline if valid, nil otherwise
194191
public func decodePolyline(encodedPolyline: String, precision: Double = 1e5) -> [CLLocation]? {
@@ -205,7 +202,7 @@ public func decodeLevels(encodedLevels: String) -> [UInt32]? {
205202
var remainingLevels = encodedLevels.unicodeScalars
206203
var decodedLevels = [UInt32]()
207204

208-
while countElements(remainingLevels) > 0 {
205+
while count(remainingLevels) > 0 {
209206
var result = extractNextChunk(&remainingLevels)
210207
if result.failed {
211208
return nil
@@ -275,7 +272,7 @@ private func encodeFiveBitComponents(value: Int) -> String {
275272

276273
// MARK: Decode Coordinate
277274

278-
// We use a byte array (UnsafePointer<Int8>) here for performance reasons. Check with swift 1.2 if we can
275+
// We use a byte array (UnsafePointer<Int8>) here for performance reasons. Check with swift 1.2 if we can
279276
// go back to using [Int8]
280277
private func decodeSingleCoordinate(#byteArray: UnsafePointer<Int8>, #length: Int, inout #position: Int, precision: Double = 1e5) -> Result<Double> {
281278

PolylineTests/FunctionalPolylineTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ class FunctionalPolylineTests : XCTestCase {
9696

9797
func testEmptyPolylineShouldBeEmptyLocationArray() {
9898
let coordinates: [CLLocationCoordinate2D] = decodePolyline("")!
99-
100-
XCTAssertEqual(countElements(coordinates), 0)
99+
100+
XCTAssertEqual(count(coordinates), 0)
101101
}
102102

103103
func testInvalidPolylineShouldReturnEmptyLocationArray() {
@@ -107,7 +107,7 @@ class FunctionalPolylineTests : XCTestCase {
107107
func testValidPolylineShouldReturnValidLocationArray() {
108108
let coordinates: [CLLocationCoordinate2D] = decodePolyline("_p~iF~ps|U_ulLnnqC_mqNvxq`@")!
109109

110-
XCTAssertEqual(countElements(coordinates), 3)
110+
XCTAssertEqual(count(coordinates), 3)
111111
XCTAssertEqualWithAccuracy(coordinates[0].latitude, 38.5, COORD_EPSILON)
112112
XCTAssertEqualWithAccuracy(coordinates[0].longitude, -120.2, COORD_EPSILON)
113113
XCTAssertEqualWithAccuracy(coordinates[1].latitude, 40.7, COORD_EPSILON)
@@ -119,7 +119,7 @@ class FunctionalPolylineTests : XCTestCase {
119119
func testAnotherValidPolylineShouldReturnValidLocationArray() {
120120
let coordinates: [CLLocationCoordinate2D] = decodePolyline("_ojiHa`tLh{IdCw{Gwc_@")!
121121

122-
XCTAssertEqual(countElements(coordinates), 3)
122+
XCTAssertEqual(count(coordinates), 3)
123123
XCTAssertEqualWithAccuracy(coordinates[0].latitude, 48.8832, COORD_EPSILON)
124124
XCTAssertEqualWithAccuracy(coordinates[0].longitude, 2.23761, COORD_EPSILON)
125125
XCTAssertEqualWithAccuracy(coordinates[1].latitude, 48.82747, COORD_EPSILON)
@@ -142,7 +142,7 @@ class FunctionalPolylineTests : XCTestCase {
142142

143143
func testEmptyLevelsShouldBeEmptyLevelArray() {
144144
if let resultArray = decodeLevels("") {
145-
XCTAssertEqual(countElements(resultArray), 0)
145+
XCTAssertEqual(count(resultArray), 0)
146146
} else {
147147
XCTFail("Level array should not be nil for empty string")
148148
}
@@ -158,7 +158,7 @@ class FunctionalPolylineTests : XCTestCase {
158158

159159
func testValidLevelsShouldReturnValidLevelArray() {
160160
if let resultArray = decodeLevels("?@AB~F") {
161-
XCTAssertEqual(countElements(resultArray), 5)
161+
XCTAssertEqual(count(resultArray), 5)
162162
XCTAssertEqual(resultArray[0], UInt32(0))
163163
XCTAssertEqual(resultArray[1], UInt32(1))
164164
XCTAssertEqual(resultArray[2], UInt32(2))

0 commit comments

Comments
 (0)