Skip to content

Commit

Permalink
Look directly at the specified gps coordinate instead of just a corne…
Browse files Browse the repository at this point in the history
…r of the tile that was selected
  • Loading branch information
oli-obk committed Nov 27, 2023
1 parent 10cb046 commit 06cc883
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 30 deletions.
2 changes: 0 additions & 2 deletions src/cam_map/mod.rs

This file was deleted.

13 changes: 0 additions & 13 deletions src/cam_map/utils.rs

This file was deleted.

23 changes: 14 additions & 9 deletions src/cam_map/geopos.rs → src/geopos.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::utils::TileName;
use bevy::prelude::*;
use std::f32::consts::PI;

/**
Expand All @@ -16,23 +16,28 @@ pub struct GeoPos {

impl GeoPos {
/**
* Calculate the tile-name(x/y) of a tile at this position
* Convert GPS coordinates to tile coordinates.
* We use the OSM naming for tiles:
* https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames.
* x and y relate to a lonitude and latitude position on Earth.
* In OSM he two values are only used as part of the filename of a tile.
* even if it is an x/y coordinate in numbers.
* @param zoom Zoom level of the OSM tile-name(x/y) system
* @return tile-name(x/y)
* @return coordinate in tile coordinates
*/
pub fn calc_tile_name(&self, zoom: u8) -> TileName {
pub fn to_tile_coordinates(&self, zoom: u8) -> Vec2 {
let pow_zoom = 2_u32.pow(zoom.into()) as f32;

// return
TileName {
Vec2 {
// Longitude, (Längengrad) West/East "index"
x: ((self.lon + 180.) / 360. * pow_zoom).floor() as i32,
x: ((self.lon + 180.) / 360. * pow_zoom),

// y: Latitude, (Breitengrad) Nort/South "index"
y: ((1. - ((self.lat * PI / 180.).tan() + 1. / (self.lat * PI / 180.).cos()).ln() / PI)
y: ((1.
- ((self.lat * PI / 180.).tan() + 1. / (self.lat * PI / 180.).cos()).ln() / PI)
/ 2.
* pow_zoom)
.floor() as i32,
* pow_zoom),
// The Nort/South y tile name part is not linear, the tiles gets stretched to the poles
// to compensate the stretching if the stretching of the West/East projection
}
Expand Down
11 changes: 5 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ use bevy_screen_diagnostics::{
Aggregate, ScreenDiagnostics, ScreenDiagnosticsPlugin, ScreenEntityDiagnosticsPlugin,
ScreenFrameDiagnosticsPlugin,
};
use geopos::GeoPos;
use http_assets::HttpAssetReaderPlugin;
use sun::Sky;

use crate::cam_map::{geopos::GeoPos, utils::TileName};

type TileMap = tilemap::TileMap<8145>;

mod cam_map;
mod flycam;
mod geopos;
mod http_assets;
mod sun;
mod tilemap;
Expand Down Expand Up @@ -61,9 +60,9 @@ fn setup(

// Just for testing:
#[allow(unused_mut)]
let mut x: i32 = 17437;
let mut x: f32 = 17437.0;
#[allow(unused_mut)]
let mut y: i32 = 11371;
let mut y: f32 = 11371.0;

let mut args = vec![];

Expand Down Expand Up @@ -100,7 +99,7 @@ fn setup(

if let Some((lat, lon)) = lat.zip(lon) {
let pos = GeoPos { lat, lon };
TileName { x, y } = pos.calc_tile_name(15);
Vec2 { x, y } = pos.to_tile_coordinates(15);
}

commands.spawn((
Expand Down

0 comments on commit 06cc883

Please sign in to comment.