Skip to content

Commit

Permalink
Expose tilecover experimental API (#1469)
Browse files Browse the repository at this point in the history
* Expose tilecover experimental API

* Use tilecover api in debug map example

* Add integration tests for tileCover

* fix spaces

* Update Changelog

* review fixes

* fix testplan

* add test for tileCover with the default parameter
  • Loading branch information
persidskiy committed Apr 5, 2023
1 parent c16081e commit ee74972
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 36 deletions.
8 changes: 4 additions & 4 deletions Apps/Examples/Examples.xcodeproj/project.pbxproj
Expand Up @@ -106,7 +106,7 @@
CADCF7302584990E0065C51B /* LayerPositionExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 666E0D4925664EE7000B8AF5 /* LayerPositionExample.swift */; };
CADCF733258499130065C51B /* Examples.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0746C14D252FA9D4001638BD /* Examples.swift */; };
CADCF736258499170065C51B /* ExampleTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0746C15F252FADE4001638BD /* ExampleTableViewController.swift */; };
CADCF7392584991C0065C51B /* UIViewController+Children.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0746C152252FAA03001638BD /* UIViewController+Children.swift */; };
CADCF7392584991C0065C51B /* UIViewController+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0746C152252FAA03001638BD /* UIViewController+Extensions.swift */; };
CADCF73C258499200065C51B /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 077C4EED252F7E88007636F1 /* AppDelegate.swift */; };
CADCF743258499570065C51B /* BasicMapExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = CADCF742258499570065C51B /* BasicMapExample.swift */; };
CAF9A9812583E49B007EF9EC /* TestableExampleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAF9A9802583E49B007EF9EC /* TestableExampleTests.swift */; };
Expand Down Expand Up @@ -149,7 +149,7 @@
072C79EA25685D23006E47A7 /* SnapshotterExample.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SnapshotterExample.swift; sourceTree = "<group>"; };
073475D625AFAE520049B0B8 /* CustomLayerExample.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomLayerExample.swift; sourceTree = "<group>"; };
0746C14D252FA9D4001638BD /* Examples.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Examples.swift; sourceTree = "<group>"; };
0746C152252FAA03001638BD /* UIViewController+Children.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIViewController+Children.swift"; sourceTree = "<group>"; };
0746C152252FAA03001638BD /* UIViewController+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIViewController+Extensions.swift"; sourceTree = "<group>"; };
0746C15F252FADE4001638BD /* ExampleTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExampleTableViewController.swift; sourceTree = "<group>"; };
077C4EEA252F7E88007636F1 /* Examples.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Examples.app; sourceTree = BUILT_PRODUCTS_DIR; };
077C4EED252F7E88007636F1 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -303,7 +303,7 @@
0746C151252FA9E1001638BD /* Extensions */ = {
isa = PBXGroup;
children = (
0746C152252FAA03001638BD /* UIViewController+Children.swift */,
0746C152252FAA03001638BD /* UIViewController+Extensions.swift */,
7412CF6527E9A7FF00F03B1C /* CLLocationCoordinate2D+Random.swift */,
7412CF6727E9A8D600F03B1C /* UIColor+Random.swift */,
74A2313027EB306F0065FB7D /* Array+Split.swift */,
Expand Down Expand Up @@ -704,7 +704,7 @@
files = (
CA03F10F26268DF700673961 /* OfflineManagerExample.swift in Sources */,
CADCF73C258499200065C51B /* AppDelegate.swift in Sources */,
CADCF7392584991C0065C51B /* UIViewController+Children.swift in Sources */,
CADCF7392584991C0065C51B /* UIViewController+Extensions.swift in Sources */,
58248C2F2695FF24009AC598 /* StoryboardMapViewExample.swift in Sources */,
74A2313827EC6B360065FB7D /* IconSizeChangeExample.swift in Sources */,
B58E470927BABE0E00D87FD6 /* AdvancedViewportGesturesExample.swift in Sources */,
Expand Down
Expand Up @@ -199,15 +199,4 @@ class SymbolClusteringExample: UIViewController, ExampleProtocol {
}
}
}

// Present an alert with a given title and message.
func showAlert(withTitle title: String, and message: String) {
let alertController = UIAlertController(title: title,
message: message,
preferredStyle: .alert)

alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))

present(alertController, animated: true, completion: nil)
}
}
15 changes: 13 additions & 2 deletions Apps/Examples/Examples/All Examples/DebugMapExample.swift
@@ -1,5 +1,5 @@
import UIKit
import MapboxMaps
@_spi(Experimental) import MapboxMaps

