Back to Entityframework

SQLite Database Provider - Spatial Data - EF Core

entity-framework/core/providers/sqlite/spatial.md

latest7.5 KB
Original Source

Spatial Data in the SQLite EF Core Provider

This page includes additional information about using spatial data with the SQLite database provider. For general information about using spatial data in EF Core, see the main Spatial Data documentation.

Installing SpatiaLite

On Windows, the native mod_spatialite library is distributed as a NuGet package dependency. Other platforms need to install it separately. This is typically done using a software package manager. For example, you can use APT on Debian and Ubuntu; and Homebrew on MacOS.

bash
# Debian/Ubuntu
apt-get install libsqlite3-mod-spatialite

# macOS
brew install libspatialite

Unfortunately, newer versions of PROJ (a dependency of SpatiaLite) are incompatible with EF's default SQLitePCLRaw bundle. You can work around this by using the system SQLite library instead.

xml
<ItemGroup>
  <!-- Use bundle_sqlite3 instead with SpatiaLite on macOS and Linux -->
  <!--<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.0" />-->
  <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="3.1.0" />
  <PackageReference Include="SQLitePCLRaw.bundle_sqlite3" Version="2.0.4" />

  <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.NetTopologySuite" Version="3.1.0" />
</ItemGroup>

On macOS, you'll also need set an environment variable before running your app so it uses Homebrew's version of SQLite. In Visual Studio for Mac, you can set this under Project > Project Options > Run > Configurations > Default

bash
DYLD_LIBRARY_PATH=/usr/local/opt/sqlite/lib

Configuring SRID

In SpatiaLite, columns need to specify an SRID per column. The default SRID is 0. Specify a different SRID using the HasSrid method.

csharp
modelBuilder.Entity<City>().Property(c => c.Location)
    .HasSrid(4326);

[!NOTE] 4326 refers to WGS 84, a standard used in GPS and other geographic systems.

Dimension

The default dimension (or ordinates) of a column is X and Y. To enable additional ordinates like Z or M, configure the column type.

csharp
modelBuilder.Entity<City>().Property(c => c.Location)
    .HasColumnType("POINTZ");

Spatial function mappings

This table shows which NetTopologySuite (NTS) members are translated into which SQL functions.

.NETSQL
geometry.AreaArea(@geometry)
geometry.AsBinary()AsBinary(@geometry)
geometry.AsText()AsText(@geometry)
geometry.BoundaryBoundary(@geometry)
geometry.Buffer(distance)Buffer(@geometry, @distance)
geometry.Buffer(distance, quadrantSegments)Buffer(@geometry, @distance, @quadrantSegments)
geometry.CentroidCentroid(@geometry)
geometry.Contains(g)Contains(@geometry, @g)
geometry.ConvexHull()ConvexHull(@geometry)
geometry.CoveredBy(g)CoveredBy(@geometry, @g)
geometry.Covers(g)Covers(@geometry, @g)
geometry.Crosses(g)Crosses(@geometry, @g)
geometry.Difference(other)Difference(@geometry, @other)
geometry.DimensionDimension(@geometry)
geometry.Disjoint(g)Disjoint(@geometry, @g)
geometry.Distance(g)Distance(@geometry, @g)
geometry.EnvelopeEnvelope(@geometry)
geometry.EqualsTopologically(g)Equals(@geometry, @g)
geometry.GeometryTypeGeometryType(@geometry)
geometry.GetGeometryN(n)GeometryN(@geometry, @n + 1)
geometry.InteriorPointPointOnSurface(@geometry)
geometry.Intersection(other)Intersection(@geometry, @other)
geometry.Intersects(g)Intersects(@geometry, @g)
geometry.IsEmptyIsEmpty(@geometry)
geometry.IsSimpleIsSimple(@geometry)
geometry.IsValidIsValid(@geometry)
geometry.IsWithinDistance(geom, distance)Distance(@geometry, @geom) <= @distance
geometry.LengthGLength(@geometry)
geometry.NumGeometriesNumGeometries(@geometry)
geometry.NumPointsNumPoints(@geometry)
geometry.OgcGeometryTypeCASE GeometryType(@geometry) WHEN 'POINT' THEN 1 ... END
geometry.Overlaps(g)Overlaps(@geometry, @g)
geometry.PointOnSurfacePointOnSurface(@geometry)
geometry.Relate(g, intersectionPattern)Relate(@geometry, @g, @intersectionPattern)
geometry.Reverse()ST_Reverse(@geometry)
geometry.SRIDSRID(@geometry)
geometry.SymmetricDifference(other)SymDifference(@geometry, @other)
geometry.ToBinary()AsBinary(@geometry)
geometry.ToText()AsText(@geometry)
geometry.Touches(g)Touches(@geometry, @g)
geometry.Union()UnaryUnion(@geometry)
geometry.Union(other)GUnion(@geometry, @other)
geometry.Within(g)Within(@geometry, @g)
geometryCollection[i]GeometryN(@geometryCollection, @i + 1)
geometryCollection.CountNumGeometries(@geometryCollection)
lineString.CountNumPoints(@lineString)
lineString.EndPointEndPoint(@lineString)
lineString.GetPointN(n)PointN(@lineString, @n + 1)
lineString.IsClosedIsClosed(@lineString)
lineString.IsRingIsRing(@lineString)
lineString.StartPointStartPoint(@lineString)
multiLineString.IsClosedIsClosed(@multiLineString)
point.MM(@point)
point.XX(@point)
point.YY(@point)
point.ZZ(@point)
polygon.ExteriorRingExteriorRing(@polygon)
polygon.GetInteriorRingN(n)InteriorRingN(@polygon, @n + 1)
polygon.NumInteriorRingsNumInteriorRing(@polygon)

Aggregate functions

.NETSQLAdded in
GeometryCombiner.Combine(group.Select(x => x.Property))Collect(Property)EF Core 7.0
ConvexHull.Create(group.Select(x => x.Property))ConvexHull(Collect(Property))EF Core 7.0
UnaryUnionOp.Union(group.Select(x => x.Property))GUnion(Property)EF Core 7.0
EnvelopeCombiner.CombineAsGeometry(group.Select(x => x.Property))Extent(Property)EF Core 7.0

Additional resources