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
SpatialArgs.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.Text;
20 using Spatial4n.Core.Context;
21 using Spatial4n.Core.Exceptions;
22 using Spatial4n.Core.Shapes;
23 
24 namespace Spatial4n.Core.Exceptions
25 {
26  [Serializable]
27  public class InvalidSpatialArgument : ArgumentException
28  {
29  public InvalidSpatialArgument(String reason)
30  : base(reason)
31  {
32  }
33  }
34 }
35 
36 namespace Lucene.Net.Spatial.Queries
37 {
38  public class SpatialArgs
39  {
40  public static readonly double DEFAULT_DISTERRPCT = 0.025d;
41 
42  public SpatialOperation Operation { get; set; }
43 
44  public SpatialArgs(SpatialOperation operation, Shape shape)
45  {
46  if (operation == null || shape == null)
47  throw new ArgumentException("operation and shape are required");
48  this.Operation = operation;
49  this.Shape = shape;
50  }
51 
52  /// <summary>
53  /// Computes the distance given a shape and the {@code distErrPct}. The
54  /// algorithm is the fraction of the distance from the center of the query
55  /// shape to its furthest bounding box corner.
56  /// </summary>
57  /// <param name="shape">Mandatory.</param>
58  /// <param name="distErrPct">0 to 0.5</param>
59  /// <param name="ctx">Mandatory</param>
60  /// <returns>A distance (in degrees).</returns>
61  public static double CalcDistanceFromErrPct(Shape shape, double distErrPct, SpatialContext ctx)
62  {
63  if (distErrPct < 0 || distErrPct > 0.5)
64  {
65  throw new ArgumentException("distErrPct " + distErrPct + " must be between [0 to 0.5]", "distErrPct");
66  }
67  if (distErrPct == 0 || shape is Point)
68  {
69  return 0;
70  }
71  Rectangle bbox = shape.GetBoundingBox();
72  //The diagonal distance should be the same computed from any opposite corner,
73  // and this is the longest distance that might be occurring within the shape.
74  double diagonalDist = ctx.GetDistCalc().Distance(
75  ctx.MakePoint(bbox.GetMinX(), bbox.GetMinY()), bbox.GetMaxX(), bbox.GetMaxY());
76  return diagonalDist*0.5*distErrPct;
77  }
78 
79  /// <summary>
80  /// Gets the error distance that specifies how precise the query shape is. This
81  /// looks at {@link #getDistErr()}, {@link #getDistErrPct()}, and {@code
82  /// defaultDistErrPct}.
83  /// </summary>
84  /// <param name="ctx"></param>
85  /// <param name="defaultDistErrPct">0 to 0.5</param>
86  /// <returns>>= 0</returns>
87  public double ResolveDistErr(SpatialContext ctx, double defaultDistErrPct)
88  {
89  if (DistErr != null)
90  return DistErr.Value;
91  double? distErrPct = (this.distErrPct ?? defaultDistErrPct);
92  return CalcDistanceFromErrPct(Shape, distErrPct.Value, ctx);
93  }
94 
95  /// <summary>
96  /// Check if the arguments make sense -- throw an exception if not
97  /// </summary>
98  public void Validate()
99  {
100  if (Operation.IsTargetNeedsArea() && !Shape.HasArea())
101  {
102  throw new ArgumentException(Operation + " only supports geometry with area");
103  }
104  }
105 
106  public override String ToString()
107  {
108  return SpatialArgsParser.WriteSpatialArgs(this);
109  }
110 
111  //------------------------------------------------
112  // Getters & Setters
113  //------------------------------------------------
114 
115  public Shape Shape { get; set; }
116 
117  /// <summary>
118  /// A measure of acceptable error of the shape as a fraction. This effectively
119  /// inflates the size of the shape but should not shrink it.
120  /// <p/>
121  /// The default is {@link #DEFAULT_DIST_PRECISION}
122  /// </summary>
123  /// <returns>0 to 0.5</returns>
124  public double? DistErrPct
125  {
126  get { return distErrPct; }
127  set
128  {
129  if (value != null)
130  distErrPct = value.Value;
131  }
132  }
133  private double? distErrPct;
134 
135  /// <summary>
136  /// The acceptable error of the shape. This effectively inflates the
137  /// size of the shape but should not shrink it.
138  /// </summary>
139  /// <returns>>= 0</returns>
140  public double? DistErr { get; set; }
141  }
142 }