Skip to content

Commit

Permalink
API cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
trasch committed Jul 25, 2024
1 parent e432f64 commit 4ca31fe
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
10 changes: 5 additions & 5 deletions Sources/GISTools/Algorithms/Conversions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,16 @@ extension GISTool {

extension GISTool {

/// Converts pixel coordinates in a given zoom level to EPSG:3857.
public static func pixelCoordinate(
pixelX: Double,
/// Converts pixel coordinates in a given zoom level to a coordinate.
public static func convertToCoordinate(
fromPixelX pixelX: Double,
pixelY: Double,
atZoom zoom: Int,
tileSideLength: Double = GISTool.tileSideLength,
projection: Projection = .epsg4326)
-> Coordinate3D
{
let resolution = metersPerPixel(at: zoom, tileSideLength: tileSideLength)
let resolution = metersPerPixel(atZoom: zoom, tileSideLength: tileSideLength)

let coordinateXY = Coordinate3D(
x: pixelX * resolution - GISTool.originShift,
Expand All @@ -130,7 +130,7 @@ extension GISTool {

/// Resolution (meters/pixel) for a given zoom level (measured at `latitude`, defaults to the equator).
public static func metersPerPixel(
at zoom: Int,
atZoom zoom: Int,
latitude: CLLocationDegrees = 0.0, // equator
tileSideLength: Double = GISTool.tileSideLength)
-> Double
Expand Down
27 changes: 16 additions & 11 deletions Sources/GISTools/Other/MapTile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public struct MapTile: CustomStringConvertible, Sendable {
let pixelX: Double = (Double(x) + 0.5) * GISTool.tileSideLength
let pixelY: Double = (Double(y) + 0.5) * GISTool.tileSideLength

return GISTool.pixelCoordinate(
pixelX: pixelX,
return GISTool.convertToCoordinate(
fromPixelX: pixelX,
pixelY: pixelY,
atZoom: z,
tileSideLength: GISTool.tileSideLength,
Expand All @@ -89,14 +89,14 @@ public struct MapTile: CustomStringConvertible, Sendable {
// Flip y
let y = (1 << z) - 1 - y

let southWest = GISTool.pixelCoordinate(
pixelX: Double(x) * GISTool.tileSideLength,
let southWest = GISTool.convertToCoordinate(
fromPixelX: Double(x) * GISTool.tileSideLength,
pixelY: Double(y) * GISTool.tileSideLength,
atZoom: z,
tileSideLength: GISTool.tileSideLength,
projection: projection)
let northEast = GISTool.pixelCoordinate(
pixelX: Double(x + 1) * GISTool.tileSideLength,
let northEast = GISTool.convertToCoordinate(
fromPixelX: Double(x + 1) * GISTool.tileSideLength,
pixelY: Double(y + 1) * GISTool.tileSideLength,
atZoom: z,
tileSideLength: GISTool.tileSideLength,
Expand Down Expand Up @@ -161,8 +161,8 @@ public struct MapTile: CustomStringConvertible, Sendable {

// MARK: - Conversion pixel to meters

/// Converts pixel coordinates in a given zoom level to EPSG:3857.
@available(*, deprecated, renamed: "GISTool.pixelCoordinate", message: "This method has been moved to the GISTool namespace")
/// Converts pixel coordinates in a given zoom level to a coordinate.
@available(*, deprecated, renamed: "GISTool.convertToCoordinate", message: "This method has been moved to the GISTool namespace")
public static func pixelCoordinate(
pixelX: Double,
pixelY: Double,
Expand All @@ -171,7 +171,12 @@ public struct MapTile: CustomStringConvertible, Sendable {
projection: Projection = .epsg4326)
-> Coordinate3D
{
GISTool.pixelCoordinate(pixelX: pixelX, pixelY: pixelY, atZoom: zoom, tileSideLength: tileSideLength, projection: projection)
GISTool.convertToCoordinate(
fromPixelX: pixelX,
pixelY: pixelY,
atZoom: zoom,
tileSideLength: tileSideLength,
projection: projection)
}

// MARK: - Meters per pixel
Expand All @@ -184,12 +189,12 @@ public struct MapTile: CustomStringConvertible, Sendable {
tileSideLength: Double = GISTool.tileSideLength)
-> Double
{
GISTool.metersPerPixel(at: zoom, latitude: latitude, tileSideLength: tileSideLength)
GISTool.metersPerPixel(atZoom: zoom, latitude: latitude, tileSideLength: tileSideLength)
}

/// Resolution (meters/pixel) for a given zoom level measured at the tile center.
public var metersPerPixel: Double {
GISTool.metersPerPixel(at: z, latitude: centerCoordinate().latitude)
GISTool.metersPerPixel(atZoom: z, latitude: centerCoordinate().latitude)
}

// MARK: - Private
Expand Down
4 changes: 2 additions & 2 deletions Tests/GISToolsTests/Algorithms/ConversionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ final class ConversionTests: XCTestCase {
for zoom in 1...20 {
mppAtZoomLevels[zoom] = mppAtZoomLevels[zoom - 1] / 2.0

XCTAssertEqual(GISTool.metersPerPixel(at: zoom),
XCTAssertEqual(GISTool.metersPerPixel(atZoom: zoom),
mppAtZoomLevels[zoom],
accuracy: 0.00001)
}
Expand All @@ -23,7 +23,7 @@ final class ConversionTests: XCTestCase {
for zoom in 1...20 {
mppAtZoomLevels[zoom] = mppAtZoomLevels[zoom - 1] / 2.0

XCTAssertEqual(GISTool.metersPerPixel(at: zoom, latitude: 45.0),
XCTAssertEqual(GISTool.metersPerPixel(atZoom: zoom, latitude: 45.0),
mppAtZoomLevels[zoom],
accuracy: 0.00001)
}
Expand Down

0 comments on commit 4ca31fe

Please sign in to comment.