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
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 
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 
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 
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 
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 
140  public double? DistErr { get; set; }
141  }
142 }