Lucene.Net  3.0.3
Lucene.Net is a .NET port of the Java Lucene Indexing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties
SpatialStrategy.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using System;
19 using Lucene.Net.Documents;
20 using Lucene.Net.Search;
21 using Lucene.Net.Search.Function;
22 using Lucene.Net.Spatial.Queries;
23 using Lucene.Net.Spatial.Util;
24 using Spatial4n.Core.Context;
25 using Spatial4n.Core.Shapes;
26 
27 namespace Lucene.Net.Spatial
28 {
37  public abstract class SpatialStrategy
38  {
39  protected readonly SpatialContext ctx;
40  protected readonly string fieldName;
41 
47  protected SpatialStrategy(SpatialContext ctx, string fieldName)
48  {
49  if (ctx == null)
50  throw new ArgumentException("ctx is required", "ctx");
51  this.ctx = ctx;
52  if (string.IsNullOrEmpty(fieldName))
53  throw new ArgumentException("fieldName is required", "fieldName");
54  this.fieldName = fieldName;
55  }
56 
57  public SpatialContext GetSpatialContext()
58  {
59  return ctx;
60  }
61 
67  public String GetFieldName()
68  {
69  return fieldName;
70  }
71 
85  public abstract AbstractField[] CreateIndexableFields(Shape shape);
86 
87  public AbstractField CreateStoredField(Shape shape)
88  {
89  return new Field(GetFieldName(), ctx.ToString(shape), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS, Field.TermVector.NO);
90  }
91 
97  public abstract ValueSource MakeDistanceValueSource(Point queryPoint);
98 
107  public virtual ConstantScoreQuery MakeQuery(SpatialArgs args)
108  {
109  return new ConstantScoreQuery(MakeFilter(args));
110  }
111 
123  public abstract Filter MakeFilter(SpatialArgs args);
124 
135  public ValueSource MakeRecipDistanceValueSource(Shape queryShape)
136  {
137  Rectangle bbox = queryShape.GetBoundingBox();
138  double diagonalDist = ctx.GetDistCalc().Distance(
139  ctx.MakePoint(bbox.GetMinX(), bbox.GetMinY()), bbox.GetMaxX(), bbox.GetMaxY());
140  double distToEdge = diagonalDist*0.5;
141  float c = (float) distToEdge*0.1f; //one tenth
142  return new ReciprocalFloatFunction(MakeDistanceValueSource(queryShape.GetCenter()), 1f, c, c);
143  }
144 
145  public override string ToString()
146  {
147  return GetType().Name + " field:" + fieldName + " ctx=" + ctx;
148  }
149  }
150 }