Skip to content

Commit 746914e

Browse files
RD-1341: Add helper converters (#217)
1 parent 27b31be commit 746914e

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// Copyright (c) 2025, MapTiler
3+
// All rights reserved.
4+
// SPDX-License-Identifier: BSD 3-Clause
5+
//
6+
// ConvertGPX.swift
7+
// MapTilerSDK
8+
//
9+
10+
import Foundation
11+
12+
package struct ConvertGPX: MTValueCommand {
13+
var gpxString: String
14+
15+
package func toJS() -> JSString {
16+
let escaped = gpxString
17+
.replacingOccurrences(of: "\\", with: "\\\\")
18+
.replacingOccurrences(of: "'", with: "\\'")
19+
.replacingOccurrences(of: "\n", with: "\\n")
20+
.replacingOccurrences(of: "\r", with: "")
21+
22+
return """
23+
(() => {
24+
const res = \(MTBridge.sdkObject).gpx('\(escaped)');
25+
return JSON.stringify(res);
26+
})();
27+
"""
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// Copyright (c) 2025, MapTiler
3+
// All rights reserved.
4+
// SPDX-License-Identifier: BSD 3-Clause
5+
//
6+
// ConvertKML.swift
7+
// MapTilerSDK
8+
//
9+
10+
import Foundation
11+
12+
package struct ConvertKML: MTValueCommand {
13+
var kmlString: String
14+
15+
package func toJS() -> JSString {
16+
let escaped = kmlString
17+
.replacingOccurrences(of: "\\", with: "\\\\")
18+
.replacingOccurrences(of: "'", with: "\\'")
19+
.replacingOccurrences(of: "\n", with: "\\n")
20+
.replacingOccurrences(of: "\r", with: "")
21+
22+
return """
23+
(() => {
24+
const res = \(MTBridge.sdkObject).kml('\(escaped)');
25+
return JSON.stringify(res);
26+
})();
27+
"""
28+
}
29+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// Copyright (c) 2025, MapTiler
3+
// All rights reserved.
4+
// SPDX-License-Identifier: BSD 3-Clause
5+
//
6+
// MTConverters.swift
7+
// MapTilerSDK
8+
//
9+
10+
import Foundation
11+
12+
public extension MTMapView {
13+
/// Converts a GPX document string to a GeoJSON FeatureCollection JSON string.
14+
/// - Parameter gpxString: Raw GPX XML content.
15+
/// - Returns: A GeoJSON FeatureCollection encoded as a JSON string.
16+
func convertGPXToGeoJSON(_ gpxString: String) async throws -> String {
17+
let result = try await bridge.execute(ConvertGPX(gpxString: gpxString))
18+
guard case .string(let json) = result else {
19+
throw MTError.unsupportedReturnType(description: "Expected GeoJSON string from GPX conversion.")
20+
}
21+
return json
22+
}
23+
24+
/// Converts a KML document string to a GeoJSON FeatureCollection JSON string.
25+
/// - Parameter kmlString: Raw KML XML content.
26+
/// - Returns: A GeoJSON FeatureCollection encoded as a JSON string.
27+
func convertKMLToGeoJSON(_ kmlString: String) async throws -> String {
28+
let result = try await bridge.execute(ConvertKML(kmlString: kmlString))
29+
guard case .string(let json) = result else {
30+
throw MTError.unsupportedReturnType(description: "Expected GeoJSON string from KML conversion.")
31+
}
32+
return json
33+
}
34+
}

0 commit comments

Comments
 (0)