@@ -83,8 +83,9 @@ pub fn fetch_elevation_data(
8383 let tiles: Vec < ( u32 , u32 ) > = get_tile_coordinates ( bbox, zoom) ;
8484
8585 // Match grid dimensions with Minecraft world size
86- let grid_width: usize = scale_factor_x as usize ;
87- let grid_height: usize = scale_factor_z as usize ;
86+ // Ensure minimum grid size of 1 to prevent division by zero and indexing errors
87+ let grid_width: usize = ( scale_factor_x as usize ) . max ( 1 ) ;
88+ let grid_height: usize = ( scale_factor_z as usize ) . max ( 1 ) ;
8889
8990 // Initialize height grid with proper dimensions
9091 let mut height_grid: Vec < Vec < f64 > > = vec ! [ vec![ f64 :: NAN ; grid_width] ; grid_height] ;
@@ -376,6 +377,11 @@ fn get_tile_coordinates(bbox: &LLBBox, zoom: u8) -> Vec<(u32, u32)> {
376377}
377378
378379fn apply_gaussian_blur ( heights : & [ Vec < f64 > ] , sigma : f64 ) -> Vec < Vec < f64 > > {
380+ // Guard against empty input
381+ if heights. is_empty ( ) || heights[ 0 ] . is_empty ( ) {
382+ return heights. to_owned ( ) ;
383+ }
384+
379385 let kernel_size: usize = ( sigma * 3.0 ) . ceil ( ) as usize * 2 + 1 ;
380386 let kernel: Vec < f64 > = create_gaussian_kernel ( kernel_size, sigma) ;
381387
@@ -445,6 +451,11 @@ fn create_gaussian_kernel(size: usize, sigma: f64) -> Vec<f64> {
445451}
446452
447453fn fill_nan_values ( height_grid : & mut [ Vec < f64 > ] ) {
454+ // Guard against empty grid
455+ if height_grid. is_empty ( ) || height_grid[ 0 ] . is_empty ( ) {
456+ return ;
457+ }
458+
448459 let height: usize = height_grid. len ( ) ;
449460 let width: usize = height_grid[ 0 ] . len ( ) ;
450461
@@ -485,6 +496,11 @@ fn fill_nan_values(height_grid: &mut [Vec<f64>]) {
485496}
486497
487498fn filter_elevation_outliers ( height_grid : & mut [ Vec < f64 > ] ) {
499+ // Guard against empty grid
500+ if height_grid. is_empty ( ) || height_grid[ 0 ] . is_empty ( ) {
501+ return ;
502+ }
503+
488504 let height = height_grid. len ( ) ;
489505 let width = height_grid[ 0 ] . len ( ) ;
490506
0 commit comments