Files
d1041231029-sig-b/api/seed_pontianak_data.php
T
2026-06-05 13:02:20 +00:00

137 lines
5.2 KiB
PHP

<?php
header('Content-Type: application/json');
require_once 'config.php';
try {
$db = getDB();
// Check if force parameter is set
$force = isset($_POST['force']) || isset($_GET['force']);
// Pontianak administrative data (dummy but more realistic non-rectangular polygons)
// GeoJSON coordinate order is [longitude, latitude].
$pontianak_data = [
[
'name' => 'Pontianak',
'area_type' => 'city',
'population' => 690000,
'geometry' => '{"type":"Polygon","coordinates":[[[109.320000,-0.005000],[109.330000,-0.004000],[109.340000,-0.005500],[109.345000,-0.008000],[109.348000,-0.012000],[109.350000,-0.016000],[109.348000,-0.020000],[109.340000,-0.023000],[109.330000,-0.024000],[109.320000,-0.023500],[109.310000,-0.022000],[109.305000,-0.019000],[109.303000,-0.016000],[109.302000,-0.012000],[109.305000,-0.008000],[109.310000,-0.006000],[109.318000,-0.004500],[109.325000,-0.003500],[109.329000,-0.004000],[109.320000,-0.005000]]]}'
],
[
'name' => 'Pontianak Barat',
'area_type' => 'kecamatan',
'population' => 112000,
'geometry' => '{"type":"Polygon","coordinates":[[[109.305000,-0.010000],[109.315000,-0.009500],[109.318000,-0.013000],[109.315000,-0.015000],[109.305000,-0.016000],[109.302000,-0.013000],[109.305000,-0.010000]]]}'
],
[
'name' => 'Pontianak Utara',
'area_type' => 'kecamatan',
'population' => 126000,
'geometry' => '{"type":"Polygon","coordinates":[[[109.318000,-0.005000],[109.327000,-0.004500],[109.330000,-0.007000],[109.328000,-0.009500],[109.320000,-0.010000],[109.315000,-0.007500],[109.318000,-0.005000]]]}'
],
[
'name' => 'Pontianak Kota',
'area_type' => 'kecamatan',
'population' => 148000,
'geometry' => '{"type":"Polygon","coordinates":[[[109.320000,-0.010000],[109.327000,-0.009000],[109.330000,-0.011000],[109.328000,-0.015000],[109.320000,-0.015500],[109.315000,-0.013000],[109.320000,-0.010000]]]}'
],
[
'name' => 'Pontianak Timur',
'area_type' => 'kecamatan',
'population' => 94000,
'geometry' => '{"type":"Polygon","coordinates":[[[109.328000,-0.009500],[109.340000,-0.010000],[109.345000,-0.012500],[109.338000,-0.016000],[109.328000,-0.015000],[109.328000,-0.009500]]]}'
],
[
'name' => 'Pontianak Selatan',
'area_type' => 'kecamatan',
'population' => 132000,
'geometry' => '{"type":"Polygon","coordinates":[[[109.315000,-0.015000],[109.328000,-0.014500],[109.330000,-0.018000],[109.318000,-0.021000],[109.308000,-0.020000],[109.315000,-0.015000]]]}'
],
[
'name' => 'Pontianak Tenggara',
'area_type' => 'kecamatan',
'population' => 91000,
'geometry' => '{"type":"Polygon","coordinates":[[[109.328000,-0.015000],[109.338000,-0.016000],[109.340000,-0.019000],[109.330000,-0.021000],[109.320000,-0.020000],[109.328000,-0.015000]]]}'
]
];
// Check if data already exists
$stmt = $db->prepare('SELECT COUNT(*) as count FROM pontianak_areas');
$stmt->execute();
$count = $stmt->fetch(PDO::FETCH_ASSOC)['count'];
if ($count > 0 && !$force) {
http_response_code(200);
echo json_encode([
'success' => true,
'message' => 'Pontianak data already exists. Use force=true to reseed.'
]);
exit;
}
// Delete existing data if force is true
if ($force && $count > 0) {
$db->exec('DELETE FROM area_statistics');
$db->exec('DELETE FROM pontianak_areas');
}
// Insert Pontianak areas
$stmt = $db->prepare('
INSERT IGNORE INTO pontianak_areas (name, area_type, geometry, population)
VALUES (?, ?, ?, ?)
');
$today = date('Y-m-d');
$stats_stmt = $db->prepare('
INSERT IGNORE INTO area_statistics (pontianak_area_id, statistic_type, value, data_date)
VALUES (?, ?, ?, ?)
');
$inserted = 0;
foreach ($pontianak_data as $data) {
$stmt->execute([
$data['name'],
$data['area_type'],
$data['geometry'],
$data['population']
]);
$inserted++;
}
// Insert initial area statistics
$areas_stmt = $db->prepare('SELECT id, population FROM pontianak_areas');
$areas_stmt->execute();
$areas = $areas_stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($areas as $area) {
// Insert population density statistic
$stats_stmt->execute([
$area['id'],
'population_density',
(float)$area['population'],
$today
]);
}
http_response_code(200);
echo json_encode([
'success' => true,
'message' => 'Pontianak data seeded successfully',
'areas_inserted' => $inserted
]);
} catch (PDOException $e) {
http_response_code(500);
echo json_encode([
'success' => false,
'error' => 'Database error: ' . $e->getMessage()
]);
} catch (Exception $e) {
http_response_code(400);
echo json_encode([
'success' => false,
'error' => $e->getMessage()
]);
}
?>