5000) $limit = 5000; // Build WHERE clause $where = "1=1"; if ($status) { $where .= " AND status = '" . $conn->real_escape_string($status) . "'"; } if ($tipe) { // Validate tipe value $valid_tipos = ['point', 'worship', 'poor_house', 'jalan', 'parsil']; if (in_array($tipe, $valid_tipos)) { $where .= " AND tipe = '" . $conn->real_escape_string($tipe) . "'"; } } // Count total $count_sql = "SELECT COUNT(*) as total FROM data_unified WHERE $where"; $count_result = $conn->query($count_sql); $count_row = $count_result->fetch_assoc(); $total = $count_row['total']; // Get data $sql = "SELECT * FROM data_unified WHERE $where LIMIT $limit OFFSET $offset"; $result = $conn->query($sql); if (!$result) { sendError('Query failed: ' . $conn->error, 500); } $features = []; while ($row = $result->fetch_assoc()) { if ($row['geom']) { // Parse existing GeoJSON $geom = json_decode($row['geom'], true); } else if ($row['latitude'] && $row['longitude']) { // Create Point geometry $geom = [ 'type' => 'Point', 'coordinates' => [floatval($row['longitude']), floatval($row['latitude'])] ]; } else { $geom = null; } if ($geom) { // Create properties $properties = [ 'id' => intval($row['id']), 'tipe' => $row['tipe'], 'nama' => $row['nama'], 'jenis' => $row['jenis'], 'alamat' => $row['alamat'], 'status' => $row['status'], 'created_at' => $row['created_at'], 'updated_at' => $row['updated_at'] ]; // Add conditional properties based on type if ($row['tipe'] === 'worship') { $properties['radius_meter'] = intval($row['radius_meter'] ?? 200); $properties['kapasitas'] = intval($row['kapasitas'] ?? 0); $properties['kegiatan'] = $row['kegiatan']; $properties['kontak'] = $row['kontak']; } if ($row['tipe'] === 'poor_house') { $properties['nama_kepala_keluarga'] = $row['nama_kepala_keluarga']; $properties['jumlah_anggota'] = intval($row['jumlah_anggota'] ?? 0); $properties['kondisi_rumah'] = $row['kondisi_rumah']; $properties['status_bantuan'] = $row['status_bantuan']; } if ($row['tipe'] === 'jalan') { $properties['panjang'] = floatval($row['panjang'] ?? 0); } if ($row['tipe'] === 'parsil') { $properties['luas'] = floatval($row['luas'] ?? 0); } $features[] = [ 'type' => 'Feature', 'geometry' => $geom, 'properties' => $properties ]; } } // Format response if ($format === 'geojson') { $response = [ 'type' => 'FeatureCollection', 'features' => $features, 'pagination' => [ 'total' => $total, 'count' => count($features), 'limit' => $limit, 'offset' => $offset ] ]; http_response_code(200); echo json_encode($response); } else if ($format === 'json') { sendResponse(true, 'Data retrieved successfully', [ 'total' => $total, 'features' => $features, 'pagination' => [ 'limit' => $limit, 'offset' => $offset ] ]); } else { sendError('Invalid format', 400); } } catch (Exception $e) { sendError('Server error: ' . $e->getMessage(), 500); } finally { $conn->close(); } ?>