Compare commits

...
4 Commits
Author SHA1 Message Date
dredgy 947f4839ce Urgent add carto API key 2026-08-27 21:49:46 +10:00
dredgy f31f1bc2fc Add airport search filter 2026-08-22 15:00:55 +10:00
dredgy 91a6873b64 Merge pull request 'Add historic airports to database' (#20) from add-historic-airports into main
Reviewed-on: #20
2026-08-22 14:14:05 +10:00
dredgy 4a9b1ad56a Add historic airports to database 2026-08-22 14:13:37 +10:00
9 changed files with 233 additions and 17 deletions
@@ -17,7 +17,10 @@ class PopulateAirportTimezones extends Command
*/
public function handle()
{
Airport::whereNull('timezone')->chunkById(100, function ($airports) {
Airport::where(function ($query) {
$query->whereNull('timezone')
->orWhere('timezone', 'Undefined');
})->chunkById(100, function ($airports) {
foreach ($airports as $airport) {
$zoneName = null;
$attempts = 0;
+26 -4
View File
@@ -58,28 +58,50 @@ class SearchController extends Controller
{
$q = request('q', '');
$len = strlen($q);
$showClosed = request()->boolean('show_closed');
$showNoCode = request()->boolean('show_no_code');
if ($len < 3) return [];
return Airport::with('region.country')
->when(!$showClosed, fn($query) => $query->where('active', true))
->when(!$showNoCode, fn($query) => $query->where(function ($sub) {
$sub->whereNotNull('iata_code')
->orWhereNotNull('icao_code');
}))
->when($len === 3, fn($query) => $query->where('iata_code', 'ilike', $q))
->when($len >= 4, fn($query) => $query->where(function ($sub) use ($q, $len) {
$sub->when($len === 4, fn($s) => $s->where('icao_code', 'ilike', $q))
->orWhere('name', 'ilike', "%{$q}%")
->orWhere('municipality', 'ilike', "%{$q}%");
})->orderByRaw("
}))
->orderByRaw("
CASE
WHEN active THEN 0
ELSE 1
END
")
->orderByRaw("
CASE
WHEN iata_code IS NOT NULL AND icao_code IS NOT NULL THEN 0
WHEN iata_code IS NOT NULL OR icao_code IS NOT NULL THEN 1
ELSE 2
END
")
->orderByRaw("
CASE
WHEN icao_code = ? THEN 0
WHEN iata_code = ? THEN 1
ELSE 2
END
", [$q, $q]))
", [$q, $q])
->limit(15)
->get(['id', 'name', 'municipality', 'iata_code', 'icao_code', 'region_id'])
->map(fn($airport) => [
->get(['id', 'name', 'municipality', 'iata_code', 'icao_code', 'region_id', 'active'])
->map(fn(Airport $airport) => [
'value' => $airport->id,
'title' => $airport->display_name,
'country_code' => strtolower($airport->region->country->code),
'active' => $airport->active,
])
->values();
}
@@ -58,6 +58,7 @@ class HandleInertiaRequests extends Middleware
->whereNull('expires_at')
->orWhere('expires_at', '>', now())
->count(),
'carto_api_key' => config('services.carto.key'),
];
}
}
+9
View File
@@ -20,12 +20,14 @@ class Airport extends Model
'icao_code',
'iata_code',
'local_code',
'active',
];
protected $casts = [
'latitude_deg' => 'float',
'longitude_deg' => 'float',
'elevation_ft' => 'integer',
'active' => 'boolean',
];
protected $appends = [
@@ -37,6 +39,8 @@ class Airport extends Model
'LHR', 'LGW', 'STN', 'LTN', 'LCY', 'SEN',
];
protected function displayName() : Attribute{
return Attribute::make(
get: function () {
@@ -53,6 +57,11 @@ class Airport extends Model
);
}
public function scopeActive($query)
{
return $query->where('active', true);
}
public function region(): BelongsTo
{
return $this->belongsTo(Region::class);
+4
View File
@@ -18,6 +18,10 @@ return [
'key' => env('POSTMARK_API_KEY'),
],
'carto' => [
'key' => env('CARTO_API_KEY'),
],
'resend' => [
'key' => env('RESEND_API_KEY'),
],
@@ -0,0 +1,133 @@
<?php
use App\Models\Airport;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('airports', function (Blueprint $table) {
$table->boolean('active')->default(true);
});
Airport::whereIataCode('TXL')->update(['active' => false]);
$this->importCsv();
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('airports', function (Blueprint $table) {
$table->dropColumn('active');
});
}
private function importCsv(): void
{
$path = storage_path('app/private/seed_data/airports.csv');
if (! file_exists($path)) {
throw new \RuntimeException("Airports CSV not found at: {$path}");
}
$handle = fopen($path, 'rb');
if ($handle === false) {
throw new \RuntimeException("Failed to open airports CSV at: {$path}");
}
// Skip header row
fgetcsv($handle);
$regionMap = DB::table('regions')->pluck('id', 'code')->all();
$batch = [];
$batchSize = 500;
$now = now()->toDateTimeString();
while (($row = fgetcsv($handle)) !== false) {
if (count($row) < 19) {
continue;
}
// id, ident, type, name, latitude_deg, longitude_deg, elevation_ft,
// continent, iso_country, iso_region, municipality, scheduled_service,
// icao_code, iata_code, gps_code, local_code, home_link, wikipedia_link, keywords
[
,
,
$type,
$name,
$latitudeDeg,
$longitudeDeg,
$elevationFt,
,
,
$isoRegion,
$municipality,
,
$icaoCode,
$iataCode,
,
$localCode,
] = $row;
$icaoCode = trim(str_replace(["\r", "\n"], '', $icaoCode));
$iataCode = trim(str_replace(["\r", "\n"], '', $iataCode));
// Only importing airports with no IATA/ICAO code — coded airports are already in the DB
if ($icaoCode !== '' || $iataCode !== '') {
continue;
}
$type = trim($type);
$name = trim($name);
$localCode = trim($localCode);
$isoRegion = trim($isoRegion);
if (! isset($regionMap[$isoRegion])) {
continue;
}
$elevationFt = trim($elevationFt);
$batch[] = [
'type' => $type,
'name' => $name,
'latitude_deg' => (float) trim($latitudeDeg),
'longitude_deg' => (float) trim($longitudeDeg),
'elevation_ft' => $elevationFt !== '' ? (int) $elevationFt : null,
'region_id' => $regionMap[$isoRegion],
'municipality' => trim($municipality) !== '' ? trim($municipality) : null,
'icao_code' => null,
'iata_code' => null,
'local_code' => $localCode !== '' ? $localCode : null,
'timezone' => 'Undefined',
'active' => $type !== 'closed',
'created_at' => $now,
'updated_at' => $now,
];
echo "Added: {$name}\n";
if (count($batch) >= $batchSize) {
DB::table('airports')->insert($batch);
$batch = [];
}
}
fclose($handle);
if (! empty($batch)) {
DB::table('airports')->insert($batch);
}
}
};
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { ref, nextTick } from 'vue'
import { ref, nextTick, watch } from 'vue'
import axios from 'axios'
const props = defineProps<{
@@ -12,8 +12,14 @@ const model = defineModel<{ value: number, title: string, country_code: string }
const airportOptions = ref(props.prefilledOptions ?? [])
const autocompleteRef = ref<any>(null)
const showClosed = ref(false)
const showNoCode = ref(false)
const lastQuery = ref('')
const menuOpen = ref(false)
const onFocus = () => {
menuOpen.value = true
nextTick(() => {
const input = autocompleteRef.value?.$el?.querySelector('input')
input?.select()
@@ -21,6 +27,8 @@ const onFocus = () => {
}
const searchAirports = async (query: string) => {
lastQuery.value = query
if (!query || query.length < 2) {
airportOptions.value = props.prefilledOptions ?? []
return
@@ -28,15 +36,27 @@ const searchAirports = async (query: string) => {
if (query === model.value?.title) return
const { data } = await axios.get('/search/airports', { params: { q: query } })
const { data } = await axios.get('/search/airports', {
params: {
q: query,
show_closed: showClosed.value ? 1 : undefined,
show_no_code: showNoCode.value ? 1 : undefined,
},
})
airportOptions.value = data
}
// Re-run the last search when either checkbox is toggled, so results update immediately
watch([showClosed, showNoCode], () => {
if (lastQuery.value) searchAirports(lastQuery.value)
})
</script>
<template>
<v-autocomplete
ref="autocompleteRef"
v-model="model"
v-model:menu="menuOpen"
:label="label ?? 'Airport'"
:items="airportOptions"
:error-messages="errorMessages"
@@ -48,6 +68,7 @@ const searchAirports = async (query: string) => {
clearable
hide-no-data
return-object
autocomplete="off"
:custom-filter="() => true"
>
<template #prepend-inner>
@@ -55,6 +76,27 @@ const searchAirports = async (query: string) => {
<span v-if="model" :class="`fi fi-${model.country_code}`"></span>
</span>
</template>
<template #prepend-item>
<div class="px-4 py-1 d-flex align-center" style="gap: 16px;" @mousedown.prevent>
<v-checkbox
v-model="showClosed"
label="Show Closed Airports"
density="compact"
hide-details
style="flex: none; transform: scale(0.65); transform-origin: left center;"
/>
<v-checkbox
v-model="showNoCode"
label="Show Airports with No Code"
density="compact"
hide-details
style="flex: none; transform: scale(0.65); transform-origin: left center;"
/>
</div>
<v-divider class="mt-1" />
</template>
<template #item="{ item, props: itemProps }">
<v-list-item v-bind="itemProps">
<template #prepend>
@@ -62,7 +104,6 @@ const searchAirports = async (query: string) => {
<span :class="`fi fi-${item.country_code}`"></span>
</span>
</template>
</v-list-item>
</template>
</v-autocomplete>
@@ -50,6 +50,7 @@ import {useUpdateSetting} from "@/Composables/useUpdateSetting";
import {usePage} from "@inertiajs/vue3";
type LngLat = [number, number]
interface RouteFlightBucket {
@@ -290,6 +291,7 @@ export default defineComponent({
const { updateSetting } = useUpdateSetting()
const page = usePage<SharedProps>().props
const carto_key = page.carto_api_key
let map: maplibregl.Map | null = null
let popup: maplibregl.Popup | null = null
@@ -736,10 +738,10 @@ export default defineComponent({
maxzoom: 19,
attribution: cartoAttribution.value,
tiles: [
'https://a.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}@2x.png',
'https://b.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}@2x.png',
'https://c.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}@2x.png',
'https://d.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}@2x.png',
`https://a.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}@2x.png?key=${carto_key}`,
`https://b.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}@2x.png?key=${carto_key}`,
`https://c.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}@2x.png?key=${carto_key}`,
`https://d.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}@2x.png?key=${carto_key}`,
],
},
},
+1
View File
@@ -110,6 +110,7 @@ export type SharedProps = import('@inertiajs/core').PageProps & {
logo_api_url: string
achievement_notifications: Notification[]
unread_notification_count: number
carto_api_key: string
}
export interface AchievementDifficulty {
id: number