Criteria SpatialRestrictions.IsWithinDistance NHibernate.Spatial

Has anyone implemented this, or know if it would be difficult to implement this/have any pointers?

public static SpatialRelationCriterion IsWithinDistance(string propertyName, object anotherGeometry, double distance)
{
    // TODO: Implement
    throw new NotImplementedException();
}

from NHibernate.Spatial.Criterion.SpatialRestrictions

I can use "where NHSP.Distance(PROPERTY, :point)" in hql. But want to combine this query with my existing Criteria query.

for the moment I'm creating a rough polygon, and using

criteria.Add(SpatialRestrictions.Intersects("PROPERTY", myPolygon));

EDIT Got a prototype working by overloading constructor on SpatialRelationCriterion, adding new SpatialRelation.Distance

public static SpatialRelationCriterion IsWithinDistance(string propertyName, object anotherGeometry, double distance)
        {
            return new SpatialRelationCriterion(propertyName, SpatialRelation.Distance, anotherGeometry, distance);
        }

added a new field to SpatialRelationCriterion

private readonly double? distance;

public SpatialRelationCriterion(string propertyName, SpatialRelation relation, object anotherGeometry, double distance)
            : this(propertyName, relation, anotherGeometry)
        {
            this.distance = distance;
        }

Edited ToSqlString

object secondGeometry = Parameter.Placeholder;
                if (!(this.anotherGeometry is IGeometry))
                {
                    secondGeometry = columns2[i];
                }

                if (distance.HasValue)
                {
                    builder.Add(spatialDialect.GetSpatialRelationString(columns1[i], this.relation, secondGeometry, distance.Value, true));
                }
                else
                {
                    builder.Add(spatialDialect.GetSpatialRelationString(columns1[i], this.relation, secondGeometry, true));
                }

overloaded ISpatialDialect.GetSpatialRelationString

implemented overload in MsSql2008SpatialDialect

public SqlString GetSpatialRelationString(object geometry, SpatialRelation relation, object anotherGeometry, double distance, bool criterion)
        {
            var x = new SqlStringBuilder(8)
                           .AddObject(geometry)
                           .Add(".ST")
                           .Add(relation.ToString())
                           .Add("(")
                           .AddObject(anotherGeometry)
                           .Add(")");

            if (criterion)
            {
                x.Add(" < ");
                x.AddObject(distance.ToString());
            }

            return x.ToSqlString();
        }

Not sure why AddParameter not being used?


Solution 1:

we are looking into this issue over at GitHub. Thanks for providing great insight and a possible solution. Here's a link to the issue: https://github.com/nhibernate/NHibernate.Spatial/issues/61

I will publish new NuGet packages as soon as this is fixed.