feat(geofences): 新增地理围栏模块并优化地图瓦片缓存

在 rid-shared 中实现二维 WGS84 地理围栏核心逻辑,支持圆形与多边形
两种几何体,包含坐标合法性校验、边界包含判断及自相交检测。
realtime-core 的 Store 新增观察者回调,在观测数据接受后按到达顺序
通知监听器。

前端方面,新增 geofence-map-adapter.js 提供基于 MapLibre 的围栏绘制
与编辑能力,配套 geofences.css 完成响应式布局样式。map-loader.js
增加懒加载入口,仅在实时工作区按需引入围栏地图模块。

同时引入 map-tile-buffer 瓦片缓冲机制,改善地图切换与刷新时的加载
体验;将默认缩放级别从 11 调整为 15,使初始视图更贴近实际监测场景。
```
This commit is contained in:
2026-10-23 13:55:08 +08:00
parent f310aa9c14
commit cd1b717e74
41 changed files with 2622 additions and 63 deletions
+171
View File
@@ -0,0 +1,171 @@
//! Two-dimensional WGS84 monitoring areas. No flight-control semantics.
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type", rename_all = "snake_case", deny_unknown_fields)]
pub enum Geometry {
Circle { center: [f64; 2], radius_m: f64 },
Polygon { vertices: Vec<[f64; 2]> },
}
fn coordinate(p: &[f64; 2]) -> bool {
p[0].is_finite()
&& p[1].is_finite()
&& (-180.0..=180.0).contains(&p[0])
&& (-90.0..=90.0).contains(&p[1])
}
fn cross(a: [f64; 2], b: [f64; 2], c: [f64; 2]) -> f64 {
(b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0])
}
fn segment(a: [f64; 2], b: [f64; 2], p: [f64; 2]) -> bool {
cross(a, b, p).abs() <= 1e-10
&& p[0] >= a[0].min(b[0]) - 1e-10
&& p[0] <= a[0].max(b[0]) + 1e-10
&& p[1] >= a[1].min(b[1]) - 1e-10
&& p[1] <= a[1].max(b[1]) + 1e-10
}
fn intersects(a: [f64; 2], b: [f64; 2], c: [f64; 2], d: [f64; 2]) -> bool {
segment(a, b, c)
|| segment(a, b, d)
|| segment(c, d, a)
|| segment(c, d, b)
|| (cross(a, b, c) * cross(a, b, d) < 0.0 && cross(c, d, a) * cross(c, d, b) < 0.0)
}
impl Geometry {
pub fn valid(&self) -> bool {
match self {
Self::Circle { center, radius_m } => {
coordinate(center) && radius_m.is_finite() && (1.0..=1_000_000.0).contains(radius_m)
}
Self::Polygon { vertices: v } => {
if !(3..=128).contains(&v.len()) || !v.iter().all(coordinate) {
return false;
}
let min = v.iter().map(|p| p[0]).fold(f64::INFINITY, f64::min);
let max = v.iter().map(|p| p[0]).fold(f64::NEG_INFINITY, f64::max);
// Reject ambiguous dateline polygons rather than drawing the opposite hemisphere.
if max - min >= 180.0 {
return false;
}
let mut area = 0.0;
for i in 0..v.len() {
let j = (i + 1) % v.len();
if v[i] == v[j] {
return false;
}
area += v[i][0] * v[j][1] - v[j][0] * v[i][1];
for k in i + 1..v.len() {
let l = (k + 1) % v.len();
if j != k && l != i && intersects(v[i], v[j], v[k], v[l]) {
return false;
}
}
}
area.abs() > 1e-10
}
}
}
/// Boundaries are inside. Invalid coordinates never match.
pub fn contains(&self, point: [f64; 2]) -> bool {
if !coordinate(&point) {
return false;
}
match self {
Self::Circle { center, radius_m } => {
let a = center[1].to_radians();
let b = point[1].to_radians();
let h = ((b - a) / 2.0).sin().powi(2)
+ a.cos() * b.cos() * ((point[0] - center[0]).to_radians() / 2.0).sin().powi(2);
6_371_008.8 * 2.0 * h.clamp(0.0, 1.0).sqrt().asin() <= *radius_m + 1e-6
}
Self::Polygon { vertices: v } => {
let mut inside = false;
for i in 0..v.len() {
let a = v[i];
let b = v[(i + 1) % v.len()];
if segment(a, b, point) {
return true;
}
if (a[1] > point[1]) != (b[1] > point[1])
&& point[0] < (b[0] - a[0]) * (point[1] - a[1]) / (b[1] - a[1]) + a[0]
{
inside = !inside;
}
}
inside
}
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Fence {
pub id: String,
pub name: String,
pub enabled: bool,
pub geometry: Geometry,
}
impl Fence {
pub fn valid(&self) -> bool {
crate::target_lists::valid_target_id(&self.id)
&& !self.name.trim().is_empty()
&& self.name.chars().count() <= 40
&& !self.name.chars().any(char::is_control)
&& self.geometry.valid()
}
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct Snapshot {
pub revision: u64,
pub entries: Vec<Fence>,
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Mutation {
pub fence: Fence,
pub expected_revision: u64,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Alert {
pub id: u64,
pub occurred_at_ms: u64,
pub event_type: String,
pub target_id: String,
pub fence_id: Option<String>,
pub fence_name: Option<String>,
pub longitude: f64,
pub latitude: f64,
pub list_kind: String,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn circle_uses_metres_and_handles_dateline() {
let g = Geometry::Circle {
center: [179.999, 0.0],
radius_m: 300.0,
};
assert!(g.valid());
assert!(g.contains([-179.999, 0.0]));
assert!(!g.contains([179.99, 0.0]));
assert!(!g.contains([f64::NAN, 0.0]));
}
#[test]
fn polygon_boundary_concavity_and_validation() {
let g = Geometry::Polygon {
vertices: vec![[0.0, 0.0], [2.0, 0.0], [1.0, 1.0], [2.0, 2.0], [0.0, 2.0]],
};
assert!(g.valid());
assert!(g.contains([0.0, 1.0]));
assert!(g.contains([0.5, 1.0]));
assert!(!g.contains([1.8, 1.0]));
for vertices in [
vec![[0.0, 0.0]; 3],
vec![[0.0, 0.0], [2.0, 2.0], [0.0, 2.0], [2.0, 0.0]],
vec![[179.0, 0.0], [-179.0, 0.0], [179.0, 1.0]],
] {
assert!(!Geometry::Polygon { vertices }.valid());
}
}
}