Skip to content

Commit 638fd89

Browse files
author
Peet Whittaker
committed
Resolve PostGIS 3.1 issues #129
1 parent ad40cf2 commit 638fd89

File tree

3 files changed

+28
-40
lines changed

3 files changed

+28
-40
lines changed

NHibernate.Spatial.PostGis/Dialect/PostGisDialect.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,9 +445,32 @@ protected string GetSpatialCreateString(string schema, string prefix)
445445
{
446446
// Intersection aggregate function by Mark Fenbers
447447
// See http://postgis.refractions.net/pipermail/postgis-users/2005-May/008156.html
448-
string script = string.Format(
449-
"DROP AGGREGATE IF EXISTS {0}{1}(GEOMETRY);" +
450-
"CREATE AGGREGATE {0}{1}(BASETYPE=GEOMETRY, SFUNC={2}Intersection, STYPE=GEOMETRY);"
448+
// NOTE: An additional paramter was added to ST_Intersection in PostGIS 3.1
449+
// https://github.com/nhibernate/NHibernate.Spatial/issues/129
450+
string script = string.Format(@"
451+
CREATE OR REPLACE FUNCTION {0}NHSP_CreateIntersectionAggregate()
452+
RETURNS void AS $$
453+
BEGIN
454+
DROP AGGREGATE IF EXISTS {0}{1}(GEOMETRY);
455+
IF (SELECT PostGIS_Lib_Version() < '3.1') THEN
456+
CREATE AGGREGATE {0}{1}(basetype=geometry, sfunc={2}Intersection, stype=geometry);
457+
ELSE
458+
CREATE OR REPLACE FUNCTION {0}NHSP_Intersection(geometry, geometry)
459+
RETURNS geometry
460+
LANGUAGE SQL
461+
AS 'SELECT {0}{2}Intersection($1, $2)'
462+
IMMUTABLE STRICT
463+
COST 10000;
464+
CREATE AGGREGATE {0}{1}(basetype=geometry, sfunc={0}NHSP_Intersection, stype=geometry);
465+
END IF;
466+
END;
467+
$$ LANGUAGE plpgsql;
468+
469+
-- NOTE: Cast to text required to avoid following error on PostgreSQL 9.0 and lower:
470+
-- Npgsql.PostgresException : 42883: no binary output function available for type void
471+
-- https://github.com/npgsql/npgsql/issues/818
472+
-- https://www.postgresql.org/message-id/21459.1437692282%40sss.pgh.pa.us
473+
SELECT {0}NHSP_CreateIntersectionAggregate()::text;"
451474
, this.QuoteSchema(schema)
452475
, IntersectionAggregateName
453476
, prefix

Tests.NHibernate.Spatial.PostGis/PostGisConformanceItemsFixture.cs

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -66,40 +66,5 @@ from t in session.Query<DividedRoute>()
6666

6767
Assert.IsTrue(expected.EqualsTopologically(geometry));
6868
}
69-
70-
/// <summary>
71-
/// Overridden because polygon vertices are returned in a different order in PostGIS
72-
/// </summary>
73-
[Test]
74-
public override void ConformanceItemT48Hql()
75-
{
76-
string query =
77-
@"select NHSP.AsText(NHSP.Difference(np.Boundary, f.Boundary))
78-
from NamedPlace np, Forest f
79-
where np.Name = 'Ashton' and f.Name = 'Green Forest'
80-
";
81-
string result = session.CreateQuery(query)
82-
.UniqueResult<string>();
83-
84-
Geometry geometry = Wkt.Read(result);
85-
Geometry expected = Wkt.Read("POLYGON ((62 48, 84 48, 84 42, 56 34, 62 48))");
86-
87-
Assert.IsTrue(expected.EqualsExact(geometry, Tolerance));
88-
}
89-
90-
[Test]
91-
public override void ConformanceItemT48Linq()
92-
{
93-
var query =
94-
from np in session.Query<NamedPlace>()
95-
from f in session.Query<Forest>()
96-
where np.Name == "Ashton" && f.Name == "Green Forest"
97-
select np.Boundary.Difference(f.Boundary);
98-
99-
Geometry geometry = query.Single();
100-
Geometry expected = Wkt.Read("POLYGON ((62 48, 84 48, 84 42, 56 34, 62 48))");
101-
102-
Assert.IsTrue(expected.EqualsExact(geometry, Tolerance));
103-
}
10469
}
10570
}

Tests.NHibernate.Spatial/OgcSfSql11Compliance/ConformanceItemsFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2556,7 +2556,7 @@ public virtual void ConformanceItemT48Hql()
25562556
Geometry geometry = Wkt.Read(result);
25572557
Geometry expected = Wkt.Read("POLYGON( ( 56 34, 62 48, 84 48, 84 42, 56 34) )");
25582558

2559-
Assert.IsTrue(expected.EqualsExact(geometry, Tolerance));
2559+
Assert.IsTrue(expected.EqualsTopologically(geometry));
25602560
}
25612561

25622562
[Test]
@@ -2571,7 +2571,7 @@ from f in session.Query<Forest>()
25712571
Geometry geometry = query.Single();
25722572
Geometry expected = Wkt.Read("POLYGON( ( 56 34, 62 48, 84 48, 84 42, 56 34) )");
25732573

2574-
Assert.IsTrue(expected.EqualsExact(geometry, Tolerance));
2574+
Assert.IsTrue(expected.EqualsTopologically(geometry));
25752575
}
25762576

25772577
/// <summary>

0 commit comments

Comments
 (0)