Core Package Overview
The @spatialdata/core package provides a TypeScript/JavaScript interface for reading and working with SpatialData Zarr stores. It mostly mirrors the Python library's API design while adapting to TypeScript idioms and the asynchronous nature of browser-based data loading. Hot paths (parquet decode) use WASM behind TypeScript interfaces.
Installation
Known risks are tracked in the MDV release checklist.
npm install @spatialdata/core
# or
pnpm add @spatialdata/core
Quick Start
import { readZarr, loadFeatureRowIndexByFeatureIndex } from '@spatialdata/core';
// Load a SpatialData store from a URL
const sdata = await readZarr('https://example.com/my-spatialdata.zarr');
// View a representation of what's in the store
console.log(sdata.toString());
// Access elements by type
const images = sdata.images; // Record<string, ImageElement>
const shapes = sdata.shapes; // Record<string, ShapesElement>
const tables = sdata.tables; // Record<string, TableElement>
const labels = sdata.labels; // Record<string, LabelsElement>
const points = sdata.points; // Record<string, PointsElement>
// Get coordinate systems
const coordinateSystems = sdata.coordinateSystems; // ['global', ...]
// Table-backed shape association (see also @spatialdata/layers encoders)
const shapesEl = sdata.shapes.cell_shapes;
const renderData = await shapesEl.loadRenderData();
const rowIndexByFeatureIndex = await loadFeatureRowIndexByFeatureIndex({
spatialData: sdata,
kind: 'shapes',
key: 'cell_shapes',
featureIds: renderData.featureIds,
});
Tables load through anndata.js on zarrita stores. Association helpers
use targeted obs-column loaders rather than anndata.js; see
Element classes.
Design Philosophy
The library follows several key principles:
-
Read-only: You can load stores, access elements, query transformations, and load data, but not create or modify stores.
-
Python API Familiarity: Method and class names mirror the Python
spatialdatalibrary where practical, making it easy to translate workflows. -
Type Safety: Full TypeScript types with Zod schema validation for metadata, giving you confidence in the data structures.
-
Lazy Loading of Heavy Data: Data is loaded on-demand rather than all at once, critical for working with large datasets in the browser.
-
Eager Loading of Metadata: Consolidated metadata from the zarr store is loaded up-front and used to build a representation of the object structure such that it can be programmatically exposed without
asyncleaking where it isn't needed, which can lead to a lot of fiddly state management. -
Explicit Error Handling: Uses a Rust-inspired
Resulttype for operations that can fail, allowing you to handle errors gracefully without exceptions.
Public API Surface
The main entry points for application code are:
| Export | Purpose |
|---|---|
readZarr(url) | Load a SpatialData store from a URL |
SpatialData | The loaded store object with element collections |
| Element classes | ImageElement, ShapesElement, LabelsElement, PointsElement, TableElement |
Result utilities | Ok, Err, isOk, isErr, unwrap, unwrapOr |
getTransformMatrix() | Convenience function for getting Matrix4 transforms |
| Points and shapes loading | PointsElement.loadPoints() / streamPoints(), ShapesElement.loadRenderData(); see Loading points and shapes |
| Resource Resolver | SpatialEntryStore, PointsResolver, ShapesResolver, Resolution, SpatialEntryError, EntryNotice; see Resource Resolver |
| Table association helpers | TableElement.getTableKeys(), SpatialData.getAssociatedTable(s), loadAssociatedTableFeatureRows, loadFeatureRowIndexByFeatureIndex, createFeatureTableAlignment |
The table association helpers follow Python spatialdata semantics: regions
are matched through region, region_key, and instance_key (when present),
with feature ids coming from SpatialElement instances such as
GeoDataFrame.index for shapes. getTableKeys() normalizes the attrs contract
and treats missing association metadata as "no region links".
Visual encoders in @spatialdata/layers consume the row alignment produced
here rather than reimplementing association rules.
For internal architecture details, module organization, and APIs intended for contributors or advanced tooling, see Internals & Architecture.
Element Types
SpatialData stores contain different types of elements:
Spatial Elements
These have coordinate transformations and can be rendered in shared coordinate systems:
- Images (
ImageElement): Multiscale raster images following OME-NGFF - Labels (
LabelsElement): Segmentation label images (also OME-NGFF) - Shapes (
ShapesElement): Vector geometries (polygons, circles) - Points (
PointsElement): Point cloud data (transcripts, etc.)
Non-Spatial Elements
- Tables (
TableElement): AnnData tables with region annotations
Next Steps
- Element Classes - Working with different element types
- Loading points and shapes - Memory caps, streaming, feature selection, tiled stores
- Transformations - Understanding coordinate systems and transforms
- Error Handling - Using the Result type pattern
- Resource Resolver - Cached, cancellable loading with typed errors and notices
- Internals - Architecture and internal APIs (for contributors)