Filter on airport category

This commit is contained in:
2023-09-13 10:56:24 -04:00
parent 73bef7fab9
commit 68a442bed2
6 changed files with 70 additions and 32 deletions

View File

@@ -41,18 +41,32 @@ pub struct Airports {
} }
impl Airports { impl Airports {
pub fn find_all(bounds: Option<Polygon<Point>>, limit: i32, page: i32) -> Result<Vec<Self>, CustomError> { pub fn find_all(bounds: Option<Polygon<Point>>, category: Option<String>, limit: i32, page: i32) -> Result<Vec<Self>, CustomError> {
let mut conn = db::connection()?; let mut conn = db::connection()?;
let airports = airports::table let airports;
.limit(limit as i64) if let Some(category) = category {
.filter(airports::id.gt(page * limit).and(match bounds { airports = airports::table
Some(b) => st_contains(b, airports::point), .limit(limit as i64)
None => { .filter(airports::id.gt(page * limit).and(match bounds {
let polygon: Polygon<Point> = Polygon::new(Some(4326)); Some(b) => st_contains(b, airports::point),
st_contains(polygon, airports::point) None => {
} let polygon: Polygon<Point> = Polygon::new(Some(4326));
})) st_contains(polygon, airports::point)
.load::<Airports>(&mut conn)?; }
}).and(airports::category.eq(category))).load::<Airports>(&mut conn)?;
} else {
airports = airports::table
.order(airports::category.asc())
.limit(limit as i64)
.filter(airports::id.gt(page * limit).and(match bounds {
Some(b) => st_contains(b, airports::point),
None => {
let polygon: Polygon<Point> = Polygon::new(Some(4326));
st_contains(polygon, airports::point)
}
}))
.load::<Airports>(&mut conn)?;
}
Ok(airports) Ok(airports)
} }

View File

@@ -11,6 +11,7 @@ struct FindAllParams {
ne_lon: f64, ne_lon: f64,
sw_lat: f64, sw_lat: f64,
sw_lon: f64, sw_lon: f64,
category: Option<String>,
limit: i32, limit: i32,
page: i32 page: i32
} }
@@ -36,7 +37,12 @@ async fn find_all(req: HttpRequest) -> HttpResponse {
polygon.add_point(Point { x: params.ne_lon, y: params.ne_lat, srid: Some(4326) }); polygon.add_point(Point { x: params.ne_lon, y: params.ne_lat, srid: Some(4326) });
polygon.add_point(Point { x: params.sw_lon, y: params.ne_lat, srid: Some(4326) }); polygon.add_point(Point { x: params.sw_lon, y: params.ne_lat, srid: Some(4326) });
polygon.add_point(Point { x: params.sw_lon, y: params.sw_lat, srid: Some(4326) }); polygon.add_point(Point { x: params.sw_lon, y: params.sw_lat, srid: Some(4326) });
match web::block(move || Airports::find_all(Some(polygon), params.limit, params.page)).await.unwrap() { let category = match &params.category {
Some(c) => Some(c.to_string()),
None => None
};
match web::block(move || Airports::find_all(Some(polygon), category, params.limit, params.page)).await.unwrap() {
Ok(a) => HttpResponse::Ok().json(a), Ok(a) => HttpResponse::Ok().json(a),
Err(err) => { Err(err) => {
error!("{}", err); error!("{}", err);

View File

@@ -150,6 +150,7 @@ impl Metars {
Ok(m) => m, Ok(m) => m,
Err(err) => return Err(CustomError { error_status_code: 500, error_message: format!("{}", err) }) Err(err) => return Err(CustomError { error_status_code: 500, error_message: format!("{}", err) })
}; };
fn get_missing_metar_icaos(db_metars: &Vec<Metars>, station_icaos: Vec<&str>) -> Vec<String> { fn get_missing_metar_icaos(db_metars: &Vec<Metars>, station_icaos: Vec<&str>) -> Vec<String> {
let mut missing_metar_icaos: Vec<String> = vec![]; let mut missing_metar_icaos: Vec<String> = vec![];
let current_time = chrono::Local::now().naive_local().timestamp(); let current_time = chrono::Local::now().naive_local().timestamp();

View File

@@ -18,7 +18,6 @@ export default function Map() {
zoom={8} zoom={8}
maxZoom={12} maxZoom={12}
minZoom={1} minZoom={1}
zoomControl={false}
style={{ height: '96.5vh' }} style={{ height: '96.5vh' }}
className='overflow-y-hidden overflow-x-hidden' className='overflow-y-hidden overflow-x-hidden'
attributionControl={false} attributionControl={false}
@@ -35,8 +34,9 @@ function MapTiles() {
const map = useMap(); const map = useMap();
const mapEvents = useMapEvents({ const mapEvents = useMapEvents({
zoomend: () => { zoomend: async () => {
setZoomLevel(mapEvents.getZoom()); setZoomLevel(mapEvents.getZoom());
await updateAirports(mapEvents.getBounds());
}, },
movestart: () => { movestart: () => {
// setDragging(true); // setDragging(true);
@@ -51,10 +51,10 @@ function MapTiles() {
const ne = bounds.getNorthEast(); const ne = bounds.getNorthEast();
const sw = bounds.getSouthWest(); const sw = bounds.getSouthWest();
const _airports = await getAirports({ const _airports = await getAirports({
ne_lat: ne.lat, bounds: {
ne_lon: ne.lng, northEast: { lat: ne.lat, lon: ne.lng },
sw_lat: sw.lat, southWest: { lat: sw.lat, lon: sw.lng }
sw_lon: sw.lng, },
limit: 100, limit: 100,
page: 1 page: 1
}); });

View File

@@ -2,14 +2,22 @@ import axios from 'axios';
import { Airport } from './airport.types'; import { Airport } from './airport.types';
interface GetAirportsProps { interface GetAirportsProps {
ne_lat: number; bounds?: Bounds;
ne_lon: number; category?: string;
sw_lat: number;
sw_lon: number;
page?: number; page?: number;
limit?: number; limit?: number;
} }
export interface Bounds {
northEast: Coordinate;
southWest: Coordinate;
}
export interface Coordinate {
lat: number;
lon: number;
}
interface GetAirportProps { interface GetAirportProps {
icao: string; icao: string;
} }
@@ -19,16 +27,19 @@ export async function getAirport({ icao }: GetAirportProps) {
return response?.data; return response?.data;
} }
export async function getAirports({ export async function getAirports({ bounds, category, limit = 10, page = 1 }: GetAirportsProps): Promise<Airport[]> {
ne_lat,
ne_lon,
sw_lat,
sw_lon,
limit = 10,
page = 1
}: GetAirportsProps): Promise<Airport[]> {
const response = await axios const response = await axios
.get(`http://localhost:5000/airports`, { params: { ne_lat, ne_lon, sw_lat, sw_lon, limit, page } }) .get(`http://localhost:5000/airports`, {
params: {
ne_lat: bounds?.northEast.lat,
ne_lon: bounds?.northEast.lon,
sw_lat: bounds?.southWest.lat,
sw_lon: bounds?.southWest.lon,
category,
limit,
page
}
})
.catch((error) => console.error(error)); .catch((error) => console.error(error));
return response?.data || []; return response?.data || [];
} }

View File

@@ -1,8 +1,14 @@
import { Metar } from './metar.types'; import { Metar } from './metar.types';
export enum AirportCategory {
SMALL = 'small_airport',
MEDIUM = 'medium_airport',
LARGE = 'large_airport'
}
export interface Airport { export interface Airport {
icao: string; icao: string;
category: string; category: AirportCategory;
full_name: string; full_name: string;
elevation_ft: string; elevation_ft: string;
continent: string; continent: string;