diff --git a/docs/mapping/nts.md b/docs/mapping/nts.md index f967997ba..c3caeb9f4 100644 --- a/docs/mapping/nts.md +++ b/docs/mapping/nts.md @@ -161,9 +161,7 @@ geom.IsWithinDistance(geom2,d) | [ST_DWithin(g EF.Functions.IsWithinDistance(geom1, geom2, d, useSpheriod) | [ST_DWithin(geom1, geom2, d, useSpheriod)](https://postgis.net/docs/ST_DWithin.html) geom.IsSimple() | [ST_IsSimple(geom)](https://postgis.net/docs/ST_IsSimple.html) geom.IsValid() | [ST_IsValid(geom)](https://postgis.net/docs/ST_IsValid.html) -lineString.Length | [ST_Length(lineString)](https://postgis.net/docs/ST_Length.html) -polygon.Length | [ST_Perimeter(polygon)](https://postgis.net/docs/ST_Perimeter.html) -multiPolygon.Length | [ST_Perimeter(multiPolygon)](https://postgis.net/docs/ST_Perimeter.html) +geom.Length | [ST_Length(geom)](https://postgis.net/docs/ST_Length.html) / [ST_Perimeter(geom)](https://postgis.net/docs/ST_Perimeter.html) | [1](#length-note) EF.Functions.LineInterpolatePoint(lineString, fraction) | [ST_LineInterpolatePoint(lineString, fraction)](https://postgis.net/docs/ST_LineInterpolatePoint.html) | Added in 11.0 EF.Functions.LineMerge(geom) | [ST_LineMerge(geom)](https://postgis.net/docs/ST_LineMerge.html) | Added in 11.0 geom.Normalized | [ST_Normalize(geom)](https://postgis.net/docs/ST_Normalize.html) @@ -194,3 +192,5 @@ UnaryUnionOp.Union(geometries) | [ST_Union(geo GeometryCombiner.Combine(geometries) | [ST_Collect(geometries)](https://postgis.net/docs/ST_Collect.html) | See [Aggregate functions](translations.md#aggregate-functions). EnvelopeCombiner.CombineAsGeometry(geometries) | [ST_Extent(geometries)::geometry](https://postgis.net/docs/ST_Extent.html) | See [Aggregate functions](translations.md#aggregate-functions). ConvexHull.Create(geometries) | [ST_ConvexHull(geometries)](https://postgis.net/docs/ST_ConvexHull.html) | See [Aggregate functions](translations.md#aggregate-functions). + +1 Lines use `ST_Length`, polygons use `ST_Perimeter`, and collections may combine both; values typed as `Geometry` use `CASE`. Geography collections require PostGIS 3.6.4 or later for correct results. diff --git a/src/EFCore.PG.NTS/Query/ExpressionTranslators/Internal/NpgsqlNetTopologySuiteMemberTranslatorPlugin.cs b/src/EFCore.PG.NTS/Query/ExpressionTranslators/Internal/NpgsqlNetTopologySuiteMemberTranslatorPlugin.cs index 9d48853e8..8b8398fad 100644 --- a/src/EFCore.PG.NTS/Query/ExpressionTranslators/Internal/NpgsqlNetTopologySuiteMemberTranslatorPlugin.cs +++ b/src/EFCore.PG.NTS/Query/ExpressionTranslators/Internal/NpgsqlNetTopologySuiteMemberTranslatorPlugin.cs @@ -169,16 +169,14 @@ public NpgsqlGeometryMemberTranslator( nameof(LineString.IsRing) => Function("ST_IsRing", [instance], typeof(bool)), nameof(Geometry.IsSimple) => Function("ST_IsSimple", [instance], typeof(bool)), nameof(Geometry.IsValid) => Function("ST_IsValid", [instance], typeof(bool)), - // Geography ST_Length measures only linear collection components; MultiPolygon also inherits GeometryCollection. + // Collections can contain both lines and polygons; MultiPolygon also inherits GeometryCollection. nameof(Geometry.Length) when typeof(Polygon).IsAssignableFrom(instanceType) || typeof(MultiPolygon).IsAssignableFrom(instanceType) => Function("ST_Perimeter", [instance], typeof(double)), nameof(Geometry.Length) when instanceType == typeof(GeometryCollection) - => isGeography - ? LengthByGeometryType() - : _sqlExpressionFactory.Add( - Function("ST_Length", [instance], typeof(double)), - Function("ST_Perimeter", [instance], typeof(double))), + => _sqlExpressionFactory.Add( + Function("ST_Length", [instance], typeof(double)), + Function("ST_Perimeter", [instance], typeof(double))), nameof(Geometry.Length) when instanceType == typeof(Geometry) => LengthByGeometryType(), nameof(Geometry.Length) => Function("ST_Length", [instance], typeof(double)), @@ -221,18 +219,18 @@ SqlExpression LengthByGeometryType() if (isGeography) { // GeometryType has a geography overload and appends M for XYM values. - SqlExpression[] polygonTypes = instanceType == typeof(GeometryCollection) - ? [_sqlExpressionFactory.Constant("MULTIPOLYGON"), _sqlExpressionFactory.Constant("MULTIPOLYGONM")] - : - [ - _sqlExpressionFactory.Constant("POLYGON"), - _sqlExpressionFactory.Constant("POLYGONM"), - _sqlExpressionFactory.Constant("MULTIPOLYGON"), - _sqlExpressionFactory.Constant("MULTIPOLYGONM") - ]; + var collectionLength = _sqlExpressionFactory.Add(length, perimeter); return _sqlExpressionFactory.Case( - [new CaseWhenClause(_sqlExpressionFactory.In(Function("GeometryType", [instance], typeof(string)), polygonTypes), perimeter)], + Function("GeometryType", [instance], typeof(string)), + [ + new CaseWhenClause(_sqlExpressionFactory.Constant("POLYGON"), perimeter), + new CaseWhenClause(_sqlExpressionFactory.Constant("POLYGONM"), perimeter), + new CaseWhenClause(_sqlExpressionFactory.Constant("MULTIPOLYGON"), perimeter), + new CaseWhenClause(_sqlExpressionFactory.Constant("MULTIPOLYGONM"), perimeter), + new CaseWhenClause(_sqlExpressionFactory.Constant("GEOMETRYCOLLECTION"), collectionLength), + new CaseWhenClause(_sqlExpressionFactory.Constant("GEOMETRYCOLLECTIONM"), collectionLength) + ], length); } diff --git a/test/EFCore.PG.FunctionalTests/Query/SpatialQueryNpgsqlGeographyTest.cs b/test/EFCore.PG.FunctionalTests/Query/SpatialQueryNpgsqlGeographyTest.cs index f6ed50b28..3bd961905 100644 --- a/test/EFCore.PG.FunctionalTests/Query/SpatialQueryNpgsqlGeographyTest.cs +++ b/test/EFCore.PG.FunctionalTests/Query/SpatialQueryNpgsqlGeographyTest.cs @@ -269,8 +269,13 @@ WHERE p."Polygon" IS NOT NULL """, // """ -SELECT CASE - WHEN GeometryType(p."Polygon") IN ('POLYGON', 'POLYGONM', 'MULTIPOLYGON', 'MULTIPOLYGONM') THEN ST_Perimeter(p."Polygon") +SELECT CASE GeometryType(p."Polygon") + WHEN 'POLYGON' THEN ST_Perimeter(p."Polygon") + WHEN 'POLYGONM' THEN ST_Perimeter(p."Polygon") + WHEN 'MULTIPOLYGON' THEN ST_Perimeter(p."Polygon") + WHEN 'MULTIPOLYGONM' THEN ST_Perimeter(p."Polygon") + WHEN 'GEOMETRYCOLLECTION' THEN ST_Length(p."Polygon") + ST_Perimeter(p."Polygon") + WHEN 'GEOMETRYCOLLECTIONM' THEN ST_Length(p."Polygon") + ST_Perimeter(p."Polygon") ELSE ST_Length(p."Polygon") END FROM "PolygonEntity" AS p @@ -285,9 +290,10 @@ WHERE p."Polygon" IS NOT NULL [InlineData("MULTILINESTRING((0 0,3 4),(0 0,3 0))", 888017.40)] [InlineData("POLYGON((0 0,4 0,4 3,0 3,0 0),(1 1,2 1,2 2,1 2,1 1))", 1997106.17)] [InlineData("MULTIPOLYGON(((0 0,4 0,4 3,0 3,0 0)),((5 0,7 0,7 2,5 2,5 0)))", 2440843.40)] - [InlineData("GEOMETRYCOLLECTION(POINT(1 2),LINESTRING(0 0,3 4),POLYGON((0 0,4 0,4 3,0 3,0 0)))", 554058.92)] - [InlineData("GEOMETRYCOLLECTION(GEOMETRYCOLLECTION(LINESTRING(0 0,3 4),POLYGON((0 0,4 0,4 3,0 3,0 0))),MULTILINESTRING((0 0,3 0)))", 888017.40)] - [InlineData("GEOMETRYCOLLECTION(MULTIPOLYGON(((0 0,1 0,1 1,0 1,0 0))))", 0d)] + [InlineData("GEOMETRYCOLLECTION(POINT(1 2),LINESTRING(0 0,3 4),POLYGON((0 0,4 0,4 3,0 3,0 0)))", 2107460.18)] + [InlineData("GEOMETRYCOLLECTION(GEOMETRYCOLLECTION(LINESTRING(0 0,3 4),POLYGON((0 0,4 0,4 3,0 3,0 0))),MULTILINESTRING((0 0,3 0)))", 2441418.65)] + [InlineData("GEOMETRYCOLLECTION(MULTIPOLYGON(((0 0,1 0,1 1,0 1,0 0))))", 443770.92)] + [InlineData("GEOMETRYCOLLECTION M(LINESTRING M(0 0 1,3 4 2),POLYGON M((0 0 1,4 0 2,4 3 3,0 3 4,0 0 1)))", 2107460.18)] [InlineData("GEOMETRYCOLLECTION EMPTY", 0d)] [InlineData("POLYGON EMPTY", 0d)] [InlineData("POLYGON M((0 0 2,4 0 3,4 3 4,0 3 5,0 0 2))", 1553401.26)] @@ -317,8 +323,13 @@ public async Task Length_polymorphic_geography_types(string wkt, double? expecte $""" p0={parameter} -SELECT CASE - WHEN GeometryType(m."Geometry") IN ('POLYGON', 'POLYGONM', 'MULTIPOLYGON', 'MULTIPOLYGONM') THEN ST_Perimeter(m."Geometry") +SELECT CASE GeometryType(m."Geometry") + WHEN 'POLYGON' THEN ST_Perimeter(m."Geometry") + WHEN 'POLYGONM' THEN ST_Perimeter(m."Geometry") + WHEN 'MULTIPOLYGON' THEN ST_Perimeter(m."Geometry") + WHEN 'MULTIPOLYGONM' THEN ST_Perimeter(m."Geometry") + WHEN 'GEOMETRYCOLLECTION' THEN ST_Length(m."Geometry") + ST_Perimeter(m."Geometry") + WHEN 'GEOMETRYCOLLECTIONM' THEN ST_Length(m."Geometry") + ST_Perimeter(m."Geometry") ELSE ST_Length(m."Geometry") END FROM ( @@ -350,16 +361,13 @@ public async Task Length_multi_polygon_and_collection(bool async) var collectionQuery = context.Set() .FromSql($"SELECT ST_GeogFromText({collection}) AS \"Geometry\"") .Select(e => ((GeometryCollection)e.Geometry).Length); - Assert.InRange(async ? await collectionQuery.SingleAsync() : collectionQuery.Single(), 554057d, 554060d); + Assert.InRange(async ? await collectionQuery.SingleAsync() : collectionQuery.Single(), 2107459d, 2107462d); AssertSql( $""" p0='{multiPolygon}' -SELECT ST_Perimeter(m."Geometry") AS "MultiPolygon", CASE - WHEN GeometryType(m."Geometry") IN ('MULTIPOLYGON', 'MULTIPOLYGONM') THEN ST_Perimeter(m."Geometry") - ELSE ST_Length(m."Geometry") -END AS "Collection" +SELECT ST_Perimeter(m."Geometry") AS "MultiPolygon", ST_Length(m."Geometry") + ST_Perimeter(m."Geometry") AS "Collection" FROM ( SELECT ST_GeogFromText(@p0) AS "Geometry" ) AS m @@ -369,10 +377,7 @@ LIMIT 2 $""" p0='{collection}' -SELECT CASE - WHEN GeometryType(m."Geometry") IN ('MULTIPOLYGON', 'MULTIPOLYGONM') THEN ST_Perimeter(m."Geometry") - ELSE ST_Length(m."Geometry") -END +SELECT ST_Length(m."Geometry") + ST_Perimeter(m."Geometry") FROM ( SELECT ST_GeogFromText(@p0) AS "Geometry" ) AS m @@ -412,8 +417,13 @@ public async Task Length_geography_configured_per_property(bool async) $""" p0='{polygon}' -SELECT CASE - WHEN GeometryType(m."Polygon") IN ('POLYGON', 'POLYGONM', 'MULTIPOLYGON', 'MULTIPOLYGONM') THEN ST_Perimeter(m."Polygon") +SELECT CASE GeometryType(m."Polygon") + WHEN 'POLYGON' THEN ST_Perimeter(m."Polygon") + WHEN 'POLYGONM' THEN ST_Perimeter(m."Polygon") + WHEN 'MULTIPOLYGON' THEN ST_Perimeter(m."Polygon") + WHEN 'MULTIPOLYGONM' THEN ST_Perimeter(m."Polygon") + WHEN 'GEOMETRYCOLLECTION' THEN ST_Length(m."Polygon") + ST_Perimeter(m."Polygon") + WHEN 'GEOMETRYCOLLECTIONM' THEN ST_Length(m."Polygon") + ST_Perimeter(m."Polygon") ELSE ST_Length(m."Polygon") END FROM ( @@ -425,10 +435,7 @@ LIMIT 2 $""" p0='{multiPolygon}' -SELECT CASE - WHEN GeometryType(m."Collection") IN ('MULTIPOLYGON', 'MULTIPOLYGONM') THEN ST_Perimeter(m."Collection") - ELSE ST_Length(m."Collection") -END +SELECT ST_Length(m."Collection") + ST_Perimeter(m."Collection") FROM ( SELECT ST_GeogFromText(@p0) AS "Collection" ) AS m @@ -438,10 +445,7 @@ LIMIT 2 $""" p0='{multiPolygon}' -SELECT CASE - WHEN GeometryType(m."Geometry") IN ('MULTIPOLYGON', 'MULTIPOLYGONM') THEN ST_Perimeter(m."Geometry") - ELSE ST_Length(m."Geometry") -END +SELECT ST_Length(m."Geometry") + ST_Perimeter(m."Geometry") FROM ( SELECT ST_GeogFromText(@p0) AS "Geometry" ) AS m