声网通用组件开发测试通过
This commit is contained in:
@@ -1,12 +1,27 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
import 'dart:math' as math;
|
||||
import 'dart:ui' as ui;
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_map/flutter_map.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:latlong2/latlong.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:xml/xml.dart';
|
||||
|
||||
/// GeoServer 基地址(公司端)
|
||||
const String kGeoserverBase = 'http://1.95.137.212:7900/geoserver';
|
||||
|
||||
/// GeoServer 工作区名(后端 siteMapName 形如 workSpace:suzhou)
|
||||
const String kGeoWorkspace = 'workSpace';
|
||||
|
||||
/// WMS 瓦片网络超时。大图层(如 neimeng/zhongke)一屏十几片、GeoServer 实时渲染排队,
|
||||
/// 单片实际等待可能远超 30s;浏览器预览肯等所以最终能出图,App 若超时太短会集体放弃、
|
||||
/// 返回透明图导致“一直出不来”。故放宽到 90s,并配合下方并发闸门分批取图。
|
||||
const Duration kWmsTileTimeout = Duration(seconds: 90);
|
||||
|
||||
/// 各场站高清图层的真实范围(取自 GeoServer GetCapabilities,原生 EPSG:4326)
|
||||
/// key = 后端下发的 siteMapName(形如 workSpace:suzhou)
|
||||
final Map<String, LatLngBounds> kStationLayerBounds = {
|
||||
@@ -43,7 +58,8 @@ final Map<String, LatLngBounds> kStationLayerBounds = {
|
||||
/// 取某场站图层的范围中心点(用作进场默认定位);未知/为空返回 null
|
||||
LatLng? stationLayerCenter(String? siteMapName) {
|
||||
if (siteMapName == null) return null;
|
||||
final b = kStationLayerBounds[siteMapName];
|
||||
// 优先用动态拉取的范围(StationLayerRepo),其内部已含硬编码兜底
|
||||
final b = StationLayerRepo.instance.bounds[siteMapName];
|
||||
if (b == null) return null;
|
||||
return LatLng((b.south + b.north) / 2, (b.west + b.east) / 2);
|
||||
}
|
||||
@@ -123,7 +139,7 @@ class GeoWmsProvider extends TileProvider {
|
||||
'STYLES': '',
|
||||
},
|
||||
);
|
||||
return NetworkImage(url.toString());
|
||||
return CachedWmsImage(url.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -204,3 +220,376 @@ final List<TileLayer> kAllStationTileLayers = kStationLayerBounds.entries
|
||||
),
|
||||
)
|
||||
.toList();
|
||||
|
||||
/// 带磁盘缓存 + 超时的 WMS 瓦片图:
|
||||
/// 命中本地缓存直接解码(秒开);未命中则联网拉取(带超时),成功后写入缓存;
|
||||
/// 失败/超时返回 1×1 透明图,避免异常刷屏。解决 WMS 实时渲染“每次都慢”的问题。
|
||||
class CachedWmsImage extends ImageProvider<CachedWmsImage> {
|
||||
CachedWmsImage(this.url) : _gen = _generation;
|
||||
|
||||
final String url;
|
||||
|
||||
/// 构造时捕获的缓存代次,纳入 key 相等性与磁盘文件名。
|
||||
/// 手动刷新时 _generation++,旧瓦片(内存 key + 磁盘文件)立即全部失效,
|
||||
/// 只影响 WMS 瓦片、不动 ESRI 底图缓存。
|
||||
final int _gen;
|
||||
static int _generation = 0;
|
||||
|
||||
static Future<Directory>? _cacheDirFuture;
|
||||
|
||||
static Future<Directory> _cacheDir() {
|
||||
return _cacheDirFuture ??= () async {
|
||||
try {
|
||||
final base = await getTemporaryDirectory();
|
||||
final dir = Directory('${base.path}${Platform.pathSeparator}wms_tiles');
|
||||
if (!await dir.exists()) {
|
||||
await dir.create(recursive: true);
|
||||
}
|
||||
return dir;
|
||||
} catch (e) {
|
||||
// 初始化失败不要把“坏 Future”永久缓存下来(否则后续所有瓦片都卡在这一步集体失败、
|
||||
// 表现为图层全不出来),置空以便下次调用重试
|
||||
_cacheDirFuture = null;
|
||||
rethrow;
|
||||
}
|
||||
}();
|
||||
}
|
||||
|
||||
/// URL + 代次 → 稳定短文件名(FNV-1a 32 位 + 长度,降低碰撞;代次前缀用于失效旧缓存)
|
||||
static String _fileName(String url, int gen) {
|
||||
var h = 0x811c9dc5;
|
||||
for (final c in url.codeUnits) {
|
||||
h ^= c;
|
||||
h = (h * 0x01000193) & 0xFFFFFFFF;
|
||||
}
|
||||
return 'g${gen}_${h.toRadixString(16).padLeft(8, '0')}_${url.length}.png';
|
||||
}
|
||||
|
||||
/// 清空全部 WMS 瓦片缓存(磁盘文件 + 递增代次使内存 key 失效)。
|
||||
/// 后端更新了同名图层的影像内容后,调用它强制丢弃旧缓存、重新联网拉取最新图。
|
||||
static Future<void> clearTileCache() async {
|
||||
_generation++;
|
||||
try {
|
||||
final dir = await _cacheDir();
|
||||
if (await dir.exists()) {
|
||||
await for (final e in dir.list()) {
|
||||
try {
|
||||
await e.delete();
|
||||
} catch (_) {}
|
||||
}
|
||||
}
|
||||
debugPrint('🧹 [WMS] 已清空瓦片磁盘缓存,代次 → $_generation');
|
||||
} catch (e) {
|
||||
debugPrint('⚠️ [WMS] 清缓存失败: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// ===== WMS 并发闸门 =====
|
||||
// 大图层(如 neimeng/zhongke)一屏可达十几片瓦片,若同时发起会把 GeoServer 压到排队渲染,
|
||||
// 每片等待时间暴涨、集体超时 → 全部返回透明图,表现为“一直出不来”。限制并发分批取,
|
||||
// 单片能更快拿到渲染资源、在超时内完成(浏览器预览能出图,正是因为它肯等且不这么挤)。
|
||||
static const int _maxConcurrentWms = 5;
|
||||
static int _activeWms = 0;
|
||||
static final List<Completer<void>> _wmsWaiters = [];
|
||||
|
||||
static Future<void> _acquireWmsSlot() async {
|
||||
if (_activeWms < _maxConcurrentWms) {
|
||||
_activeWms++;
|
||||
return;
|
||||
}
|
||||
final c = Completer<void>();
|
||||
_wmsWaiters.add(c);
|
||||
await c.future; // 被唤醒即表示 slot 已由 _releaseWmsSlot 直接转交,_activeWms 不变
|
||||
}
|
||||
|
||||
static void _releaseWmsSlot() {
|
||||
if (_wmsWaiters.isNotEmpty) {
|
||||
_wmsWaiters.removeAt(0).complete(); // 有等待者:slot 直接转交队首,_activeWms 保持
|
||||
} else {
|
||||
_activeWms--;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<CachedWmsImage> obtainKey(ImageConfiguration configuration) =>
|
||||
SynchronousFuture<CachedWmsImage>(this);
|
||||
|
||||
@override
|
||||
ImageStreamCompleter loadImage(
|
||||
CachedWmsImage key,
|
||||
ImageDecoderCallback decode,
|
||||
) {
|
||||
return MultiFrameImageStreamCompleter(
|
||||
codec: _loadAsync(decode),
|
||||
scale: 1.0,
|
||||
debugLabel: 'CachedWmsImage',
|
||||
);
|
||||
}
|
||||
|
||||
Future<ui.Codec> _loadAsync(ImageDecoderCallback decode) async {
|
||||
final sw = Stopwatch()..start();
|
||||
try {
|
||||
final dir = await _cacheDir();
|
||||
final file =
|
||||
File('${dir.path}${Platform.pathSeparator}${_fileName(url, _gen)}');
|
||||
|
||||
// 1) 命中磁盘缓存 → 秒开
|
||||
if (await file.exists()) {
|
||||
final cached = await file.readAsBytes();
|
||||
if (cached.isNotEmpty) {
|
||||
return decode(await ui.ImmutableBuffer.fromUint8List(cached));
|
||||
}
|
||||
}
|
||||
|
||||
// 2) 未命中 → 联网拉取。先过并发闸门,避免大图层一次性压垮 GeoServer 导致集体超时
|
||||
debugPrint('🌐 [WMS] MISS 联网取瓦片: ${_shortLayerTag(url)}');
|
||||
await _acquireWmsSlot();
|
||||
try {
|
||||
final resp = await http.get(Uri.parse(url)).timeout(kWmsTileTimeout);
|
||||
if (resp.statusCode == 200 && resp.bodyBytes.isNotEmpty) {
|
||||
try {
|
||||
await file.writeAsBytes(resp.bodyBytes, flush: false);
|
||||
} catch (_) {}
|
||||
debugPrint(
|
||||
'✅ [WMS] 已缓存(${sw.elapsedMilliseconds}ms, ${resp.bodyBytes.length}B): ${_shortLayerTag(url)}',
|
||||
);
|
||||
return decode(await ui.ImmutableBuffer.fromUint8List(resp.bodyBytes));
|
||||
}
|
||||
debugPrint(
|
||||
'⚠️ [WMS] 响应异常 status=${resp.statusCode}(${sw.elapsedMilliseconds}ms): ${_shortLayerTag(url)}',
|
||||
);
|
||||
} finally {
|
||||
_releaseWmsSlot();
|
||||
}
|
||||
} catch (e) {
|
||||
// 超时 / 网络 / 解码异常 → 透明兜底
|
||||
debugPrint('❌ [WMS] 瓦片失败(${sw.elapsedMilliseconds}ms): $e');
|
||||
}
|
||||
return decode(
|
||||
await ui.ImmutableBuffer.fromUint8List(TileProvider.transparentImage),
|
||||
);
|
||||
}
|
||||
|
||||
/// 从 URL 提取 LAYERS 参数做简短日志标签(避免整条 URL 刷屏)
|
||||
static String _shortLayerTag(String url) {
|
||||
final m = RegExp(r'LAYERS=([^&]+)').firstMatch(url);
|
||||
return m?.group(1) ?? 'unknown';
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
other is CachedWmsImage && other.url == url && other._gen == _gen;
|
||||
|
||||
@override
|
||||
int get hashCode => Object.hash(url, _gen);
|
||||
}
|
||||
|
||||
/// 场站图层范围仓库:
|
||||
/// - 优先从 GeoServer WMS GetCapabilities 动态拉取各图层真实范围
|
||||
/// (后台增/改/删图层,App 不发版即可跟上)
|
||||
/// - 拉取或解析失败时,回退到内置 kStationLayerBounds(离线/异常仍可用)
|
||||
class StationLayerRepo {
|
||||
StationLayerRepo._();
|
||||
|
||||
static final StationLayerRepo instance = StationLayerRepo._();
|
||||
|
||||
Map<String, LatLngBounds> _bounds =
|
||||
Map<String, LatLngBounds>.of(kStationLayerBounds);
|
||||
bool _dynamicLoaded = false;
|
||||
Future<void>? _inflight;
|
||||
final List<void Function()> _listeners = [];
|
||||
|
||||
/// 当前生效的范围表(动态成功后为最新;否则为内置兜底)
|
||||
Map<String, LatLngBounds> get bounds => _bounds;
|
||||
|
||||
/// 范围表是否来自动态拉取
|
||||
bool get isDynamic => _dynamicLoaded;
|
||||
|
||||
void addListener(void Function() cb) {
|
||||
if (!_listeners.contains(cb)) _listeners.add(cb);
|
||||
}
|
||||
|
||||
void removeListener(void Function() cb) => _listeners.remove(cb);
|
||||
|
||||
void _notify() {
|
||||
for (final cb in List<void Function()>.of(_listeners)) {
|
||||
cb();
|
||||
}
|
||||
}
|
||||
|
||||
/// 拉取 GetCapabilities 刷新范围表;并发调用自动合并;失败保持兜底不变
|
||||
Future<void> refresh() {
|
||||
return _inflight ??= _doRefresh().whenComplete(() => _inflight = null);
|
||||
}
|
||||
|
||||
Future<void> _doRefresh() async {
|
||||
final sw = Stopwatch()..start();
|
||||
try {
|
||||
// 用工作区专用端点:只返回 workSpace 下图层,XML 小、快、稳(全局端点图层多易超时)
|
||||
final url = Uri.parse('$kGeoserverBase/$kGeoWorkspace/wms').replace(
|
||||
queryParameters: {
|
||||
'SERVICE': 'WMS',
|
||||
'VERSION': '1.1.1',
|
||||
'REQUEST': 'GetCapabilities',
|
||||
},
|
||||
);
|
||||
debugPrint('🌐 [StationLayerRepo] 拉取 GetCapabilities...');
|
||||
final resp = await http.get(url).timeout(const Duration(seconds: 20));
|
||||
if (resp.statusCode != 200) {
|
||||
debugPrint(
|
||||
'⚠️ [StationLayerRepo] status=${resp.statusCode},保持兜底',
|
||||
);
|
||||
return;
|
||||
}
|
||||
final parsed = parseStationLayerBounds(resp.body);
|
||||
debugPrint(
|
||||
'📦 [StationLayerRepo] 解析到 ${parsed.length} 个图层(${sw.elapsedMilliseconds}ms): ${parsed.keys.join(', ')}',
|
||||
);
|
||||
if (parsed.isEmpty) {
|
||||
debugPrint('⚠️ [StationLayerRepo] 解析为空,保持内置兜底');
|
||||
return;
|
||||
}
|
||||
// 数量防护:动态解析出的图层数若明显少于内置兜底(如网络截断只解析到个别图层),
|
||||
// 直接整体替换会让其余图层凭空消失。改用“兜底打底 + 动态覆盖同名”的合并策略,
|
||||
// 既跟上后端最新范围,又保证已有图层不会因一次异常响应而集体消失。
|
||||
if (parsed.length < kStationLayerBounds.length) {
|
||||
debugPrint(
|
||||
'⚠️ [StationLayerRepo] 动态解析图层数(${parsed.length}) < 兜底(${kStationLayerBounds.length}),采用合并策略防止图层消失',
|
||||
);
|
||||
_bounds = {...kStationLayerBounds, ...parsed};
|
||||
} else {
|
||||
_bounds = parsed;
|
||||
debugPrint('✅ [StationLayerRepo] 已切换为动态范围');
|
||||
}
|
||||
_dynamicLoaded = true;
|
||||
_notify();
|
||||
} catch (e) {
|
||||
// 保持兜底,不打断地图
|
||||
debugPrint(
|
||||
'❌ [StationLayerRepo] GetCapabilities 失败(${sw.elapsedMilliseconds}ms),保持兜底: $e',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 基于当前范围表构建全部场站叠加图层(滑到哪个场站区域就叠加哪个)
|
||||
List<TileLayer> buildLayers({double minZoom = 16, double maxZoom = 21}) {
|
||||
return _bounds.entries.map((entry) {
|
||||
// 自愈兜底:万一范围表里仍残留非法 bounds(历史污染等),回退到内置兜底的同名范围,
|
||||
// 避免生成“框不住任何瓦片”的坏图层导致该场站整体不显示
|
||||
final b = _isValidLayerBounds(entry.value)
|
||||
? entry.value
|
||||
: (kStationLayerBounds[entry.key] ?? entry.value);
|
||||
return TileLayer(
|
||||
tileProvider: GeoWmsProvider(
|
||||
layerName: entry.key,
|
||||
bounds: b,
|
||||
maxLevel: maxZoom.toInt(),
|
||||
),
|
||||
userAgentPackageName: 'com.example.app',
|
||||
minZoom: minZoom,
|
||||
maxZoom: maxZoom,
|
||||
maxNativeZoom: maxZoom.toInt(),
|
||||
tileBounds: b,
|
||||
);
|
||||
}).toList();
|
||||
}
|
||||
}
|
||||
|
||||
/// 校验图层范围是否为合法且有意义的经纬度矩形:
|
||||
/// - 纬度 ∈ [-90,90]、经度 ∈ [-180,180]
|
||||
/// - 南北/东西跨度 > 0 且 ≤ 5°(场站地块都是几百米级小范围,跨度过大必是投影错误/脏数据)
|
||||
/// 非法范围一旦混入范围表,会让 getImage 的相交判断恒为 false → 瓦片全透明 → 图层“整体消失”,
|
||||
/// 这正是“放大也不出来、只能退出重登(页面重建重置图层列表)才恢复”的根因。
|
||||
bool _isValidLayerBounds(LatLngBounds b) {
|
||||
final s = b.south;
|
||||
final n = b.north;
|
||||
final w = b.west;
|
||||
final e = b.east;
|
||||
if (s < -90 || s > 90 || n < -90 || n > 90) return false;
|
||||
if (w < -180 || w > 180 || e < -180 || e > 180) return false;
|
||||
final dLat = n - s;
|
||||
final dLng = e - w;
|
||||
if (dLat <= 0 || dLng <= 0) return false;
|
||||
if (dLat > 5 || dLng > 5) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// 解析 WMS GetCapabilities,提取 workSpace 下每个图层的 name → EPSG:4326 范围。
|
||||
/// 同时兼容 1.1.1(LatLongBoundingBox)与 1.3.0(EX_GeographicBoundingBox)。
|
||||
Map<String, LatLngBounds> parseStationLayerBounds(String xmlStr) {
|
||||
final result = <String, LatLngBounds>{};
|
||||
try {
|
||||
final doc = XmlDocument.parse(xmlStr);
|
||||
for (final layer in doc.findAllElements('Layer')) {
|
||||
final nameEls = layer.findElements('Name');
|
||||
if (nameEls.isEmpty) continue;
|
||||
final rawName = nameEls.first.innerText.trim();
|
||||
// 兼容两种命名:全局端点返回 workSpace:xxx;工作区端点可能只返回裸名 xxx
|
||||
final String name;
|
||||
if (rawName.startsWith('$kGeoWorkspace:')) {
|
||||
name = rawName;
|
||||
} else if (!rawName.contains(':')) {
|
||||
name = '$kGeoWorkspace:$rawName';
|
||||
} else {
|
||||
continue; // 其它工作区图层,跳过
|
||||
}
|
||||
|
||||
LatLngBounds? b;
|
||||
|
||||
// WMS 1.1.1
|
||||
final bbEls = layer.findElements('LatLongBoundingBox');
|
||||
if (bbEls.isNotEmpty) {
|
||||
final bb = bbEls.first;
|
||||
final minx = double.tryParse(bb.getAttribute('minx') ?? '');
|
||||
final miny = double.tryParse(bb.getAttribute('miny') ?? '');
|
||||
final maxx = double.tryParse(bb.getAttribute('maxx') ?? '');
|
||||
final maxy = double.tryParse(bb.getAttribute('maxy') ?? '');
|
||||
if (minx != null &&
|
||||
miny != null &&
|
||||
maxx != null &&
|
||||
maxy != null &&
|
||||
maxx > minx &&
|
||||
maxy > miny) {
|
||||
b = LatLngBounds(LatLng(miny, minx), LatLng(maxy, maxx));
|
||||
}
|
||||
}
|
||||
|
||||
// WMS 1.3.0 兜底
|
||||
if (b == null) {
|
||||
final exEls = layer.findElements('EX_GeographicBoundingBox');
|
||||
if (exEls.isNotEmpty) {
|
||||
final ex = exEls.first;
|
||||
double? read(String tag) {
|
||||
final els = ex.findElements(tag);
|
||||
return els.isEmpty
|
||||
? null
|
||||
: double.tryParse(els.first.innerText.trim());
|
||||
}
|
||||
|
||||
final w = read('westBoundLongitude');
|
||||
final e = read('eastBoundLongitude');
|
||||
final n = read('northBoundLatitude');
|
||||
final s = read('southBoundLatitude');
|
||||
if (w != null && e != null && n != null && s != null && e > w && n > s) {
|
||||
b = LatLngBounds(LatLng(s, w), LatLng(n, e));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (b != null) {
|
||||
if (_isValidLayerBounds(b)) {
|
||||
result[name] = b;
|
||||
} else {
|
||||
// 范围非法(经纬度越界 / 跨度过大或为 0)→ 丢弃该图层,
|
||||
// 避免污染范围表导致瓦片框不住、图层整体消失
|
||||
debugPrint(
|
||||
'⚠️ [StationLayerRepo] 图层 $name 范围非法已丢弃: S=${b.south} N=${b.north} W=${b.west} E=${b.east}',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (_) {
|
||||
// 解析失败返回空表,调用方保持兜底
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user