File size: 7,395 Bytes
85d2d3c
 
 
 
 
 
 
 
 
 
 
 
 
057dfdd
85d2d3c
05fb374
85d2d3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6f6e7b
 
 
85d2d3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6f6e7b
85d2d3c
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
---
license: cc-by-3.0
pretty_name: GLiM API Backend Data
size_categories:
- 1M<n<10M
tags:
- geospatial
- geology
- lithology
- earth-science
- gis
- pandas
---

<p align="center">
  <img src="https://i.ibb.co/YTTkV4W5/file-0000000001748243a5fdcd08844666f0.png" alt="GLiM API Backend banner" width="100%">
</p>

<h1 align="center">GLiM API Backend</h1>
<p align="center"><i>A lightweight, production-ready API for global lithological data — built for AI training, geospatial analysis, and low-memory deployment.</i></p>

<p align="center">
  <img src="https://img.shields.io/badge/Python-3.10%2B-blue?logo=python&logoColor=white" alt="Python">
  <img src="https://img.shields.io/badge/GeoPandas-Spatial%20Engine-green?logo=geopandas&logoColor=white" alt="GeoPandas">
  <img src="https://img.shields.io/badge/Data-GLiM%20v1.0-orange" alt="GLiM">
  <img src="https://img.shields.io/badge/License-CC--BY--3.0-lightgrey" alt="License">
  <img src="https://img.shields.io/badge/Deploy-Render-46E3B7?logo=render&logoColor=white" alt="Render">
</p>

---

## Overview

The **GLiM API Backend** serves lithological (rock type) data for any point on Earth's land surface, sourced from the [Global Lithological Map (GLiM)](https://doi.org/10.1029/2012GC004370) database (Hartmann & Moosdorf, 2012). It exposes fast point and batch lookups without requiring the ~2.7 GB raw geodatabase to be loaded in full — making it practical to run on constrained environments like Render's free tier.

This is a sibling project to [**AGDFS**](https://agdfs.onrender.com) (Automated Geological Data Fetching System), following the same design philosophy: unify large, awkward-to-host geoscience datasets behind a simple API, so downstream AI training and analysis pipelines never need to touch the raw source files directly.

---

## Data Source

| | |
|---|---|
| **Dataset** | Global Lithological Map (GLiM) Geodatabase |
| **Authors** | Hartmann, J. & Moosdorf, N. (2012) |
| **Citation** | Hartmann, J., Moosdorf, N. (2012). *The new global lithological map database GLiM: A representation of rock properties at the Earth surface.* Geochemistry, Geophysics, Geosystems, 13, Q12004. https://doi.org/10.1029/2012GC004370 |
| **License** | CC-BY-3.0 — attribution required in any derived product |
| **Raw size** | ~1.1 GB compressed / ~2.7 GB uncompressed (File Geodatabase, ~1.24M polygons) |

> **Note:** Always retain the citation above wherever this API's data is used downstream — it's a condition of the CC-BY-3.0 license.

---

## Production Files

Rather than serving the raw geodatabase, the pipeline splits GLiM into two purpose-built files that keep the API's memory footprint low:

| File | Contents | Purpose |
|---|---|---|
| `glim_spatial_index.parquet` | `polygon_id`, `geometry` only | Loaded at API startup for fast spatial lookups via GeoPandas' spatial index. Stripped of attribute columns to minimize RAM. |
| `glim_metadata.csv` | All lithological attributes (age, rock type, lithology class, etc.), indexed by `polygon_id` | Queried **only after** a spatial match is found — attributes are never loaded per-polygon during the spatial search itself. |

This split is the core of the memory optimization: the expensive part of a lookup (spatial search) runs against a geometry-only file, and the cheap part (attribute retrieval) runs against a plain indexed table.

---

## How It Works

```
Incoming request (lat, lon)


Spatial index query — geopandas .sindex.query(point, predicate="intersects")

        ├── Match found ──────────────► polygon_id

        └── No match (near-coast / boundary)


        Fallback: .nearest() neighbor search → closest land polygon


        polygon_id


Attribute lookup — glim_metadata.csv indexed by polygon_id


Response: { lithology, rock_class, age, ..., polygon_id }
```

1. **Spatial query** — the incoming point is tested against `glim_spatial_index.parquet`'s in-memory R-tree via `sindex.query(point, predicate="intersects")`.
2. **Fallback logic** — if the point falls just offshore or on a polygon boundary and returns no direct match, a `.nearest()` neighbor search finds the closest land polygon instead of returning empty.
3. **Attribute fetch** — once a `polygon_id` is resolved, the corresponding row is pulled from `glim_metadata.csv` (or a database equivalent) and merged into the response.

---

## API Endpoints

> Endpoint paths below reflect the intended workflow described above — adjust to match your actual route implementation.

| Method | Path | Description |
|---|---|---|
| `GET` | `/lithology/point?lat={lat}&lon={lon}` | Single coordinate lookup — returns lithology attributes for the containing (or nearest) polygon. |
| `POST` | `/lithology/batch` | Batch lookup — accepts a list of `{lat, lon}` points, returns results for all in one call. |
| `GET` | `/health` | Service health check. |
| `GET` | `/mcp` | MCP layer for agent/tool integration (if enabled, matching the AGDFS pattern). |

---

## Tech Stack

- **[GeoPandas](https://geopandas.org/)** + **[Pyogrio](https://pyogrio.readthedocs.io/)** — fast geodatabase and Parquet I/O
- **[Shapely](https://shapely.readthedocs.io/)** — geometric operations and point-in-polygon tests
- **[PyArrow](https://arrow.apache.org/docs/python/)** — high-performance Parquet storage engine
- **FastAPI** — API layer (recommended, consistent with AGDFS)

---

## Project Structure

```
glim-api-backend/
├── data/
│   ├── glim_spatial_index.parquet   # geometry + polygon_id only
│   └── glim_metadata.csv            # attributes indexed by polygon_id
├── app/
│   ├── main.py                      # FastAPI app + endpoints
│   ├── lookup.py                    # spatial query + fallback logic
│   └── config.py
├── requirements.txt
└── README.md
```

---

## Getting Started

```bash
git clone https://github.com/Nora-Research-Lab/<repo-name>.git
cd <repo-name>
pip install -r requirements.txt
uvicorn app.main:app --reload
```

---

## Data Hosting

Given the size of the production files, large data assets are hosted externally rather than committed to the repo — following the same pattern as [AGDFS's Hugging Face dataset](https://huggingface.co/datasets/Adedoyinjames/AGDFS-DATA):

```
https://huggingface.co/datasets/<your-org-or-username>/GLiM-DATA
```

The API downloads/caches `glim_spatial_index.parquet` and `glim_metadata.csv` from this location at startup or build time. *(Replace with your actual dataset repo once published.)*

---

## Deployment Notes

Built with Render's free tier (and similarly memory-constrained hosts) in mind:
- Only the stripped-down spatial index is loaded into memory — never the full 2.7 GB geodatabase.
- The spatial index (`sindex`) is built **once** at startup and reused across all requests.
- Attribute data stays out-of-memory-critical-path, queried lazily per match.

---

## License

Code: add your preferred license here.
Data: GLiM is licensed **CC-BY-3.0** — see citation above.

---

<p align="center"><i>Part of the <a href="https://github.com/Nora-Research-Lab">NORA Research Lab</a> geoscience API suite.</i></p>