> ## Documentation Index
> Fetch the complete documentation index at: https://docs.wherobots.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Build RasterFlow Mosaics

> Build Zarr mosaics from the RasterFlow built-in datasets for one area of interest or several.

Public Preview

A mosaic is one continuous raster image of an area of interest (AOI) for a time range, made by combining many overlapping satellite or aerial scenes. RasterFlow puts the scenes onto one pixel grid. Where scenes overlap, each dataset has its own rule for choosing the pixel value, such as the median of the observations left after [cloud and quality masking](https://docs.wherobots.com/develop/rasterflow/rasterflow-datasets.md#cloud-and-quality-filtering) (`s2_median`) or the valid scene closest to a date (`s2_closest_valid`).

The `build_mosaics` task builds mosaics from the [built-in datasets](https://docs.wherobots.com/develop/rasterflow/rasterflow-datasets.md#rasterflow-built-in-datasets), such as Sentinel-2 and NAIP, and writes each one as a Zarr store. To build mosaics from your own imagery, use `build_gti_mosaics`, described in [Bring your own rasters](https://docs.wherobots.com/develop/rasterflow/rasterflow-datasets.md#bring-your-own-rasters).

> [!NOTE]
>
> The examples on this page run RasterFlow through the [Tasks API](https://docs.wherobots.com/api-reference/tasks/run-a-task.md), in two tabs. **Python** uses the [Wherobots Python SDK](https://pypi.org/project/wherobots-python-sdk/), and **API** calls the REST API with `curl`. Outside a Wherobots notebook or job, both need a [Wherobots API key](https://docs.wherobots.com/get-started/wherobots-cloud/api-keys.md) in `WHEROBOTS_API_KEY`. Each example starts a task run; to wait for it and read its results, see [Get started](https://docs.wherobots.com/develop/rasterflow.md#get-started) and [Run RasterFlow Tasks](https://docs.wherobots.com/develop/rasterflow/rasterflow-tasks.md).

## Build a mosaic

Pass `build_mosaics` the datasets to use, an AOI in `aoi`, a time range in `start` and `end`, and the output grid's CRS in `target_crs`. [Areas of interest](https://docs.wherobots.com/develop/rasterflow/rasterflow-tasks.md#areas-of-interest) lists the formats `aoi` accepts, and [RasterFlow Datasets](https://docs.wherobots.com/develop/rasterflow/rasterflow-datasets.md) lists the `datasets` values and their time rules.

This example builds 2023 planting- and harvest-season composites for Haskell County, Kansas:

```python Python theme={"system"}
# pip install wherobots-python-sdk
import wherobots

wb = wherobots.Wherobots()  # outside Wherobots, reads WHEROBOTS_API_KEY

run = wb.tasks.run("build_mosaics", {
    "datasets": ["s2med_planting", "s2med_harvest"],
    "aoi": "s3://wherobots-examples/rasterflow/aois/haskell.parquet",
    "start": "2023-01-01T00:00:00Z",
    "end": "2024-01-01T00:00:00Z",  # seasonal datasets need full calendar years
    "target_crs": "EPSG:32614",
}, region="aws-us-west-2")
# When the run succeeds, results["mosaic_index_uri"] lists the mosaic stores.
```

```bash API theme={"system"}
curl -X POST "https://api.cloud.wherobots.com/tasks/build_mosaics/runs" \
  -H "X-API-Key: $WHEROBOTS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "region": "aws-us-west-2",
    "name": "Haskell County seasonal mosaics",
    "inputs": {
      "datasets": ["s2med_planting", "s2med_harvest"],
      "aoi": "s3://wherobots-examples/rasterflow/aois/haskell.parquet",
      "start": "2023-01-01T00:00:00Z",
      "end": "2024-01-01T00:00:00Z",
      "target_crs": "EPSG:32614"
    }
  }'

# When the run succeeds, results.mosaic_index_uri lists the mosaic stores.
```

The run's output, `mosaic_index_uri`, is a [mosaic index](https://docs.wherobots.com/develop/rasterflow/rasterflow-tasks.md#task-run-results) that lists the Zarr stores the run wrote.

## Build mosaics for several areas in one run

`build_mosaics` builds one mosaic for each row of an AOI file. To build mosaics for several separate areas in one task run, write each area as its own row of a GeoParquet file, then pass the file's URI in `aoi`.

This sample writes three areas of about 2 km × 2 km to your Managed Storage. All three are in UTM zone 18N, so one `target_crs` fits them all. The geometries carry SRID 4326, which the GeoParquet writer records as the file's CRS.

```python Python theme={"system"}
import os

from sedona.spark import SedonaContext

sedona = SedonaContext.create(SedonaContext.builder().getOrCreate())

aois = sedona.sql("""
    SELECT
        name,
        ST_SetSRID(ST_GeomFromWKT(wkt), 4326) AS geometry
    FROM VALUES
        ('college_park_md', 'POLYGON ((-76.94 38.98, -76.92 38.98, -76.92 39.00, -76.94 39.00, -76.94 38.98))'),
        ('annapolis_md',    'POLYGON ((-76.50 38.96, -76.48 38.96, -76.48 38.98, -76.50 38.98, -76.50 38.96))'),
        ('wilmington_de',   'POLYGON ((-75.56 39.73, -75.54 39.73, -75.54 39.75, -75.56 39.75, -75.56 39.73))')
    AS t(name, wkt)
""")

# USER_S3_PATH is your Managed Storage folder in a Wherobots notebook or job
aoi_uri = os.getenv("USER_S3_PATH") + "multi_aoi_demo/aois.parquet"
aois.coalesce(1).write.mode("overwrite").format("geoparquet").save(aoi_uri)
```

Pass the file to `build_mosaics`:

```python Python theme={"system"}
import wherobots

wb = wherobots.Wherobots()

mosaic_index_uri = wb.tasks.run_and_wait("build_mosaics", {
    "datasets": ["s2_median"],
    "aoi": aoi_uri,
    "start": "2024-06-01T00:00:00Z",
    "end": "2024-09-01T00:00:00Z",
    "target_crs": "EPSG:32618",
}, name="Three Sentinel-2 mosaics", region="aws-us-west-2").output

index = sedona.read.format("geoparquet").load(mosaic_index_uri)
index.select("name", "aoi_coverage_percent", "location").show(truncate=False)
```

```bash API theme={"system"}
curl -X POST "https://api.cloud.wherobots.com/tasks/build_mosaics/runs" \
  -H "X-API-Key: $WHEROBOTS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "region": "aws-us-west-2",
    "name": "Three Sentinel-2 mosaics",
    "inputs": {
      "datasets": ["s2_median"],
      "aoi": "s3://<managed-storage-path>/multi_aoi_demo/aois.parquet",
      "start": "2024-06-01T00:00:00Z",
      "end": "2024-09-01T00:00:00Z",
      "target_crs": "EPSG:32618"
    }
  }'
```

The mosaic index has one row for each AOI row. It keeps the AOI file's columns, here `name` and `geometry`, and adds `aoi_coverage_percent` and the store's URI in `location`. The sample prints:

```text theme={"system"}
+---------------+--------------------+--------------------------------------------------------------------------------------+
|name           |aoi_coverage_percent|location                                                                              |
+---------------+--------------------+--------------------------------------------------------------------------------------+
|college_park_md|100.0               |s3://<managed-storage>/tasks/build_mosaics/mosaics/<task-run-id>/zarr/433697999adeae53.zarr|
|annapolis_md   |100.0               |s3://<managed-storage>/tasks/build_mosaics/mosaics/<task-run-id>/zarr/4617fc2e2ba34ef6.zarr|
|wilmington_de  |100.0               |s3://<managed-storage>/tasks/build_mosaics/mosaics/<task-run-id>/zarr/3a1be1e6a085fdc7.zarr|
+---------------+--------------------+--------------------------------------------------------------------------------------+
```

Tasks that read a mosaic, such as [`build_zarr_multiscales`](https://docs.wherobots.com/develop/rasterflow/rasterflow-multiscales#limitations), take one store per run, so start one run for each value in `location`.

### Areas in different UTM zones

The sample above passes one `target_crs` because all three areas are in UTM zone 18N. For areas in different zones, leave out `target_crs` and give each row its own output CRS: an integer EPSG code in a column named `crs:epsg`. The colon in the name needs backticks in SQL. `crs:epsg` sets only the output grid. The geometries stay in longitude and latitude, so they keep SRID 4326.

This sample writes one area in UTM zone 18N and one in UTM zone 13N:

```python Python theme={"system"}
utm_aois = sedona.sql("""
    SELECT
        name,
        epsg AS `crs:epsg`,
        ST_SetSRID(ST_GeomFromWKT(wkt), 4326) AS geometry
    FROM VALUES
        ('college_park_md', 32618, 'POLYGON ((-76.94 38.98, -76.92 38.98, -76.92 39.00, -76.94 39.00, -76.94 38.98))'),
        ('denver_co',       32613, 'POLYGON ((-105.00 39.73, -104.98 39.73, -104.98 39.75, -105.00 39.75, -105.00 39.73))')
    AS t(name, epsg, wkt)
""")

utm_aoi_uri = os.getenv("USER_S3_PATH") + "multi_aoi_demo/aois_two_zones.parquet"
utm_aois.coalesce(1).write.mode("overwrite").format("geoparquet").save(utm_aoi_uri)
```

Pass the file to `build_mosaics` without `target_crs`:

```python Python theme={"system"}
utm_index_uri = wb.tasks.run_and_wait("build_mosaics", {
    "datasets": ["s2_median"],
    "aoi": utm_aoi_uri,
    "start": "2024-06-01T00:00:00Z",
    "end": "2024-09-01T00:00:00Z",
}, name="Sentinel-2 mosaics in two UTM zones", region="aws-us-west-2").output
```

```bash API theme={"system"}
curl -X POST "https://api.cloud.wherobots.com/tasks/build_mosaics/runs" \
  -H "X-API-Key: $WHEROBOTS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "region": "aws-us-west-2",
    "name": "Sentinel-2 mosaics in two UTM zones",
    "inputs": {
      "datasets": ["s2_median"],
      "aoi": "s3://<managed-storage-path>/multi_aoi_demo/aois_two_zones.parquet",
      "start": "2024-06-01T00:00:00Z",
      "end": "2024-09-01T00:00:00Z"
    }
  }'
```

The run writes one store for each row, in that row's CRS: UTM zone 18N for College Park and UTM zone 13N for Denver. The mosaic index keeps the `crs:epsg` column.

Set the output CRS one way, not both. If you pass `target_crs` for an AOI file that has a `crs:epsg` column, the run fails with `CRS provided both in AOI and as argument. Pick one.`

## Build one mosaic across several areas

A MULTIPOLYGON is one geometry made of several separate polygons, such as a farm's fields or a group of islands stored as one feature. In WKT, it lists each polygon in its own set of parentheses: `MULTIPOLYGON (((…)), ((…)))`.

A MULTIPOLYGON fills one row of an AOI file, so `build_mosaics` builds one mosaic for it. That mosaic's grid covers the bounding box of all the parts: the smallest rectangle that contains every polygon, including the space between them. To build separate mosaics without the space between them, write each part as its own row.

## Next steps

* [Build multiscales](https://docs.wherobots.com/develop/rasterflow/rasterflow-multiscales.md) for a mosaic, so a map can display it at any zoom level.
* [View the mosaic](https://docs.wherobots.com/develop/rasterflow/rasterflow-visualization.md) on a map with Wherobots-GL.
* Write the mosaics to your own bucket with `storage_uri`, described in [RasterFlow Storage Integration](https://docs.wherobots.com/develop/rasterflow/rasterflow-storage-integration.md).
* Follow a complete mosaic build in the [Sentinel-2 Mosaic](https://docs.wherobots.com/tutorials/example-notebooks/rasterflow-s2-mosaic.md) and [NAIP Mosaic](https://docs.wherobots.com/tutorials/example-notebooks/rasterflow-naip-mosaic.md) example notebooks.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.