---
title: "Geo bounding box search in C#"
url: https://recursiveai.org/oneindex/cookbook/geo-bounding-box-search-csharp/
description: "Search indexed locations inside a map bounding box in C#, including edges and boxes crossing the antimeridian. Runnable OneIndex example."
version: "0.4.0-preview.2"
---

# Geo bounding box search in C#

**OneIndex** is a free, open-source search library for .NET from Recursive AI. MIT or Apache-2.0, preview, runs in-process.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneIndex) · [GitHub](https://github.com/recursive-ai-oss/oneindex-csharp) · [Docs](https://recursiveai.org/oneindex/docs/) · [Download example (.zip)](https://recursiveai.org/oneindex/cookbook/geo-bounding-box-search-csharp/geo-bounding-box-search-csharp.zip)

Filter indexed locations to a rectangular map viewport using latitude and longitude in degrees.

```shell
dotnet add package RecursiveAI.OneIndex --prerelease
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OneIndex;

using var index =
    SearchIndex.InMemory<Place>();
index.Upsert(new Place {
    Location = new(45, 9) });
index.Commit();
var hits = index.Search().Where(p =>
    // north, south, west, east
    Geo.WithinBoundingBox(
        p.Location, 46, 44, 8, 10))
    .Execute();
Console.WriteLine($"Matches: {hits.Count}");

public class Place
{
    [SearchId]
    public string Id { get; set; } = "1";
    [Geo]
    public GeoPoint Location { get; set; }
}
```

```text
Matches: 1
```

Unzip [the example](https://recursiveai.org/oneindex/cookbook/geo-bounding-box-search-csharp/geo-bounding-box-search-csharp.zip), then `dotnet run`.

## The full program

The download indexes six synthetic points, all shown in the program. The ordinary box includes a point on its northeast corner and excludes the point north of it. This example uses OneIndex 0.4.0-preview.2, a preview release.

```csharp
using OneIndex;

using var index = SearchIndex.InMemory<Place>();
index.Upsert([
    new() { Id = "inside", Location = new(45, 9) },
    new() { Id = "edge", Location = new(46, 10) },
    new() { Id = "outside", Location = new(47, 9) },
    new() { Id = "east", Location = new(0, 175) },
    new() { Id = "west", Location = new(0, -175) },
    new() { Id = "middle", Location = new(0, 0) }
]);
index.Commit();
var hits = index.Search().Where(p =>
    // north, south, west, east
    Geo.WithinBoundingBox(p.Location, 46, 44, 8, 10))
    .OrderBy(p => p.Id).Execute();
foreach (var hit in hits)
    Console.WriteLine(hit.Document.Id);

public class Place
{
    [SearchId] public string Id { get; set; } = "";
    [Geo] public GeoPoint Location { get; set; }
}
```

`dotnet run` prints:

```text
edge
inside
```

Invalid coordinates are rejected; latitude must be from −90 to 90 and longitude from −180 to 180.

## Variations

To cross the antimeridian, replace the query and print loop above with this. It selects the strip from 170° east across 180° to 170° west:

```csharp
var hits = index.Search().Where(p =>
    // north, south, west, east
    Geo.WithinBoundingBox(p.Location, 10, -10, 170, -170))
    .OrderBy(p => p.Id).Execute();
foreach (var hit in hits)
    Console.WriteLine(hit.Document.Id);
```

```text
east
west
```

## Related recipes

[Search by radius and distance](https://recursiveai.org/oneindex/cookbook/geo-radius-search-csharp/) · [Full-text search](https://recursiveai.org/oneindex/cookbook/full-text-search-csharp/)

## The short answer

Use `Geo.WithinBoundingBox` inside a OneIndex `Where` expression. Its arguments are the point property, north latitude, south latitude, west longitude and east longitude. Mark the location property `[Geo]` to enable bounding-box filters. The snippet at the top creates one point and returns one match; the downloadable program shows inside, outside and boundary cases.

## What is a bounding box in GIS?

For this search, a bounding box is a geographic rectangle bounded by two latitudes and two longitudes. Use it when the question is “which indexed places are visible in this map viewport?” It tests point membership, not how far a point is from a center. Its edges follow parallels and meridians, not great circles. A point in a corner can therefore be much farther from the center than a point halfway along an edge.

OneIndex includes all four edges. In the full example, `edge` at latitude 46 and longitude 10 matches alongside `inside`. The point at latitude 47 does not. You don’t need to enlarge the query slightly to retain a marker on a boundary: the [geo documentation](https://recursiveai.org/oneindex/docs/geo/) specifies inclusive comparisons. Points exactly on an edge match.

This task searches points already stored in the index. It does not turn an address into coordinates or calculate a route. Populate `GeoPoint` from your location data before committing the documents. A document without a point does not match a box; a document with several points matches if any one is inside.

## What are bbox coordinates?

For the LINQ call on this page, write north, south, west, east explicitly in that order. Don’t pass a map SDK’s four-element bounds array directly without checking its ordering. `GeoPoint` itself takes latitude first and longitude second. Latitude is north-positive; longitude is east-positive. The constructor rejects out-of-range values and NaN.

The example’s ordinary bounds are north 46, south 44, west 8 and east 10. Sorting by ID here only makes the printed output easy to read; it does not change membership.

For a viewport crossing the antimeridian, keep the west value greater than the east value. The variation uses west 170 and east −170. It returns `east` and `west`, but not `middle` at longitude zero. Reordering those longitudes numerically would describe the opposite, much wider region. A box from west −180 to east 180 covers every longitude.

If the UI instead promises “within this many meters,” use a radius query. A rectangle is not a substitute for that circle, even if you draw the rectangle around it. The related radius recipe adds distance ordering.
