120 lines
4.3 KiB
PHP
120 lines
4.3 KiB
PHP
<?php
|
|
// geojson.php — Baca file JSON & konversi koordinat UTM Zone 49S → WGS84
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
header('Access-Control-Allow-Origin: *');
|
|
error_reporting(0);
|
|
|
|
// ── Hanya file ini yang diizinkan ──
|
|
$allowed = ['Populasi_Kecamatan.json'];
|
|
|
|
$file = $_GET['file'] ?? '';
|
|
if (!in_array($file, $allowed)) {
|
|
http_response_code(400);
|
|
echo json_encode(['error' => 'File tidak diizinkan: ' . $file]);
|
|
exit;
|
|
}
|
|
|
|
$path = __DIR__ . '/' . $file;
|
|
if (!file_exists($path)) {
|
|
http_response_code(404);
|
|
echo json_encode(['error' => 'File tidak ditemukan: ' . $file]);
|
|
exit;
|
|
}
|
|
|
|
ini_set('max_execution_time', 120);
|
|
ini_set('memory_limit', '256M');
|
|
|
|
$raw = file_get_contents($path);
|
|
$gj = json_decode($raw, true);
|
|
if (!$gj) {
|
|
http_response_code(500);
|
|
echo json_encode(['error' => 'JSON tidak valid atau file rusak']);
|
|
exit;
|
|
}
|
|
|
|
// ─────────────────────────────────────────────────────
|
|
// KONVERSI UTM Zone 49S → WGS84 (lat/lng)
|
|
// Dipakai untuk koordinat Pontianak (Northing ~9.99 juta)
|
|
// ─────────────────────────────────────────────────────
|
|
function utmToLatLng($easting, $northing, $zone = 49, $isSouth = true) {
|
|
$a = 6378137.0;
|
|
$e2 = 0.00669437999014;
|
|
$k0 = 0.9996;
|
|
|
|
$x = $easting - 500000.0;
|
|
$y = $isSouth ? $northing - 10000000.0 : $northing;
|
|
|
|
$lon0 = deg2rad(($zone - 1) * 6 - 180 + 3);
|
|
$e1 = (1 - sqrt(1 - $e2)) / (1 + sqrt(1 - $e2));
|
|
$M = $y / $k0;
|
|
$mu = $M / ($a * (1 - $e2/4 - 3*$e2*$e2/64 - 5*$e2*$e2*$e2/256));
|
|
|
|
$phi1 = $mu
|
|
+ (3*$e1/2 - 27*pow($e1,3)/32) * sin(2*$mu)
|
|
+ (21*$e1*$e1/16 - 55*pow($e1,4)/32) * sin(4*$mu)
|
|
+ (151*pow($e1,3)/96) * sin(6*$mu)
|
|
+ (1097*pow($e1,4)/512) * sin(8*$mu);
|
|
|
|
$N1 = $a / sqrt(1 - $e2 * pow(sin($phi1), 2));
|
|
$T1 = pow(tan($phi1), 2);
|
|
$C1 = $e2 / (1 - $e2) * pow(cos($phi1), 2);
|
|
$R1 = $a * (1 - $e2) / pow(1 - $e2 * pow(sin($phi1), 2), 1.5);
|
|
$D = $x / ($N1 * $k0);
|
|
|
|
$lat = $phi1 - ($N1 * tan($phi1) / $R1) * (
|
|
$D*$D/2
|
|
- (5 + 3*$T1 + 10*$C1 - 4*$C1*$C1 - 9*$e2/(1-$e2)) * pow($D,4)/24
|
|
+ (61 + 90*$T1 + 298*$C1 + 45*$T1*$T1 - 252*$e2/(1-$e2) - 3*$C1*$C1) * pow($D,6)/720
|
|
);
|
|
|
|
$lon = $lon0 + (
|
|
$D
|
|
- (1 + 2*$T1 + $C1) * pow($D,3)/6
|
|
+ (5 - 2*$C1 + 28*$T1 - 3*$C1*$C1 + 8*$e2/(1-$e2) + 24*$T1*$T1) * pow($D,5)/120
|
|
) / cos($phi1);
|
|
|
|
return [round(rad2deg($lon), 8), round(rad2deg($lat), 8)];
|
|
}
|
|
|
|
// ─────────────────────────────────────────────────────
|
|
// Deteksi apakah koordinat perlu dikonversi
|
|
// Koordinat UTM Pontianak: Easting ~300000-400000, Northing ~9900000+
|
|
// Koordinat WGS84: lng ~109, lat ~-0.1
|
|
// ─────────────────────────────────────────────────────
|
|
function isUTM($x, $y) {
|
|
return (abs($x) > 1000 || abs($y) > 1000);
|
|
}
|
|
|
|
function convertCoords($coords) {
|
|
if (!is_array($coords)) return $coords;
|
|
|
|
// Koordinat tunggal [x, y]
|
|
if (count($coords) >= 2 && is_numeric($coords[0]) && is_numeric($coords[1])) {
|
|
$x = (float)$coords[0];
|
|
$y = (float)$coords[1];
|
|
if (isUTM($x, $y)) {
|
|
return utmToLatLng($x, $y);
|
|
}
|
|
return [$x, $y]; // Sudah WGS84, biarkan
|
|
}
|
|
|
|
// Array of array → rekursif
|
|
$result = [];
|
|
foreach ($coords as $item) {
|
|
if (!is_array($item)) continue;
|
|
$result[] = convertCoords($item);
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
// Proses semua fitur
|
|
if (isset($gj['features']) && is_array($gj['features'])) {
|
|
foreach ($gj['features'] as &$feat) {
|
|
if (isset($feat['geometry']['coordinates']) && is_array($feat['geometry']['coordinates'])) {
|
|
$feat['geometry']['coordinates'] = convertCoords($feat['geometry']['coordinates']);
|
|
}
|
|
}
|
|
unset($feat);
|
|
}
|
|
|
|
echo json_encode($gj); |