Skip to content

Commit dfd3bcb

Browse files
committed
Partial fix for tile loading at antimeridian
Signed-off-by: Kyle Corry <[email protected]>
1 parent 45281c3 commit dfd3bcb

File tree

4 files changed

+52
-19
lines changed

4 files changed

+52
-19
lines changed

app/src/main/java/com/kylecorry/trail_sense/shared/andromeda_temp/Extensions.kt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ import com.kylecorry.andromeda.core.ui.ReactiveComponent
88
import com.kylecorry.andromeda.fragments.observeFlow
99
import com.kylecorry.andromeda.fragments.useBackgroundEffect
1010
import com.kylecorry.luna.coroutines.ParallelCoroutineRunner
11+
import com.kylecorry.sol.math.SolMath.deltaAngle
1112
import com.kylecorry.sol.math.SolMath.isCloseTo
1213
import com.kylecorry.sol.science.geology.CoordinateBounds
14+
import com.kylecorry.sol.science.geology.CoordinateBounds.Companion.empty
1315
import com.kylecorry.sol.units.Bearing
1416
import com.kylecorry.sol.units.CompassDirection
17+
import com.kylecorry.sol.units.Coordinate
1518
import kotlinx.coroutines.CoroutineScope
1619
import kotlinx.coroutines.Dispatchers
1720
import kotlin.coroutines.CoroutineContext
@@ -200,4 +203,41 @@ inline fun IntArray.get(x: Int, y: Int, width: Int): Int {
200203

201204
inline fun IntArray.set(x: Int, y: Int, width: Int, value: Int) {
202205
this[y * width + x] = value
206+
}
207+
208+
// TODO: Sol
209+
fun CoordinateBounds.Companion.from2(points: List<Coordinate>): CoordinateBounds {
210+
val west = getWestLongitudeBound(points) ?: return empty
211+
val east = getEastLongitudeBound(points) ?: return empty
212+
val north = getNorthLatitudeBound(points) ?: return empty
213+
val south = getSouthLatitudeBound(points) ?: return empty
214+
return CoordinateBounds(north, east, south, west)
215+
}
216+
217+
private fun getWestLongitudeBound(locations: List<Coordinate>): Double? {
218+
val first = locations.firstOrNull() ?: return null
219+
return locations.minByOrNull {
220+
deltaAngle(
221+
first.longitude.toFloat() + 180,
222+
it.longitude.toFloat() + 180
223+
)
224+
}?.longitude
225+
}
226+
227+
private fun getEastLongitudeBound(locations: List<Coordinate>): Double? {
228+
val first = locations.firstOrNull() ?: return null
229+
return locations.maxByOrNull {
230+
deltaAngle(
231+
first.longitude.toFloat() + 180,
232+
it.longitude.toFloat() + 180
233+
)
234+
}?.longitude
235+
}
236+
237+
private fun getSouthLatitudeBound(locations: List<Coordinate>): Double? {
238+
return locations.minByOrNull { it.latitude }?.latitude
239+
}
240+
241+
private fun getNorthLatitudeBound(locations: List<Coordinate>): Double? {
242+
return locations.maxByOrNull { it.latitude }?.latitude
203243
}

app/src/main/java/com/kylecorry/trail_sense/shared/dem/DEM.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,15 @@ object DEM {
9999

100100
val longitudes = Interpolation.getMultiplesBetween(
101101
bounds.west - resolution,
102-
bounds.east + resolution,
102+
(if (bounds.west < bounds.east) bounds.east else bounds.east + 360) + resolution,
103103
resolution
104104
)
105105

106106
gridCache.getOrPut(getGridKey(latitudes, longitudes, resolution)) {
107107
val toLookupCoordinates = mutableListOf<Coordinate>()
108108
latitudes.forEach { lat ->
109109
longitudes.forEach { lon ->
110-
toLookupCoordinates.add(Coordinate(lat, lon))
110+
toLookupCoordinates.add(Coordinate(lat, Coordinate.toLongitude(lon)))
111111
}
112112
}
113113

app/src/main/java/com/kylecorry/trail_sense/shared/map_layers/tiles/TileMath.kt

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,25 +79,14 @@ object TileMath {
7979
distancePerPixelToZoom(metersPerPixel, (minLat + maxLat) / 2).coerceAtMost(maxZoom)
8080
val northWestTile = latLonToTileXY(bounds.north, bounds.west, zoom).getBounds()
8181
val southEastTile = latLonToTileXY(bounds.south, bounds.east, zoom).getBounds()
82-
return CoordinateBounds.fromBounds(
83-
listOf(
84-
northWestTile,
85-
southEastTile
86-
)
82+
return CoordinateBounds(
83+
northWestTile.north,
84+
southEastTile.east,
85+
southEastTile.south,
86+
northWestTile.west
8787
)
8888
}
8989

90-
private fun CoordinateBounds.Companion.fromBounds(bounds: List<CoordinateBounds>): CoordinateBounds {
91-
return CoordinateBounds.from(bounds.flatMap {
92-
listOf(
93-
it.northWest,
94-
it.northEast,
95-
it.southWest,
96-
it.southEast
97-
)
98-
})
99-
}
100-
10190
fun latLonToTileXY(lat: Double, lon: Double, zoom: Int): Tile {
10291
val latRad = Math.toRadians(lat)
10392
val n = 1 shl zoom

app/src/main/java/com/kylecorry/trail_sense/shared/map_layers/ui/layers/tiles/FullRegionMapTileSource.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package com.kylecorry.trail_sense.shared.map_layers.ui.layers.tiles
22

3+
import com.kylecorry.sol.math.SolMath.deltaAngle
34
import com.kylecorry.sol.science.geology.CoordinateBounds
5+
import com.kylecorry.sol.science.geology.CoordinateBounds.Companion.empty
6+
import com.kylecorry.sol.units.Coordinate
7+
import com.kylecorry.trail_sense.shared.andromeda_temp.from2
48
import com.kylecorry.trail_sense.shared.map_layers.tiles.IGeographicImageRegionLoader
59
import com.kylecorry.trail_sense.shared.map_layers.tiles.ITileSourceSelector
610
import kotlinx.coroutines.sync.Mutex
@@ -14,7 +18,7 @@ abstract class FullRegionMapTileSource : ITileSourceSelector {
1418
abstract fun getLoader(fullBounds: CoordinateBounds): FullRegionMapTileLoader
1519

1620
override suspend fun getRegionLoaders(bounds: List<CoordinateBounds>): List<List<IGeographicImageRegionLoader>> {
17-
val fullBounds = CoordinateBounds.from(bounds.flatMap {
21+
val fullBounds = CoordinateBounds.from2(bounds.flatMap {
1822
listOf(
1923
it.northWest,
2024
it.southEast

0 commit comments

Comments
 (0)