```
feat(map-settings): 新增地图设置面板,支持底图偏好与离线瓦片下载 共享层新增 map_settings 模块,定义 Bounds、Region、Preferences 和 DownloadRequest 数据结构及验证逻辑,单次下载上限 20000 张瓦片。 前端新增独立地图设置视图,包含: - 底图切换(影像/路网)与默认定位点配置 - 地图选点、框选下载范围交互 - 常用区域保存与离线下载任务管理 实时地图适配器改为从 Preferences 读取初始中心点和缩放级别, 使设置面板与工作台地图共享视图状态。 同时微调目标列表行高与 footer 布局,在紧凑模式下隐藏黑名单提示。 ```
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
//! Versioned synthetic telemetry contract. This is not a device wire protocol.
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub mod map_settings;
|
||||
pub mod target_lists;
|
||||
|
||||
pub const PROTOCOL_VERSION: u16 = 1;
|
||||
|
||||
@@ -0,0 +1,180 @@
|
||||
//! Persistent map preferences and bounded manual download coverage.
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub const MAX_DOWNLOAD_TILES: u64 = 20_000;
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
pub struct Bounds {
|
||||
pub west: f64,
|
||||
pub south: f64,
|
||||
pub east: f64,
|
||||
pub north: f64,
|
||||
}
|
||||
|
||||
impl Bounds {
|
||||
pub fn valid(&self) -> bool {
|
||||
[self.west, self.south, self.east, self.north]
|
||||
.iter()
|
||||
.all(|v| v.is_finite())
|
||||
&& self.west >= -180.0
|
||||
&& self.east <= 180.0
|
||||
&& self.south >= -85.051128
|
||||
&& self.north <= 85.051128
|
||||
&& self.west < self.east
|
||||
&& self.south < self.north
|
||||
}
|
||||
|
||||
pub fn tile_range(&self, z: u8) -> (u32, u32, u32, u32) {
|
||||
let n = f64::from(1u32 << z);
|
||||
let x = |lon: f64| (((lon + 180.0) / 360.0 * n).floor() as u32).min((1u32 << z) - 1);
|
||||
let y = |lat: f64| {
|
||||
let rad = lat.to_radians();
|
||||
((1.0 - (rad.tan() + 1.0 / rad.cos()).ln() / std::f64::consts::PI) / 2.0 * n)
|
||||
.floor()
|
||||
.clamp(0.0, n - 1.0) as u32
|
||||
};
|
||||
(x(self.west), x(self.east), y(self.north), y(self.south))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
pub struct Region {
|
||||
pub name: String,
|
||||
pub bounds: Bounds,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
pub struct Preferences {
|
||||
pub basemap: String,
|
||||
pub longitude: f64,
|
||||
pub latitude: f64,
|
||||
pub zoom: f64,
|
||||
pub regions: Vec<Region>,
|
||||
pub revision: u64,
|
||||
}
|
||||
|
||||
impl Default for Preferences {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
basemap: "imagery".into(),
|
||||
longitude: 120.1538,
|
||||
latitude: 30.2874,
|
||||
zoom: 11.0,
|
||||
regions: vec![],
|
||||
revision: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Preferences {
|
||||
pub fn valid(&self) -> bool {
|
||||
matches!(self.basemap.as_str(), "road" | "imagery")
|
||||
&& self.longitude.is_finite()
|
||||
&& (-180.0..=180.0).contains(&self.longitude)
|
||||
&& self.latitude.is_finite()
|
||||
&& (-85.051128..=85.051128).contains(&self.latitude)
|
||||
&& self.zoom.is_finite()
|
||||
&& (0.0..=18.0).contains(&self.zoom)
|
||||
&& self.regions.len() <= 30
|
||||
&& self.regions.iter().enumerate().all(|(i, r)| {
|
||||
!r.name.trim().is_empty()
|
||||
&& r.name.chars().count() <= 40
|
||||
&& !r.name.chars().any(char::is_control)
|
||||
&& r.bounds.valid()
|
||||
&& !self.regions[..i].iter().any(|p| p.name == r.name)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
pub struct DownloadRequest {
|
||||
pub bounds: Bounds,
|
||||
pub min_zoom: u8,
|
||||
pub max_zoom: u8,
|
||||
pub road: bool,
|
||||
pub imagery: bool,
|
||||
}
|
||||
impl DownloadRequest {
|
||||
pub fn layers(&self) -> Vec<&'static str> {
|
||||
let mut layers = vec![];
|
||||
if self.road {
|
||||
layers.extend(["vec_w", "cva_w"]);
|
||||
}
|
||||
if self.imagery {
|
||||
layers.extend(["img_w", "cia_w"]);
|
||||
}
|
||||
layers
|
||||
}
|
||||
pub fn count(&self) -> Result<u64, &'static str> {
|
||||
if !self.bounds.valid()
|
||||
|| self.min_zoom > self.max_zoom
|
||||
|| self.max_zoom > 18
|
||||
|| (!self.road && !self.imagery)
|
||||
{
|
||||
return Err("范围、层级或图层无效");
|
||||
}
|
||||
let mut count = 0u64;
|
||||
for z in self.min_zoom..=self.max_zoom {
|
||||
let (x0, x1, y0, y1) = self.bounds.tile_range(z);
|
||||
count += u64::from(x1 - x0 + 1) * u64::from(y1 - y0 + 1) * self.layers().len() as u64;
|
||||
if count > MAX_DOWNLOAD_TILES {
|
||||
return Err("单次下载最多 20000 张瓦片,请缩小区域或降低层级");
|
||||
}
|
||||
}
|
||||
Ok(count)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn rejects_invalid_and_excessive_coverage() {
|
||||
let mut r = DownloadRequest {
|
||||
bounds: Bounds {
|
||||
west: 120.15,
|
||||
south: 30.28,
|
||||
east: 120.16,
|
||||
north: 30.29,
|
||||
},
|
||||
min_zoom: 11,
|
||||
max_zoom: 11,
|
||||
road: true,
|
||||
imagery: true,
|
||||
};
|
||||
assert_eq!(r.count(), Ok(4));
|
||||
r.bounds.west = f64::NAN;
|
||||
assert!(r.count().is_err());
|
||||
r.bounds = Bounds {
|
||||
west: -180.0,
|
||||
south: -85.0,
|
||||
east: 180.0,
|
||||
north: 85.0,
|
||||
};
|
||||
r.max_zoom = 18;
|
||||
assert!(r.count().is_err());
|
||||
r.min_zoom = 19;
|
||||
assert!(r.count().is_err());
|
||||
}
|
||||
#[test]
|
||||
fn validates_defaults_and_regions() {
|
||||
let mut p = Preferences::default();
|
||||
assert!(p.valid());
|
||||
p.latitude = 90.0;
|
||||
assert!(!p.valid());
|
||||
p.latitude = 30.0;
|
||||
p.regions.push(Region {
|
||||
name: " ".into(),
|
||||
bounds: Bounds {
|
||||
west: 1.0,
|
||||
south: 1.0,
|
||||
east: 2.0,
|
||||
north: 2.0,
|
||||
},
|
||||
});
|
||||
assert!(!p.valid());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user