共用方式為


SQLite EF Core 提供者中的空間資料

此頁面包含搭配 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 函式。

.NET SQL
幾何。地區 區域(@geometry)
幾何。AsBinary() AsBinary(@geometry)
幾何。AsText() AsText(@geometry)
幾何。邊界 界限(@geometry)
幾何。緩衝區(距離) Buffer(@geometry, @distance)
幾何。Buffer(distance, quadrantSegments) Buffer(@geometry, , @distance@quadrantSegments)
幾何。質心 心形(@geometry)
幾何。Contains(g) Contains(@geometry, @g)
幾何。ConvexHull() ConvexHull(@geometry)
幾何。CoveredBy(g) CoveredBy(@geometry, @g)
幾何。封面(g) 封面(@geometry, @g)
幾何。十字(g) 十字(@geometry, @g)
幾何。差異(其他) Difference(@geometry, @other)
幾何。維 度 維度(@geometry)
幾何。脫節(g) 脫節(@geometry, @g)
幾何。Distance(g) Distance(@geometry, @g)
幾何。信封 信封(@geometry)
幾何。EqualsTopologyly(g) Equals(@geometry, @g)
幾何。GeometryType GeometryType(@geometry)
幾何。GetGeometryN(n) GeometryN(@geometry+ @n 1)
幾何。InteriorPoint PointOnSurface(@geometry)
幾何。交集(其他) 交集(@geometry, @other)
幾何。交集(g) 交集(@geometry, @g)
幾何。IsEmpty 伊斯皮蒂(@geometry)
幾何。IsSimple IsSimple(@geometry)
幾何。IsValid IsValid(@geometry)
幾何。IsWithinDistance(geom, distance) Distance(@geometry, @geom)< =@distance
幾何。長度 格朗思(@geometry)
幾何。NumGeometries NumGeometries(@geometry)
幾何。NumPoints NumPoints(@geometry)
幾何。OgcGeometryType CASE GeometryType(@geometry) 當 'POINT' 然後 1 ...結束
幾何。重迭(g) 重迭(@geometry, @g)
幾何。PointOnSurface PointOnSurface(@geometry)
幾何。Relate(g, intersectionPattern) Relate(@geometry, , @g@intersectionPattern)
幾何。Reverse() ST_Reverse(@geometry)
幾何。SRID SRID(@geometry)
幾何。SymmetricDifference(其他) SymDifference(@geometry, @other)
幾何。ToBinary() AsBinary(@geometry)
幾何。ToText() AsText(@geometry)
幾何。觸控(g) 觸控(@geometry, @g)
幾何。Union() 一元統一(@geometry)
幾何。聯盟(其他) GUnion(@geometry, @other)
幾何。內(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)
點。M M(@point)
點。X X(@point)
點。Y Y(@point)
點。Z Z(@point)
多邊形。外部Ring 外部Ring(@polygon)
多邊形。GetInteriorRingN(n) InteriorRingN(@polygon, @n + 1)
多邊形。NumInteriorRings NumInteriorRing(@polygon)

彙總函數

.NET SQL 已在 中新增
GeometryCombiner.Combine(group.Select(x = > x.Property)) Collect(屬性) 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(屬性) EF Core 7.0
EnvelopeCombiner.CombineAsGeometry(group.Select(x = > x.Property)) Extent(屬性) EF Core 7.0

其他資源