'Failed to download GFS file', 'http_code' => $http_code]); exit; } } // --- Convert GRIB to XYZ using gdal_translate --- // We'll use band 1 for this example. You can use gdalinfo to see available bands. $gdal_command = 'gdal_translate -b 1 -of XYZ ' . escapeshellarg($grib_file) . ' ' . escapeshellarg($xyz_file); $gdal_output = shell_exec($gdal_command); if (!file_exists($xyz_file)) { echo json_encode(['error' => 'Failed to convert GRIB to XYZ', 'gdal_output' => $gdal_output]); unlink($grib_file); exit; } // --- Read the XYZ file and create GeoJSON features --- $features = []; $handle = fopen($xyz_file, 'r'); $line_count = 0; $sample_rate = 100; // Process 1 in every 100 lines if ($handle) { while (($line = fgets($handle)) !== false) { $line_count++; if ($line_count % $sample_rate !== 0) { continue; } $parts = preg_split('/\s+/', trim($line)); if (count($parts) === 3) { $lon = floatval($parts[0]); $lat = floatval($parts[1]); $value = floatval($parts[2]); // Skip points that are exactly zero, often represent no data if ($value === 0.0) { continue; } $features[] = [ 'type' => 'Feature', 'properties' => ['value' => $value], 'geometry' => [ 'type' => 'Point', 'coordinates' => [$lon, $lat] ] ]; } } fclose($handle); } // --- Clean up temporary files --- unlink($grib_file); unlink($xyz_file); // --- Output the GeoJSON --- echo json_encode([ 'type' => 'FeatureCollection', 'features' => $features ]); ?>