private protocol DebugOptionSettingsDelegate: AnyObject {
func debugOptionSettingsDidChange(_ controller: SettingsViewController)
Expand Down Expand Up @@ -36,7 +36,12 @@ final class DebugMapExample: UIViewController, ExampleProtocol, DebugOptionSetti
barButtonSystemItem: .edit,
target: self,
action: #selector(openDebugOptionsMenu(_:)))
navigationItem.rightBarButtonItem = debugOptionsBarItem
let tileCover = UIBarButtonItem(
title: "Tiles",
style: .plain,
target: self,
action: #selector(tileCover))
navigationItem.rightBarButtonItems = [debugOptionsBarItem, tileCover]
}

override func viewDidAppear(_ animated: Bool) {
Expand All @@ -56,6 +61,12 @@ final class DebugMapExample: UIViewController, ExampleProtocol, DebugOptionSetti
present(navigationController, animated: true, completion: nil)
}

@objc private func tileCover() {
let tileIds = mapView.mapboxMap.tileCover(for: TileCoverOptions(tileSize: 512, minZoom: 0, maxZoom: 22, roundZoom: false))
let message = tileIds.map { "\($0.z)/\($0.x)/\($0.y)" }.joined(separator: "\n")
showAlert(withTitle: "Displayed tiles", and: message)
}

fileprivate func debugOptionSettingsDidChange(_ controller: SettingsViewController) {
controller.dismiss(animated: true, completion: nil)
mapView.mapboxMap.debugOptions = Array(controller.enabledDebugOptions)
Expand Down
19 changes: 0 additions & 19 deletions Apps/Examples/Examples/Extensions/UIViewController+Children.swift

This file was deleted.

@@ -0,0 +1,30 @@
import UIKit

extension UIViewController {
internal func addChildViewController(_ child: UIViewController) {
addChild(child)
view.addSubview(child.view)
child.didMove(toParent: self)
}

internal func remove() {
guard parent != nil else {
return
}

willMove(toParent: nil)
view.removeFromSuperview()
removeFromParent()
}

// Present an alert with a given title and message.
func showAlert(withTitle title: String, and message: String) {
let alertController = UIAlertController(title: title,
message: message,
preferredStyle: .alert)

alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))

present(alertController, animated: true, completion: nil)
}
}
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -15,6 +15,7 @@ Mapbox welcomes participation and contributions from everyone.

* Bump MapboxCoreMaps to 10.12.1
* Fix missing feature properties for `nil`/`null` values.
* Added experimental `tileCover` method to `MapboxMap` that returns tile ids covering the map.

## 10.12.0 - March 22, 2023

Expand Down
16 changes: 16 additions & 0 deletions Sources/MapboxMaps/Foundation/MapboxMap.swift
Expand Up @@ -1144,6 +1144,22 @@ extension MapboxMap {

}

// MARK: - TileCover

extension MapboxMap {

/// Returns array of tile identifiers that cover current map camera.
///
/// - Parameters:
/// - options: Options for the tile cover method.
@_spi(Experimental)
public func tileCover(for options: TileCoverOptions) -> [CanonicalTileID] {
__map.__tileCover(
for: MapboxCoreMaps.TileCoverOptions(options),
cameraOptions: nil)
}
}

// MARK: - Map Recorder

extension MapboxMap {
Expand Down
45 changes: 45 additions & 0 deletions Sources/MapboxMaps/Foundation/TileCoverOptions.swift
@@ -0,0 +1,45 @@
import MapboxCoreMaps

/// Various options needed for tile cover.
@_spi(Experimental)
public struct TileCoverOptions {
/// Tile size of the source. Defaults to 512.
public var tileSize: UInt16?

/// Min zoom defined in the source between range [0, 22].
/// if not provided or is out of range, defaults to 0.
public var minZoom: UInt8?

/// Max zoom defined in the source between range [0, 22].
/// Should be greater than or equal to minZoom.
/// If not provided or is out of range, defaults to 22.
public var maxZoom: UInt8?

/// Whether to round zoom values when calculating tilecover.
/// Set this to true for raster and raster-dem sources.
/// If not specified, defaults to false.
public var roundZoom: Bool?

/// Creates the TileCoverOptions.
public init(
tileSize: UInt16? = nil,
minZoom: UInt8? = nil,
maxZoom: UInt8? = nil,
roundZoom: Bool? = nil
) {
self.tileSize = tileSize
self.minZoom = minZoom
self.maxZoom = maxZoom
self.roundZoom = roundZoom
}
}

extension MapboxCoreMaps.TileCoverOptions {
internal convenience init(_ options: TileCoverOptions) {
self.init(
__tileSize: options.tileSize.map { NSNumber(value: $0) },
minZoom: options.minZoom.map { NSNumber(value: $0) },
maxZoom: options.maxZoom.map { NSNumber(value: $0) },
roundZoom: options.roundZoom.map { NSNumber(value: $0) })
}
}
@@ -0,0 +1,23 @@
import XCTest
@_spi(Experimental) import MapboxMaps

class TileCoverIntegrationTests: MapViewIntegrationTestCase {
func testTileCover() {
mapView.mapboxMap.setCamera(to: CameraOptions(zoom: 5.5))
let tileIds = mapView.mapboxMap.tileCover(for: TileCoverOptions(tileSize: 512, minZoom: 0, maxZoom: 22, roundZoom: false))
XCTAssertFalse(tileIds.isEmpty)
for tileId in tileIds {
XCTAssertEqual(tileId.z, 5) // sanity check
}
}

func testTileCoverDefaultParameters() throws {
throw XCTSkip("Default value resolution will be fixed in MAPSNAT-1024")
mapView.mapboxMap.setCamera(to: CameraOptions(zoom: 5.5))
let tileIds = mapView.mapboxMap.tileCover(for: TileCoverOptions())
XCTAssertFalse(tileIds.isEmpty)
for tileId in tileIds {
XCTAssertEqual(tileId.z, 5) // sanity check
}
}
}
1 change: 1 addition & 0 deletions Tests/TestPlans/IntegrationTests.xctestplan
Expand Up @@ -62,6 +62,7 @@
"StyleLoadIntegrationTests",
"StyleLocalizationTests",
"SymbolLayerIntegrationTests",
"TileCoverIntegrationTests\/testTileCover()",
"VectorSourceIntegrationTests"
],
"target" : {
Expand Down
1 change: 1 addition & 0 deletions Tests/TestPlans/UnitTests.xctestplan
Expand Up @@ -74,6 +74,7 @@
"StyleLoadIntegrationTests",
"StyleLocalizationTests",
"SymbolLayerIntegrationTests",
"TileCoverIntegrationTests",
"VectorSourceIntegrationTests"
],
"target" : {
Expand Down

0 comments on commit ee74972

Please sign in to comment.