# GigaSpatial

Acquires published geospatial datasets into a data store you control, loads them for a source, and processes them. A generator maps a dataset onto points of interest or zones. The result is a view: those places with attributes from the datasets attached. That mapping is one use of the data, not the purpose of the library.

Install with `pip install giga-spatial`. Import `gigaspatial`.

A handler is a dataset published as files. A fetcher is a dataset served as a query. A source is the place you load for, or an explicit list of stored files. A data store is where a handler keeps its files. `LocalDataStore` is the default.

# Tutorial

This tutorial builds H3 cells for Rwanda and attaches WorldPop population to each cell. The result is a view.

The first run downloads the country's population raster into the data store. That can take a few minutes. Later runs read the stored files. WorldPop does not need an API key.

```python
from gigaspatial import H3ViewGenerator

zones = H3ViewGenerator(source="RWA", resolution=6)
view = zones.map_wp_pop(country="RWA")
print(view.head())
```

`source` is the country the cells cover. `resolution=6` is the H3 size: about 36 km² per cell. `map_wp_pop` loads the WorldPop raster for that country and sums it onto the cells. `view` is a table of cells with a `population` column and a geometry.

`S2ViewGenerator` and `MercatorViewGenerator` follow the same pattern: construct them with a country, then call `map_wp_pop`.

A handler acquired the WorldPop files and stored them. The generator mapped that dataset onto zones. The view is those zones with population attached. That attachment is enrichment.

# Common workflows

## Combine WorldPop, building footprints, and OpenStreetMap onto H3 hexagons for a country

```python
from gigaspatial import H3ViewGenerator, OSMLocationFetcher

zones = H3ViewGenerator(source="RWA", resolution=6)
zones.map_wp_pop(country="RWA")
zones.map_buildings(country="RWA")

schools = OSMLocationFetcher(country="RWA", location_types=["school"]).fetch_locations()
zones.add_variable_to_view(zones.map_points(schools), "school_count")

view = zones.view
print(view[["zone_id", "population", "building_count", "school_count"]].head())
```

## Estimate how many people live near each school or clinic in a country

```python
from gigaspatial import OSMLocationFetcher, PoiViewGenerator

schools = OSMLocationFetcher(country="RWA", location_types=["school"]).fetch_locations()
view = PoiViewGenerator(schools).map_wp_pop(country="RWA", map_radius_meters=1000)
print(view[["population"]].head())
```

## Download and load Google and Microsoft building footprints for a country

```python
from gigaspatial import (
    GoogleMSBuildingsHandler,
    GoogleOpenBuildingsHandler,
    MSBuildingsHandler,
)

combined = GoogleMSBuildingsHandler().load_data("RWA")
google_buildings = GoogleOpenBuildingsHandler().load_polygons("RWA")
ms_buildings = MSBuildingsHandler().load_data("RWA")
```

# Datasets

A dataset is a named product the library can acquire, such as WorldPop population or Google Open Buildings.

## Handler

A handler is a dataset published as files. It keeps those files in a data store and loads them for a source.

A source is the place you load for, or an explicit list of stored files. A place is a country, a geometry, points, or a frame.

```python
from gigaspatial import GHSLDataHandler

population = GHSLDataHandler(product="GHS_POP", year=2020, resolution=1000)
frame = population.load_into_dataframe("RWA")
```

The first call downloads the tiles that cover Rwanda. `load_data` and `load_into_dataframe` take the source as the first argument.

`crop_to_source=True` cuts the loaded data to that source. A path source is not cropped. A country source is not cropped when the dataset is already one file for that country. An unknown keyword raises `TypeError`.

```python
frame = population.load_into_dataframe("RWA", crop_to_source=True)
```

### Built-in handlers

- `WPPopulationHandler`: WorldPop population counts, densities, age and sex structures, school-age and under-18 population, and degree of urbanisation
- `GHSLDataHandler`: Global Human Settlement Layer (`GHS_POP`, `GHS_BUILT_S`, `GHS_SMOD`, `GHS_BUILT_H`, `GHS_BUILT_V`)
- `GoogleMSBuildingsHandler`: combined Google Open Buildings and Microsoft Global Buildings footprints in GeoParquet
- `GoogleOpenBuildingsHandler`: Google Open Buildings polygons and points by S2 tile
- `MSBuildingsHandler`: Microsoft Global ML Building Footprints by country and quadkey
- `OSMRoadsHandler`: country-scale OpenStreetMap road networks built from Geofabrik extracts, returned as a `SpatialNetwork`
- `RWIHandler`: Meta Relative Wealth Index tiles per country from HDX
- `HDXHandler`: Humanitarian Data Exchange datasets and resources
- `OoklaSpeedtestHandler`: Ookla fixed and mobile network performance tiles
- `OpenCellIDHandler`: OpenCellID cell tower locations by country
- `NasaSRTMHandler`: NASA SRTM 30 m digital elevation model tiles
- `GlofasHandler`: Copernicus GloFAS global river flood hazard depth and water masks by return period
- `AqueductFloodHandler`: WRI Aqueduct coastal and riverine flood hazard rasters
- `AdminBoundaries`: country and subnational administrative boundary polygons

