Files

82 lines
1.8 KiB
PHP

<?php
include 'koneksi.php';
// Baca file GeoJSON
$file = 'geojson/Jaringan_Jalan.json';
if (!file_exists($file)) {
die("❌ File tidak ditemukan!");
}
$json = file_get_contents($file);
$data = json_decode($json, true);
if (!$data || !isset($data['features'])) {
die("❌ Format GeoJSON tidak valid!");
}
// Prepare statement (AMAN)
$stmt = $conn->prepare("
INSERT INTO jalan (nama_jalan, status, panjang, geojson)
VALUES (?, ?, ?, ?)
");
$total = 0;
$success = 0;
$failed = 0;
foreach ($data['features'] as $feature) {
$total++;
// Ambil data dari properties
$nama = $feature['properties']['nama_jalan'] ?? 'Tanpa Nama';
$status = $feature['properties']['status_jal'] ?? 'Kabupaten';
$panjang = $feature['properties']['Plan_Len'] ?? 0;
// Validasi status (biar sesuai ENUM DB)
$allowed_status = ['Nasional', 'Provinsi', 'Kabupaten', 'Kota'];
if (!in_array($status, $allowed_status)) {
$status = 'Kabupaten';
}
// Ambil koordinat
if (!isset($feature['geometry']['coordinates'])) {
$failed++;
continue;
}
$coordinates = $feature['geometry']['coordinates'];
// Convert dari [lng, lat] → [lat, lng]
$geo = [];
foreach ($coordinates as $coord) {
if (count($coord) < 2) continue;
$geo[] = [$coord[1], $coord[0]];
}
if (count($geo) < 2) {
$failed++;
continue;
}
$geojson = json_encode($geo);
// Bind & execute
$stmt->bind_param("ssds", $nama, $status, $panjang, $geojson);
if ($stmt->execute()) {
$success++;
} else {
$failed++;
}
}
$stmt->close();
$conn->close();
// Output hasil
echo "<h2>✅ Import Selesai</h2>";
echo "Total data: $total <br>";
echo "Berhasil: $success <br>";
echo "Gagal: $failed <br>";
?>