Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
SpatialPrefixTreeFactory.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 System.Collections.Generic;
20 using Spatial4n.Core.Context;
21 using Spatial4n.Core.Distance;
22 
23 namespace Lucene.Net.Spatial.Prefix.Tree
24 {
25  /// <summary>
26  /// Abstract Factory for creating {@link SpatialPrefixTree} instances with useful
27  /// defaults and passed on configurations defined in a Map.
28  /// </summary>
29  public abstract class SpatialPrefixTreeFactory
30  {
31  private const double DEFAULT_GEO_MAX_DETAIL_KM = 0.001; //1m
32  public static readonly String PREFIX_TREE = "prefixTree";
33  public static readonly String MAX_LEVELS = "maxLevels";
34  public static readonly String MAX_DIST_ERR = "maxDistErr";
35 
36  protected Dictionary<String, String> args;
37  protected SpatialContext ctx;
38  protected int? maxLevels;
39 
40  /// <summary>
41  /// The factory is looked up via "prefixTree" in args, expecting "geohash" or "quad".
42  /// If its neither of these, then "geohash" is chosen for a geo context, otherwise "quad" is chosen.
43  /// </summary>
44  /// <param name="args"></param>
45  /// <param name="ctx"></param>
46  /// <returns></returns>
47  public static SpatialPrefixTree MakeSPT(Dictionary<String, String> args, SpatialContext ctx)
48  {
49  SpatialPrefixTreeFactory instance;
50  String cname;
51  if (!args.TryGetValue(PREFIX_TREE, out cname) || cname == null)
52  cname = ctx.IsGeo() ? "geohash" : "quad";
53  if ("geohash".Equals(cname, StringComparison.InvariantCultureIgnoreCase))
54  instance = new GeohashPrefixTree.Factory();
55  else if ("quad".Equals(cname, StringComparison.InvariantCultureIgnoreCase))
56  instance = new QuadPrefixTree.Factory();
57  else
58  {
59  Type t = Type.GetType(cname);
60  instance = (SpatialPrefixTreeFactory)Activator.CreateInstance(t);
61  }
62  instance.Init(args, ctx);
63  return instance.NewSPT();
64  }
65 
66  protected void Init(Dictionary<String, String> args, SpatialContext ctx)
67  {
68  this.args = args;
69  this.ctx = ctx;
70  InitMaxLevels();
71  }
72 
73  protected void InitMaxLevels()
74  {
75  String mlStr;
76  if (args.TryGetValue(MAX_LEVELS, out mlStr) && mlStr != null)
77  {
78  maxLevels = int.Parse(mlStr);
79  return;
80  }
81 
82  double degrees;
83  if (!args.TryGetValue(MAX_DIST_ERR, out mlStr) || mlStr == null)
84  {
85  if (!ctx.IsGeo())
86  return; //let default to max
87  degrees = DistanceUtils.Dist2Degrees(DEFAULT_GEO_MAX_DETAIL_KM, DistanceUtils.EARTH_MEAN_RADIUS_KM);
88  }
89  else
90  {
91  degrees = Double.Parse(mlStr);
92  }
93  maxLevels = GetLevelForDistance(degrees);
94  }
95 
96  /* Calls {@link SpatialPrefixTree#getLevelForDistance(double)}. */
97  protected abstract int GetLevelForDistance(double degrees);
98 
99  protected abstract SpatialPrefixTree NewSPT();
100 
101  }
102 }