94

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?

perissf
  • 15,117
  • 13
  • 73
  • 116
Ian
  • 1,589
  • 11
  • 20
  • 3
    I have the same problem, and haven't found any complete patch/fix/whatever so far. Did you solve it, or did you go with the HQL variant? – Liedman Nov 07 '11 at 10:21
  • 1
    Think went with above approach, and recompilled dll to work, but was still experimental code. – Ian Feb 29 '12 at 16:49
  • 2
    @Amresh are you not satisfied with the proposed solution OP gave? – Eranga Sep 12 '12 at 08:56
  • Recompile the DLL for it to work. – cowboy911 Nov 24 '12 at 11:58
  • According to [Rich Lander of Microsoft](http://blogs.msdn.com/b/dotnet/archive/2012/11/30/asynchronous-programming-for-windows-store-apps-net-is-up-to-the-task.aspx#comments), you might stand a better chance should you raise this issue on [NHibernate forums](https://forum.hibernate.org/viewforum.php?f=25). – Annie Dec 05 '12 at 10:31

2 Answers2

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.

andrerav
  • 295
  • 4
  • 13
  • this SO Question too is about a similar issue with a different solution http://stackoverflow.com/questions/1833879/advanced-search-with-distances-using-nhibernate-and-sql-server-geography – Surya Pratap Sep 17 '16 at 04:23
0

Yes I think that Recompile the DLL is the best solution for now.

ilce
  • 898
  • 1
  • 10
  • 20