Skip to content

Commit

Permalink
Robustness, check for invalid points; ignore zoomTo rects of size z…
Browse files Browse the repository at this point in the history
…ero.
  • Loading branch information
nighthawk committed Oct 3, 2024
1 parent ee3fc97 commit 963b49f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
17 changes: 15 additions & 2 deletions Sources/GeoDrawer/GeoDrawer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,11 @@ extension GeoDrawer {
// 3. Now translate the projected points into point coordinates to draw
let converted = projected
.map { (unproj, projected) -> (Point, Point?, Grouping) in
assert(unproj.isGood)
if let projected {
return (unproj, projection.translate(projected, to: size, zoomTo: zoomTo, insets: insets), projection.willWrap(unproj) ? .wrapped : .notWrapped)
let proj = projection.translate(projected, to: size, zoomTo: zoomTo, insets: insets)
assert(proj.isGood)
return (unproj, proj, projection.willWrap(unproj) ? .wrapped : .notWrapped)
} else {
return (unproj, nil, .notProjected)
}
Expand Down Expand Up @@ -265,7 +268,11 @@ extension GeoDrawer {
// When "resuming" the same group, connect with the previous points
// in the group, but interpolate again.
let interpolated = Interpolator.interpolate(from: last, to: unproj, maxDiff: 0.0025, projector: projection.project(_:))
let translated = interpolated.map { ($0.0, projection.translate($0.1, to: size, zoomTo: zoomTo, insets: insets)) }
let translated = interpolated.map {
let translated = projection.translate($0.1, to: size, zoomTo: zoomTo, insets: insets)
assert(translated.isGood)
return ($0.0, translated)
}
new.append(contentsOf: translated)
}
if let proj {
Expand All @@ -287,3 +294,9 @@ extension GeoDrawer {
return [wraps.map(\.1), unwraps.map(\.1)].filter { !$0.isEmpty }
}
}

fileprivate extension Point {
var isGood: Bool {
!x.isNaN && !y.isNaN && !x.isInfinite && !y.isInfinite
}
}
14 changes: 12 additions & 2 deletions Sources/GeoProjector/Projection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,12 @@ extension Projection {
height: size.height - insets.top - insets.bottom
)
let pointInAvailable: Point
if let zoomTo {
if let zoomTo, zoomTo.size != .zero {
pointInAvailable = zoomedTranslate(point, zoomTo: zoomTo, to: availableSize)
} else {
pointInAvailable = simpleTranslate(point, to: availableSize)
}
assert(pointInAvailable.isGood)

let x: Double = pointInAvailable.x + insets.left
let y: Double
Expand All @@ -104,7 +105,9 @@ extension Projection {
#else
y = (availableSize.height - pointInAvailable.y) + insets.top
#endif
return Point(x: x, y: y)
let result = Point(x: x, y: y)
assert(result.isGood)
return result
}

private func simpleTranslate(_ point: Point, to size: Size) -> Point {
Expand Down Expand Up @@ -137,6 +140,7 @@ extension Projection {
}

private func zoomedTranslate(_ point: Point, zoomTo: Rect, to size: Size) -> Point {
assert(zoomTo.size != .zero)
let myRatio = zoomTo.size.aspectRatio
let targetRatio = size.aspectRatio

Expand Down Expand Up @@ -183,3 +187,9 @@ extension Size {
width / height
}
}

fileprivate extension Point {
var isGood: Bool {
!x.isNaN && !y.isNaN && !x.isInfinite && !y.isInfinite
}
}

0 comments on commit 963b49f

Please sign in to comment.