此页面包含有关将空间数据与 SQLite 数据库提供程序结合使用的其他信息。 有关在 EF Core 中使用空间数据的一般信息,请参阅主要的 空间数据 文档。

安装 SpatiaLite

在 Windows 上,本机 mod_spatialite 库作为 NuGet 包依赖项分发。 其他平台需要单独安装。 这通常是使用软件包管理器完成的。 例如,可以在 Debian 和 Ubuntu 上使用 APT 以及在 MacOS 上使用 Homebrew。

# Debian/Ubuntu
apt-get install libsqlite3-mod-spatialite
# macOS
brew install libspatialite

遗憾的是,较新版本的 PROJ(SpatiaLite 的依赖项)与 EF 的默认 SQLitePCLRaw 捆绑包不兼容。 改为使用系统 SQLite 库便可解决此问题。

<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>

在 macOS 上,还需在运行应用之前设置环境变量,以便它使用 Homebrew 的 SQLite 版本。 在Visual Studio for Mac中,可以在“项目>项目选项>”“运行>配置>”“默认”下设置此项

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

配置 SRID

在 SpatiaLite 中,列需要为每个列指定一个 SRID。 默认 SRID 为 0。 使用 HasSrid 方法指定其他 SRID。

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

4326 指的是 WGS 84,一种在 GPS 和其他地理系统中使用的标准。

列的默认维度(或坐标)为 X 和 Y。要启用 Z 或 M 等其他坐标,请配置列类型。

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

空间函数映射

此表显示哪些了 NetTopologySuite (NTS) 成员被转换为哪些 SQL 函数。

geometry.Buffer(distance, quadrantSegments) Buffer (@geometry、 @distance@quadrantSegments) geometry.Centroid Centroid(@geometry) geometry.Contains(g) 包含 (@geometry, @g) geometry.ConvexHull() ConvexHull(@geometry) geometry.CoveredBy(g) CoveredBy (@geometry, @g) geometry.Covers(g) Cover (@geometry, @g) geometry.Crosses(g) 十字 (@geometry, @g) geometry.Difference(other) 差异 (@geometry, @other) geometry.Dimension Dimension(@geometry) geometry.Disjoint(g) disjoint (@geometry, @g) geometry.Distance(g) distance (@geometry, @g) geometry.Envelope Envelope(@geometry) geometry.EqualsTopologically(g) 等于 (@geometry, @g) geometry.GeometryType GeometryType(@geometry) geometry.GetGeometryN(n) GeometryN (@geometry, @n + 1) geometry.InteriorPoint PointOnSurface(@geometry) geometry.Intersection(other) 交集 (@geometry, @other) geometry.Intersects(g) 相交 (@geometry, @g) geometry.IsEmpty IsEmpty(@geometry) geometry.IsSimple IsSimple(@geometry) geometry.IsValid IsValid(@geometry) geometry.IsWithinDistance(geom, distance) Distance (@geometry, @geom)<= @distance geometry.Length GLength(@geometry) geometry.NumGeometries NumGeometries(@geometry) geometry.NumPoints NumPoints(@geometry) geometry.OgcGeometryType CASE GeometryType(@geometry) WHEN 'POINT' THEN 1 ... END geometry.Overlaps(g) 重叠 (@geometry, @g) geometry.PointOnSurface PointOnSurface(@geometry) geometry.Relate(g, intersectionPattern) 关联 (@geometry、 @g、 @intersectionPattern) geometry.Reverse() ST_Reverse(@geometry) geometry.SRID SRID(@geometry) geometry.SymmetricDifference(other) SymDifference (@geometry, @other) geometry.ToBinary() AsBinary(@geometry) geometry.ToText() AsText(@geometry) geometry.Touches(g) 触摸 (@geometry, @g) geometry.Union() UnaryUnion(@geometry) geometry.Union(other) GUnion (@geometry, @other) geometry.Within(g) 在 (@geometry 中, @g) geometryCollection[i] GeometryN (@geometryCollection, @i + 1) geometryCollection.Count NumGeometries(@geometryCollection) lineString.Count NumPoints(@lineString) lineString.EndPoint EndPoint(@lineString) lineString.GetPointN(n) PointN (@lineString, @n + 1) lineString.IsClosed IsClosed(@lineString) lineString.IsRing IsRing(@lineString) lineString.StartPoint StartPoint(@lineString) multiLineString.IsClosed IsClosed(@multiLineString) point.M M(@point) point.X X(@point) point.Y Y(@point) point.Z Z(@point) polygon.ExteriorRing ExteriorRing(@polygon) polygon.GetInteriorRingN(n) InteriorRingN (@polygon, @n + 1) polygon.NumInteriorRings NumInteriorRing(@polygon)