-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
233 lines (187 loc) · 6.89 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
const dotenv = require('dotenv');
const express = require('express');
const axios = require('axios');
const cors = require('cors');
const xlsx = require('xlsx');
const path = require('path');
dotenv.config({ path: '.env.development.local' });
const PORT = process.env.PORT ?? 8000;
const app = express();
app.use(cors());
app.disable('x-powered-by');
const coordsFilePath = path.join(__dirname, 'assets', 'coords.xlsx');
function loadCoordinates() {
const workbook = xlsx.readFile(coordsFilePath);
const sheet = workbook.Sheets[workbook.SheetNames[0]];
const data = xlsx.utils.sheet_to_json(sheet);
const coordinatesMap = new Map();
data.forEach(row => {
const key = `${row.PATH}-${row.ROW}`;
coordinatesMap.set(key, {
ctr_lat: row['CTR LAT'],
ctr_lon: row['CTR LON'],
ul_lat: row['UL LAT'],
ul_lon: row['UL LON'],
ur_lat: row['UR LAT'],
ur_lon: row['UR LON'],
ll_lat: row['LL LAT'],
ll_lon: row['LL LON'],
lr_lat: row['LR LAT'],
lr_lon: row['LR LON'],
});
});
return coordinatesMap;
}
const coordinatesMap = loadCoordinates();
app.get('/path', async (req, res) => {
const lat = parseFloat(req.query.lat);
const lon = parseFloat(req.query.lon);
if (isNaN(lat) || isNaN(lon)) {
return res.status(400).json({ error: 'Invalid params.' });
}
const nimbusUrl = `https://nimbus.cr.usgs.gov/arcgis/rest/services/LLook_Outlines/MapServer/1/query?where=MODE=%27D%27&geometry=${lon},%20${lat}&geometryType=esriGeometryPoint&spatialRel=esriSpatialRelIntersects&outFields=*&returnGeometry=false&f=json`;
const cyclesUrl = 'https://landsat.usgs.gov/sites/default/files/landsat_acq/assets/json/cycles_full.json';
try {
const [nimbusResponse, cyclesResponse] = await Promise.all([
axios.get(nimbusUrl),
axios.get(cyclesUrl),
]);
const nimbusData = nimbusResponse.data;
const cyclesData = cyclesResponse.data;
if (nimbusData.features && nimbusData.features.length > 0) {
const chunks = nimbusData.features.map(feature => {
const path = feature.attributes.PATH;
const row = feature.attributes.ROW;
const key = `${path}-${row}`;
const additionalCoords = coordinatesMap.get(key) || {};
return {
path: path,
row: row,
ctr_lat: additionalCoords.ctr_lat || null,
ctr_lon: additionalCoords.ctr_lon || null,
ul_lat: additionalCoords.ul_lat || null,
ul_lon: additionalCoords.ul_lon || null,
ur_lat: additionalCoords.ur_lat || null,
ur_lon: additionalCoords.ur_lon || null,
ll_lat: additionalCoords.ll_lat || null,
ll_lon: additionalCoords.ll_lon || null,
lr_lat: additionalCoords.lr_lat || null,
lr_lon: additionalCoords.lr_lon || null,
};
});
const landsatDates = {
'Landsat 8': [],
'Landsat 9': [],
};
for (const [landsatKey, dates] of Object.entries(cyclesData)) {
const landsatNumber = landsatKey.replace('landsat_', '');
for (const [date, data] of Object.entries(dates)) {
const paths = data.path.split(',').map(p => parseInt(p.trim(), 10));
const isPathIncluded = nimbusData.features.some(feature => paths.includes(feature.attributes.PATH));
if (isPathIncluded) {
if (landsatNumber === '8') landsatDates['Landsat 8'].push(date);
if (landsatNumber === '9') landsatDates['Landsat 9'].push(date);
}
}
}
const result = {
latitude: lat,
longitude: lon,
chunks: chunks,
dates: landsatDates,
};
res.json(result);
} else {
res.status(404).json({ error: 'No se encontraron datos coincidentes' });
}
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Error al obtener datos externos' });
}
});
app.get('/today', async (req, res) => {
const cyclesUrl = 'https://landsat.usgs.gov/sites/default/files/landsat_acq/assets/json/cycles_full.json';
try {
const today = new Date();
const formattedDate = `${today.getMonth() + 1}/${today.getDate()}/${today.getFullYear()}`;
const cyclesResponse = await axios.get(cyclesUrl);
const cyclesData = cyclesResponse.data;
const results = {};
for (const [landsatKey, dates] of Object.entries(cyclesData)) {
const landsatNumber = landsatKey.replace('landsat_', '');
if (dates.hasOwnProperty(formattedDate)) {
const data = dates[formattedDate];
const paths = data.path.split(',').map(p => parseInt(p.trim(), 10));
const key = `Landsat ${landsatNumber}`;
results[key] = paths;
}
}
if (Object.keys(results).length > 0) {
res.json(results);
} else {
res.status(404).json({ error: 'No se encontraron paths para la fecha de hoy' });
}
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Error al obtener datos externos' });
}
});
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
app.get('/search', async (req, res) => {
try {
const serviceUrl = 'https://m2m.cr.usgs.gov/api/api/json/stable/';
const path = parseFloat(req.query.path);
const row = parseFloat(req.query.row);
const sceneSearchUrl = `${serviceUrl}scene-search`;
const sceneSearchPayload = {
datasetName: 'landsat_ot_c2_l2',
sceneFilter: {
filterType: 'and',
childFilters: [
{
filterType: 'value',
filterId: '5e83d15051254e26',
value: ' ' + path,
operand: '='
},
{
filterType: 'value',
filterId: '5e83d15038163a68',
value: ' ' + row,
operand: '='
}
]
}
};
const sceneSearchResponse = await axios.post(sceneSearchUrl, sceneSearchPayload, {
headers: {
'Content-Type': 'application/json',
'X-Auth-Token': 'eyJjaWQiOjI3Mjk3MTkyLCJzIjoiMTcyODI1MDMxMCIsInIiOjkzMCwicCI6WyJ1c2VyIl19'
}
});
const scenes = sceneSearchResponse.data.data;
if (!scenes || scenes.length === 0) {
return res.json(null);
}
res.json(scenes);
} catch (error) {
console.error('Error al realizar la búsqueda de escenas:', error.response ? error.response.data : error.message);
if (error.response && error.response.data && error.response.data.errorCode === 'AUTH_EXPIRED') {
return res.json(null);
}
res.status(500).json({ error: 'Error al realizar la búsqueda de escenas.' });
}
});
app.post('/', (req, res) => {
res.send('POST');
});
app.use((req, res) => {
res.statusCode = 404;
res
.setHeader('content', 'text/plain; charset=utf8')
.send('404 Not Found');
});
app.listen(PORT, () => {
console.log(`Server listening on http://localhost:${PORT}`);
});
module.exports = app;