Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add conversions between enumerations and associated values #159

Merged
merged 3 commits into from
Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion Sources/Turf/FeatureIdentifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Foundation
/**
A [feature identifier](https://datatracker.ietf.org/doc/html/rfc7946#section-3.2) identifies a `Feature` object.
*/
public enum FeatureIdentifier: Equatable {
public enum FeatureIdentifier: Hashable {
/// A string.
case string(_ string: String)

Expand Down
29 changes: 29 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,27 @@ extension GeoJSONObject: Codable {
}
}
}

/**
A type that can be represented as a `GeoJSONObject` instance.
*/
public protocol GeoJSONObjectConvertible {
1ec5 marked this conversation as resolved.
Show resolved Hide resolved
/// The instance wrapped in a `GeoJSONObject` instance.
var geoJSONObject: GeoJSONObject { get }
}

extension GeoJSONObject: GeoJSONObjectConvertible {
public var geoJSONObject: GeoJSONObject { return self }
}

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) }
}
2 changes: 1 addition & 1 deletion Sources/Turf/JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Foundation

This type does not represent the `null` value in JSON. Use `Optional<JSONValue>` wherever `null` is accepted.
*/
public enum JSONValue: Equatable {
public enum JSONValue: Hashable {
// case null would be redundant to Optional.none

/// A string.
Expand Down
28 changes: 28 additions & 0 deletions Tests/TurfTests/GeoJSONTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,34 @@ 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(Geometry(Geometry(Geometry(Geometry(Point(nullIsland))))), .point(.init(nullIsland)))

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: [])))

XCTAssertEqual(GeoJSONObject(GeoJSONObject(GeoJSONObject(GeoJSONObject(Geometry(Point(nullIsland)))))),
.geometry(.point(.init(nullIsland))))
}

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