@@ -343,33 +343,18 @@ export function addCommands(
343
343
commands . addCommand ( CommandIDs . buffer , {
344
344
label : trans . __ ( 'Buffer' ) ,
345
345
isEnabled : ( ) => {
346
- const model = tracker . currentWidget ?. model ;
347
- const localState = model ?. sharedModel . awareness . getLocalState ( ) ;
348
-
349
- if ( ! model || ! localState || ! localState [ 'selected' ] ?. value ) {
346
+ const selected = getSelectedLayer ( tracker ) ;
347
+ if ( ! selected ) {
350
348
return false ;
351
349
}
352
-
353
- const selectedLayers = localState [ 'selected' ] . value ;
354
-
355
- if ( Object . keys ( selectedLayers ) . length > 1 ) {
356
- return false ;
357
- }
358
-
359
- const layerId = Object . keys ( selectedLayers ) [ 0 ] ;
360
- const layer = model . getLayer ( layerId ) ;
361
-
362
- if ( ! layer ) {
363
- return false ;
364
- }
365
-
366
- const isValidLayer = [ 'VectorLayer' , 'ShapefileLayer' ] . includes (
367
- layer . type
368
- ) ;
369
-
370
- return isValidLayer ;
350
+ return [ 'VectorLayer' , 'ShapefileLayer' ] . includes ( selected . selectedLayer . type ) ;
371
351
} ,
372
352
execute : async ( ) => {
353
+ const selected = getSelectedLayer ( tracker ) ;
354
+ if ( ! selected ) {
355
+ console . error ( 'No valid selected layer.' ) ;
356
+ return ;
357
+ }
373
358
const layers = tracker . currentWidget ?. model . sharedModel . layers ?? { } ;
374
359
const sources = tracker . currentWidget ?. model . sharedModel . sources ?? { } ;
375
360
@@ -390,7 +375,7 @@ export function addCommands(
390
375
const dialog = new ProcessingFormDialog ( {
391
376
title : 'Buffer' ,
392
377
schema : schema ,
393
- model : tracker . currentWidget ?. model as IJupyterGISModel ,
378
+ model : model ,
394
379
sourceData : {
395
380
inputLayer : selectedLayerId ,
396
381
bufferDistance : 10 ,
@@ -422,30 +407,14 @@ export function addCommands(
422
407
const sourceId = inputLayer . parameters . source ;
423
408
const source = sources [ sourceId ] ;
424
409
425
- if ( ! source . parameters ) {
410
+ if ( ! source || ! source . parameters ) {
426
411
console . error ( `Source with ID ${ sourceId } not found or missing path.` ) ;
427
412
return ;
428
413
}
429
414
430
- let geojsonString : string ;
431
-
432
- if ( source . parameters . path ) {
433
- const fileContent = await loadFile ( {
434
- filepath : source . parameters . path ,
435
- type : source . type ,
436
- model : tracker . currentWidget ?. model as IJupyterGISModel
437
- } ) ;
438
-
439
- geojsonString =
440
- typeof fileContent === 'object'
441
- ? JSON . stringify ( fileContent )
442
- : fileContent ;
443
- } else if ( source . parameters . data ) {
444
- geojsonString = JSON . stringify ( source . parameters . data ) ;
445
- } else {
446
- throw new Error (
447
- `Source ${ sourceId } is missing both 'path' and 'data' parameters.`
448
- ) ;
415
+ const geojsonString = await getGeoJSONString ( source , model ) ;
416
+ if ( ! geojsonString ) {
417
+ return ;
449
418
}
450
419
451
420
const fileBlob = new Blob ( [ geojsonString ] , {
@@ -468,14 +437,10 @@ export function addCommands(
468
437
` ;
469
438
470
439
const options = [
471
- '-f' ,
472
- 'GeoJSON' ,
473
- '-t_srs' ,
474
- formValues . projection ,
475
- '-dialect' ,
476
- 'SQLITE' ,
477
- '-sql' ,
478
- sqlQuery ,
440
+ '-f' , 'GeoJSON' ,
441
+ '-t_srs' , formValues . projection ,
442
+ '-dialect' , 'SQLITE' ,
443
+ '-sql' , sqlQuery ,
479
444
'output.geojson'
480
445
] ;
481
446
@@ -496,18 +461,13 @@ export function addCommands(
496
461
497
462
const layerModel : IJGISLayer = {
498
463
type : 'VectorLayer' ,
499
- parameters : {
500
- source : newSourceId
501
- } ,
464
+ parameters : { source : newSourceId } ,
502
465
visible : true ,
503
466
name : inputLayer . name + ' Buffer'
504
467
} ;
505
468
506
- tracker . currentWidget ?. model . sharedModel . addSource (
507
- newSourceId ,
508
- sourceModel
509
- ) ;
510
- tracker . currentWidget ?. model . addLayer ( UUID . uuid4 ( ) , layerModel ) ;
469
+ model . sharedModel . addSource ( newSourceId , sourceModel ) ;
470
+ model . addLayer ( UUID . uuid4 ( ) , layerModel ) ;
511
471
}
512
472
}
513
473
} ) ;
0 commit comments