205 lines
5.0 KiB
Markdown
205 lines
5.0 KiB
Markdown
# Microsoft Planetary Computer - Giải pháp Timeout
|
|
|
|
## ❌ Vấn đề
|
|
```
|
|
The request exceeded the maximum allowed time
|
|
```
|
|
|
|
## ✅ Giải pháp
|
|
|
|
### 1. **Giảm Parameters** (Quan trọng nhất)
|
|
|
|
**Thử theo thứ tự:**
|
|
|
|
```python
|
|
# ❌ QUÁ LỚN - Dễ timeout
|
|
bbox = [105.48, 9.77, 106.14, 10.35] # ~70km x 60km
|
|
start_date = "2023-01-01"
|
|
end_date = "2023-12-31" # 12 months
|
|
max_scenes = 12
|
|
```
|
|
|
|
```python
|
|
# ✅ VỪA PHẢI - Tốt
|
|
bbox = [105.8, 10.0, 105.9, 10.1] # ~10km x 10km
|
|
start_date = "2024-01-01"
|
|
end_date = "2024-01-31" # 1 month
|
|
max_scenes = 5
|
|
```
|
|
|
|
```python
|
|
# ✅ RẤT NHỎ - Luôn work
|
|
bbox = [105.85, 10.05, 105.87, 10.07] # ~2km x 2km
|
|
start_date = "2024-01-15"
|
|
end_date = "2024-01-22" # 1 week
|
|
max_scenes = 3
|
|
```
|
|
|
|
### 2. **Chiến lược Progressive Loading**
|
|
|
|
Thay vì load toàn bộ vùng lớn 1 lúc, chia nhỏ:
|
|
|
|
```python
|
|
# Ví dụ: Chia bbox lớn thành 4 phần nhỏ
|
|
original_bbox = [105.48, 9.77, 106.14, 10.35]
|
|
|
|
# Tính mid points
|
|
min_lon, min_lat, max_lon, max_lat = original_bbox
|
|
mid_lon = (min_lon + max_lon) / 2
|
|
mid_lat = (min_lat + max_lat) / 2
|
|
|
|
# 4 sub-regions
|
|
sub_regions = [
|
|
[min_lon, min_lat, mid_lon, mid_lat], # Bottom-left
|
|
[mid_lon, min_lat, max_lon, mid_lat], # Bottom-right
|
|
[min_lon, mid_lat, mid_lon, max_lat], # Top-left
|
|
[mid_lon, mid_lat, max_lon, max_lat], # Top-right
|
|
]
|
|
|
|
# Load từng region riêng, sau đó merge
|
|
```
|
|
|
|
### 3. **Tăng Timeout trong Code**
|
|
|
|
Sửa `fetch_sentinel_items_with_retry`:
|
|
|
|
```python
|
|
# Thử với timeout dài hơn và ít items hơn
|
|
for attempt in range(max_retries):
|
|
try:
|
|
# Giảm target xuống còn 2-3 items cho lần đầu
|
|
target_items = min(3, max_scenes) if attempt == 0 else 2
|
|
|
|
search = catalog.search(
|
|
collections=["sentinel-2-l2a"],
|
|
bbox=bbox,
|
|
datetime=time_range,
|
|
query={"eo:cloud_cover": {"lt": cloud_cover}},
|
|
limit=10 # Giảm từ 20-50 xuống 10
|
|
)
|
|
|
|
# Set timeout cho iterator
|
|
items = []
|
|
import signal
|
|
|
|
def timeout_handler(signum, frame):
|
|
raise TimeoutError("Item fetch timeout")
|
|
|
|
signal.signal(signal.SIGALRM, timeout_handler)
|
|
signal.alarm(30) # 30 giây timeout
|
|
|
|
try:
|
|
for item in search.items():
|
|
items.append(item)
|
|
if len(items) >= target_items:
|
|
break
|
|
finally:
|
|
signal.alarm(0) # Cancel alarm
|
|
```
|
|
|
|
### 4. **Alternative: Dùng Dữ liệu Local**
|
|
|
|
Nếu Planetary Computer liên tục timeout:
|
|
|
|
#### **a) Download trước (Recommended)**
|
|
|
|
```bash
|
|
# Dùng sentinelsat để download
|
|
pip install sentinelsat
|
|
|
|
# Download Sentinel-2 về máy
|
|
python download_sentinel2.py --bbox 105.8,10.0,105.9,10.1 \
|
|
--start 2024-01-01 --end 2024-01-31
|
|
```
|
|
|
|
#### **b) Dùng Google Earth Engine** (Nếu có account)
|
|
|
|
```python
|
|
import ee
|
|
ee.Initialize()
|
|
|
|
# Load Sentinel-2 từ GEE thay vì Planetary Computer
|
|
image = ee.ImageCollection('COPERNICUS/S2_SR') \
|
|
.filterBounds(ee.Geometry.Rectangle(bbox)) \
|
|
.filterDate(start_date, end_date) \
|
|
.median()
|
|
```
|
|
|
|
### 5. **Cache Aggressive**
|
|
|
|
Khi đã load được data, cache ngay:
|
|
|
|
```python
|
|
# Trong prediction interface, enable cache by default
|
|
use_cache = True # ALWAYS
|
|
|
|
# Khi load thành công, lưu cache ngay
|
|
if items and len(items) > 0:
|
|
cache_file = f"cache_{bbox_hash}_{date_hash}.joblib"
|
|
joblib.dump({
|
|
'items': items,
|
|
's2_data': s2_data,
|
|
'timestamp': datetime.now()
|
|
}, cache_file)
|
|
```
|
|
|
|
## 🎯 **Action Plan Ngay Bây Giờ**
|
|
|
|
### **Bước 1: Test với bbox CỰC NHỎ**
|
|
|
|
Web interface → Prediction:
|
|
- Min Lon: **105.80**
|
|
- Min Lat: **10.00**
|
|
- Max Lon: **105.82** (chỉ 0.02 độ = ~2km)
|
|
- Max Lat: **10.02**
|
|
- Start: **2024-01-15**
|
|
- End: **2024-01-17** (3 ngày)
|
|
- Max Scenes: **2**
|
|
- Cloud Cover: 50%
|
|
|
|
→ Nếu vẫn timeout → Vấn đề là internet/firewall/server PC quá tải
|
|
|
|
### **Bước 2: Nếu Step 1 OK → Tăng dần**
|
|
|
|
- Tăng bbox lên 0.05 độ (~5km)
|
|
- Tăng time range lên 1 tuần
|
|
- Tăng max_scenes lên 5
|
|
|
|
### **Bước 3: Dùng Batch Processing**
|
|
|
|
Thay vì 1 query lớn:
|
|
- Chia thành nhiều queries nhỏ
|
|
- Dùng `/api/batch/start`
|
|
- Mỗi job = 1 vùng nhỏ
|
|
- Merge results sau
|
|
|
|
## 🔧 **Debug Commands**
|
|
|
|
```bash
|
|
# Check internet
|
|
ping -c 3 planetarycomputer.microsoft.com
|
|
|
|
# Check DNS
|
|
nslookup planetarycomputer.microsoft.com
|
|
|
|
# Test với curl
|
|
curl -I https://planetarycomputer.microsoft.com/api/stac/v1
|
|
|
|
# Monitor network
|
|
sudo tcpdump -i any host planetarycomputer.microsoft.com
|
|
```
|
|
|
|
## 📝 **Token Info** (FYI)
|
|
|
|
Microsoft Planetary Computer **KHÔNG CẦN** manual token:
|
|
- ✅ SAS tokens tự động gen bởi `planetary_computer.sign()`
|
|
- ✅ Auto-refresh khi cần
|
|
- ✅ Không cần API key/registration (public access)
|
|
- ❌ KHÔNG có "hết token" - chỉ có timeout/rate limit
|
|
|
|
Nếu thấy authentication error:
|
|
```python
|
|
# Cài lại thư viện
|
|
pip install --upgrade planetary-computer pystac-client
|
|
```
|