Skip to main content

qualia_client_core/chora/layers/
nasa_gibs.rs

1pub struct GibsRequest {
2    pub layer: String,
3    pub projection: String,
4    pub width: u32,
5    pub height: u32,
6}
7
8impl GibsRequest {
9    /// URL.
10    pub fn url(&self) -> String {
11        let bbox = if self.projection == "epsg4326" {
12            "-90,-180,90,180"
13        } else {
14            "-20037508.34,-20037508.34,20037508.34,20037508.34"
15        };
16        let crs = if self.projection == "epsg4326" {
17            "EPSG:4326"
18        } else {
19            "EPSG:3857"
20        };
21        format!(
22            "https://gibs.earthdata.nasa.gov/wms/{}/best/wms.cgi?\
23             SERVICE=WMS&REQUEST=GetMap&VERSION=1.3.0\
24             &LAYERS={}&CRS={}&BBOX={}\
25             &WIDTH={}&HEIGHT={}&FORMAT=image/jpeg\
26             &STYLES=&TRANSPARENT=FALSE",
27            self.projection, self.layer, crs, bbox, self.width, self.height
28        )
29    }
30}
31
32pub struct EarthTexture {
33    pub width: u32,
34    pub height: u32,
35    pub rgb: Vec<[u8; 3]>,
36}
37
38impl EarthTexture {
39    /// Samples.
40    pub fn sample(&self, lat_deg: f32, lon_deg: f32) -> [f32; 3] {
41        let lat = lat_deg.clamp(-90.0, 90.0);
42        let lon = lon_deg.clamp(-180.0, 180.0);
43        let v = ((90.0 - lat) / 180.0 * self.height as f32) as u32;
44        let u = (((lon + 180.0) / 360.0) * self.width as f32) as u32;
45        let v = v.min(self.height - 1);
46        let u = u.min(self.width - 1);
47        let idx = (v * self.width + u) as usize;
48        let [r, g, b] = self.rgb.get(idx).copied().unwrap_or([0, 0, 80]);
49        [r as f32 / 255.0, g as f32 / 255.0, b as f32 / 255.0]
50    }
51
52    /// Samples a vertex.
53    pub fn sample_vertex(&self, x: f32, y: f32, z: f32) -> [f32; 3] {
54        let lat = y.asin().to_degrees();
55        let lon = z.atan2(x).to_degrees();
56        self.sample(lat, lon)
57    }
58}
59
60/// Decodes JPEG to RGB.
61pub fn decode_jpeg_rgb(data: &[u8]) -> Result<EarthTexture, String> {
62    let img = image::load_from_memory_with_format(data, image::ImageFormat::Jpeg)
63        .map_err(|e| format!("JPEG decode: {e}"))?;
64    let rgb_img = img.to_rgb8();
65    let width = rgb_img.width();
66    let height = rgb_img.height();
67    let mut rgb = Vec::with_capacity((width * height) as usize);
68    for pixel in rgb_img.pixels() {
69        rgb.push([pixel[0], pixel[1], pixel[2]]);
70    }
71    Ok(EarthTexture { width, height, rgb })
72}
73
74/// Downloads a GIBS texture.
75pub async fn download_gibs_texture(req: &GibsRequest) -> Result<EarthTexture, String> {
76    let url = req.url();
77    let response = reqwest::get(&url)
78        .await
79        .map_err(|e| format!("GIBS request: {e}"))?;
80    if !response.status().is_success() {
81        return Err(format!("GIBS returned {}", response.status()));
82    }
83    let bytes = response
84        .bytes()
85        .await
86        .map_err(|e| format!("GIBS body: {e}"))?;
87    decode_jpeg_rgb(&bytes)
88}