Initial commit
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
$host = 'localhost';
|
||||
$dbname = 'latihan_webgis';
|
||||
$username = 'root';
|
||||
$password = '';
|
||||
|
||||
try {
|
||||
$db = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $username, $password);
|
||||
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
|
||||
} catch (PDOException $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['status' => 'error', 'message' => 'Koneksi database gagal: ' . $e->getMessage()]);
|
||||
exit;
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
require __DIR__ . '/db.php';
|
||||
|
||||
header('Content-Type: application/json');
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
|
||||
header('Access-Control-Allow-Headers: Content-Type');
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { http_response_code(200); exit(); }
|
||||
|
||||
$action = $_GET['action'] ?? '';
|
||||
if ($action === '') {
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$action = $input['action'] ?? '';
|
||||
}
|
||||
|
||||
try {
|
||||
switch ($action) {
|
||||
case 'list':
|
||||
$rows = $db->query("SELECT * FROM jalan ORDER BY createdat DESC")->fetchAll();
|
||||
echo json_encode(['status' => 'success', 'data' => $rows]);
|
||||
break;
|
||||
|
||||
case 'create':
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$nama = trim($input['nama'] ?? '');
|
||||
$status = $input['status'] ?? 'kabupaten';
|
||||
$panjang = isset($input['panjang_meter']) ? (float)$input['panjang_meter'] : null;
|
||||
$geom = $input['geom'] ?? null;
|
||||
|
||||
if (!$nama) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Nama jalan wajib diisi.']);
|
||||
break;
|
||||
}
|
||||
if (!in_array($status, ['nasional','provinsi','kabupaten'])) $status = 'kabupaten';
|
||||
|
||||
// Validasi dan konversi geometri LineString
|
||||
$geojson = null;
|
||||
if (is_array($geom)) {
|
||||
if (isset($geom['type']) && $geom['type'] === 'LineString' && isset($geom['coordinates'])) {
|
||||
$geojson = json_encode($geom);
|
||||
} elseif (isset($geom[0]) && is_array($geom[0])) {
|
||||
// array of points [[lng,lat],...]
|
||||
$geojson = json_encode([
|
||||
'type' => 'LineString',
|
||||
'coordinates' => $geom
|
||||
]);
|
||||
}
|
||||
} elseif (is_string($geom)) {
|
||||
$decoded = json_decode($geom, true);
|
||||
if ($decoded && isset($decoded['type']) && $decoded['type'] === 'LineString') {
|
||||
$geojson = $geom;
|
||||
} else {
|
||||
echo json_encode(['status'=>'error','message'=>'Format geometri tidak valid.']);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$geojson) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Geometri tidak valid. Harus berupa GeoJSON LineString atau array koordinat.']);
|
||||
break;
|
||||
}
|
||||
|
||||
$stmt = $db->prepare("INSERT INTO jalan (nama, status, panjang_meter, geom) VALUES (?, ?, ?, ?)");
|
||||
$stmt->execute([$nama, $status, $panjang, $geojson]);
|
||||
echo json_encode(['status'=>'success','message'=>'Data jalan berhasil disimpan!','id'=>$db->lastInsertId()]);
|
||||
break;
|
||||
|
||||
case 'update':
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$id = (int)($input['id'] ?? 0);
|
||||
$nama = trim($input['nama'] ?? '');
|
||||
$status = $input['status'] ?? 'kabupaten';
|
||||
|
||||
if (!$id) { echo json_encode(['status'=>'error','message'=>'ID tidak valid.']); break; }
|
||||
if (!$nama) { echo json_encode(['status'=>'error','message'=>'Nama jalan wajib diisi.']); break; }
|
||||
if (!in_array($status, ['nasional','provinsi','kabupaten'])) $status = 'kabupaten';
|
||||
|
||||
$stmt = $db->prepare("UPDATE jalan SET nama=?, status=?, updatedat=NOW() WHERE id=?");
|
||||
$stmt->execute([$nama, $status, $id]);
|
||||
echo json_encode(['status'=>'success','message'=>'Data jalan berhasil diperbarui.']);
|
||||
break;
|
||||
|
||||
case 'delete':
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$id = (int)($input['id'] ?? 0);
|
||||
if (!$id) { echo json_encode(['status'=>'error','message'=>'ID tidak valid.']); break; }
|
||||
$stmt = $db->prepare("DELETE FROM jalan WHERE id=?");
|
||||
$stmt->execute([$id]);
|
||||
echo json_encode(['status'=>'success','message'=>'Data jalan berhasil dihapus.']);
|
||||
break;
|
||||
|
||||
default:
|
||||
echo json_encode(['status'=>'error','message'=>'Aksi tidak dikenali.']);
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
echo json_encode(['status'=>'error','message'=>'Database error: '.$e->getMessage()]);
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
require __DIR__ . '/db.php';
|
||||
|
||||
header('Content-Type: application/json');
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
|
||||
header('Access-Control-Allow-Headers: Content-Type');
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { http_response_code(200); exit(); }
|
||||
|
||||
$action = $_GET['action'] ?? '';
|
||||
if ($action === '') {
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$action = $input['action'] ?? '';
|
||||
}
|
||||
|
||||
try {
|
||||
switch ($action) {
|
||||
case 'list':
|
||||
$rows = $db->query("SELECT * FROM parsil ORDER BY createdat DESC")->fetchAll();
|
||||
echo json_encode(['status' => 'success', 'data' => $rows]);
|
||||
break;
|
||||
|
||||
case 'create':
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$nama = trim($input['nama'] ?? '');
|
||||
$status = $input['status_kepemilikan'] ?? 'SHM';
|
||||
$luas = isset($input['luas_m2']) ? (float)$input['luas_m2'] : null;
|
||||
$geom = $input['geom'] ?? null;
|
||||
|
||||
if (!$nama) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Nama parsil wajib diisi.']);
|
||||
break;
|
||||
}
|
||||
$validStatus = ['SHM','HGB','HGU','HP'];
|
||||
if (!in_array($status, $validStatus)) $status = 'SHM';
|
||||
|
||||
// Validasi dan konversi geometri Polygon
|
||||
$geojson = null;
|
||||
if (is_array($geom)) {
|
||||
if (isset($geom['type']) && $geom['type'] === 'Polygon' && isset($geom['coordinates'])) {
|
||||
$geojson = json_encode($geom);
|
||||
} elseif (isset($geom[0]) && is_array($geom[0])) {
|
||||
// array of rings [[lng,lat],...]
|
||||
$geojson = json_encode([
|
||||
'type' => 'Polygon',
|
||||
'coordinates' => $geom
|
||||
]);
|
||||
}
|
||||
} elseif (is_string($geom)) {
|
||||
$decoded = json_decode($geom, true);
|
||||
if ($decoded && isset($decoded['type']) && $decoded['type'] === 'Polygon') {
|
||||
$geojson = $geom;
|
||||
} else {
|
||||
echo json_encode(['status'=>'error','message'=>'Format geometri tidak valid.']);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$geojson) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Geometri tidak valid. Harus berupa GeoJSON Polygon atau array koordinat.']);
|
||||
break;
|
||||
}
|
||||
|
||||
$stmt = $db->prepare("INSERT INTO parsil (nama, status_kepemilikan, luas_m2, geom) VALUES (?, ?, ?, ?)");
|
||||
$stmt->execute([$nama, $status, $luas, $geojson]);
|
||||
echo json_encode(['status'=>'success','message'=>'Data parsil berhasil disimpan!','id'=>$db->lastInsertId()]);
|
||||
break;
|
||||
|
||||
case 'update':
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$id = (int)($input['id'] ?? 0);
|
||||
$nama = trim($input['nama'] ?? '');
|
||||
$status = $input['status_kepemilikan'] ?? 'SHM';
|
||||
$geom = $input['geom'] ?? null;
|
||||
$luas = isset($input['luas_m2']) ? (float)$input['luas_m2'] : null;
|
||||
|
||||
if (!$id) { echo json_encode(['status'=>'error','message'=>'ID tidak valid.']); break; }
|
||||
if (!$nama) { echo json_encode(['status'=>'error','message'=>'Nama parsil wajib diisi.']); break; }
|
||||
$validStatus = ['SHM','HGB','HGU','HP'];
|
||||
if (!in_array($status, $validStatus)) $status = 'SHM';
|
||||
|
||||
// If geometry provided, validate/convert to GeoJSON string
|
||||
$geojson = null;
|
||||
if ($geom) {
|
||||
if (is_array($geom)) {
|
||||
if (isset($geom['type']) && $geom['type'] === 'Polygon' && isset($geom['coordinates'])) {
|
||||
$geojson = json_encode($geom);
|
||||
} elseif (isset($geom[0]) && is_array($geom[0])) {
|
||||
$geojson = json_encode(['type'=>'Polygon','coordinates'=>$geom]);
|
||||
}
|
||||
} elseif (is_string($geom)) {
|
||||
$decoded = json_decode($geom, true);
|
||||
if ($decoded && isset($decoded['type']) && $decoded['type'] === 'Polygon') $geojson = $geom;
|
||||
}
|
||||
if (!$geojson) { echo json_encode(['status'=>'error','message'=>'Format geometri tidak valid untuk update.']); break; }
|
||||
}
|
||||
|
||||
// Build update query dynamically
|
||||
$fields = ['nama = ?', 'status_kepemilikan = ?', 'updatedat = NOW()'];
|
||||
$params = [$nama, $status];
|
||||
if ($luas !== null) { $fields[] = 'luas_m2 = ?'; $params[] = $luas; }
|
||||
if ($geojson !== null) { $fields[] = 'geom = ?'; $params[] = $geojson; }
|
||||
$params[] = $id;
|
||||
|
||||
$sql = "UPDATE parsil SET " . implode(', ', $fields) . " WHERE id=?";
|
||||
$stmt = $db->prepare($sql);
|
||||
$stmt->execute($params);
|
||||
echo json_encode(['status'=>'success','message'=>'Data parsil berhasil diperbarui.']);
|
||||
break;
|
||||
|
||||
case 'delete':
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$id = (int)($input['id'] ?? 0);
|
||||
if (!$id) { echo json_encode(['status'=>'error','message'=>'ID tidak valid.']); break; }
|
||||
$stmt = $db->prepare("DELETE FROM parsil WHERE id=?");
|
||||
$stmt->execute([$id]);
|
||||
echo json_encode(['status'=>'success','message'=>'Data parsil berhasil dihapus.']);
|
||||
break;
|
||||
|
||||
default:
|
||||
echo json_encode(['status'=>'error','message'=>'Aksi tidak dikenali.']);
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
echo json_encode(['status'=>'error','message'=>'Database error: '.$e->getMessage()]);
|
||||
}
|
||||
+141
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
require __DIR__ . '/db.php';
|
||||
|
||||
header('Content-Type: application/json');
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
|
||||
header('Access-Control-Allow-Headers: Content-Type');
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { http_response_code(200); exit(); }
|
||||
|
||||
$action = $_GET['action'] ?? '';
|
||||
if ($action === '') {
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$action = $input['action'] ?? '';
|
||||
}
|
||||
|
||||
try {
|
||||
switch ($action) {
|
||||
case 'list':
|
||||
$rows = $db->query("SELECT id, namaspbu, nomorwa, statusbuka,
|
||||
JSON_EXTRACT(koordinat, '$.coordinates[1]') as lat,
|
||||
JSON_EXTRACT(koordinat, '$.coordinates[0]') as lng,
|
||||
koordinat
|
||||
FROM spbu ORDER BY createdat DESC")->fetchAll();
|
||||
foreach ($rows as &$r) {
|
||||
$r['lat'] = (float)$r['lat'];
|
||||
$r['lng'] = (float)$r['lng'];
|
||||
}
|
||||
echo json_encode(['status' => 'success', 'data' => $rows]);
|
||||
break;
|
||||
|
||||
case 'create':
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$namaspbu = trim($input['namaspbu'] ?? '');
|
||||
$nomorwa = trim($input['nomorwa'] ?? '');
|
||||
$statusbuka = $input['statusbuka'] ?? 'limited';
|
||||
$koordinat = $input['koordinat'] ?? null;
|
||||
|
||||
if (!$namaspbu) {
|
||||
echo json_encode(['status'=>'error','message'=>'Nama SPBU wajib diisi.']);
|
||||
break;
|
||||
}
|
||||
if (!in_array($statusbuka, ['full','limited'])) $statusbuka = 'limited';
|
||||
|
||||
// Validasi dan konversi geometri
|
||||
$geojson = null;
|
||||
if (is_array($koordinat)) {
|
||||
// Cek apakah sudah dalam format GeoJSON Point
|
||||
if (isset($koordinat['type']) && $koordinat['type'] === 'Point' && isset($koordinat['coordinates'])) {
|
||||
$geojson = json_encode($koordinat);
|
||||
}
|
||||
// Mungkin hanya array [lng, lat]
|
||||
elseif (isset($koordinat[0]) && isset($koordinat[1])) {
|
||||
$geojson = json_encode([
|
||||
'type' => 'Point',
|
||||
'coordinates' => [(float)$koordinat[0], (float)$koordinat[1]]
|
||||
]);
|
||||
}
|
||||
// Mungkin objek dengan lat,lng
|
||||
elseif (isset($koordinat['lat']) && isset($koordinat['lng'])) {
|
||||
$geojson = json_encode([
|
||||
'type' => 'Point',
|
||||
'coordinates' => [(float)$koordinat['lng'], (float)$koordinat['lat']]
|
||||
]);
|
||||
}
|
||||
} elseif (is_string($koordinat)) {
|
||||
// Coba parse JSON
|
||||
$decoded = json_decode($koordinat, true);
|
||||
if ($decoded && isset($decoded['type']) && $decoded['type'] === 'Point') {
|
||||
$geojson = $koordinat;
|
||||
} else {
|
||||
echo json_encode(['status'=>'error','message'=>'Format koordinat tidak valid.']);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$geojson) {
|
||||
echo json_encode(['status'=>'error','message'=>'Geometri tidak valid. Harus berupa GeoJSON Point atau koordinat [lng, lat].']);
|
||||
break;
|
||||
}
|
||||
|
||||
$stmt = $db->prepare("INSERT INTO spbu (namaspbu, nomorwa, statusbuka, koordinat) VALUES (?, ?, ?, ?)");
|
||||
$stmt->execute([$namaspbu, $nomorwa, $statusbuka, $geojson]);
|
||||
echo json_encode(['status'=>'success','message'=>'Data berhasil disimpan!','id'=>$db->lastInsertId()]);
|
||||
break;
|
||||
|
||||
case 'update':
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$id = (int)($input['id'] ?? 0);
|
||||
$namaspbu = trim($input['namaspbu'] ?? '');
|
||||
$nomorwa = trim($input['nomorwa'] ?? '');
|
||||
$statusbuka = $input['statusbuka'] ?? 'limited';
|
||||
$koordinat = $input['koordinat'] ?? null;
|
||||
|
||||
if (!$id) {
|
||||
echo json_encode(['status'=>'error','message'=>'ID tidak valid.']);
|
||||
break;
|
||||
}
|
||||
|
||||
// ===============================
|
||||
// JIKA ADA KOORDINAT → UPDATE KOORDINAT
|
||||
// ===============================
|
||||
if ($koordinat) {
|
||||
$geojson = json_encode($koordinat);
|
||||
|
||||
$stmt = $db->prepare("UPDATE spbu SET koordinat=?, updatedat=NOW() WHERE id=?");
|
||||
$stmt->execute([$geojson, $id]);
|
||||
|
||||
echo json_encode(['status'=>'success','message'=>'Koordinat berhasil diperbarui!']);
|
||||
break;
|
||||
}
|
||||
|
||||
// ===============================
|
||||
// UPDATE DATA BIASA
|
||||
// ===============================
|
||||
if (!$namaspbu) {
|
||||
echo json_encode(['status'=>'error','message'=>'Nama SPBU wajib diisi.']);
|
||||
break;
|
||||
}
|
||||
|
||||
$stmt = $db->prepare("UPDATE spbu SET namaspbu=?, nomorwa=?, statusbuka=?, updatedat=NOW() WHERE id=?");
|
||||
$stmt->execute([$namaspbu, $nomorwa, $statusbuka, $id]);
|
||||
|
||||
echo json_encode(['status'=>'success','message'=>'Data berhasil diperbarui!']);
|
||||
break;
|
||||
|
||||
case 'delete':
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$id = (int)($input['id'] ?? 0);
|
||||
if (!$id) { echo json_encode(['status'=>'error','message'=>'ID tidak valid.']); break; }
|
||||
$stmt = $db->prepare("DELETE FROM spbu WHERE id=?");
|
||||
$stmt->execute([$id]);
|
||||
echo json_encode(['status'=>'success','message'=>'Data berhasil dihapus.']);
|
||||
break;
|
||||
|
||||
default:
|
||||
echo json_encode(['status'=>'error','message'=>'Aksi tidak dikenali.']);
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
echo json_encode(['status'=>'error','message'=>'Database error: '.$e->getMessage()]);
|
||||
}
|
||||
Reference in New Issue
Block a user