このページには、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 ライブラリを使用して、この問題を回避できます。
Important
macOS および Linux では、SpatiaLite で Microsoft.EntityFrameworkCore.Sqlite または Microsoft.Data.Sqlite を使用しないでください。 どちらのパッケージも、既定で SQLitePCLRaw.bundle_e_sqlite3 を取り込みます。これは、システムにインストールされた SQLite と互換性のない、バンドルされたバージョンの SQLite です。 これを使用すると、実行時にサイレント クラッシュが発生する可能性があります。 次に示すように、システム SQLite プロバイダーと共に、代わりに Microsoft.EntityFrameworkCore.Sqlite.Core または Microsoft.Data.Sqlite.Core を使用します。
また、システムにインストールされている SQLite ライブラリには、バンドルされているバージョンとは異なる コンパイル時オプション がある場合があることにも注意してください。 これは、数学関数やフルテキスト検索などの機能の可用性に影響する可能性があります。 SQLite ビルドのオプションをすばやく調べるには、 PRAGMA compile_options; を実行し、アプリケーションが依存している機能を確認します。
Microsoft.EntityFrameworkCore.SqliteをMicrosoft.EntityFrameworkCore.Sqlite.Coreに置き換え、SQLitePCLRaw.provider.sqlite3 パッケージを参照してシステム SQLite ライブラリを使用します。
<ItemGroup>
<!-- Use Sqlite.Core with the system SQLite provider instead of Microsoft.EntityFrameworkCore.Sqlite -->
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="10.0.0" />
<PackageReference Include="SQLitePCLRaw.provider.sqlite3" Version="2.1.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.NetTopologySuite" Version="10.0.0" />
</ItemGroup>
手記
EF Core 11 (SQLitePCLRaw 3.0) 以降では、 SQLitePCLRaw.provider.sqlite3 バージョンを 3.x.xに置き換えます。 詳細については、互換性に影響を与える変更を参照してください。
SQLite を使用する前に明示的な初期化を追加します。
SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_sqlite3());
macOS では、アプリを実行する前に環境変数を設定して、Homebrew のバージョンの SQLite を使用する必要もあります。
DYLD_LIBRARY_PATH="$(brew --prefix sqlite)/lib"
SRID の構成
SpatiaLite では、列ごとに SRID を指定する必要があります。 既定の SRID は 0です。 HasSrid メソッドを使用して別の SRID を指定します。
modelBuilder.Entity<City>().Property(c => c.Location)
.HasSrid(4326);
手記
4326 は、GPS やその他の地理システムで使用される標準である WGS 84 を指します。
次元
列の既定のディメンション (または座標) は X と Y です。Z や M などの追加の序数を有効にするには、列の種類を構成します。
modelBuilder.Entity<City>().Property(c => c.Location)
.HasColumnType("POINTZ");
空間関数マッピング
次の表は、どの NetTopologySuite (NTS) メンバーがどの SQL 関数に変換されるかを示しています。
| .NET | SQL |
|---|---|
| geometry.Area | 面積(@geometry) |
| geometry.AsBinary() | AsBinary(@geometry) |
| geometry.AsText() | AsText(@geometry) |
| 幾何学。境界 | 境界(@geometry) |
| geometry.Buffer(distance) | Buffer(@geometry, @distance)) |
| ジオメトリ。Buffer(distance, クアドラントセグメント) | Buffer(@geometry, @distance, @quadrantSegments) |
| geometry.Centroid | Centroid(@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) | クロス(@geometry, @g)) |
| geometry.Difference(other) | 差異(@geometry, @other) |
| 幾何学。次元 | Dimension(@geometry) |
| geometry.Disjoint(g) | 不連続(@geometry, @g)) |
| geometry.Distance(g) | Distance(@geometry, @g) |
| ジオメトリ.エンベロープ | エンベロープ(@geometry) |
| geometry.EqualsTopologically(g) | Equals(@geometry, @g)) |
| geometry.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) |
| ジオメトリ.IsWithinDistance(geom, 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 ...終わり |
| geometry.Overlaps(g) | 重複(@geometry, @g) |
| 幾何学。PointOnSurface | PointOnSurface(@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) | 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.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 | スタートポイント(@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) |
集計関数
| .NET | SQL | 以下で追加 |
|---|---|---|
| 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)) | 範囲(プロパティ) | EF Core 7.0 |
その他のリソース
.NET