Skip to content

Commit

Permalink
Bugfix: Query bounding boxes in the western hemisphere
Browse files Browse the repository at this point in the history
  • Loading branch information
trasch committed Oct 14, 2023
1 parent 8955886 commit d2905b0
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 40 deletions.
6 changes: 4 additions & 2 deletions Sources/MVTTools/VectorTileQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,14 @@ extension VectorTile {
return (features: features, results: results)
}

private static func queryBoundingBox(
static func queryBoundingBox(
at coordinate: Coordinate3D,
tolerance: CLLocationDistance,
projection: Projection)
-> BoundingBox
{
let tolerance = fabs(tolerance)

switch projection {
case .epsg3857, .noSRID:
return BoundingBox(
Expand All @@ -261,8 +263,8 @@ extension VectorTile {

case .epsg4326:
// Length of one minute at this latitude
let oneDegreeLongitudeDistanceInMeters: Double = cos(coordinate.longitude * Double.pi / 180.0) * 111_000.0
let oneDegreeLatitudeDistanceInMeters = 111_000.0
let oneDegreeLongitudeDistanceInMeters: Double = fabs(cos(coordinate.longitude * Double.pi / 180.0) * oneDegreeLatitudeDistanceInMeters)

let longitudeDistance: Double = (tolerance / oneDegreeLongitudeDistanceInMeters)
let latitudeDistance: Double = (tolerance / oneDegreeLatitudeDistanceInMeters)
Expand Down
56 changes: 56 additions & 0 deletions Tests/MVTToolsTests/QueryTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#if !os(Linux)
import CoreLocation
#endif
import GISTools
import XCTest

@testable import MVTTools

final class QueryTests: XCTestCase {

func testQueryBoundingBox() {
let coordinate = Coordinate3D(latitude: 47.0, longitude: -120.0)
let queryBoundingBox = VectorTile.queryBoundingBox(at: coordinate, tolerance: 15.0, projection: .epsg4326)
XCTAssertGreaterThan(queryBoundingBox.northEast.latitude, queryBoundingBox.southWest.latitude)
XCTAssertGreaterThan(queryBoundingBox.northEast.longitude, queryBoundingBox.southWest.longitude)
}

func testQuery() {
let tileName: String = "14_8716_8015.vector.mvt"
let mvt = TestData.dataFromFile(name: tileName)
XCTAssertFalse(mvt.isEmpty)

guard let tile = VectorTile(data: mvt, x: 8716, y: 8015, z: 14) else {
XCTAssert(false, "Unable to parse the vector tile \(tileName)")
return
}

XCTAssertFalse(tile.isIndexed)

measure {
let result = tile.query(at: Coordinate3D(latitude: 3.870163, longitude: 11.518585), tolerance: 100.0)
XCTAssertNotNil(result)
XCTAssertEqual(result.count, 68)
}
}

func testQueryWithIndex() {
let tileName: String = "14_8716_8015.vector.mvt"
let mvt = TestData.dataFromFile(name: tileName)
XCTAssertFalse(mvt.isEmpty)

guard let tile = VectorTile(data: mvt, x: 8716, y: 8015, z: 14, indexed: .hilbert) else {
XCTAssert(false, "Unable to parse the vector tile \(tileName)")
return
}

XCTAssertTrue(tile.isIndexed)

measure {
let resultWithIndex = tile.query(at: Coordinate3D(latitude: 3.870163, longitude: 11.518585), tolerance: 100.0)
XCTAssertNotNil(resultWithIndex)
XCTAssertEqual(resultWithIndex.count, 68)
}
}

}
38 changes: 0 additions & 38 deletions Tests/MVTToolsTests/VectorTileTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,44 +37,6 @@ final class VectorTileTests: XCTestCase {
// try? string?.write(to: URL(fileURLWithPath: "/\(NSHomeDirectory())/Desktop/test.json"), atomically: true, encoding: .utf8)
}

func testQuery() {
let tileName: String = "14_8716_8015.vector.mvt"
let mvt = TestData.dataFromFile(name: tileName)
XCTAssertFalse(mvt.isEmpty)

guard let tile = VectorTile(data: mvt, x: 8716, y: 8015, z: 14) else {
XCTAssert(false, "Unable to parse the vector tile \(tileName)")
return
}

XCTAssertFalse(tile.isIndexed)

measure {
let result = tile.query(at: Coordinate3D(latitude: 3.870163, longitude: 11.518585), tolerance: 100.0)
XCTAssertNotNil(result)
XCTAssertEqual(result.count, 68)
}
}

func testQueryWithIndex() {
let tileName: String = "14_8716_8015.vector.mvt"
let mvt = TestData.dataFromFile(name: tileName)
XCTAssertFalse(mvt.isEmpty)

guard let tile = VectorTile(data: mvt, x: 8716, y: 8015, z: 14, indexed: .hilbert) else {
XCTAssert(false, "Unable to parse the vector tile \(tileName)")
return
}

XCTAssertTrue(tile.isIndexed)

measure {
let resultWithIndex = tile.query(at: Coordinate3D(latitude: 3.870163, longitude: 11.518585), tolerance: 100.0)
XCTAssertNotNil(resultWithIndex)
XCTAssertEqual(resultWithIndex.count, 68)
}
}

func testWriteMvt() {
guard var tile = VectorTile(x: 8716, y: 8015, z: 14) else {
XCTAssert(false, "Unable to create a vector tile")
Expand Down

0 comments on commit d2905b0

Please sign in to comment.