```
feat(protocol): Observation坐标字段可选化及协议v2扩展 - 将Observation中longitude/latitude/altitude_m/heading_deg改为Option类型, 支持无位置信息的观测数据 - 新增DeviceTelemetry结构体承载设备遥测扩展字段(serial, mac, model, rssi, speed, vertical_speed, pressure_altitude, geometric_altitude, height_agl, pilot_position等) - 新增LegacyObservation兼容结构体及From转换,保持旧接收器协议输入兼容 - PROTOCOL_VERSION从1升至2 - 新增coordinates()辅助方法统一提取有效坐标 - validate()适配Option类型,允许坐标字段同时缺失 - Store新增position_generation机制追踪位置可用性变化,确保合并帧间的 位置丢失仍能正确断开轨迹 - geofence-map-adapter过滤无效坐标避免渲染异常 - 新增appearance.css外观主题系统,支持blue/graphite/white三套配色 ```
This commit is contained in:
@@ -5,14 +5,15 @@ maplibre.setWorkerUrl('/vendor/maplibre-gl-worker.mjs');
|
||||
let tileBuffer;
|
||||
let map, layer, latest, ids = [], selected = '', pending = false;
|
||||
let frameTimes = [], lastFrame = 0;
|
||||
let marker=null, history=[], lastTrail=0;
|
||||
let marker=null, history=[], lastTrail=0, generations=null, selectedGeneration=null;
|
||||
let centered=false, workerWasmBytes=0, resizeObserver;
|
||||
let basemap="imagery",tileRevision=0,mapCreations=0;
|
||||
const basemapLayers={road:["vec_w","cva_w"],imagery:["img_w","cia_w"]};
|
||||
function tileUrl(index){return `/api/maps/tile/${basemapLayers[basemap][index]}/{z}/{x}/{y}${tileRevision?`?v=${tileRevision}`:''}`;}
|
||||
let classification=new Map(),listFilter="all";
|
||||
function targetKind(id){return classification.get(id)||"unknown";}
|
||||
function targetVisible(id){return listFilter==="all"||targetKind(id)===listFilter;}
|
||||
function located(id,i=ids.indexOf(id)){return i>=0&&latest&&Number.isFinite(latest[i*4])&&Number.isFinite(latest[i*4+1]);}
|
||||
function targetVisible(id,i=ids.indexOf(id)){return located(id,i)&&(listFilter==="all"||targetKind(id)===listFilter);}
|
||||
function setClassification(value,filter="all"){classification=new Map(Object.entries(value||{}).filter(([,kind])=>kind==="white"||kind==="black"));listFilter=["white","black","unknown"].includes(filter)?filter:"all";flush();}
|
||||
let occlusion={top:0,right:0,bottom:0,left:0};
|
||||
function shader(gl, type, source) {
|
||||
@@ -94,7 +95,7 @@ function create(container,initialBasemap="imagery",preferences=null) {
|
||||
});
|
||||
map.on('click',event=>{
|
||||
if(!latest)return;let index=-1,distance=18;
|
||||
for(let i=0;i<ids.length;i++){if(!targetVisible(ids[i]))continue;const p=map.project([latest[i*4],latest[i*4+1]]);const d=Math.hypot(p.x-event.point.x,p.y-event.point.y);if(d<distance){index=i;distance=d;}}
|
||||
for(let i=0;i<ids.length;i++){if(!targetVisible(ids[i],i))continue;const p=map.project([latest[i*4],latest[i*4+1]]);const d=Math.hypot(p.x-event.point.x,p.y-event.point.y);if(d<distance){index=i;distance=d;}}
|
||||
if(index>=0)window.dispatchEvent(new CustomEvent('rid-select',{detail:ids[index]}));
|
||||
});
|
||||
map.on('error',e=>{if(!e.sourceId?.startsWith('tdt-'))console.error('map:',e.error?.message || e);});
|
||||
@@ -102,7 +103,7 @@ function create(container,initialBasemap="imagery",preferences=null) {
|
||||
}
|
||||
function flush(){
|
||||
pending=false;if(!latest||!layer?.gl)return;const gl=layer.gl;
|
||||
const data=new Float32Array(ids.length*4);let visible=0;for(let i=0;i<ids.length;i++){if(!targetVisible(ids[i]))continue;const j=visible++;const p=maplibre.MercatorCoordinate.fromLngLat([latest[i*4],latest[i*4+1]]);data[j*4]=p.x-layer.origin.x;data[j*4+1]=p.y-layer.origin.y;data[j*4+2]=-latest[i*4+3]*Math.PI/180;const kind=targetKind(ids[i]);data[j*4+3]=(kind==="black"?4:kind==="white"?2:0)+(ids[i]===selected?1:0);}
|
||||
const data=new Float32Array(ids.length*4);let visible=0;for(let i=0;i<ids.length;i++){if(!targetVisible(ids[i],i))continue;const j=visible++;const p=maplibre.MercatorCoordinate.fromLngLat([latest[i*4],latest[i*4+1]]);data[j*4]=p.x-layer.origin.x;data[j*4+1]=p.y-layer.origin.y;data[j*4+2]=-(Number.isFinite(latest[i*4+3])?latest[i*4+3]:0)*Math.PI/180;const kind=targetKind(ids[i]);data[j*4+3]=(kind==="black"?4:kind==="white"?2:0)+(ids[i]===selected?1:0);}
|
||||
gl.bindBuffer(gl.ARRAY_BUFFER,layer.instances);gl.bufferSubData(gl.ARRAY_BUFFER,0,data);layer.count=visible;map.triggerRepaint();
|
||||
const index=ids.indexOf(selected);
|
||||
if(index>=0&&targetVisible(selected)){
|
||||
@@ -113,9 +114,9 @@ function flush(){
|
||||
map.getSource('selected-trail')?.setData({type:'FeatureCollection',features:history.length<2?[]:[{type:'Feature',properties:{},geometry:{type:'LineString',coordinates:history.map(p=>p.coordinate)}}]});}
|
||||
}else{marker?.remove();marker=null;history=[];map.getSource('selected-trail')?.setData({type:'FeatureCollection',features:[]});}
|
||||
}
|
||||
function update(positions,newIds){latest=positions;if(newIds)ids=Array.from(newIds);if(!centered&&latest.length>=4){centered=true;focusCoordinate([latest[0],latest[1]], false);}if(!pending){pending=true;requestAnimationFrame(flush);}}
|
||||
function select(id){if(selected!==id){selected=id;history=[];flush();}const i=ids.indexOf(id);return i>=0&&latest?Array.from(latest.slice(i*4,i*4+4)):[];}
|
||||
function metrics(){const sorted=[...frameTimes].sort((a,b)=>a-b);const gl=layer?.gl;const ext=gl?.getExtension('WEBGL_debug_renderer_info');const projected=map&&latest&&ids.includes(selected)?map.project([latest[ids.indexOf(selected)*4],latest[ids.indexOf(selected)*4+1]]):null;return {tileBuffer:tileBuffer?.metrics(),basemap,mapCreations,camera:map?{center:map.getCenter().toArray(),zoom:map.getZoom(),pitch:map.getPitch(),bearing:map.getBearing()}:null,baseTiles:map?.getSource('tdt-base')?.tiles,labelTiles:map?.getSource('tdt-labels')?.tiles,selectedPixel:projected?{x:projected.x,y:projected.y}:null,occlusion:{...occlusion},mapReady:!!layer?.gl,styleReady:map?.isStyleLoaded()||false,mapCanvasWidth:map?.getCanvas().clientWidth||0,mapCanvasHeight:map?.getCanvas().clientHeight||0,samples:sorted.length,drawSubmissionIntervalP95Ms:sorted[Math.floor(sorted.length*.95)]||0,targets:ids.length,visibleTargets:ids.filter(targetVisible).length,listFilter,classifiedTargets:{white:ids.filter(id=>targetKind(id)==="white").length,black:ids.filter(id=>targetKind(id)==="black").length},renderer:ext?gl.getParameter(ext.UNMASKED_RENDERER_WEBGL):'unavailable',selectedTrailPoints:history.length,workerWasmBytes};}
|
||||
function update(positions,newIds,newGenerations){latest=positions;generations=newGenerations;if(newIds)ids=Array.from(newIds);const selectedIndex=ids.indexOf(selected);const generation=selectedIndex>=0?generations?.[selectedIndex]:null;if(!located(selected)||generation!==selectedGeneration){history=[];lastTrail=0;}selectedGeneration=generation;if(!centered){const i=ids.findIndex(located);if(i>=0){centered=true;focusCoordinate([latest[i*4],latest[i*4+1]], false);}}if(!pending){pending=true;requestAnimationFrame(flush);}}
|
||||
function select(id){if(selected!==id){selected=id;history=[];selectedGeneration=generations?.[ids.indexOf(id)]??null;flush();}const i=ids.indexOf(id);return located(id)?Array.from(latest.slice(i*4,i*4+4)):[];}
|
||||
function metrics(){const sorted=[...frameTimes].sort((a,b)=>a-b);const gl=layer?.gl;const ext=gl?.getExtension('WEBGL_debug_renderer_info');const projected=map&&located(selected)?map.project([latest[ids.indexOf(selected)*4],latest[ids.indexOf(selected)*4+1]]):null;return {tileBuffer:tileBuffer?.metrics(),basemap,mapCreations,camera:map?{center:map.getCenter().toArray(),zoom:map.getZoom(),pitch:map.getPitch(),bearing:map.getBearing()}:null,baseTiles:map?.getSource('tdt-base')?.tiles,labelTiles:map?.getSource('tdt-labels')?.tiles,selectedPixel:projected?{x:projected.x,y:projected.y}:null,occlusion:{...occlusion},mapReady:!!layer?.gl,styleReady:map?.isStyleLoaded()||false,mapCanvasWidth:map?.getCanvas().clientWidth||0,mapCanvasHeight:map?.getCanvas().clientHeight||0,samples:sorted.length,drawSubmissionIntervalP95Ms:sorted[Math.floor(sorted.length*.95)]||0,targets:ids.length,visibleTargets:ids.filter(targetVisible).length,listFilter,classifiedTargets:{white:ids.filter(id=>targetKind(id)==="white").length,black:ids.filter(id=>targetKind(id)==="black").length},renderer:ext?gl.getParameter(ext.UNMASKED_RENDERER_WEBGL):'unavailable',selectedTrailPoints:history.length,workerWasmBytes};}
|
||||
function connection(connected){const canvas=document.getElementById('rt-map');if(canvas)canvas.dataset.stale=connected?'false':'true';}
|
||||
function refresh(){tileRevision++;tileBuffer?.schedule();for(const [i,id]of ['tdt-base','tdt-labels'].entries())map?.getSource(id)?.setTiles([tileUrl(i)]);}
|
||||
function setBasemap(value){const next=value==='road'?'road':'imagery';if(next!==basemap){basemap=next;for(const [i,id]of ['tdt-base','tdt-labels'].entries())map?.getSource(id)?.setTiles([tileUrl(i)]);tileBuffer?.schedule();}}
|
||||
@@ -143,5 +144,5 @@ function focusCoordinate(center,animate=true){
|
||||
const options={center,offset:[(p.left-p.right)/2,(p.top-p.bottom)/2]};
|
||||
if(animate)map.flyTo({...options,zoom:Math.max(map.getZoom(),13),duration:400});else map.easeTo({...options,duration:0});
|
||||
}
|
||||
function centerSelected(){if(!map||!latest||!selected)return;const i=ids.indexOf(selected);if(i>=0)focusCoordinate([latest[i*4],latest[i*4+1]]);}
|
||||
window.ridMap={workerMemory(bytes){if(Number.isFinite(bytes))workerWasmBytes=bytes;},create,update,select,metrics,connection,refresh,setBasemap,setClassification,baseReady,setOcclusion,focusVisible(){requestAnimationFrame(()=>requestAnimationFrame(centerSelected));},zoomIn(){if(map)map.zoomIn();},zoomOut(){if(map)map.zoomOut();},centerSelected,resetBearing(){if(map)map.resetNorthPitch({duration:400});},toggle3d(){if(!map)return;const p=map.getPitch();map.easeTo({pitch:p>10?0:55,duration:500});},destroy(){tileBuffer?.destroy();tileBuffer=null;resizeObserver?.disconnect();resizeObserver=null;marker?.remove();marker=null;history=[];map?.remove();map=null;layer=null;latest=null;ids=[];centered=false;classification=new Map();listFilter="all";occlusion={top:0,right:0,bottom:0,left:0};}};
|
||||
function centerSelected(){if(!map||!latest||!selected)return;const i=ids.indexOf(selected);if(i>=0&&located(selected))focusCoordinate([latest[i*4],latest[i*4+1]]);}
|
||||
window.ridMap={workerMemory(bytes){if(Number.isFinite(bytes))workerWasmBytes=bytes;},create,update,select,metrics,connection,refresh,setBasemap,setClassification,baseReady,setOcclusion,focusVisible(){requestAnimationFrame(()=>requestAnimationFrame(centerSelected));},zoomIn(){if(map)map.zoomIn();},zoomOut(){if(map)map.zoomOut();},centerSelected,resetBearing(){if(map)map.resetNorthPitch({duration:400});},toggle3d(){if(!map)return;const p=map.getPitch();map.easeTo({pitch:p>10?0:55,duration:500});},destroy(){tileBuffer?.destroy();tileBuffer=null;resizeObserver?.disconnect();resizeObserver=null;marker?.remove();marker=null;history=[];generations=null;selectedGeneration=null;map?.remove();map=null;layer=null;latest=null;ids=[];centered=false;classification=new Map();listFilter="all";occlusion={top:0,right:0,bottom:0,left:0};}};
|
||||
|
||||
Reference in New Issue
Block a user