Database Operations
SpatialDatabase
Section titled “SpatialDatabase”The entry point for all database operations. Wraps a SQLite database with spatial capabilities.
Creating a database
Section titled “Creating a database”// File-backed databaseusing var db = new SpatialDatabase("mydata.db");
// In-memory database (useful for testing)using var db = new SpatialDatabase(":memory:");Table management
Section titled “Table management”// Create a new spatial tablevar table = db.CreateTable( "cities", CoordinateSystem.Wgs84, new PropertyDefinition("name", PropertyType.Text), new PropertyDefinition("population", PropertyType.Integer), new PropertyDefinition("area", PropertyType.Real));
// Get an existing tablevar table = db.GetTable("cities");
// Check if a table existsbool exists = db.TableExists("cities");
// Drop a tabledb.DropTable("cities");Coordinate systems
Section titled “Coordinate systems”| System | Description | Distance Calculation |
|---|---|---|
CoordinateSystem.Wgs84 | GPS/geographic coordinates (longitude/latitude) | Haversine (great-circle) |
CoordinateSystem.Cartesian | Flat Euclidean coordinate system | Euclidean distance |
Property types
Section titled “Property types”| Type | Description | .NET Type |
|---|---|---|
PropertyType.Text | String values | string |
PropertyType.Integer | Whole numbers | long |
PropertyType.Real | Floating-point numbers | double |
PropertyType.Blob | Binary data | byte[] |
SpatialTable
Section titled “SpatialTable”Represents a spatial table and provides all CRUD and query operations.
Properties
Section titled “Properties”| Property | Type | Description |
|---|---|---|
Name | string | Table name |
CoordinateSystem | CoordinateSystem | WGS84 or Cartesian |
Properties | IReadOnlyList<PropertyDefinition> | User-defined property definitions |
SpatialFeature
Section titled “SpatialFeature”A container that holds a geometry and its associated properties.
// Create a feature with propertiesvar feature = new SpatialFeature(new Point(-104.99, 39.74)){ Properties = { ["name"] = "Denver", ["population"] = 715000L }};| Property | Type | Description |
|---|---|---|
Id | long | Feature ID (assigned after insert) |
Geometry | Geometry | The spatial geometry |
Properties | Dictionary<string, object?> | User-defined properties |
Insert Operations
Section titled “Insert Operations”Single insert
Section titled “Single insert”var feature = new SpatialFeature(new Point(-104.99, 39.74)){ Properties = { ["name"] = "Denver", ["population"] = 715000L }};
long id = table.Insert(feature);Bulk insert
Section titled “Bulk insert”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);Read Operations
Section titled “Read Operations”Get by ID
Section titled “Get by ID”var feature = table.GetById(1);if (feature != null){ var name = feature.Properties["name"]; var geometry = feature.Geometry;}long total = table.Count();Update Operations
Section titled “Update Operations”var feature = table.GetById(1)!;feature.Properties["population"] = 750000L;table.Update(feature);Delete Operations
Section titled “Delete Operations”bool deleted = table.Delete(1); // returns true if feature existed