Skip to content

Commit

Permalink
Convert between enumerations and associated values
Browse files Browse the repository at this point in the history
  • Loading branch information
1ec5 committed Oct 6, 2021
1 parent 932dea0 commit 53910de
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 1 deletion.
11 changes: 10 additions & 1 deletion Sources/Turf/Feature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,18 @@ public struct Feature: Equatable {
- parameter geometry: The geometry at which the feature is located.
*/
public init(geometry: Geometry?) {
public init(geometry: Geometry) {
self.geometry = geometry
}

/**
Initializes a feature defined by the given geometry-convertible instance.
- parameter geometry: The geometry-convertible instance that bounds the feature.
*/
public init(geometry: GeometryConvertible?) {
self.geometry = geometry?.geometry
}
}

extension Feature: Codable {
Expand Down
25 changes: 25 additions & 0 deletions Sources/Turf/GeoJSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public enum GeoJSONObject: Equatable {
- parameter featureCollection: The GeoJSON object as a FeatureCollection object.
*/
case featureCollection(_ featureCollection: FeatureCollection)

/// Initializes a GeoJSON object representing the given GeoJSON object–convertible instance.
public init(_ object: GeoJSONObjectConvertible) {
self = object.geoJSONObject
}
}

extension GeoJSONObject: Codable {
Expand Down Expand Up @@ -60,3 +65,23 @@ extension GeoJSONObject: Codable {
}
}
}

/**
A type that can be represented as a `GeoJSONObject` instance.
*/
public protocol GeoJSONObjectConvertible {
/// The instance wrapped in a `GeoJSONObject` instance.
var geoJSONObject: GeoJSONObject { get }
}

extension Geometry: GeoJSONObjectConvertible {
public var geoJSONObject: GeoJSONObject { return .geometry(self) }
}

extension Feature: GeoJSONObjectConvertible {
public var geoJSONObject: GeoJSONObject { return .feature(self) }
}

extension FeatureCollection: GeoJSONObjectConvertible {
public var geoJSONObject: GeoJSONObject { return .featureCollection(self) }
}
45 changes: 45 additions & 0 deletions Sources/Turf/Geometry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public enum Geometry: Equatable {

/// A heterogeneous collection of geometries that are related.
case geometryCollection(_ geometry: GeometryCollection)

/// Initializes a geometry representing the given geometry–convertible instance.
public init(_ geometry: GeometryConvertible) {
self = geometry.geometry
}
}

extension Geometry: Codable {
Expand Down Expand Up @@ -85,3 +90,43 @@ extension Geometry: Codable {
}
}
}

/**
A type that can be represented as a `Geometry` instance.
*/
public protocol GeometryConvertible {
/// The instance wrapped in a `Geometry` instance.
var geometry: Geometry { get }
}

extension Geometry: GeometryConvertible {
public var geometry: Geometry { return self }
}

extension Point: GeometryConvertible {
public var geometry: Geometry { return .point(self) }
}

extension LineString: GeometryConvertible {
public var geometry: Geometry { return .lineString(self) }
}

extension Polygon: GeometryConvertible {
public var geometry: Geometry { return .polygon(self) }
}

extension MultiPoint: GeometryConvertible {
public var geometry: Geometry { return .multiPoint(self) }
}

extension MultiLineString: GeometryConvertible {
public var geometry: Geometry { return .multiLineString(self) }
}

extension MultiPolygon: GeometryConvertible {
public var geometry: Geometry { return .multiPolygon(self) }
}

extension GeometryCollection: GeometryConvertible {
public var geometry: Geometry { return .geometryCollection(self) }
}
23 changes: 23 additions & 0 deletions Tests/TurfTests/GeoJSONTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,29 @@ import struct Turf.Polygon
import CoreLocation

class GeoJSONTests: XCTestCase {
func testConversion() {
let nullIsland = LocationCoordinate2D(latitude: 0, longitude: 0)
XCTAssertEqual(Geometry(Point(nullIsland)),
.point(Point(nullIsland)))
XCTAssertEqual(Geometry(LineString([nullIsland, nullIsland])),
.lineString(LineString([nullIsland, nullIsland])))
XCTAssertEqual(Geometry(Polygon([[nullIsland, nullIsland, nullIsland]])),
.polygon(Polygon([[nullIsland, nullIsland, nullIsland]])))
XCTAssertEqual(Geometry(MultiPoint([nullIsland, nullIsland, nullIsland])),
.multiPoint(MultiPoint([nullIsland, nullIsland, nullIsland])))
XCTAssertEqual(Geometry(MultiLineString([[nullIsland, nullIsland, nullIsland]])),
.multiLineString(MultiLineString([[nullIsland, nullIsland, nullIsland]])))
XCTAssertEqual(Geometry(MultiPolygon([[[nullIsland, nullIsland, nullIsland]]])),
.multiPolygon(MultiPolygon([[[nullIsland, nullIsland, nullIsland]]])))
XCTAssertEqual(Geometry(GeometryCollection(geometries: [])),
.geometryCollection(GeometryCollection(geometries: [])))

XCTAssertEqual(GeoJSONObject(Geometry(Point(nullIsland))), .geometry(.point(.init(nullIsland))))
XCTAssertEqual(GeoJSONObject(Feature(geometry: nil)), .feature(.init(geometry: nil)))
let nullGeometry: Geometry? = nil
XCTAssertEqual(GeoJSONObject(Feature(geometry: nullGeometry)), .feature(.init(geometry: nil)))
XCTAssertEqual(GeoJSONObject(FeatureCollection(features: [])), .featureCollection(.init(features: [])))
}

func testPoint() {
let coordinate = LocationCoordinate2D(latitude: 10, longitude: 30)
Expand Down

0 comments on commit 53910de

Please sign in to comment.