Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/mapping/nts.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) | <sup>[1](#length-note)</sup>
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)
Expand Down Expand Up @@ -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).

<sup id="length-note">1</sup> 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.
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)]
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -350,16 +361,13 @@ public async Task Length_multi_polygon_and_collection(bool async)
var collectionQuery = context.Set<PointEntity>()
.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
Expand All @@ -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
Expand Down Expand Up @@ -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 (
Expand All @@ -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
Expand All @@ -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
Expand Down
Loading