@@ -320,26 +320,77 @@ export const getColsForRgtYatcFromFile = async (
320320 }
321321} ;
322322
323- export const duckDbReadAndUpdateElevationData = async ( req_id : number , layerName :string ) : Promise < ElevationDataItem | null > => {
323+ export async function getRepresentativeElevationPointForReq (
324+ req_id : number
325+ ) : Promise < ElevationDataItem | null > {
326+ const start = performance . now ( ) ;
327+ try {
328+ const fileName = await indexedDb . getFilename ( req_id ) ;
329+ const duckDbClient = await createDuckDbClient ( ) ;
330+ await duckDbClient . insertOpfsParquet ( fileName ) ;
331+
332+ const fns = useFieldNameStore ( ) ;
333+ const rgtCol = fns . getUniqueTrkFieldName ( req_id ) ;
334+ const cycleCol = fns . getUniqueOrbitIdFieldName ( req_id ) ;
335+ const spotCol = fns . getUniqueSpotIdFieldName ( req_id ) ;
336+
337+ const esc = duckDbClient . escape ;
338+
339+ const sql = `
340+ WITH top_combo AS (
341+ SELECT ${ esc ( rgtCol ) } AS rgt, ${ esc ( cycleCol ) } AS cycle, ${ esc ( spotCol ) } AS spot
342+ FROM '${ fileName } '
343+ GROUP BY ${ esc ( rgtCol ) } , ${ esc ( cycleCol ) } , ${ esc ( spotCol ) }
344+ ORDER BY COUNT(*) DESC
345+ LIMIT 1
346+ )
347+ SELECT t.*
348+ FROM '${ fileName } ' t
349+ JOIN top_combo tc
350+ ON t.${ esc ( rgtCol ) } = tc.rgt
351+ AND t.${ esc ( cycleCol ) } = tc.cycle
352+ AND t.${ esc ( spotCol ) } = tc.spot
353+ LIMIT 1;
354+ ` ;
355+
356+ const q = await duckDbClient . query ( sql ) ;
357+ for await ( const chunk of q . readRows ( ) ) {
358+ if ( chunk . length > 0 ) return chunk [ 0 ] as ElevationDataItem ;
359+ }
360+ return null ;
361+ } catch ( err ) {
362+ console . error ( "getRepresentativeElevationPointForReq error:" , err ) ;
363+ return null ;
364+ } finally {
365+ console . log (
366+ "getRepresentativeElevationPointForReq took" ,
367+ ( performance . now ( ) - start ) . toFixed ( 1 ) ,
368+ "ms"
369+ ) ;
370+ }
371+ }
372+
373+
374+ export const duckDbReadAndUpdateElevationData = async ( req_id : number , layerName : string ) : Promise < ElevationDataItem | null > => {
324375 console . log ( 'duckDbReadAndUpdateElevationData req_id:' , req_id ) ;
325- const startTime = performance . now ( ) ; // Start time
376+ const startTime = performance . now ( ) ;
326377
327378 let firstRec : ElevationDataItem | null = null ;
328379 let numRows = 0 ;
329380 let srViewName = await indexedDb . getSrViewName ( req_id ) ;
330-
381+
331382 if ( ! srViewName || srViewName === '' || srViewName === 'Global' ) {
332383 srViewName = 'Global Mercator Esri' ;
333384 console . error ( `HACK ALERT!! inserting srViewName:${ srViewName } for reqId:${ req_id } ` ) ;
334385 }
335386
336387 const projName = srViews . value [ srViewName ] . projectionName ;
337-
388+
338389 if ( ! req_id ) {
339390 console . error ( 'duckDbReadAndUpdateElevationData Bad req_id:' , req_id ) ;
340391 return null ;
341392 }
342-
393+
343394 const pntData = useAnalysisMapStore ( ) . getPntDataByReqId ( req_id . toString ( ) ) ;
344395 pntData . isLoading = true ;
345396
@@ -348,23 +399,37 @@ export const duckDbReadAndUpdateElevationData = async (req_id: number,layerName:
348399 console . error ( 'duckDbReadAndUpdateElevationData req_id:' , req_id , ' status is error SKIPPING!' ) ;
349400 return null ;
350401 }
351- const mission = useFieldNameStore ( ) . getMissionForReqId ( req_id ) ;
352402
353403 const duckDbClient = await createDuckDbClient ( ) ;
354404 const filename = await indexedDb . getFilename ( req_id ) ;
355405 await duckDbClient . insertOpfsParquet ( filename ) ;
356406
407+ // ─────────────────────────────────────────────────────────
408+ // NEW: pick representative point from top (rgt,cycle,spot)
409+ // default: median by x-axis (falls back internally)
410+ // alternative: pass "strongest"
411+ // ─────────────────────────────────────────────────────────
412+ try {
413+ const rep = await getRepresentativeElevationPointForReq ( req_id ) ;
414+ if ( rep ) {
415+ firstRec = rep ; // <- set early; we still build/render the layer from chunks below
416+ }
417+ } catch ( e ) {
418+ console . warn ( 'Representative point selection failed; will fall back to first clickable row:' , e ) ;
419+ }
420+
357421 let rows : ElevationDataItem [ ] = [ ] ;
358422 let positions : SrPosition [ ] = [ ] ; // Precompute positions
359- const pntData = useAnalysisMapStore ( ) . getPntDataByReqId ( req_id . toString ( ) ) ;
360- pntData . totalPnts = 0 ;
361- pntData . currentPnts = 0 ;
423+ const pntDataLocal = useAnalysisMapStore ( ) . getPntDataByReqId ( req_id . toString ( ) ) ;
424+ pntDataLocal . totalPnts = 0 ;
425+ pntDataLocal . currentPnts = 0 ;
426+
362427 try {
363428 const sample_fraction = await computeSamplingRate ( req_id ) ;
364429 const result = await duckDbClient . queryChunkSampled ( `SELECT * FROM read_parquet('${ filename } ')` , sample_fraction ) ;
365430
366431 if ( result . totalRows ) {
367- pntData . totalPnts = result . totalRows ;
432+ pntDataLocal . totalPnts = result . totalRows ;
368433 } else if ( result . schema === undefined ) {
369434 console . warn ( 'duckDbReadAndUpdateElevationData totalRows and schema are undefined result:' , result ) ;
370435 }
@@ -375,24 +440,23 @@ export const duckDbReadAndUpdateElevationData = async (req_id: number,layerName:
375440 if ( ! done && value ) {
376441 rows = value as ElevationDataItem [ ] ;
377442 numRows = rows . length ;
378- pntData . currentPnts = numRows ;
379- //console.log('duckDbReadAndUpdateElevationData rows:', rows);
380- // **Find the first valid elevation point**
381- firstRec = rows . find ( isClickable ) || null ;
382- if ( ! firstRec ) {
383- console . warn ( 'duckDbReadAndUpdateElevationData find(isClickable) firstRec is null' ) ;
384- if ( rows . length > 0 ) {
443+ pntDataLocal . currentPnts = numRows ;
444+
445+ // Fallback if representative point was not found
446+ if ( ! firstRec ) {
447+ firstRec = rows . find ( isClickable ) || null ;
448+ if ( ! firstRec && rows . length > 0 ) {
385449 firstRec = rows [ 0 ] ;
386450 }
387451 }
452+
388453 if ( firstRec ) {
389- // Precompute position data for all rows
390454 const lat_fieldname = useFieldNameStore ( ) . getLatFieldName ( req_id ) ;
391455 const lon_fieldname = useFieldNameStore ( ) . getLonFieldName ( req_id ) ;
392456 positions = rows . map ( d => [
393457 d [ lon_fieldname ] ,
394458 d [ lat_fieldname ] ,
395- 0 // always make this 0 so the tracks are flat
459+ 0 // keep tracks flat
396460 ] as SrPosition ) ;
397461 } else {
398462 console . warn ( `No valid elevation points found in ${ numRows } rows.` ) ;
@@ -413,14 +477,12 @@ export const duckDbReadAndUpdateElevationData = async (req_id: number,layerName:
413477
414478 if ( summary ?. extHMean ) {
415479 useCurReqSumStore ( ) . setSummary ( {
416- req_id : req_id ,
480+ req_id,
417481 extLatLon : summary . extLatLon ,
418482 extHMean : summary . extHMean ,
419483 numPoints : summary . numPoints
420484 } ) ;
421- //console.log('duckDbReadAndUpdateElevationData',height_fieldname,'positions:', positions);
422485 updateDeckLayerWithObject ( layerName , rows , summary . extHMean , height_fieldname , positions , projName ) ;
423-
424486 } else {
425487 console . error ( 'duckDbReadAndUpdateElevationData summary is undefined' ) ;
426488 }
@@ -431,9 +493,9 @@ export const duckDbReadAndUpdateElevationData = async (req_id: number,layerName:
431493 console . error ( 'duckDbReadAndUpdateElevationData error:' , error ) ;
432494 throw error ;
433495 } finally {
434- const pntData = useAnalysisMapStore ( ) . getPntDataByReqId ( req_id . toString ( ) ) ;
435- pntData . isLoading = false ;
436- const endTime = performance . now ( ) ; // End time
496+ const pntDataFinal = useAnalysisMapStore ( ) . getPntDataByReqId ( req_id . toString ( ) ) ;
497+ pntDataFinal . isLoading = false ;
498+ const endTime = performance . now ( ) ;
437499 console . log ( `duckDbReadAndUpdateElevationData for ${ req_id } took ${ endTime - startTime } milliseconds.` ) ;
438500 return { firstRec, numRows } ;
439501 }
0 commit comments