Skip to content

Commit 8eedf47

Browse files
committed
Clarify what the bounding box constructors do and don't.
Add new GeoJSON constructors that can provide bounding boxes
1 parent 82037ea commit 8eedf47

1 file changed

Lines changed: 14 additions & 6 deletions

File tree

Sources/GeoJSONKit/GeoJSON.swift

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,11 @@ public struct GeoJSON: Hashable {
328328
public let maximumElevation: Distance?
329329
private let minWasFirst: Bool
330330

331+
/// Creates a bounding box encapsulating the provided positions
332+
///
333+
/// - warning: This does not deal with spanning the antimeridian. So, the Fiji case from the GeoJSON specs would result in a 355 degree bounding box and not a 5 degree one. To deal with this, apply the appropriate logic on a level above and then use the `init(coordinates:)` constructor.
334+
///
335+
/// - Parameter positions: Positions to create the bounding box from; has to be at least one!
331336
public init(positions: [Position]) {
332337
guard let first = positions.first else { preconditionFailure() }
333338
var minLat = first.latitude
@@ -349,6 +354,9 @@ public struct GeoJSON: Hashable {
349354
minWasFirst = false
350355
}
351356

357+
358+
/// Create a bounding box from the provided coordinates
359+
/// - Parameter coordinates: 4 or 6 elements in order: south-westerly longitude, south-westerly latitude, (elevation 1), north-easterly lontigude, north-easterly latitude, (elevation 2); where the elevations are optional (but either provide both or none) and there's no required order in which is the minimum and which is the maximum elevation.
352360
public init(coordinates: [Degrees]) throws {
353361
switch coordinates.count {
354362
case 6:
@@ -398,25 +406,25 @@ public struct GeoJSON: Hashable {
398406
public var additionalFields: [String: AnyHashable]
399407

400408
/// Initialises a new FeatureCollection.
401-
public init(features: [Feature], additionalFields: [String: AnyHashable] = [:]) {
409+
public init(features: [Feature], additionalFields: [String: AnyHashable] = [:], boundingBox: BoundingBox? = nil) {
402410
type = .featureCollection
403411
object = .featureCollection(features)
404412
self.additionalFields = additionalFields
405-
boundingBox = nil
413+
self.boundingBox = boundingBox
406414
}
407415

408-
public init(feature: Feature, additionalFields: [String: AnyHashable] = [:]) {
416+
public init(feature: Feature, additionalFields: [String: AnyHashable] = [:], boundingBox: BoundingBox? = nil) {
409417
type = .feature
410418
object = .feature(feature)
411419
self.additionalFields = additionalFields
412-
boundingBox = nil
420+
self.boundingBox = boundingBox
413421
}
414422

415-
public init(geometry: GeometryObject, additionalFields: [String: AnyHashable] = [:]) {
423+
public init(geometry: GeometryObject, additionalFields: [String: AnyHashable] = [:], boundingBox: BoundingBox? = nil) {
416424
type = geometry.type
417425
object = .geometry(geometry)
418426
self.additionalFields = additionalFields
419-
boundingBox = nil
427+
self.boundingBox = boundingBox
420428
}
421429

422430
public init(data: Data, textEncoding: String? = nil) throws {

0 commit comments

Comments
 (0)