Recursive AI

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.

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

Terminal
dotnet add package RecursiveAI.OneIndex --prerelease

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
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, 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.

C#
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:

C#
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

Search by radius and distance · Full-text search

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 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.

All oneindex recipes