68 lines
2.3 KiB
PHP
68 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Website;
|
|
|
|
use App\Exports\ParameterSubDistrictExport;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\SubDistrict;
|
|
use GuzzleHttp\Client;
|
|
use Illuminate\Http\Request;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
|
|
class ParameterSubDistrictController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
return view('website.app.parameter-sub-district');
|
|
}
|
|
|
|
public function getDataSubDistrict()
|
|
{
|
|
$allSubDistrict = SubDistrict::select('id', 'sub_district', 'latitude', 'longitude')->get();
|
|
|
|
return response()->json([
|
|
'allSubDistrict' => $allSubDistrict,
|
|
]);
|
|
}
|
|
|
|
public function excelParameterSubDistrict($latitude, $longitude, $year)
|
|
{
|
|
$subDistrict = SubDistrict::select('sub_district')
|
|
->where('latitude', $latitude)
|
|
->where('longitude', $longitude)
|
|
->first();
|
|
|
|
// dd($this->generateParameterSubDistrict($subDistrict['sub_district'], $latitude, $longitude));
|
|
|
|
$dataParameterSubDistrict = $this->generateParameterSubDistrict($subDistrict['sub_district'], $latitude, $longitude, $year);
|
|
|
|
return Excel::download(new ParameterSubDistrictExport($dataParameterSubDistrict), 'parameter-cuaca-' . $subDistrict['sub_district'] . '-tahun-' . $year . '.xlsx');
|
|
// return view('website.apps.export.excel-parameter-subdistrict');
|
|
}
|
|
|
|
public function generateParameterSubDistrict($subdistrict, $latitude, $longitude, $year)
|
|
{
|
|
$subDistrictParameter = [];
|
|
$client = new Client();
|
|
|
|
$url = "https://power.larc.nasa.gov/api/temporal/monthly/point?start={$year}&end={$year}&latitude={$latitude}&longitude={$longitude}&community=ag¶meters=prectotcorr,T2M,WS2M,RH2M,ALLSKY_SFC_SW_DWN&format=json&header=true&time-standard=lst";
|
|
|
|
try {
|
|
// Kirim permintaan GET ke URL
|
|
$response = $client->request('GET', $url);
|
|
|
|
$data = json_decode($response->getBody()->getContents(), true);
|
|
|
|
$subDistrictParameter[$subdistrict] = [
|
|
$year,
|
|
$data['properties']['parameter'],
|
|
];
|
|
} catch (\Exception $e) {
|
|
// Tangani kesalahan jika terjadi
|
|
echo "Error untuk latitude {$latitude} dan longitude {$longitude}: " . $e->getMessage() . "\n";
|
|
}
|
|
|
|
return $subDistrictParameter;
|
|
}
|
|
}
|