Criteria SpatialRestrictions.IsWithinDistance NHibernate.Spatial

.NetNhibernateGeospatialSpatial

.Net Problem Overview


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?

.Net Solutions


Solution 1 - .Net

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.

Solution 2 - .Net

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIanView Question on Stackoverflow
Solution 1 - .NetandreravView Answer on Stackoverflow
Solution 2 - .NetilceView Answer on Stackoverflow