Skip to content

Commit 53910de

Browse files
committed
Convert between enumerations and associated values
1 parent 932dea0 commit 53910de

File tree

4 files changed

+103
-1
lines changed

4 files changed

+103
-1
lines changed

Sources/Turf/Feature.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,18 @@ public struct Feature: Equatable {
2525

2626
- parameter geometry: The geometry at which the feature is located.
2727
*/
28-
public init(geometry: Geometry?) {
28+
public init(geometry: Geometry) {
2929
self.geometry = geometry
3030
}
31+
32+
/**
33+
Initializes a feature defined by the given geometry-convertible instance.
34+
35+
- parameter geometry: The geometry-convertible instance that bounds the feature.
36+
*/
37+
public init(geometry: GeometryConvertible?) {
38+
self.geometry = geometry?.geometry
39+
}
3140
}
3241

3342
extension Feature: Codable {

Sources/Turf/GeoJSON.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public enum GeoJSONObject: Equatable {
2828
- parameter featureCollection: The GeoJSON object as a FeatureCollection object.
2929
*/
3030
case featureCollection(_ featureCollection: FeatureCollection)
31+
32+
/// Initializes a GeoJSON object representing the given GeoJSON object–convertible instance.
33+
public init(_ object: GeoJSONObjectConvertible) {
34+
self = object.geoJSONObject
35+
}
3136
}
3237

3338
extension GeoJSONObject: Codable {
@@ -60,3 +65,23 @@ extension GeoJSONObject: Codable {
6065
}
6166
}
6267
}
68+
69+
/**
70+
A type that can be represented as a `GeoJSONObject` instance.
71+
*/
72+
public protocol GeoJSONObjectConvertible {
73+
/// The instance wrapped in a `GeoJSONObject` instance.
74+
var geoJSONObject: GeoJSONObject { get }
75+
}
76+
77+
extension Geometry: GeoJSONObjectConvertible {
78+
public var geoJSONObject: GeoJSONObject { return .geometry(self) }
79+
}
80+
81+
extension Feature: GeoJSONObjectConvertible {
82+
public var geoJSONObject: GeoJSONObject { return .feature(self) }
83+
}
84+
85+
extension FeatureCollection: GeoJSONObjectConvertible {
86+
public var geoJSONObject: GeoJSONObject { return .featureCollection(self) }
87+
}

Sources/Turf/Geometry.swift

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ public enum Geometry: Equatable {
2727

2828
/// A heterogeneous collection of geometries that are related.
2929
case geometryCollection(_ geometry: GeometryCollection)
30+
31+
/// Initializes a geometry representing the given geometry–convertible instance.
32+
public init(_ geometry: GeometryConvertible) {
33+
self = geometry.geometry
34+
}
3035
}
3136

3237
extension Geometry: Codable {
@@ -85,3 +90,43 @@ extension Geometry: Codable {
8590
}
8691
}
8792
}
93+
94+
/**
95+
A type that can be represented as a `Geometry` instance.
96+
*/
97+
public protocol GeometryConvertible {
98+
/// The instance wrapped in a `Geometry` instance.
99+
var geometry: Geometry { get }
100+
}
101+
102+
extension Geometry: GeometryConvertible {
103+
public var geometry: Geometry { return self }
104+
}
105+
106+
extension Point: GeometryConvertible {
107+
public var geometry: Geometry { return .point(self) }
108+
}
109+
110+
extension LineString: GeometryConvertible {
111+
public var geometry: Geometry { return .lineString(self) }
112+
}
113+
114+
extension Polygon: GeometryConvertible {
115+
public var geometry: Geometry { return .polygon(self) }
116+
}
117+
118+
extension MultiPoint: GeometryConvertible {
119+
public var geometry: Geometry { return .multiPoint(self) }
120+
}
121+
122+
extension MultiLineString: GeometryConvertible {
123+
public var geometry: Geometry { return .multiLineString(self) }
124+
}
125+
126+
extension MultiPolygon: GeometryConvertible {
127+
public var geometry: Geometry { return .multiPolygon(self) }
128+
}
129+
130+
extension GeometryCollection: GeometryConvertible {
131+
public var geometry: Geometry { return .geometryCollection(self) }
132+
}

Tests/TurfTests/GeoJSONTests.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,29 @@ import struct Turf.Polygon
66
import CoreLocation
77

88
class GeoJSONTests: XCTestCase {
9+
func testConversion() {
10+
let nullIsland = LocationCoordinate2D(latitude: 0, longitude: 0)
11+
XCTAssertEqual(Geometry(Point(nullIsland)),
12+
.point(Point(nullIsland)))
13+
XCTAssertEqual(Geometry(LineString([nullIsland, nullIsland])),
14+
.lineString(LineString([nullIsland, nullIsland])))
15+
XCTAssertEqual(Geometry(Polygon([[nullIsland, nullIsland, nullIsland]])),
16+
.polygon(Polygon([[nullIsland, nullIsland, nullIsland]])))
17+
XCTAssertEqual(Geometry(MultiPoint([nullIsland, nullIsland, nullIsland])),
18+
.multiPoint(MultiPoint([nullIsland, nullIsland, nullIsland])))
19+
XCTAssertEqual(Geometry(MultiLineString([[nullIsland, nullIsland, nullIsland]])),
20+
.multiLineString(MultiLineString([[nullIsland, nullIsland, nullIsland]])))
21+
XCTAssertEqual(Geometry(MultiPolygon([[[nullIsland, nullIsland, nullIsland]]])),
22+
.multiPolygon(MultiPolygon([[[nullIsland, nullIsland, nullIsland]]])))
23+
XCTAssertEqual(Geometry(GeometryCollection(geometries: [])),
24+
.geometryCollection(GeometryCollection(geometries: [])))
25+
26+
XCTAssertEqual(GeoJSONObject(Geometry(Point(nullIsland))), .geometry(.point(.init(nullIsland))))
27+
XCTAssertEqual(GeoJSONObject(Feature(geometry: nil)), .feature(.init(geometry: nil)))
28+
let nullGeometry: Geometry? = nil
29+
XCTAssertEqual(GeoJSONObject(Feature(geometry: nullGeometry)), .feature(.init(geometry: nil)))
30+
XCTAssertEqual(GeoJSONObject(FeatureCollection(features: [])), .featureCollection(.init(features: [])))
31+
}
932

1033
func testPoint() {
1134
let coordinate = LocationCoordinate2D(latitude: 10, longitude: 30)

0 commit comments

Comments
 (0)