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 GeoJSON #1

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/swift-geo-Package.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,20 @@
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "GeoJSON"
BuildableName = "GeoJSON"
BlueprintName = "GeoJSON"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
Expand Down Expand Up @@ -310,6 +324,16 @@
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "GeoJSONTests"
BuildableName = "GeoJSONTests"
BlueprintName = "GeoJSONTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
Expand Down
15 changes: 15 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ let package = Package(
.library(name: "WGS84", targets: ["WGS84"]),
.library(name: "GeodeticGeometry", targets: ["GeodeticGeometry"]),
.library(name: "Turf", targets: ["Turf"]),
.library(name: "GeoJSON", targets: ["GeoJSON"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-algorithms", .upToNextMajor(from: "1.0.0")),
Expand Down Expand Up @@ -137,5 +138,19 @@ let package = Package(
"GeodeticConversions",
"WGS84Conversions",
]),

// 📄 GeoJSON representation
.target(
name: "GeoJSON",
dependencies: [
.target(name: "GeodeticGeometry"),
.target(name: "WGS84Turf"),
.product(name: "NonEmpty", package: "swift-nonempty"),
]
),
.testTarget(
name: "GeoJSONTests",
dependencies: ["GeoJSON"]
),
]
)
61 changes: 61 additions & 0 deletions Sources/GeoJSON/BoundingBox.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// BoundingBox.swift
// GeoSwift
//
// Created by Rémi Bardon on 04/02/2022.
// Copyright © 2022 Rémi Bardon. All rights reserved.
//

import GeoModels

/// A [GeoJSON Bounding Box](https://datatracker.ietf.org/doc/html/rfc7946#section-5).
public protocol BoundingBox: GeoModels.BoundingBox, Codable {

/// This bonding box, but type-erased.
var asAny: AnyBoundingBox { get }

}

/// A two-dimensional ``BoundingBox``.
public typealias BoundingBox2D = GeoModels.BoundingBox2D

extension BoundingBox2D: BoundingBox {

public var asAny: AnyBoundingBox { .twoDimensions(self) }

}

/// A three-dimensional ``BoundingBox``.
public typealias BoundingBox3D = GeoModels.BoundingBox3D

extension BoundingBox3D: BoundingBox {

public var asAny: AnyBoundingBox { .threeDimensions(self) }

}

/// A type-erased ``BoundingBox``.
public enum AnyBoundingBox: BoundingBox, Hashable, Codable {

public static var zero: AnyBoundingBox = .twoDimensions(.zero)

public func union(_ other: AnyBoundingBox) -> AnyBoundingBox {
switch (self, other) {
case let (.twoDimensions(self), .twoDimensions(other)):
return .twoDimensions(self.union(other))

case let (.twoDimensions(bbox2d), .threeDimensions(bbox3d)),
let (.threeDimensions(bbox3d), .twoDimensions(bbox2d)):
return .threeDimensions(bbox3d.union(bbox2d))

case let (.threeDimensions(self), .threeDimensions(other)):
return .threeDimensions(self.union(other))
}
}

case twoDimensions(BoundingBox2D)
case threeDimensions(BoundingBox3D)

public var asAny: AnyBoundingBox { self }

}
17 changes: 17 additions & 0 deletions Sources/GeoJSON/Errors.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// Errors.swift
// SwiftGeo
//
// Created by Rémi Bardon on 07/02/2022.
// Copyright © 2022 Rémi Bardon. All rights reserved.
//

import Foundation

/// Error when creating ``LinearRingCoordinates``.
///
/// See [RFC 7946, section 3.1.6](https://datatracker.ietf.org/doc/html/rfc7946#section-3.1.6).
public enum LinearRingError: Error {
case firstAndLastPositionsShouldBeEquivalent
case notEnoughPoints
}
Loading