Skip to content

Database Operations

The entry point for all database operations. Wraps a SQLite database with spatial capabilities.

// File-backed database
using var db = new SpatialDatabase("mydata.db");
// In-memory database (useful for testing)
using var db = new SpatialDatabase(":memory:");
// Create a new spatial table
var table = db.CreateTable(
"cities",
CoordinateSystem.Wgs84,
new PropertyDefinition("name", PropertyType.Text),
new PropertyDefinition("population", PropertyType.Integer),
new PropertyDefinition("area", PropertyType.Real)
);
// Get an existing table
var table = db.GetTable("cities");
// Check if a table exists
bool exists = db.TableExists("cities");
// Drop a table
db.DropTable("cities");
SystemDescriptionDistance Calculation
CoordinateSystem.Wgs84GPS/geographic coordinates (longitude/latitude)Haversine (great-circle)
CoordinateSystem.CartesianFlat Euclidean coordinate systemEuclidean distance
TypeDescription.NET Type
PropertyType.TextString valuesstring
PropertyType.IntegerWhole numberslong
PropertyType.RealFloating-point numbersdouble
PropertyType.BlobBinary databyte[]

Represents a spatial table and provides all CRUD and query operations.

PropertyTypeDescription
NamestringTable name
CoordinateSystemCoordinateSystemWGS84 or Cartesian
PropertiesIReadOnlyList<PropertyDefinition>User-defined property definitions

A container that holds a geometry and its associated properties.

// Create a feature with properties
var feature = new SpatialFeature(new Point(-104.99, 39.74))
{
Properties =
{
["name"] = "Denver",
["population"] = 715000L
}
};
PropertyTypeDescription
IdlongFeature ID (assigned after insert)
GeometryGeometryThe spatial geometry
PropertiesDictionary<string, object?>User-defined properties
var feature = new SpatialFeature(new Point(-104.99, 39.74))
{
Properties = { ["name"] = "Denver", ["population"] = 715000L }
};
long id = table.Insert(feature);

Wraps all inserts in a single transaction for better performance (~20% faster than individual inserts).

var features = new List<SpatialFeature>
{
new SpatialFeature(new Point(-104.99, 39.74))
{
Properties = { ["name"] = "Denver", ["population"] = 715000L }
},
new SpatialFeature(new Point(-105.27, 40.01))
{
Properties = { ["name"] = "Boulder", ["population"] = 105000L }
}
};
table.BulkInsert(features);
var feature = table.GetById(1);
if (feature != null)
{
var name = feature.Properties["name"];
var geometry = feature.Geometry;
}
long total = table.Count();
var feature = table.GetById(1)!;
feature.Properties["population"] = 750000L;
table.Update(feature);
bool deleted = table.Delete(1); // returns true if feature existed