From 06cc8834746b83664d8a679c5997fca70d45f62f Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Mon, 27 Nov 2023 00:16:07 +0100 Subject: [PATCH] Look directly at the specified gps coordinate instead of just a corner of the tile that was selected --- src/cam_map/mod.rs | 2 -- src/cam_map/utils.rs | 13 ------------- src/{cam_map => }/geopos.rs | 23 ++++++++++++++--------- src/lib.rs | 11 +++++------ 4 files changed, 19 insertions(+), 30 deletions(-) delete mode 100644 src/cam_map/mod.rs delete mode 100644 src/cam_map/utils.rs rename src/{cam_map => }/geopos.rs (54%) diff --git a/src/cam_map/mod.rs b/src/cam_map/mod.rs deleted file mode 100644 index c9b9ab9..0000000 --- a/src/cam_map/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod geopos; -pub mod utils; diff --git a/src/cam_map/utils.rs b/src/cam_map/utils.rs deleted file mode 100644 index 689e2c1..0000000 --- a/src/cam_map/utils.rs +++ /dev/null @@ -1,13 +0,0 @@ -use bevy::prelude::*; - -/** - * OSM x/y tile "name" - parts - * - * 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. - * So we named this TypeScript variable/class-type as "Tile_Name", - * even if it is an x/y coordinate in numbers. - */ -pub type TileName = IVec2; diff --git a/src/cam_map/geopos.rs b/src/geopos.rs similarity index 54% rename from src/cam_map/geopos.rs rename to src/geopos.rs index 4370766..8fbbb8f 100644 --- a/src/cam_map/geopos.rs +++ b/src/geopos.rs @@ -1,4 +1,4 @@ -use super::utils::TileName; +use bevy::prelude::*; use std::f32::consts::PI; /** @@ -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 } diff --git a/src/lib.rs b/src/lib.rs index 6712232..3d8ea54 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; @@ -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![]; @@ -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((