Skip to content

Commit

Permalink
add GeoJSON encoding to AirSpace
Browse files Browse the repository at this point in the history
  • Loading branch information
ekurutepe committed May 28, 2018
1 parent 9100259 commit e25458e
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
4 changes: 4 additions & 0 deletions Iguazu.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
51EDE51D20BC7B3D00F53408 /* GeoJsonEncodable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51EDE51C20BC7B3D00F53408 /* GeoJsonEncodable.swift */; };
8716DE371D115D63005312B2 /* IGCHeaderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8716DE361D115D63005312B2 /* IGCHeaderTests.swift */; };
871E012B1DFC72AD0072487D /* AirSpaceMapDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 871E012A1DFC72AD0072487D /* AirSpaceMapDelegate.swift */; };
8722EF311D0DD90500DA4509 /* Iguazu.h in Headers */ = {isa = PBXBuildFile; fileRef = 8722EF301D0DD90500DA4509 /* Iguazu.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -53,6 +54,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
51EDE51C20BC7B3D00F53408 /* GeoJsonEncodable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GeoJsonEncodable.swift; sourceTree = "<group>"; };
8716DE361D115D63005312B2 /* IGCHeaderTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IGCHeaderTests.swift; sourceTree = "<group>"; };
871E012A1DFC72AD0072487D /* AirSpaceMapDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AirSpaceMapDelegate.swift; sourceTree = "<group>"; };
8722EF2D1D0DD90500DA4509 /* Iguazu.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Iguazu.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -148,6 +150,7 @@
877B9CF41DFC1BE300D2EB91 /* CharacterSetExtensions.swift */,
877B9CF61DFC1C0F00D2EB91 /* ScannerExtensions.swift */,
871E012A1DFC72AD0072487D /* AirSpaceMapDelegate.swift */,
51EDE51C20BC7B3D00F53408 /* GeoJsonEncodable.swift */,
);
path = Iguazu;
sourceTree = "<group>";
Expand Down Expand Up @@ -342,6 +345,7 @@
8722EF4D1D0DDCEC00DA4509 /* IGCHeader.swift in Sources */,
8722EF481D0DDB8200DA4509 /* IGCData.swift in Sources */,
873CB3141DB3708800A6325E /* DateExtensions.swift in Sources */,
51EDE51D20BC7B3D00F53408 /* GeoJsonEncodable.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
50 changes: 45 additions & 5 deletions Iguazu/AirSpace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public enum AirSpaceAltitude {
}

guard let value = Int(impliedMSLString.trimmingCharacters(in: CharacterSet.decimalDigits.inverted).trimmingCharacters(in: .whitespaces)) else { return nil }

if impliedMSLString.hasPrefix("fl") {
self = .fl(flightLevel: value)
return
}

let unit: UnitLength

Expand All @@ -71,11 +76,6 @@ public enum AirSpaceAltitude {
unit = .feet
}

if impliedMSLString.hasPrefix("fl") {
self = .fl(flightLevel: value)
return
}

if impliedMSLString.hasSuffix("agl") {
self = .agl(altitude: Altitude(value: Double(value), unit: unit))
return
Expand Down Expand Up @@ -314,3 +314,43 @@ public extension AirSpace {
}
}

extension AirSpaceAltitude {
var asNSDictionary: NSDictionary {
switch self {
case .surface:
return [ "type": "surface" ] as NSDictionary
case .fl(let lvl):
return [ "type": "fl", "value": lvl ] as NSDictionary
case .agl(let alt):
return [ "type": "agl", "value": alt.value, "unit": alt.unit.symbol ] as NSDictionary
case .msl(let alt):
return [ "type": "msl", "value": alt.value, "unit": alt.unit.symbol ] as NSDictionary
}
}
}

extension AirSpace: GeoJsonEncodable {
var geoJsonString: String? {
let coordinatesArray: [[Double]] = (self.polygonCoordinates+[self.polygonCoordinates[0]]).reversed().map { [$0.longitude, $0.latitude] }

let dict: NSDictionary = [
"type": "Feature",
"properties": [
"name": self.name as NSString,
"type": self.class.rawValue as NSString,
"floor": self.floor.asNSDictionary,
"ceiling": self.ceiling.asNSDictionary,
] as NSDictionary,
"geometry": [
"type": "Polygon" as NSString,
"coordinates": [ coordinatesArray as NSArray ] as NSArray,
] as NSDictionary
]

guard JSONSerialization.isValidJSONObject(dict) else { return nil }
guard let data = try? JSONSerialization.data(withJSONObject: dict, options: []) else { return nil }
return String(data: data, encoding: .utf8)

}
}

13 changes: 13 additions & 0 deletions Iguazu/GeoJsonEncodable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// GeoJsonEncodable.swift
// Iguazu
//
// Created by Engin Kurutepe on 28.05.18.
// Copyright © 2018 Fifteen Jugglers Software. All rights reserved.
//

import Foundation

protocol GeoJsonEncodable {
var geoJsonString: String? { get }
}
9 changes: 9 additions & 0 deletions IguazuTests/AirSpaceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,13 @@ class AirSpaceTests : XCTestCase {
XCTAssertNotNil(airSpaces)
XCTAssertTrue(airSpaces!.count > 0)
}

func testGeoJsonEncoding() {
let airSpaces = AirSpace.airSpaces(from: self.openAirString)
XCTAssertNotNil(airSpaces)
let asp = airSpaces![0]
let geoJson = asp.geoJsonString
XCTAssertEqual(geoJson, "{\"type\":\"Feature\",\"properties\":{\"ceiling\":{\"type\":\"fl\",\"value\":65},\"name\":\"TMZ-EDLW 129.875\",\"type\":\"TMZ\",\"floor\":{\"type\":\"msl\",\"value\":4500,\"unit\":\"ft\"}},\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[7.3055555555555554,51.516666666666666],[7.3055555555555554,51.516666666666666],[7.3433333333333328,51.423888888888889],[7.4625000000000004,51.31305555555555],[7.4519444444444449,51.408333333333331],[7.3741666666666665,51.493055555555557],[7.3055555555555554,51.516666666666666]]]}}")

}
}

0 comments on commit e25458e

Please sign in to comment.