## Fetcher

A fetcher is a dataset served as a query, from an API or a database. It returns records, or a road network. It does not store the dataset as files.

```python
from gigaspatial import OSMLocationFetcher

clinics = OSMLocationFetcher(country="RWA", location_types=["clinic"]).fetch_locations()
```

`MLabFetcher` queries M-Lab speed tests in BigQuery. `OSMRoadsFetcher` returns a road network instead of records; see [Road networks](https://unicef.github.io/giga-spatial/latest/user-guide/road-networks/).

### Built-in fetchers

- `OSMLocationFetcher`: OpenStreetMap points of interest (schools, clinics, hospitals, amenities) via Overpass
- `OSMRoadsFetcher`: OpenStreetMap road networks via Overpass, returned as a `SpatialNetwork`
- `GigaSchoolLocationFetcher`, `GigaSchoolProfileFetcher`, `GigaSchoolMeasurementsFetcher`, `GigaCountriesFetcher`: Giga school locations, profiles, connectivity measurements, and country metadata
- `HealthSitesFetcher`: Healthsites.io health facility locations
- `OvertureAmenityFetcher`: Overture Maps amenities
- `UnicefDataFetcher`: UNICEF SDG and indicator datasets
- `MLabFetcher`: M-Lab NDT7 speed tests in BigQuery
- `GEEProfiler`: Google Earth Engine raster and vector collections

## Data store

A data store is where a handler keeps its files. `LocalDataStore` is the default. It may also be remote (`ADLSDataStore`, `SnowflakeDataStore`, `DeltaSharingDataStore`). A client, such as `BigQueryClient`, queries a database and is not a data store.

Pass `data_store=` when you construct a handler or a generator to choose the store.

# How GigaSpatial compares to other tools

- **Hand-built `geopandas`, `rasterio`, `rasterstats`, `pyrosm`, and `osmnx` pipelines**: GigaSpatial uses `geopandas`, `rasterio`, and `h3` under the hood, and replaces custom per-dataset download scripts, tile stitching, CRS projection, and buffer or zonal loops with handlers, fetchers, and generators that run in a few lines of Python.
- **Grid libraries (`h3-py`, `h3ronpy`, `h3-toolkit`)**: `h3-py` indexes coordinates and geometries into H3 cell IDs. `H3ViewGenerator`, `S2ViewGenerator`, `MercatorViewGenerator`, `AdminBoundariesViewGenerator`, and `GeometryBasedZonalViewGenerator` in GigaSpatial acquire published country datasets (WorldPop, GHSL, Google and Microsoft buildings, Relative Wealth Index) into your data store and map them directly onto H3 hexagons, S2 cells, Mercator tiles, administrative boundaries, or custom polygons.
- **Cloud catalogs and spatial engines (Google Earth Engine, Microsoft Planetary Computer, PostGIS, DuckDB, Apache Sedona)**: Cloud platforms and spatial databases run queries inside their own infrastructure. GigaSpatial downloads and caches published datasets into a data store you control (local disk, Azure Data Lake Storage, or Snowflake) and also provides optional `GEEProfiler` (`giga-spatial[gee]`), `BigQueryClient` (`giga-spatial[bq]`), and DuckDB or Trino integrations when you want them.

# Where to read more

- Tutorial: https://unicef.github.io/giga-spatial/latest/getting-started/quickstart/
- Datasets: https://unicef.github.io/giga-spatial/latest/getting-started/datasets/
- Combining datasets for a country: https://unicef.github.io/giga-spatial/latest/user-guide/country-datasets/
- Population around points: https://unicef.github.io/giga-spatial/latest/user-guide/population-accessibility/
- Points and nearest buildings: https://unicef.github.io/giga-spatial/latest/user-guide/points-and-buildings/
- Road networks: https://unicef.github.io/giga-spatial/latest/user-guide/road-networks/
- API reference: https://unicef.github.io/giga-spatial/latest/api/
- Handler base: https://unicef.github.io/giga-spatial/latest/api/handler-base/
- Source: https://github.com/unicef/giga-spatial
