| 1 | import { NextResponse } from 'next/server'; |
| 2 | |
| 3 | // Sentinel-1 SAR Satellite — STAC Catalog via Element84 Earth Search + Copernicus fallback |
| 4 | export async function GET(req: Request) { |
| 5 | const { searchParams } = new URL(req.url); |
| 6 | const lat = parseFloat(searchParams.get('lat') || '0'); |
| 7 | const lng = parseFloat(searchParams.get('lng') || '0'); |
| 8 | const radius = parseFloat(searchParams.get('radius') || '2'); |
| 9 | const days = parseInt(searchParams.get('days') || '30'); // Expanded to 30 days for more results |
| 10 | |
| 11 | if (isNaN(lat) || isNaN(lng)) { |
| 12 | return NextResponse.json({ error: 'Missing lat/lng parameters' }, { status: 400 }); |
| 13 | } |
| 14 | |
| 15 | try { |
| 16 | const bbox = [lng - radius, lat - radius, lng + radius, lat + radius]; |
| 17 | const now = new Date(); |
| 18 | const from = new Date(now.getTime() - days * 86400000); |
| 19 | const datetime = `${from.toISOString().split('.')[0]}Z/${now.toISOString().split('.')[0]}Z`; |
| 20 | |
| 21 | let scenes: any[] = []; |
| 22 | let source = ''; |
| 23 | let total = 0; |
| 24 | |
| 25 | // Source 1: Element84 Earth Search v1 |
| 26 | try { |
| 27 | const res = await fetch('https://earth-search.aws.element84.com/v1/search', { |
| 28 | method: 'POST', |
| 29 | headers: { 'Content-Type': 'application/json' }, |
| 30 | signal: AbortSignal.timeout(12000), |
| 31 | body: JSON.stringify({ |
| 32 | collections: ['sentinel-1-grd'], |
| 33 | bbox, |
| 34 | datetime, |
| 35 | limit: 20, |
| 36 | sortby: [{ field: 'datetime', direction: 'desc' }], |
| 37 | }), |
| 38 | }); |
| 39 | if (res.ok) { |
| 40 | const data = await res.json(); |
| 41 | scenes = (data.features || []).map(formatScene); |
| 42 | total = data.numberMatched || scenes.length; |
| 43 | source = 'element84'; |
| 44 | } |
| 45 | } catch (e) { console.warn('[OSIRIS] Suppressed error:', e instanceof Error ? e.message : e); } |
| 46 | |
| 47 | // Source 2: Try sentinel-2 if sentinel-1 is empty |
| 48 | if (scenes.length === 0) { |
| 49 | try { |
| 50 | const res = await fetch('https://earth-search.aws.element84.com/v1/search', { |
| 51 | method: 'POST', |
| 52 | headers: { 'Content-Type': 'application/json' }, |
| 53 | signal: AbortSignal.timeout(12000), |
| 54 | body: JSON.stringify({ |
| 55 | collections: ['sentinel-2-l2a'], |
| 56 | bbox, |
| 57 | datetime, |
| 58 | limit: 20, |
| 59 | sortby: [{ field: 'datetime', direction: 'desc' }], |
| 60 | }), |
| 61 | }); |
| 62 | if (res.ok) { |
| 63 | const data = await res.json(); |
| 64 | scenes = (data.features || []).map(formatScene); |
| 65 | total = data.numberMatched || scenes.length; |
| 66 | source = 'element84-s2'; |
| 67 | } |
| 68 | } catch (e) { console.warn('[OSIRIS] Suppressed error:', e instanceof Error ? e.message : e); } |
| 69 | } |
| 70 | |
| 71 | // Source 3: Copernicus STAC fallback |
| 72 | if (scenes.length === 0) { |
| 73 | try { |
| 74 | const fallbackRes = await fetch('https://catalogue.dataspace.copernicus.eu/stac/search', { |
| 75 | method: 'POST', |
| 76 | headers: { 'Content-Type': 'application/json' }, |
| 77 | signal: AbortSignal.timeout(12000), |
| 78 | body: JSON.stringify({ |
| 79 | collections: ['SENTINEL-1'], |
| 80 | bbox, |
| 81 | datetime, |
| 82 | limit: 10, |
| 83 | }), |
| 84 | }); |
| 85 | if (fallbackRes.ok) { |
| 86 | const data = await fallbackRes.json(); |
| 87 | scenes = (data.features || []).map(formatScene); |
| 88 | total = data.numberMatched || scenes.length; |
| 89 | source = 'copernicus'; |
| 90 | } |
| 91 | } catch (e) { console.warn('[OSIRIS] Suppressed error:', e instanceof Error ? e.message : e); } |
| 92 | } |
| 93 | |
| 94 | return NextResponse.json({ |
| 95 | source, |
| 96 | scenes, |
| 97 | total, |
| 98 | bbox, |
| 99 | datetime, |
| 100 | timestamp: new Date().toISOString(), |
| 101 | }, { |
| 102 | headers: { 'Cache-Control': 'public, s-maxage=300, stale-while-revalidate=600' }, |
| 103 | }); |
| 104 | } catch (e) { |
| 105 | return NextResponse.json({ error: 'Sentinel lookup failed', scenes: [] }, { status: 500 }); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | function formatScene(feature: any) { |
| 110 | const props = feature.properties || {}; |
| 111 | return { |
| 112 | id: feature.id, |
| 113 | datetime: props.datetime, |
| 114 | platform: props.platform || props['sar:instrument_mode'] || 'Sentinel', |
| 115 | orbit: props['sat:orbit_state'] || props.orbitDirection, |
| 116 | polarization: props['sar:polarizations'] || props.polarisation, |
| 117 | mode: props['sar:instrument_mode'] || props.productType, |
| 118 | resolution: props['sar:resolution_range'] || null, |
| 119 | pass_direction: props['sat:relative_orbit'] || null, |
| 120 | cloud_cover: props['eo:cloud_cover'] ?? null, |
| 121 | bbox: feature.bbox, |
| 122 | thumbnail: feature.assets?.thumbnail?.href || null, |
| 123 | preview: feature.assets?.preview?.href || null, |
| 124 | geometry_type: feature.geometry?.type, |
| 125 | area_km2: feature.bbox ? estimateArea(feature.bbox) : null, |
| 126 | }; |
| 127 | } |
| 128 | |
| 129 | function estimateArea(bbox: number[]): number { |
| 130 | if (bbox.length < 4) return 0; |
| 131 | const [minLng, minLat, maxLng, maxLat] = bbox; |
| 132 | const latDiff = Math.abs(maxLat - minLat) * 111; |
| 133 | const lngDiff = Math.abs(maxLng - minLng) * 111 * Math.cos((minLat + maxLat) / 2 * Math.PI / 180); |
| 134 | return Math.round(latDiff * lngDiff); |
| 135 | } |