Files
flutterApp/lib/features/home/presentation/widgets/map/geoserver_suzhou_layer.dart

187 lines
6.3 KiB
Dart
Raw Normal View History

import 'dart:math' as math;
import 'package:flutter/widgets.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:latlong2/latlong.dart';
/// GeoServer 基地址(公司端)
const String kGeoserverBase = 'http://1.95.137.212:7900/geoserver';
/// 各场站高清图层的真实范围(取自 GeoServer GetCapabilities,原生 EPSG:4326)
/// key = 后端下发的 siteMapName(形如 workSpace:suzhou)
final Map<String, LatLngBounds> kStationLayerBounds = {
'workSpace:liaoding': LatLngBounds(
const LatLng(41.19998369457442, 120.59074514010273),
const LatLng(41.202002066385475, 120.59343722827124),
),
'workSpace:loudi': LatLngBounds(
const LatLng(27.846682995656035, 111.52814279952669),
const LatLng(27.847939704917945, 111.52928081352842),
),
'workSpace:neimeng': LatLngBounds(
const LatLng(41.321581124009896, 112.62468603332228),
const LatLng(41.325573311689986, 112.63034797059984),
),
'workSpace:result': LatLngBounds(
const LatLng(27.36490549568922, 116.84490756003203),
const LatLng(27.36673908366302, 116.84661654295478),
),
'workSpace:sanxiamen': LatLngBounds(
const LatLng(34.732748259670494, 111.43051478686061),
const LatLng(34.734205267863345, 111.43201022647355),
),
'workSpace:suzhou': LatLngBounds(
const LatLng(31.082450793459877, 120.83906484264836),
const LatLng(31.084141983135456, 120.84164596446476),
),
'workSpace:zhongke': LatLngBounds(
const LatLng(32.0406442971753, 120.8057183175354),
const LatLng(32.045144217672636, 120.81100452381844),
),
};
/// 取某场站图层的范围中心点(用作进场默认定位);未知/为空返回 null
LatLng? stationLayerCenter(String? siteMapName) {
if (siteMapName == null) return null;
final b = kStationLayerBounds[siteMapName];
if (b == null) return null;
return LatLng((b.south + b.north) / 2, (b.west + b.east) / 2);
}
/// 叠加层取瓦片方式开关:
/// - false(当前):WMS GetMap 兜底,不依赖 GWC 缓存
/// - true:WMTS GetTile(公司补配 GWC EPSG:3857 gridset 并 Seed 后切换)
const bool kUseWmtsTile = false;
/// WMS 取图提供器:
/// 实测 GeoServer 对 workSpace:suzhou 只有原生 EPSG:4326 能出图,
/// 请求 EPSG:3857(WMS 1.1.1 / 1.3.0 均验证过)返回纯白图。
/// 因此这里按瓦片 XYZ 反算经纬度 bbox,以 EPSG:4326 请求 WMS。
class GeoWmsProvider extends TileProvider {
GeoWmsProvider({
required this.layerName,
required this.bounds,
this.maxLevel = 21,
});
final String layerName;
final LatLngBounds bounds;
final int maxLevel;
/// Web 墨卡托地球周长(米)
static const double _full = 40075016.685578488;
/// Web 墨卡托 Y(米)→ 纬度(度)
static double _mercatorYToLat(double y) {
final t = y / 6378137.0;
final sinh = (math.exp(t) - math.exp(-t)) / 2; // dart:math 无 sinh,手动实现
return math.atan(sinh) * 180 / math.pi;
}
@override
ImageProvider<Object> getImage(
TileCoordinates coordinates,
TileLayer options,
) {
if (coordinates.z > maxLevel) {
return MemoryImage(TileProvider.transparentImage);
}
final span = _full / math.pow(2, coordinates.z);
final westM = -_full / 2 + coordinates.x * span;
final eastM = westM + span;
final northM = _full / 2 - coordinates.y * span;
final southM = northM - span;
final westLng = westM / _full * 360;
final eastLng = eastM / _full * 360;
final southLat = _mercatorYToLat(southM);
final northLat = _mercatorYToLat(northM);
// 与图层实际范围不相交的瓦片直接返回透明图,不发无谓请求
final intersects = westLng < bounds.east &&
eastLng > bounds.west &&
southLat < bounds.north &&
northLat > bounds.south;
if (!intersects) {
return MemoryImage(TileProvider.transparentImage);
}
final url = Uri.parse('$kGeoserverBase/wms').replace(
queryParameters: {
'SERVICE': 'WMS',
'REQUEST': 'GetMap',
'VERSION': '1.1.1',
'LAYERS': layerName,
'BBOX': '${westLng.toStringAsFixed(6)},${southLat.toStringAsFixed(6)},'
'${eastLng.toStringAsFixed(6)},${northLat.toStringAsFixed(6)}',
'WIDTH': '256',
'HEIGHT': '256',
'FORMAT': 'image/png',
'TRANSPARENT': 'true',
'SRS': 'EPSG:4326',
'STYLES': '',
},
);
return NetworkImage(url.toString());
}
}
/// GeoServer 场站无人机高清影像叠加层(WGS84 基准,覆盖在 ESRI 底图上)
/// 按当前选中场站的 siteMapName 动态加载对应图层;地块很小,默认 minZoom = 16
class GeoSuzhouOverlay extends StatelessWidget {
const GeoSuzhouOverlay({
super.key,
this.siteMapName,
this.minZoom = 16,
this.maxZoom = 21,
});
/// 当前选中场站对应的 GeoServer 图层名(形如 workSpace:suzhou)
final String? siteMapName;
/// 小于该级别不请求瓦片(地块太小,低层级看不见)
final double minZoom;
/// 大于该级别不请求瓦片
final double maxZoom;
@override
Widget build(BuildContext context) {
final layer = siteMapName;
final bounds = layer == null ? null : kStationLayerBounds[layer];
// 未选中场站或图层未知 → 不渲染叠加层
if (layer == null || bounds == null) {
return const SizedBox.shrink();
}
if (kUseWmtsTile) {
// === WMTS 版:公司把 EPSG:3857 gridset 加入 GWC 并 Seed 后生效 ===
return TileLayer(
urlTemplate: '$kGeoserverBase/gwc/service/wmts'
'?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0'
'&LAYER=$layer&TILEMATRIXSET=EPSG:3857'
'&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&FORMAT=image/png',
userAgentPackageName: 'com.example.app',
minZoom: minZoom,
maxZoom: maxZoom,
maxNativeZoom: maxZoom.toInt(),
tileBounds: bounds,
);
}
// === WMS 版(当前兜底):按原生 EPSG:4326 请求,GeoServer 实时渲染 ===
return TileLayer(
tileProvider: GeoWmsProvider(
layerName: layer,
bounds: bounds,
maxLevel: maxZoom.toInt(),
),
userAgentPackageName: 'com.example.app',
minZoom: minZoom,
maxZoom: maxZoom,
maxNativeZoom: maxZoom.toInt(),
tileBounds: bounds,
);
}
}