# Nearest Neighbor Analysis

Find nearest features from a target dataset for each feature in a source dataset.

## Overview

Nearest neighbor analysis identifies the closest features between two datasets, calculating distances and joining attributes.

## Inputs

- **Source Dataset**: Dataset to find nearest neighbors for
- **Target Dataset**: Dataset to search for neighbors
- **Max Distance**: Maximum search distance (optional)
- **Limit**: Maximum neighbors per feature (default: 1)

## Outputs

New dataset containing:

- Source feature geometry
- Nearest target feature information
- **Distance**: Distance to nearest neighbor
- Attributes from both datasets

## Example

```json
{
  "source_dataset_id": 123,
  "target_dataset_id": 124,
  "max_distance": 5000,
  "limit": 1
}
```

## Background Jobs

This analysis runs as a background job. See [Nearest Analysis Worker](../workers/nearest_analysis.md) for details.

## Use Cases

- Service accessibility analysis
- Nearest facility identification
- Distance calculations
- Spatial joins

## Notes

- Spatial indexes critical for performance
- Large max_distance values may slow processing
- Multiple neighbors can be found per feature
- Results include distance in dataset units

## Related Documentation

- [Nearest Analysis Worker](../workers/nearest_analysis.md)
- [Analysis API](../api/analysis.md)

