Skip to content

Commit

Permalink
Resolve PostGIS 3.1 issues #129
Browse files Browse the repository at this point in the history
  • Loading branch information
Peet Whittaker committed Aug 15, 2022
1 parent ad40cf2 commit 638fd89
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 40 deletions.
29 changes: 26 additions & 3 deletions NHibernate.Spatial.PostGis/Dialect/PostGisDialect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -445,9 +445,32 @@ protected string GetSpatialCreateString(string schema, string prefix)
{
// Intersection aggregate function by Mark Fenbers
// See http://postgis.refractions.net/pipermail/postgis-users/2005-May/008156.html
string script = string.Format(
"DROP AGGREGATE IF EXISTS {0}{1}(GEOMETRY);" +
"CREATE AGGREGATE {0}{1}(BASETYPE=GEOMETRY, SFUNC={2}Intersection, STYPE=GEOMETRY);"
// NOTE: An additional paramter was added to ST_Intersection in PostGIS 3.1
// https://github.com/nhibernate/NHibernate.Spatial/issues/129
string script = string.Format(@"
CREATE OR REPLACE FUNCTION {0}NHSP_CreateIntersectionAggregate()
RETURNS void AS $$
BEGIN
DROP AGGREGATE IF EXISTS {0}{1}(GEOMETRY);
IF (SELECT PostGIS_Lib_Version() < '3.1') THEN
CREATE AGGREGATE {0}{1}(basetype=geometry, sfunc={2}Intersection, stype=geometry);
ELSE
CREATE OR REPLACE FUNCTION {0}NHSP_Intersection(geometry, geometry)
RETURNS geometry
LANGUAGE SQL
AS 'SELECT {0}{2}Intersection($1, $2)'
IMMUTABLE STRICT
COST 10000;
CREATE AGGREGATE {0}{1}(basetype=geometry, sfunc={0}NHSP_Intersection, stype=geometry);
END IF;
END;
$$ LANGUAGE plpgsql;
-- NOTE: Cast to text required to avoid following error on PostgreSQL 9.0 and lower:
-- Npgsql.PostgresException : 42883: no binary output function available for type void
-- https://github.com/npgsql/npgsql/issues/818
-- https://www.postgresql.org/message-id/21459.1437692282%40sss.pgh.pa.us
SELECT {0}NHSP_CreateIntersectionAggregate()::text;"
, this.QuoteSchema(schema)
, IntersectionAggregateName
, prefix
Expand Down
35 changes: 0 additions & 35 deletions Tests.NHibernate.Spatial.PostGis/PostGisConformanceItemsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,40 +66,5 @@ public override void ConformanceItemT31Linq()

Assert.IsTrue(expected.EqualsTopologically(geometry));
}

/// <summary>
/// Overridden because polygon vertices are returned in a different order in PostGIS
/// </summary>
[Test]
public override void ConformanceItemT48Hql()
{
string query =
@"select NHSP.AsText(NHSP.Difference(np.Boundary, f.Boundary))
from NamedPlace np, Forest f
where np.Name = 'Ashton' and f.Name = 'Green Forest'
";
string result = session.CreateQuery(query)
.UniqueResult<string>();

Geometry geometry = Wkt.Read(result);
Geometry expected = Wkt.Read("POLYGON ((62 48, 84 48, 84 42, 56 34, 62 48))");

Assert.IsTrue(expected.EqualsExact(geometry, Tolerance));
}

[Test]
public override void ConformanceItemT48Linq()
{
var query =
from np in session.Query<NamedPlace>()
from f in session.Query<Forest>()
where np.Name == "Ashton" && f.Name == "Green Forest"
select np.Boundary.Difference(f.Boundary);

Geometry geometry = query.Single();
Geometry expected = Wkt.Read("POLYGON ((62 48, 84 48, 84 42, 56 34, 62 48))");

Assert.IsTrue(expected.EqualsExact(geometry, Tolerance));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2556,7 +2556,7 @@ public virtual void ConformanceItemT48Hql()
Geometry geometry = Wkt.Read(result);
Geometry expected = Wkt.Read("POLYGON( ( 56 34, 62 48, 84 48, 84 42, 56 34) )");

Assert.IsTrue(expected.EqualsExact(geometry, Tolerance));
Assert.IsTrue(expected.EqualsTopologically(geometry));
}

[Test]
Expand All @@ -2571,7 +2571,7 @@ public virtual void ConformanceItemT48Linq()
Geometry geometry = query.Single();
Geometry expected = Wkt.Read("POLYGON( ( 56 34, 62 48, 84 48, 84 42, 56 34) )");

Assert.IsTrue(expected.EqualsExact(geometry, Tolerance));
Assert.IsTrue(expected.EqualsTopologically(geometry));
}

/// <summary>
Expand Down

0 comments on commit 638fd89

Please sign in to comment.