---
title: "Make a Spatial Query"
url: "https://docs.ravendb.net/6.2/client-api/session/querying/how-to-make-a-spatial-query"
version: "6.2"
language: "csharp"
---

# Make a Spatial Query

> Client language: C#. Also available for [Java](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-make-a-spatial-query.java.md), [Python](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-make-a-spatial-query.python.md), [Node.js](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-make-a-spatial-query.nodejs.md).
>
> RavenDB publishes an official agent skill for coding assistants at https://docs.ravendb.net/skills/ravendb/SKILL.md (also installable with `npx skills add ravendb/skills`). The skill describes itself as: "Read this before writing any RavenDB query, client call or /databases/ request. Covers server setup, document modelling, the Node.js, .NET and Python clients, RQL, indexes, vector search and AI agents. Code written from recall tends to run and return the wrong rows, because RQL resembles SQL and the clients take field names as plain strings."

> **Note**
>
> - Documents that contain spatial data can be queried by spatial queries that employ geographical criteria.  
>   You can use either *Dynamic spatial query* or *Spatial index query*.
>
>   - **Dynamic spatial query**  
>     Make a dynamic spatial query on a collection (described below).  
>     An auto-index will be created by the server.
>
>   - **Spatial index query**  
>     Index your documents' spatial data in a static-index (see [indexing spatial data](https://docs.ravendb.net/6.2/indexes/indexing-spatial-data.md))
>     and then make a spatial query on this index (see [query a spatial index](https://docs.ravendb.net/6.2/indexes/querying/spatial.md)).
>
> - To perform a spatial search,  
>   use the `Spatial` method, which provides a wide range of spatial functionalities.
>
> - When making a dynamic spatial query from Studio,  
>   results are also displayed on the global map. See [spatial queries map view](https://docs.ravendb.net/6.2/studio/database/queries/spatial-queries-map-view.md).
>
> - In this page:
>
>   - [Search by radius](#search-by-radius)
>   - [Search by shape](#search-by-shape)
>     - [Circle](#circle)
>     - [Polygon](#polygon)
>   - [Spatial sorting](#spatial-sorting)
>     - [Order by distance](#order-by-distance)
>     - [Order by distance descending](#order-by-distance-descending)
>     - [Sort results by rounded distance](#sort-results-by-rounded-distance)
>     - [Get resulting distance](#get-resulting-distance)
>   - [Spatial API](#spatial-api)
>     - [`Spatial`](#section)
>     - [`DynamicSpatialFieldFactory`](#section-1)
>     - [`SpatialCriteriaFactory`](#section-2)
>     - [`OrderByDistance`](#section-3)
>     - [`OrderByDistanceDescending`](#section-4)

## Search by radius

Use the `WithinRadius` method to search for all documents containing spatial data that is located  
within the specified distance from the given center point.

### Query

```csharp
// This query will return all matching employee entities
// that are located within 20 kilometers radius
// from point (47.623473 latitude, -122.3060097 longitude).

// Define a dynamic query on Employees collection
List<Employee> employeesWithinRadius = session
  .Query<Employee>()
   // Call 'Spatial' method
  .Spatial(
      // Call 'Point'
      // Pass the path to the document fields containing the spatial data
      pointField => pointField.Point(
          x => x.Address.Location.Latitude, 
          x => x.Address.Location.Longitude),
      // Set the geographical area in which to search for matching documents
      // Call 'WithinRadius', pass the radius and the center points coordinates  
      criteria => criteria.WithinRadius(20, 47.623473, -122.3060097))
  .ToList();
```

### Query\_async

```csharp
// This query will return all matching employee entities
// that are located within 20 kilometers radius
// from point (47.623473 latitude, -122.3060097 longitude).

// Define a dynamic query on Employees collection
List<Employee> employeesWithinRadius = await asyncSession
  .Query<Employee>()
   // Call 'Spatial' method
  .Spatial(
      // Call 'Point'
      // Pass the path to the document fields containing the spatial data
      pointField => pointField.Point(
          x => x.Address.Location.Latitude, 
          x => x.Address.Location.Longitude),
      // Set the geographical area in which to search for matching documents
      // Call 'WithinRadius', pass the radius and the center points coordinates  
      criteria => criteria.WithinRadius(20, 47.623473, -122.3060097))
  .ToListAsync();
```

### DocumentQuery

```csharp
// This query will return all matching employee entities
// that are located within 20 kilometers radius
// from point (47.623473 latitude, -122.3060097 longitude).

// Define a dynamic query on Employees collection
List<Employee> employeesWithinRadius = session.Advanced
  .DocumentQuery<Employee>()
  // Call 'Spatial' method
  .Spatial(
      // Call 'Point'
      // Pass the path to the document fields containing the spatial data
      pointField => pointField.Point(
          x => x.Address.Location.Latitude, 
          x => x.Address.Location.Longitude),
      // Set the geographical area in which to search for matching documents
      // Call 'WithinRadius', pass the radius and the center points coordinates  
      criteria => criteria.WithinRadius(20, 47.623473, -122.3060097))
  .ToList();
```

### RQL

```sql
// This query will return all matching employee entities
// that are located within 20 kilometers radius
// from point (47.623473 latitude, -122.3060097 longitude).

from Employees
where spatial.within(
  spatial.point(Address.Location.Latitude, Address.Location.Longitude),
  spatial.circle(20, 47.623473, -122.3060097)
)
```

## Search by shape

- Use the `RelatesToShape` method to search for all documents containing spatial data that is located  
  in the specified relation to the given shape.

- The shape is specified as either a **circle** or a **polygon** in a [WKT](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry) format.

- The relation to the shape can be one of: `Within`, `Contains`, `Disjoint`, `Intersects`.

#### Circle

##### Query

```csharp
// This query will return all matching employee entities
// that are located within 20 kilometers radius
// from point (47.623473 latitude, -122.3060097 longitude).

// Define a dynamic query on Employees collection
List<Employee> employeesWithinShape = session
  .Query<Employee>()
   // Call 'Spatial' method
  .Spatial(
      // Call 'Point'
      // Pass the path to the document fields containing the spatial data
      factory => factory.Point(
          x => x.Address.Location.Latitude,
          x => x.Address.Location.Longitude),
      // Set the geographical search criteria, call 'RelatesToShape'
      criteria => criteria.RelatesToShape(
          // Specify the WKT string. Note: longitude is written FIRST
          shapeWkt: "CIRCLE(-122.3060097 47.623473 d=20)",
          // Specify the relation between the WKT shape and the documents spatial data
          relation: SpatialRelation.Within,
          // Optional: customize radius units (default is Kilometers)
          units: SpatialUnits.Miles))  
  .ToList();
```

##### Query\_async

```csharp
// This query will return all matching employee entities
// that are located within 20 kilometers radius
// from point (47.623473 latitude, -122.3060097 longitude).

// Define a dynamic query on Employees collection
List<Employee> employeesWithinShape = await asyncSession
  .Query<Employee>()
   // Call 'Spatial' method
  .Spatial(
      // Call 'Point'
      // Pass the path to the document fields containing the spatial data
      factory => factory.Point(
          x => x.Address.Location.Latitude,
          x => x.Address.Location.Longitude),
      // Set the geographical search criteria, call 'RelatesToShape'
      criteria => criteria.RelatesToShape(
          // Specify the WKT string. Note: longitude is written FIRST
          shapeWkt: "CIRCLE(-122.3060097 47.623473 d=20)",
          // Specify the relation between the WKT shape and the documents spatial data
          relation: SpatialRelation.Within,
          // Optional: customize radius units (default is Kilometers)
          units: SpatialUnits.Miles))  
  .ToListAsync();
```

##### DocumentQuery

```csharp
// This query will return all matching employee entities
// that are located within 20 kilometers radius
// from point (47.623473 latitude, -122.3060097 longitude).

// Define a dynamic query on Employees collection
List<Employee> employeesWithinShape = session.Advanced
  .DocumentQuery<Employee>()
   // Call 'Spatial' method
  .Spatial(
      // Call 'Point'
      // Pass the path to the document fields containing the spatial data
      factory => factory.Point(
          x => x.Address.Location.Latitude,
          x => x.Address.Location.Longitude),
      // Set the geographical search criteria, call 'RelatesToShape'
      criteria => criteria.RelatesToShape(
          // Specify the WKT string. Note: longitude is written FIRST
          shapeWkt: "CIRCLE(-122.3060097 47.623473 d=20)",
          // Specify the relation between the WKT shape and the documents spatial data
          relation: SpatialRelation.Within,
          // Optional: customize radius units (default is Kilometers)
          units: SpatialUnits.Miles))  
  .ToList();
```

##### RQL

```sql
// This query will return all matching employee entities
// that are located within 20 kilometers radius
// from point (47.623473 latitude, -122.3060097 longitude).

from Employees
where spatial.within(
  spatial.point(Address.Location.Latitude, Address.Location.Longitude),
  spatial.wkt("CIRCLE(-122.3060097 47.623473 d=20)", "miles")
)
```

#### Polygon

##### Query

```csharp
// This query will return all matching company entities
// that are located within the specified polygon.

// Define a dynamic query on Companies collection
List<Company> companiesWithinShape = session
  .Query<Company>()
   // Call 'Spatial' method
  .Spatial(
      // Call 'Point'
      // Pass the path to the document fields containing the spatial data
      factory => factory.Point(
          x => x.Address.Location.Latitude,
          x => x.Address.Location.Longitude),
      // Set the geographical search criteria, call 'RelatesToShape'
      criteria => criteria.RelatesToShape(
          // Specify the WKT string
          shapeWkt: @"POLYGON ((
                         -118.6527948 32.7114894,
                         -95.8040242 37.5929338,
                         -102.8344151 53.3349629,
                         -127.5286633 48.3485664,
                         -129.4620208 38.0786067,
                         -118.7406746 32.7853769,
                         -118.6527948 32.7114894
                    ))",
          // Specify the relation between the WKT shape and the documents spatial data
          relation: SpatialRelation.Within))  
  .ToList();
```

##### Query\_async

```csharp
// This query will return all matching company entities
// that are located within the specified polygon.

// Define a dynamic query on Companies collection
List<Company> companiesWithinShape = await asyncSession
  .Query<Company>()
   // Call 'Spatial' method
  .Spatial(
      // Call 'Point'
      // Pass the path to the document fields containing the spatial data
      factory => factory.Point(
          x => x.Address.Location.Latitude,
          x => x.Address.Location.Longitude),
      // Set the geographical search criteria, call 'RelatesToShape'
      criteria => criteria.RelatesToShape(
          // Specify the WKT string
          shapeWkt: @"POLYGON ((
                         -118.6527948 32.7114894,
                         -95.8040242 37.5929338,
                         -102.8344151 53.3349629,
                         -127.5286633 48.3485664,
                         -129.4620208 38.0786067,
                         -118.7406746 32.7853769,
                         -118.6527948 32.7114894
                    ))",
          // Specify the relation between the WKT shape and the documents spatial data
          relation: SpatialRelation.Within))  
  .ToListAsync();
```

##### DocumentQuery

```csharp
// This query will return all matching company entities
// that are located within the specified polygon.

// Define a dynamic query on Companies collection
List<Company> companiesWithinShape = session.Advanced
  .DocumentQuery<Company>()
  // Call 'Spatial' method
  .Spatial(
      // Call 'Point'
      // Pass the path to the document fields containing the spatial data
      factory => factory.Point(
          x => x.Address.Location.Latitude,
          x => x.Address.Location.Longitude),
      // Set the geographical search criteria, call 'RelatesToShape'
      criteria => criteria.RelatesToShape(
          // Specify the WKT string
          shapeWkt: @"POLYGON ((
                         -118.6527948 32.7114894,
                         -95.8040242 37.5929338,
                         -102.8344151 53.3349629,
                         -127.5286633 48.3485664,
                         -129.4620208 38.0786067,
                         -118.7406746 32.7853769,
                         -118.6527948 32.7114894
                    ))",
          // Specify the relation between the WKT shape and the documents spatial data
          relation: SpatialRelation.Within))  
  .ToList();
```

##### RQL

```sql
// This query will return all matching company entities
// that are located within the specified polygon.

from companies
where spatial.within(
  spatial.point(Address.Location.Latitude, Address.Location.Longitude),
  spatial.wkt("POLYGON ((
      -118.6527948 32.7114894,
      -95.8040242 37.5929338,
      -102.8344151 53.3349629,
      -127.5286633 48.3485664,
      -129.4620208 38.0786067,
      -118.7406746 32.7853769,
      -118.6527948 32.7114894))")
)
```

> **Info:** Polygon rules {#polygon-rules}
>
> - The polygon's coordinates must be provided in counterclockwise order.
>
> - The first and last coordinates must mark the same location to form a closed region.
>
> ![WKT polygon](https://docs.ravendb.net/assets/images/spatial_1-779c509ab611ef1c93896d4b50a1790d.png)

## Spatial sorting

- Use `OrderByDistance` or `OrderByDistanceDescending` to sort the results by distance from a given point.

- By default, distance is measured by RavenDB in **kilometers**.  
  The distance can be rounded to a specific range.

#### Order by distance

##### Query

```csharp
// Return all matching employee entities located within 20 kilometers radius
// from point (47.623473 latitude, -122.3060097 longitude).

// Sort the results by their distance from a specified point,
// the closest results will be listed first.

List<Employee> employeesSortedByDistance = session
  .Query<Employee>()
   // Provide the query criteria:
  .Spatial(
      pointField => pointField.Point(
          x => x.Address.Location.Latitude,
          x => x.Address.Location.Longitude
      ),
      criteria => criteria.WithinRadius(20, 47.623473, -122.3060097))
   // Call 'OrderByDistance'
  .OrderByDistance(
      factory => factory.Point(
          // Pass the path to the document fields containing the spatial data
          x => x.Address.Location.Latitude,
          x => x.Address.Location.Longitude
      ),
      // Sort the results by their distance from this point: 
      47.623473, -122.3060097)
  .ToList();
```

##### Query\_async

```csharp
// Return all matching employee entities located within 20 kilometers radius
// from point (47.623473 latitude, -122.3060097 longitude).

// Sort the results by their distance from a specified point,
// the closest results will be listed first.

List<Employee> employeesSortedByDistance = await asyncSession
  .Query<Employee>()
   // Provide the query criteria:
  .Spatial(
      pointField => pointField.Point(
          x => x.Address.Location.Latitude,
          x => x.Address.Location.Longitude
      ),
      criteria => criteria.WithinRadius(20, 47.623473, -122.3060097))
   // Call 'OrderByDistance'
  .OrderByDistance(
      factory => factory.Point(
          // Pass the path to the document fields containing the spatial data
          x => x.Address.Location.Latitude,
          x => x.Address.Location.Longitude
      ),
      // Sort the results by their distance from this point: 
      47.623473, -122.3060097)
  .ToListAsync();
```

##### DocumentQuery

```csharp
// Return all matching employee entities located within 20 kilometers radius
// from point (47.623473 latitude, -122.3060097 longitude).

// Sort the results by their distance from a specified point,
// the closest results will be listed first.

List<Employee> employeesSortedByDistance = session.Advanced
  .DocumentQuery<Employee>()
   // Provide the query criteria:
  .Spatial(
      pointField => pointField.Point(
          x => x.Address.Location.Latitude,
          x => x.Address.Location.Longitude
      ),
      criteria => criteria.WithinRadius(20, 47.623473, -122.3060097))
   // Call 'OrderByDistance'
  .OrderByDistance(
      factory => factory.Point(
          // Pass the path to the document fields containing the spatial data
          x => x.Address.Location.Latitude,
          x => x.Address.Location.Longitude
      ),
      // Sort the results by their distance from this point: 
      47.623473, -122.3060097)
  .ToList();
```

##### RQL

```sql
// Return all matching employee entities located within 20 kilometers radius
// from point (47.623473 latitude, -122.3060097 longitude).

// Sort the results by their distance from a specified point,
// the closest results will be listed first.       

from Employees
where spatial.within(
  spatial.point(Address.Location.Latitude, Address.Location.Longitude),
  spatial.circle(20, 47.623473, -122.3060097)
)
order by spatial.distance(
  spatial.point(Address.Location.Latitude, Address.Location.Longitude),
  spatial.point(47.623473, -122.3060097)
)
```

#### Order by distance descending

##### Query

```csharp
// Return all employee entities sorted by their distance from a specified point.
// The farthest results will be listed first.

List<Employee> employeesSortedByDistanceDesc = session
  .Query<Employee>()
   // Call 'OrderByDistanceDescending'
  .OrderByDistanceDescending(
      factory => factory.Point(
          // Pass the path to the document fields containing the spatial data
          x => x.Address.Location.Latitude,
          x => x.Address.Location.Longitude
      ),
      // Sort the results by their distance (descending) from this point: 
      47.623473, -122.3060097)
  .ToList();
```

##### Query\_async

```csharp
// Return all employee entities sorted by their distance from a specified point.
// The farthest results will be listed first.

List<Employee> employeesSortedByDistanceDesc = await asyncSession
  .Query<Employee>()
   // Call 'OrderByDistanceDescending'
  .OrderByDistanceDescending(
      factory => factory.Point(
          // Pass the path to the document fields containing the spatial data
          x => x.Address.Location.Latitude,
          x => x.Address.Location.Longitude
      ),
      // Sort the results by their distance (descending) from this point: 
      47.623473, -122.3060097)
  .ToListAsync();
```

##### DocumentQuery

```csharp
// Return all employee entities sorted by their distance from a specified point.
// The farthest results will be listed first.

List<Employee> employeesSortedByDistanceDesc = session.Advanced
  .DocumentQuery<Employee>()
   // Call 'OrderByDistanceDescending'
  .OrderByDistanceDescending(
      factory => factory.Point(
          // Pass the path to the document fields containing the spatial data
          x => x.Address.Location.Latitude,
          x => x.Address.Location.Longitude
      ),
      // Sort the results by their distance (descending) from this point: 
      47.623473, -122.3060097)
  .ToList();
```

##### RQL

```sql
// Return all employee entities sorted by their distance from a specified point.
// The farthest results will be listed first.

from Employees
order by spatial.distance(
  spatial.point(Address.Location.Latitude, Address.Location.Longitude),
  spatial.point(47.623473, -122.3060097)
) desc
```

#### Sort results by rounded distance

##### Query

```csharp
// Return all employee entities.
// Results are sorted by their distance to a specified point rounded to the nearest 100 km interval.
// A secondary sort can be applied within the 100 km range, e.g. by field LastName.

List<Employee> employeesSortedByRoundedDistance = session
  .Query<Employee>()
   // Call 'OrderByDistance'
  .OrderByDistance(
      factory => factory.Point(
              // Pass the path to the document fields containing the spatial data
              x => x.Address.Location.Latitude,
              x => x.Address.Location.Longitude)
           // Round up distance to 100 km 
          .RoundTo(100),
      // Sort the results by their distance from this point: 
      47.623473, -122.3060097)
   // A secondary sort can be applied
  .ThenBy(x => x.LastName)
  .ToList();
```

##### Query\_async

```csharp
// Return all employee entities.
// Results are sorted by their distance to a specified point rounded to the nearest 100 km interval.
// A secondary sort can be applied within the 100 km range, e.g. by field LastName.

List<Employee> employeesSortedByRoundedDistance = await asyncSession
  .Query<Employee>()
   // Call 'OrderByDistance'
  .OrderByDistance(
      factory => factory.Point(
              // Pass the path to the document fields containing the spatial data
              x => x.Address.Location.Latitude,
              x => x.Address.Location.Longitude)
           // Round up distance to 100 km 
          .RoundTo(100),
      // Sort the results by their distance from this point: 
      47.623473, -122.3060097)
   // A secondary sort can be applied
  .ThenBy(x => x.LastName)
  .ToListAsync();
```

##### DocumentQuery

```csharp
// Return all employee entities.
// Results are sorted by their distance to a specified point rounded to the nearest 100 km interval.
// A secondary sort can be applied within the 100 km range, e.g. by field LastName.

List<Employee> employeesSortedByRoundedDistance = session.Advanced
  .DocumentQuery<Employee>()
   // Call 'OrderByDistance'
  .OrderByDistance(
      factory => factory.Point(
              // Pass the path to the document fields containing the spatial data
              x => x.Address.Location.Latitude,
              x => x.Address.Location.Longitude)
           // Round up distance to 100 km 
          .RoundTo(100),
      // Sort the results by their distance from this point: 
      47.623473, -122.3060097)
   // A secondary sort can be applied
  .OrderBy(x => x.LastName)
  .ToList();
```

##### RQL

```sql
// Return all employee entities.
// Results are sorted by their distance to a specified point rounded to the nearest 100 km interval.
// A secondary sort can be applied within the 100 km range, e.g. by field LastName.

from Employees
order by spatial.distance(
  spatial.point(Address.Location.Latitude, Address.Location.Longitude),
  spatial.point(47.623473, -122.3060097),
  100
), LastName
```

#### Get resulting distance

- The distance is available in the `@spatial` metadata property within each result.
- Note the following difference between the underlying search engines:
  - When using **Lucene**:  
    This metadata property is always available in the results.
  - When using **Corax**:  
    In order to enhance performance, this property is not included in the results by default.  
    To get this metadata property you must set the [Indexing.Corax.IncludeSpatialDistance](https://docs.ravendb.net/6.2/server/configuration/indexing-configuration.md#indexingcoraxincludespatialdistance) configuration value to *true*.  
    Learn about the available methods for setting an indexing configuration key in this [indexing-configuration](https://docs.ravendb.net/6.2/server/configuration/indexing-configuration.md) article.

```csharp
// Get the distance of the results:
// ================================

// Call 'GetMetadataFor', pass an entity from the resulting employees list
var metadata = session.Advanced.GetMetadataFor(employeesSortedByDistance[0]);

// The distance is available in the '@spatial' metadata property
var spatialResults = (IDictionary<string, object>)metadata[Constants.Documents.Metadata.SpatialResult];

var distance = spatialResults["Distance"];   // The distance of the entity from the queried location
var latitude = spatialResults["Latitude"];   // The entity's longitude value
var longitude = spatialResults["Longitude"]; // The entity's longitude value
```

## Spatial API

#### `Spatial`

```csharp
IRavenQueryable<T> Spatial<T>(
  Expression<Func<T, object>> path,
  Func<SpatialCriteriaFactory, SpatialCriteria> clause);

IRavenQueryable<T> Spatial<T>(
  string fieldName,
  Func<SpatialCriteriaFactory, SpatialCriteria> clause);

IRavenQueryable<T> Spatial<T>(
  Func<DynamicSpatialFieldFactory<T>, DynamicSpatialField> field,
  Func<SpatialCriteriaFactory, SpatialCriteria> clause);

IRavenQueryable<T> Spatial<T>(
  DynamicSpatialField field,
  Func<SpatialCriteriaFactory, SpatialCriteria> clause);
```

| Parameters | Type | Description |
| - | - | - |
| **path** | `Expression<Func<T, object>>` | Path to spatial field in an index<br />(when querying an index) |
| **fieldName** | `string` | Path to spatial field in an index<br />(when querying an index) |
| **field** | `Func<DynamicSpatialFieldFactory<T>, DynamicSpatialField>`<br />or<br />`DynamicSpatialField` | Factory or field that points to a document field<br />(when making a dynamic query).<br />Either `PointField` or `WktField`. |
| **clause** | `Func<SpatialCriteriaFactory, SpatialCriteria>` | Spatial criteria that will be executed on a given spatial field |

#### `DynamicSpatialFieldFactory`

```csharp
PointField Point(
  Expression<Func<T, object>> latitudePath,
  Expression<Func<T, object>> longitudePath);

WktField Wkt(Expression<Func<T, object>> wktPath);
```

| Parameters | Type | Description |
| - | - | - |
| **latitudePath** / **longitudePath** / **wktPath** | `Expression<Func<T, object>>` | Path to the field in a document containing either longitude, latitude or WKT |

#### `SpatialCriteriaFactory`

```csharp
SpatialCriteria RelatesToShape(
  string shapeWkt,
  SpatialRelation relation,
  double distErrorPercent = Constants.Documents.Indexing.Spatial.DefaultDistanceErrorPct);

SpatialCriteria RelatesToShape(
  string shapeWkt,
  SpatialRelation relation,
  SpatialUnits units,
  double distErrorPercent = Constants.Documents.Indexing.Spatial.DefaultDistanceErrorPct);

SpatialCriteria Intersects(
  string shapeWkt,
  double distErrorPercent = Constants.Documents.Indexing.Spatial.DefaultDistanceErrorPct);

SpatialCriteria Intersects(
  string shapeWkt,
  SpatialUnits units,
  double distErrorPercent = Constants.Documents.Indexing.Spatial.DefaultDistanceErrorPct);

SpatialCriteria Contains(
  string shapeWkt,
  double distErrorPercent = Constants.Documents.Indexing.Spatial.DefaultDistanceErrorPct);

SpatialCriteria Contains(
  string shapeWkt,
  SpatialUnits units,
  double distErrorPercent = Constants.Documents.Indexing.Spatial.DefaultDistanceErrorPct);

SpatialCriteria Disjoint(
  string shapeWkt,
  double distErrorPercent = Constants.Documents.Indexing.Spatial.DefaultDistanceErrorPct);

SpatialCriteria Disjoint(
  string shapeWkt,
  SpatialUnits units,
  double distErrorPercent = Constants.Documents.Indexing.Spatial.DefaultDistanceErrorPct);

SpatialCriteria Within(
  string shapeWkt,
  double distErrorPercent = Constants.Documents.Indexing.Spatial.DefaultDistanceErrorPct);

SpatialCriteria Within(
  string shapeWkt,
  SpatialUnits units,
  double distErrorPercent = Constants.Documents.Indexing.Spatial.DefaultDistanceErrorPct);

SpatialCriteria WithinRadius(
  double radius,
  double latitude,
  double longitude,
  SpatialUnits? radiusUnits = null,
  double distErrorPercent = Constants.Documents.Indexing.Spatial.DefaultDistanceErrorPct);
```

| Parameter | Type | Description |
| - | - | - |
| **shapeWkt** | `string` | [WKT](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry)-based shape used in query criteria |
| **relation** | `SpatialRelation` | Relation of the shape to the spatial data in the document/index.<br />Can be `Within`, `Contains`, `Disjoint`, `Intersects`. |
| **distErrorPercent** | `double` | Maximum distance error tolerance in percents. Default: 0.025 |
| **radius** / **latitude** / **longitude** | `double` | Used to define a radius of a circle |
| **radiusUnits** / **units** | `SpatialUnits` | Determines if circle or shape should be calculated in `Kilometers` or `Miles`.<br />By default, distances are measured in kilometers. |

#### `OrderByDistance`

```csharp
// From point
IOrderedQueryable<T> OrderByDistance<T>(
  Func<DynamicSpatialFieldFactory<T>, DynamicSpatialField> field,
  double latitude,
  double longitude);

IOrderedQueryable<T> OrderByDistance<T>(
  DynamicSpatialField field,
  double latitude,
  double longitude);

IOrderedQueryable<T> OrderByDistance<T>(
  Expression<Func<T, object>> path,
  double latitude,
  double longitude);

IOrderedQueryable<T> OrderByDistance<T>(
  string fieldName,
  double latitude,
  double longitude);

// From center of WKT shape
IOrderedQueryable<T> OrderByDistance<T>(
  Func<DynamicSpatialFieldFactory<T>, DynamicSpatialField> field,
  string shapeWkt);

IOrderedQueryable<T> OrderByDistance<T>(
  DynamicSpatialField field,
  string shapeWkt);

IOrderedQueryable<T> OrderByDistance<T>(
  Expression<Func<T, object>> path,
  string shapeWkt);

IOrderedQueryable<T> OrderByDistance<T>(
  string fieldName,
  string shapeWkt);

// Rounding
IOrderedQueryable<T> OrderByDistance<T>(
  Expression<Func<T, object>> path,
  double latitude,
  double longitude,
  double roundFactor);

IOrderedQueryable<T> OrderByDistance<T>(
  string fieldName,
  double latitude,
  double longitude,
  double roundFactor);

IOrderedQueryable<T> OrderByDistance<T>(
  Expression<Func<T, object>> path,
  string shapeWkt,
  double roundFactor);

IOrderedQueryable<T> OrderByDistance<T>(
  string fieldName,
  string shapeWkt,
  double roundFactor);
```

#### `OrderByDistanceDescending`

```csharp
// From point
IOrderedQueryable<T> OrderByDistanceDescending<T>(
  Func<DynamicSpatialFieldFactory<T>, DynamicSpatialField> field,
  double latitude,
  double longitude);

IOrderedQueryable<T> OrderByDistanceDescending<T>(
  DynamicSpatialField field,
  double latitude,
  double longitude);

IOrderedQueryable<T> OrderByDistanceDescending<T>(
  Expression<Func<T, object>> path,
  double latitude,
  double longitude);

IOrderedQueryable<T> OrderByDistanceDescending<T>(
  string fieldName,
  double latitude,
  double longitude);

// From center of WKT shape
IOrderedQueryable<T> OrderByDistanceDescending<T>(
  Func<DynamicSpatialFieldFactory<T>, DynamicSpatialField> field,
  string shapeWkt);

IOrderedQueryable<T> OrderByDistanceDescending<T>(
  DynamicSpatialField field,
  string shapeWkt);

IOrderedQueryable<T> OrderByDistanceDescending<T>(
  Expression<Func<T, object>> path,
  string shapeWkt);

IOrderedQueryable<T> OrderByDistanceDescending<T>(
  string fieldName,
  string shapeWkt);

// Rounding
IOrderedQueryable<T> OrderByDistanceDescending<T>(
  Expression<Func<T, object>> path,
  double latitude,
  double longitude,
  double roundFactor);

IOrderedQueryable<T> OrderByDistanceDescending<T>(
  string fieldName,
  double latitude,
  double longitude,
  double roundFactor);

IOrderedQueryable<T> OrderByDistanceDescending<T>(
  Expression<Func<T, object>> path,
  string shapeWkt,
  double roundFactor);

IOrderedQueryable<T> OrderByDistanceDescending<T>(
  string fieldName,
  string shapeWkt,
  double roundFactor);
```

| Parameter | Type | Description |
| - | - | - |
| **path** | `Expression<Func<T, object>>` | Path to spatial field in index<br />(when querying an index) |
| **fieldName** | `string` | Path to spatial field in index<br />(when querying an index) |
| **field** | `Func<DynamicSpatialFieldFactory<T>, DynamicSpatialField>`<br />or<br />`DynamicSpatialField` | Factory or field that points to a document field<br />(when making a dynamic query).<br />Either `PointField` or `WktField`. |
| **shapeWkt** | `string` | [WKT](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry)-based shape to be used as a point from which distance will be measured. If the shape is not a single point, then the center of the shape will be used as a reference. |
| **latitude** / **longitude** | `double` | Used to define a point from which distance will be measured |
| **roundFactor** | `double` | A distance interval in kilometers.<br />The distance from the point is rounded up to the nearest interval.<br />The results within the same interval can be sorted by a secondary order.<br />If no other order was specified, then by ascending order of document Id. |

## See also

- [How to Query](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-query.md)
- [Querying: Basics](https://docs.ravendb.net/6.2/indexes/querying/query-index.md)
- [Indexing Spatial Data](https://docs.ravendb.net/6.2/indexes/indexing-spatial-data.md)
- [Query spatial index](https://docs.ravendb.net/6.2/indexes/querying/spatial.md)
- [Spatial query view](https://docs.ravendb.net/6.2/studio/database/queries/spatial-queries-map-view.md)
