Class SpatialIndex
- Namespace
- YndigoBlue.Velocity.Model
- Assembly
- YndigoBlue.Velocity.dll
Represents a spatial index that accelerates queries on geometric and geographic data columns.
public class SpatialIndex : IEquatable<SpatialIndex>, IComparable<SpatialIndex>
- Inheritance
-
SpatialIndex
- Implements
Examples
Creating a spatial index on a geometry column:
using NetTopologySuite.Geometries;
Table locationsTable = schema.CreateTable("locations");
locationsTable.AddColumn("id", DataType.Integer);
locationsTable.AddColumn("name", DataType.VarChar, size: 100);
locationsTable.AddColumn("area", DataType.Geometry);
// Create spatial index with bounding box covering San Francisco Bay Area
// Bounding box: roughly -123 to -121 longitude, 37 to 38 latitude
SpatialIndex areaIndex = locationsTable.AddSpatialIndex(
"sidx_locations_area",
"area",
xmin: -123.0,
ymin: 37.0,
xmax: -121.0,
ymax: 38.0,
smallGridSize: 0.01,
mediumGridSize: 0.1,
largeGridSize: 1.0,
SpatialIndexType.Geometry
);
manager.BuildSchema(schema);
// SQL Server: CREATE SPATIAL INDEX sidx_locations_area ON locations (area)
// USING GEOMETRY_GRID WITH (BOUNDING_BOX = (-123, 37, -121, 38))
Spatial index for geography column (GPS coordinates):
Table citiesTable = schema.CreateTable("cities");
citiesTable.AddColumn("id", DataType.Integer);
citiesTable.AddColumn("name", DataType.VarChar, size: 100);
citiesTable.AddColumn("location", DataType.Geography);
// Create spatial index for geography (WGS 84 coordinates)
// Bounding box covers the entire world
SpatialIndex locationIndex = citiesTable.AddSpatialIndex(
"sidx_cities_location",
"location",
xmin: -180.0, // Minimum longitude
ymin: -90.0, // Minimum latitude
xmax: 180.0, // Maximum longitude
ymax: 90.0, // Maximum latitude
smallGridSize: 1.0,
mediumGridSize: 10.0,
largeGridSize: 100.0,
SpatialIndexType.Geography
);
manager.BuildSchema(schema);
Using spatial index for efficient queries:
// After creating spatial index, spatial queries are automatically optimized
// Find all locations within a bounding box
Coordinate[] boundingBox = new Coordinate[]
{
new Coordinate(-122.5, 37.7),
new Coordinate(-122.5, 37.8),
new Coordinate(-122.4, 37.8),
new Coordinate(-122.4, 37.7),
new Coordinate(-122.5, 37.7)
};
Polygon searchArea = new Polygon(new LinearRing(boundingBox));
// Database will use spatial index to quickly find matching locations
Query query = new Query();
query.AddFromItem(locationsTable);
query.AddSelectItem(locationsTable["name"]);
// Use database-specific spatial functions via Statement
Statement intersectsCondition = new Statement(
$"area.STIntersects(geography::STGeomFromText('{searchArea.AsText()}', 4326)) = 1"
);
query.AddWhereClause(new Filter(intersectsCondition));
IEnumerable<Result> results = manager.Retrieve(query);
Spatial index with custom grid sizes:
// Fine-grained index for detailed spatial data
SpatialIndex detailedIndex = buildingsTable.AddSpatialIndex(
"sidx_buildings_footprint",
"footprint",
xmin: -122.45,
ymin: 37.75,
xmax: -122.35,
ymax: 37.85,
smallGridSize: 0.001, // Very fine grid (~ 100 meters)
mediumGridSize: 0.01, // Medium grid (~ 1 km)
largeGridSize: 0.1, // Coarse grid (~ 10 km)
SpatialIndexType.Geometry
);
manager.BuildSchema(schema);
Spatial index for points (city locations):
Table storesTable = schema.CreateTable("stores");
storesTable.AddColumn("id", DataType.Integer);
storesTable.AddColumn("name", DataType.VarChar, size: 100);
storesTable.AddColumn("location", DataType.Geography); // Point data
// Index for point-based queries (find nearest stores)
SpatialIndex storeIndex = storesTable.AddSpatialIndex(
"sidx_stores_location",
"location",
xmin: -180.0,
ymin: -90.0,
xmax: 180.0,
ymax: 90.0,
smallGridSize: 1.0,
mediumGridSize: 10.0,
largeGridSize: 100.0,
SpatialIndexType.Geography
);
manager.BuildSchema(schema);
// Queries for "stores within 5km" will use this index
Remarks
WARNING: Spatial indexes are not available in the Community Edition of Velocity. They require the Full Edition.
Spatial indexes optimize queries involving spatial relationships (intersects, contains, distance, etc.) on Geometry and Geography columns. They work by organizing spatial data into a hierarchical grid structure, allowing the database to quickly eliminate large regions that don't match a query's spatial criteria.
Database Support:
Spatial indexing is database-dependent:
- SQL Server: Fully supported with automatic and manual tessellation schemes
- PostgreSQL: Supported via GiST and SP-GiST indexes
- MySQL: Supported via R-tree indexes
- Oracle: Supported via R-tree indexes
- SQLite, DB2, Teradata: Limited or no native support
SpatialIndex requires configuration of a bounding box (XMin, YMin, XMax, YMax) that encompasses all spatial data in the column. For SQL Server, grid sizes control the tessellation granularity.
Spatial indexes are created on tables using AddSpatialIndex(SpatialIndex). The database creates the index when the schema is built with Engine.Manager.BuildSchema(Schema).
Properties
- Column
Gets the name of the geometry or geography column this index is built on.
- LargeGridSize
Gets the size of the largest grid cells in the spatial index tessellation.
- MediumGridSize
Gets the size of the medium grid cells in the spatial index tessellation.
- Name
Gets the name of the spatial index.
- SchemaName
Gets the name of the schema containing this spatial index's table.
- SchemaUpdate
Gets or sets whether this spatial index was created during a schema update operation.
- SmallGridSize
Gets the size of the smallest grid cells in the spatial index tessellation.
- SpatialIndexType
Gets the type of spatial index (Geometry or Geography).
- TableName
Gets the name of the table this spatial index belongs to.
- XMax
Gets the maximum X coordinate (longitude) of the bounding box.
- XMin
Gets the minimum X coordinate (longitude) of the bounding box.
- YMax
Gets the maximum Y coordinate (latitude) of the bounding box.
- YMin
Gets the minimum Y coordinate (latitude) of the bounding box.
Methods
- CompareTo(SpatialIndex)
Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
- Equals(SpatialIndex)
Indicates whether the current object is equal to another object of the same type.
- ToString()
Returns a string that represents the current object.