Skip to content

Commit 675587f

Browse files
committed
Implement Relate spatial restriction criteria
1 parent 6728310 commit 675587f

File tree

5 files changed

+37
-6
lines changed

5 files changed

+37
-6
lines changed

NHibernate.Spatial.MsSql/Dialect/MsSql2012FunctionRegistration.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,8 @@ public SqlString GetSpatialRelationString(object geometry, SpatialRelation relat
450450
.Add(" THEN 1 ELSE 0 END")
451451
.Add(criterion ? " = 1" : "")
452452
.ToSqlString();
453+
case SpatialRelation.Relate:
454+
return GetSpatialRelateString(geometry, anotherGeometry, parameter, true, criterion);
453455
default:
454456
throw new ArgumentOutOfRangeException(nameof(relation), relation, "Unsupported spatial relation");
455457
}

NHibernate.Spatial.PostGis/Dialect/PostGis20Dialect.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,18 @@ public SqlString GetSpatialRelationString(object geometry, SpatialRelation relat
351351
.Add(parameter.ToString())
352352
.Add(")")
353353
.ToSqlString();
354+
case SpatialRelation.Relate:
355+
return new SqlStringBuilder()
356+
.Add(SpatialDialect.IsoPrefix)
357+
.Add(relation.ToString())
358+
.Add("(")
359+
.AddObject(geometry)
360+
.Add(", ")
361+
.AddObject(anotherGeometry)
362+
.Add(", '")
363+
.Add(parameter.ToString())
364+
.Add("')")
365+
.ToSqlString();
354366
default:
355367
throw new ArgumentOutOfRangeException(nameof(relation), relation, "Unsupported spatial relation");
356368
}

NHibernate.Spatial/Criterion/SpatialRestrictions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,7 @@ public static SpatialRelationCriterion Overlaps(string propertyName, object anot
197197
/// <returns></returns>
198198
public static SpatialRelationCriterion Relate(string propertyName, object anotherGeometry, string intersectionPatternMatrix)
199199
{
200-
// TODO: Implement
201-
throw new NotImplementedException();
200+
return new SpatialRelationCriterion(propertyName, SpatialRelation.Relate, anotherGeometry, intersectionPatternMatrix);
202201
}
203202

204203
/// <summary>

NHibernate.Spatial/SpatialRelation.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ public enum SpatialRelation
6767
/// </summary>
6868
Overlaps,
6969

70+
/// <summary>
71+
///
72+
/// </summary>
73+
Relate,
74+
7075
/// <summary>
7176
///
7277
/// </summary>
@@ -75,6 +80,6 @@ public enum SpatialRelation
7580
/// <summary>
7681
///
7782
/// </summary>
78-
Within,
83+
Within
7984
}
8085
}

Tests.NHibernate.Spatial/NtsTestCases/NtsTestCasesFixture.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,9 @@ public virtual void BooleanRelate()
396396
var results = _session.CreateCriteria(typeof(NtsTestCase))
397397
.Add(Restrictions.Eq("Operation", "Relate"))
398398
.SetProjection(Projections.ProjectionList()
399+
.Add(Projections.Property("Id"))
399400
.Add(Projections.Property("Description"))
401+
.Add(Projections.Property("Parameter"))
400402
.Add(Projections.Property("BooleanResult"))
401403
.Add(SpatialProjections.Relate("GeometryA", "GeometryB", "Parameter"))
402404
)
@@ -406,11 +408,22 @@ public virtual void BooleanRelate()
406408

407409
foreach (object[] result in results)
408410
{
409-
string description = (string) result[0];
410-
bool expected = (bool) result[1];
411-
bool operation = (bool) result[2];
411+
long id = (long) result[0];
412+
string description = (string) result[1];
413+
string parameter = (string) result[2];
414+
bool expected = (bool) result[3];
415+
bool operation = (bool) result[4];
412416

413417
Assert.AreEqual(expected, operation);
418+
419+
// Spatial restriction
420+
long rowCount = _session.CreateCriteria(typeof(NtsTestCase))
421+
.Add(Restrictions.Eq("Id", id))
422+
.Add(SpatialRestrictions.Relate("GeometryA", "GeometryB", parameter))
423+
.SetProjection(Projections.RowCountInt64())
424+
.UniqueResult<long>();
425+
426+
Assert.AreEqual(expected, Convert.ToBoolean(rowCount));
414427
}
415428
}
416429

0 commit comments

Comments
 (0)