title: Criteria SpatialRestrictions.IsWithinDistance NHibernate.Spatial
body: <p>Has anyone implemented this, or know if it would be difficult to implement this/have any pointers?</p>
  
  <pre><code>public static SpatialRelationCriterion IsWithinDistance(string propertyName, object anotherGeometry, double distance)
  {
      // TODO: Implement
      throw new NotImplementedException();
  }
  </code></pre>
  
  <p>from NHibernate.Spatial.Criterion.SpatialRestrictions</p>
  
  <p>I can use "where NHSP.Distance(PROPERTY, :point)" in hql. But want to combine this query with my existing Criteria query.</p>
  
  <p>for the moment I'm creating a rough polygon, and using </p>
  
  <pre><code>criteria.Add(SpatialRestrictions.Intersects("PROPERTY", myPolygon));
  </code></pre>
  
  <p><strong><em>EDIT</em></strong>
  Got a prototype working by overloading constructor on SpatialRelationCriterion, adding new SpatialRelation.Distance</p>
  
  <pre><code>public static SpatialRelationCriterion IsWithinDistance(string propertyName, object anotherGeometry, double distance)
          {
              return new SpatialRelationCriterion(propertyName, SpatialRelation.Distance, anotherGeometry, distance);
          }
  </code></pre>
  
  <p>added a new field to SpatialRelationCriterion</p>
  
  <pre><code>private readonly double? distance;
  
  public SpatialRelationCriterion(string propertyName, SpatialRelation relation, object anotherGeometry, double distance)
              : this(propertyName, relation, anotherGeometry)
          {
              this.distance = distance;
          }
  </code></pre>
  
  <p>Edited ToSqlString</p>
  
  <pre><code>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));
                  }
  </code></pre>
  
  <p>overloaded ISpatialDialect.GetSpatialRelationString</p>
  
  <p>implemented overload in MsSql2008SpatialDialect</p>
  
  <pre><code>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(" &lt; ");
                  x.AddObject(distance.ToString());
              }
  
              return x.ToSqlString();
          }
  </code></pre>
  
  <p>Not sure why AddParameter not being used?</p>