Skip to content

Commit

Permalink
First implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
csanfilippo committed Jul 3, 2022
1 parent d4906b9 commit e712cd1
Show file tree
Hide file tree
Showing 49 changed files with 5,635 additions and 13 deletions.
21 changes: 9 additions & 12 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,15 @@ timeline.xctimeline
playground.xcworkspace

# Swift Package Manager
#
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
# Packages/
# Package.pins
# Package.resolved
# *.xcodeproj
#
# Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata
# hence it is not needed unless you have added a package configuration file to your project
# .swiftpm

.build/
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/
DerivedData/
.swiftpm/config/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc

# CocoaPods
#
Expand Down
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions .idea/swift-sgp.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// swift-tools-version: 5.6

import PackageDescription

let package = Package(
name: "SGPKit",
platforms: [.iOS(.v13)],
products: [
.library(
name: "SGPKit",
targets: ["SGPKit"]),
],
dependencies: [

],
targets: [
.target(
name: "SGPKit",
dependencies: ["SGPKitOBJC"]),
.target(
name: "SGPKitCPP",
path: "Sources/sgp4-f5cb54b"
),
.target(
name: "SGPKitOBJC",
dependencies: ["SGPKitCPP"],
path: "Sources/OBJC"),
.testTarget(
name: "SGPKitTests",
dependencies: ["SGPKit"]),
]
)
41 changes: 40 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,41 @@
# swift-spg
A Swift package to compute satellite positions from two-line elements (TLE).
A Swift package to compute satellite positions from two-line elements (TLE), wrapping the [sgp4lib](https://www.danrw.com/sgp4/) library (by Daniel Warner)


## Usage

```swift
import SGPKit

let firstLine = "1 25544U 98067A 13165.59097222 .00004759 00000-0 88814-4 0 47"
let secondLine = "2 25544 51.6478 121.2152 0011003 68.5125 263.9959 15.50783143834295"

// Instantiate a new TLE descriptor
let tle = TLE(firstLine: firstLine, secondLine: secondLine)

// Instantiate the interpreter
let interpreter = TLEInterpreter()

// Obtain the data
let data: SatelliteData = interpreter.satelliteData(from: tle, date: .now)

print(data.latitude)
print(data.longitude)
print(data.altitude)
print(data.speed)
```

## Installation

### Swift Package Manager

If you want to use SGPKit in any other project that uses [SwiftPM](https://swift.org/package-manager/), add the package as a dependency in `Package.swift`:

```swift
dependencies: [
.package(
url: "https://github.com/csanfilippo/swift-sgp",
from: "1.0.0"
),
]
```
84 changes: 84 additions & 0 deletions Sources/OBJC/SGP4Wrapper.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
MIT License
Copyright (c) 2022 Calogero Sanfilippo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#import "SGP4Wrapper.h"
#import <Foundation/Foundation.h>
#import <iostream>
#import <SGP4.h>
#import <Tle.h>
#import <DateTime.h>
#import <CoordGeodetic.h>
#import <Eci.h>
using namespace std;

@implementation SGP4Wrapper

- (SatelliteData* _Nonnull) getSatelliteDataFrom:(TLEWrapper*) tleWrapper date:(NSDate*) date {
SGP4 (^getSGP4)(void) = ^{

string first("ISS (ZARYA)");
string second(tleWrapper.firstLine.UTF8String);
string third(tleWrapper.secondLine.UTF8String);

Tle tle(first, second, third);
SGP4 sgp4(tle);
return sgp4;
};

SGP4 sgp4 = getSGP4();

DateTime currentTime = [self dateTimeFrom: date];

SatelliteData *data = [self issDataFromSgp4:sgp4 Date:currentTime];

return data;
}

- (DateTime) dateTimeFrom:(NSDate*) date {

NSCalendar *calendar = [NSCalendar currentCalendar];
calendar.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];

int day = static_cast<int>([calendar component:NSCalendarUnitDay fromDate:date]);
int month = static_cast<int>([calendar component:NSCalendarUnitMonth fromDate:date]);
int year = static_cast<int>([calendar component:NSCalendarUnitYear fromDate:date]);
int hour = static_cast<int>([calendar component:NSCalendarUnitHour fromDate:date]);
int minute = static_cast<int>([calendar component:NSCalendarUnitMinute fromDate:date]);
int second = static_cast<int>([calendar component:NSCalendarUnitSecond fromDate:date]);
int micro = static_cast<int>([calendar component:NSCalendarUnitNanosecond fromDate:date] / 1000);

DateTime time = DateTime(year, month, day, hour, minute, second);
time.Initialise(year, month, day, hour, minute, second, micro);
return time;
}

- (SatelliteData *) issDataFromSgp4:(SGP4) sgp4 Date:(DateTime) date {
Eci eci = sgp4.FindPosition(date);
CoordGeodetic geo = eci.ToGeodetic();

double velocity = eci.Velocity().Magnitude() * 3600.0;
return [[SatelliteData alloc] initWithLatitude:geo.latitude * 180.0 / M_PI longitude:geo.longitude * 180.0 / M_PI speed:velocity altitude:geo.altitude];
}

@end
46 changes: 46 additions & 0 deletions Sources/OBJC/SatelliteData.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
MIT License
Copyright (c) 2022 Calogero Sanfilippo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#import "SatelliteData.h"

@interface SatelliteData ()
@property(readwrite) double latitude;
@property(readwrite) double longitude;
@property(readwrite) double speed;
@property(readwrite) double altitude;
@end

@implementation SatelliteData
- (instancetype) initWithLatitude:(double) latitude longitude:(double) longitude speed:(double) speed altitude:(double) altitude {
self = [super init];
if (self) {
self.latitude = latitude;
self.longitude = longitude;
self.speed = speed;
self.altitude = altitude;
}
return self;
}

@end
40 changes: 40 additions & 0 deletions Sources/OBJC/TLEWrapper.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
MIT License
Copyright (c) 2022 Calogero Sanfilippo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#import "TLEWrapper.h"

@implementation TLEWrapper

- (instancetype)initWithFirstLine:(NSString *)firstLine secondLine:(NSString *)secondLine {
self = [super init];

if (self) {
self.firstLine = firstLine;
self.secondLine = secondLine;
}

return self;
}

@end
37 changes: 37 additions & 0 deletions Sources/OBJC/include/SGP4Wrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
MIT License
Copyright (c) 2022 Calogero Sanfilippo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#import <Foundation/Foundation.h>
#import "SatelliteData.h"
#import "TLEWrapper.h"

NS_ASSUME_NONNULL_BEGIN

@interface SGP4Wrapper : NSObject

- (SatelliteData* _Nonnull) getSatelliteDataFrom:(TLEWrapper* _Nonnull) tle date:(NSDate* _Nonnull) date;

@end

NS_ASSUME_NONNULL_END
35 changes: 35 additions & 0 deletions Sources/OBJC/include/SatelliteData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
MIT License
Copyright (c) 2022 Calogero Sanfilippo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#import <Foundation/Foundation.h>

@interface SatelliteData : NSObject
@property(readonly) double latitude;
@property(readonly) double longitude;
@property(readonly) double speed;
@property(readonly) double altitude;

- (instancetype) initWithLatitude:(double) latitude longitude:(double) longitude speed:(double) speed altitude:(double) altitude;

@end
Loading

0 comments on commit e712cd1

Please sign in to comment.