-
Notifications
You must be signed in to change notification settings - Fork 256
Bring back NetTopologySuite plugin #1000
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/EFCore.PG.NTS/Extensions/NpgsqlNetTopologySuiteServiceCollectionExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
194 changes: 96 additions & 98 deletions
194
....NTS/Query/ExpressionTranslators/Internal/NpgsqlNetTopologySuiteMemberTranslatorPlugin.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,126 +1,124 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq.Expressions; | ||
| using GeoAPI.Geometries; | ||
| using Microsoft.EntityFrameworkCore.Query.Expressions; | ||
| using Microsoft.EntityFrameworkCore.Query.ExpressionTranslators; | ||
| using System.Diagnostics; | ||
| using System.Reflection; | ||
| using Microsoft.EntityFrameworkCore.Query; | ||
| using Microsoft.EntityFrameworkCore.Query.SqlExpressions; | ||
| using Microsoft.EntityFrameworkCore.Storage; | ||
| using NetTopologySuite.Geometries; | ||
|
|
||
| // ReSharper disable once CheckNamespace | ||
| namespace Npgsql.EntityFrameworkCore.PostgreSQL.Query.ExpressionTranslators.Internal | ||
| { | ||
| public class NpgsqlNetTopologySuiteMemberTranslatorPlugin : IMemberTranslatorPlugin | ||
| { | ||
| public virtual IEnumerable<IMemberTranslator> Translators { get; } = new IMemberTranslator[] | ||
| { | ||
| new NpgsqlGeometryMemberTranslator() | ||
| }; | ||
| public NpgsqlNetTopologySuiteMemberTranslatorPlugin( | ||
| IRelationalTypeMappingSource typeMappingSource, | ||
| ISqlExpressionFactory sqlExpressionFactory) | ||
| => Translators = new IMemberTranslator[] | ||
| { | ||
| new NpgsqlGeometryMemberTranslator(sqlExpressionFactory, typeMappingSource), | ||
| }; | ||
|
|
||
| public virtual IEnumerable<IMemberTranslator> Translators { get; } | ||
| } | ||
|
|
||
| public class NpgsqlGeometryMemberTranslator : IMemberTranslator | ||
| { | ||
| static readonly CaseWhenClause[] _ogcGeometryTypeWhenThenList = new[] | ||
| readonly ISqlExpressionFactory _sqlExpressionFactory; | ||
| readonly IRelationalTypeMappingSource _typeMappingSource; | ||
| readonly CaseWhenClause[] _ogcGeometryTypeWhenThenList; | ||
|
|
||
| public NpgsqlGeometryMemberTranslator(ISqlExpressionFactory sqlExpressionFactory, IRelationalTypeMappingSource typeMappingSource) | ||
| { | ||
| new CaseWhenClause(Expression.Constant("ST_CircularString"), Expression.Constant(OgcGeometryType.CircularString)), | ||
| new CaseWhenClause(Expression.Constant("ST_CompoundCurve"), Expression.Constant(OgcGeometryType.CompoundCurve)), | ||
| new CaseWhenClause(Expression.Constant("ST_CurvePolygon"), Expression.Constant(OgcGeometryType.CurvePolygon)), | ||
| new CaseWhenClause(Expression.Constant("ST_GeometryCollection"), Expression.Constant(OgcGeometryType.GeometryCollection)), | ||
| new CaseWhenClause(Expression.Constant("ST_LineString"), Expression.Constant(OgcGeometryType.LineString)), | ||
| new CaseWhenClause(Expression.Constant("ST_MultiCurve"), Expression.Constant(OgcGeometryType.MultiCurve)), | ||
| new CaseWhenClause(Expression.Constant("ST_MultiLineString"), Expression.Constant(OgcGeometryType.MultiLineString)), | ||
| new CaseWhenClause(Expression.Constant("ST_MultiPoint"), Expression.Constant(OgcGeometryType.MultiPoint)), | ||
| new CaseWhenClause(Expression.Constant("ST_MultiPolygon"), Expression.Constant(OgcGeometryType.MultiPolygon)), | ||
| new CaseWhenClause(Expression.Constant("ST_MultiSurface"), Expression.Constant(OgcGeometryType.MultiSurface)), | ||
| new CaseWhenClause(Expression.Constant("ST_Point"), Expression.Constant(OgcGeometryType.Point)), | ||
| new CaseWhenClause(Expression.Constant("ST_Polygon"), Expression.Constant(OgcGeometryType.Polygon)), | ||
| new CaseWhenClause(Expression.Constant("ST_PolyhedralSurface"), Expression.Constant(OgcGeometryType.PolyhedralSurface)), | ||
| new CaseWhenClause(Expression.Constant("ST_Tin"), Expression.Constant(OgcGeometryType.TIN)) | ||
| }; | ||
| _sqlExpressionFactory = sqlExpressionFactory; | ||
| _typeMappingSource = typeMappingSource; | ||
|
|
||
| _ogcGeometryTypeWhenThenList = new[] | ||
| { | ||
| new CaseWhenClause(_sqlExpressionFactory.Constant("ST_CircularString"), _sqlExpressionFactory.Constant(OgcGeometryType.CircularString)), | ||
| new CaseWhenClause(_sqlExpressionFactory.Constant("ST_CompoundCurve"), _sqlExpressionFactory.Constant(OgcGeometryType.CompoundCurve)), | ||
| new CaseWhenClause(_sqlExpressionFactory.Constant("ST_CurvePolygon"), _sqlExpressionFactory.Constant(OgcGeometryType.CurvePolygon)), | ||
| new CaseWhenClause(_sqlExpressionFactory.Constant("ST_GeometryCollection"), _sqlExpressionFactory.Constant(OgcGeometryType.GeometryCollection)), | ||
| new CaseWhenClause(_sqlExpressionFactory.Constant("ST_LineString"), _sqlExpressionFactory.Constant(OgcGeometryType.LineString)), | ||
| new CaseWhenClause(_sqlExpressionFactory.Constant("ST_MultiCurve"), _sqlExpressionFactory.Constant(OgcGeometryType.MultiCurve)), | ||
| new CaseWhenClause(_sqlExpressionFactory.Constant("ST_MultiLineString"), _sqlExpressionFactory.Constant(OgcGeometryType.MultiLineString)), | ||
| new CaseWhenClause(_sqlExpressionFactory.Constant("ST_MultiPoint"), _sqlExpressionFactory.Constant(OgcGeometryType.MultiPoint)), | ||
| new CaseWhenClause(_sqlExpressionFactory.Constant("ST_MultiPolygon"), _sqlExpressionFactory.Constant(OgcGeometryType.MultiPolygon)), | ||
| new CaseWhenClause(_sqlExpressionFactory.Constant("ST_MultiSurface"), _sqlExpressionFactory.Constant(OgcGeometryType.MultiSurface)), | ||
| new CaseWhenClause(_sqlExpressionFactory.Constant("ST_Point"), _sqlExpressionFactory.Constant(OgcGeometryType.Point)), | ||
| new CaseWhenClause(_sqlExpressionFactory.Constant("ST_Polygon"), _sqlExpressionFactory.Constant(OgcGeometryType.Polygon)), | ||
| new CaseWhenClause(_sqlExpressionFactory.Constant("ST_PolyhedralSurface"), _sqlExpressionFactory.Constant(OgcGeometryType.PolyhedralSurface)), | ||
| new CaseWhenClause(_sqlExpressionFactory.Constant("ST_Tin"), _sqlExpressionFactory.Constant(OgcGeometryType.TIN)) | ||
| }; | ||
| } | ||
|
|
||
| public Expression Translate(MemberExpression e) | ||
| public SqlExpression Translate(SqlExpression instance, MemberInfo member, Type returnType) | ||
| { | ||
| var declaringType = e.Member.DeclaringType; | ||
| var declaringType = member.DeclaringType; | ||
|
|
||
| if (typeof(IPoint).IsAssignableFrom(declaringType)) | ||
| if (!typeof(Geometry).IsAssignableFrom(declaringType)) | ||
| return null; | ||
|
|
||
| var typeMapping = instance.TypeMapping; | ||
| Debug.Assert(typeMapping != null, "Instance must have typeMapping assigned."); | ||
| var storeType = instance.TypeMapping.StoreType; | ||
| var resultGeometryTypeMapping = typeof(Geometry).IsAssignableFrom(returnType) | ||
| ? _typeMappingSource.FindMapping(returnType, storeType) | ||
| : null; | ||
|
|
||
| if (typeof(Point).IsAssignableFrom(declaringType)) | ||
| { | ||
| switch (e.Member.Name) | ||
| var function = member.Name switch | ||
| { | ||
| case "X": | ||
| return new SqlFunctionExpression("ST_X", typeof(double), new[] { e.Expression }); | ||
| case "Y": | ||
| return new SqlFunctionExpression("ST_Y", typeof(double), new[] { e.Expression }); | ||
| case "Z": | ||
| return new SqlFunctionExpression("ST_Z", typeof(double), new[] { e.Expression }); | ||
| case "M": | ||
| return new SqlFunctionExpression("ST_M", typeof(double), new[] { e.Expression }); | ||
| } | ||
| nameof(Point.X) => "ST_X", | ||
| nameof(Point.Y) => "ST_Y", | ||
| nameof(Point.Z) => "ST_Z", | ||
| nameof(Point.M) => "ST_M", | ||
| _ => null | ||
| }; | ||
|
|
||
| if (function != null) | ||
| return _sqlExpressionFactory.Function(function, new[] { instance }, typeof(double)); | ||
| } | ||
|
|
||
| if (typeof(ILineString).IsAssignableFrom(declaringType)) | ||
| if (typeof(LineString).IsAssignableFrom(declaringType)) | ||
| { | ||
| switch (e.Member.Name) | ||
| { | ||
| case "Count": | ||
| return new SqlFunctionExpression("ST_NumPoints", typeof(int), new[] { e.Expression }); | ||
| } | ||
| if (member.Name == "Count") | ||
| return _sqlExpressionFactory.Function("ST_NumPoints", new[] { instance }, typeof(int)); | ||
| } | ||
|
|
||
| if (typeof(IGeometry).IsAssignableFrom(declaringType)) | ||
| return member.Name switch | ||
| { | ||
| switch (e.Member.Name) | ||
| { | ||
| case "Area": | ||
| return new SqlFunctionExpression("ST_Area", typeof(double), new[] { e.Expression }); | ||
| case "Boundary": | ||
| return new SqlFunctionExpression("ST_Boundary", typeof(IGeometry), new[] { e.Expression }); | ||
| case "Centroid": | ||
| return new SqlFunctionExpression("ST_Centroid", typeof(Point), new[] { e.Expression }); | ||
| case "Count": | ||
| return new SqlFunctionExpression("ST_NumGeometries", typeof(int), new[] { e.Expression }); | ||
| case "Dimension": | ||
| return new SqlFunctionExpression("ST_Dimension", typeof(Dimension), new[] { e.Expression }); | ||
| case "EndPoint": | ||
| return new SqlFunctionExpression("ST_EndPoint", typeof(Point), new[] { e.Expression }); | ||
| case "Envelope": | ||
| return new SqlFunctionExpression("ST_Envelope", typeof(IGeometry), new[] { e.Expression }); | ||
| case "ExteriorRing": | ||
| return new SqlFunctionExpression("ST_ExteriorRing", typeof(ILineString), new[] { e.Expression }); | ||
| case "GeometryType": | ||
| return new SqlFunctionExpression("GeometryType", typeof(string), new[] { e.Expression }); | ||
| case "IsClosed": | ||
| return new SqlFunctionExpression("ST_IsClosed", typeof(bool), new[] { e.Expression }); | ||
| case "IsEmpty": | ||
| return new SqlFunctionExpression("ST_IsEmpty", typeof(bool), new[] { e.Expression }); | ||
| case "IsRing": | ||
| return new SqlFunctionExpression("ST_IsRing", typeof(bool), new[] { e.Expression }); | ||
| case "IsSimple": | ||
| return new SqlFunctionExpression("ST_IsSimple", typeof(bool), new[] { e.Expression }); | ||
| case "IsValid": | ||
| return new SqlFunctionExpression("ST_IsValid", typeof(bool), new[] { e.Expression }); | ||
| case "Length": | ||
| return new SqlFunctionExpression("ST_Length", typeof(double), new[] { e.Expression }); | ||
| case "NumGeometries": | ||
| return new SqlFunctionExpression("ST_NumGeometries", typeof(int), new[] { e.Expression }); | ||
| case "NumInteriorRings": | ||
| return new SqlFunctionExpression("ST_NumInteriorRings", typeof(int), new[] { e.Expression }); | ||
| case "NumPoints": | ||
| return new SqlFunctionExpression("ST_NumPoints", typeof(int), new[] { e.Expression }); | ||
| case "OgcGeometryType": | ||
| return new CaseExpression( | ||
| new SqlFunctionExpression("ST_GeometryType", typeof(string), new[] { e.Expression }), | ||
| _ogcGeometryTypeWhenThenList); | ||
| case "PointOnSurface": | ||
| case "InteriorPoint": | ||
| return new SqlFunctionExpression("ST_PointOnSurface", typeof(IGeometry), new[] { e.Expression }); | ||
| case "SRID": | ||
| return new SqlFunctionExpression("ST_SRID", typeof(int), new[] { e.Expression }); | ||
| case "StartPoint": | ||
| return new SqlFunctionExpression("ST_StartPoint", typeof(IPoint), new[] { e.Expression }); | ||
| default: | ||
| return null; | ||
| } | ||
| } | ||
| nameof(Geometry.Area) => _sqlExpressionFactory.Function("ST_Area", new[] { instance }, typeof(double)), | ||
| nameof(Geometry.Boundary) => _sqlExpressionFactory.Function("ST_Boundary", new[] { instance }, typeof(Geometry), resultGeometryTypeMapping), | ||
| nameof(Geometry.Centroid) => _sqlExpressionFactory.Function("ST_Centroid", new[] { instance }, typeof(Point), resultGeometryTypeMapping), | ||
| nameof(GeometryCollection.Count) => _sqlExpressionFactory.Function("ST_NumGeometries", new[] { instance }, typeof(int)), | ||
| nameof(Geometry.Dimension) => _sqlExpressionFactory.Function("ST_Dimension", new[] { instance }, typeof(Dimension)), | ||
| nameof(LineString.EndPoint) => _sqlExpressionFactory.Function("ST_EndPoint", new[] { instance }, typeof(Point), resultGeometryTypeMapping), | ||
| nameof(Geometry.Envelope) => _sqlExpressionFactory.Function("ST_Envelope", new[] { instance }, typeof(Geometry), resultGeometryTypeMapping), | ||
| nameof(Polygon.ExteriorRing) => _sqlExpressionFactory.Function("ST_ExteriorRing", new[] { instance }, typeof(LineString), resultGeometryTypeMapping), | ||
| nameof(Geometry.GeometryType) => _sqlExpressionFactory.Function("GeometryType", new[] { instance }, typeof(string)), | ||
| nameof(LineString.IsClosed) => _sqlExpressionFactory.Function("ST_IsClosed", new[] { instance }, typeof(bool)), | ||
| nameof(Geometry.IsEmpty) => _sqlExpressionFactory.Function("ST_IsEmpty", new[] { instance }, typeof(bool)), | ||
| nameof(LineString.IsRing) => _sqlExpressionFactory.Function("ST_IsRing", new[] { instance }, typeof(bool)), | ||
| nameof(Geometry.IsSimple) => _sqlExpressionFactory.Function("ST_IsSimple", new[] { instance }, typeof(bool)), | ||
| nameof(Geometry.IsValid) => _sqlExpressionFactory.Function("ST_IsValid", new[] { instance }, typeof(bool)), | ||
| nameof(Geometry.Length) => _sqlExpressionFactory.Function("ST_Length", new[] { instance }, typeof(double)), | ||
| nameof(Geometry.NumGeometries) => _sqlExpressionFactory.Function("ST_NumGeometries", new[] { instance }, typeof(int)), | ||
| nameof(Polygon.NumInteriorRings) => _sqlExpressionFactory.Function("ST_NumInteriorRings", new[] { instance }, typeof(int)), | ||
| nameof(Geometry.NumPoints) => _sqlExpressionFactory.Function("ST_NumPoints", new[] { instance }, typeof(int)), | ||
| nameof(Geometry.PointOnSurface) => _sqlExpressionFactory.Function("ST_PointOnSurface", new[] { instance }, typeof(Geometry), resultGeometryTypeMapping), | ||
| nameof(Geometry.InteriorPoint) => _sqlExpressionFactory.Function("ST_PointOnSurface", new[] { instance }, typeof(Geometry), resultGeometryTypeMapping), | ||
| nameof(Geometry.SRID) => _sqlExpressionFactory.Function("ST_SRID", new[] { instance }, typeof(int)), | ||
| nameof(LineString.StartPoint) => _sqlExpressionFactory.Function("ST_StartPoint", new[] { instance }, typeof(Point), resultGeometryTypeMapping), | ||
|
|
||
| nameof(Geometry.OgcGeometryType) => _sqlExpressionFactory.Case( | ||
| _sqlExpressionFactory.Function("ST_GeometryType", new[] { instance }, typeof(string)), | ||
| _ogcGeometryTypeWhenThenList), | ||
|
|
||
| return null; | ||
| _ => (SqlExpression)null | ||
roji marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.