-
Notifications
You must be signed in to change notification settings - Fork 623
/
SpatialOperation.cs
227 lines (187 loc) · 8.28 KB
/
SpatialOperation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
using Spatial4n.Core.Shapes;
using System;
using System.Collections.Generic;
using System.Globalization;
using JCG = J2N.Collections.Generic;
namespace Lucene.Net.Spatial.Queries
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/// <summary>
/// A clause that compares a stored geometry to a supplied geometry. For more
/// explanation of each operation, consider looking at the source implementation
/// of <see cref="Evaluate(IShape, IShape)"/>.
/// <para>
/// See <a href="http://edndoc.esri.com/arcsde/9.1/general_topics/understand_spatial_relations.htm">
/// ESRIs docs on spatial relations</a>
/// </para>
/// @lucene.experimental
/// </summary>
#if FEATURE_SERIALIZABLE
[Serializable]
#endif
public abstract class SpatialOperation
{
// Private registry
private static readonly IDictionary<string, SpatialOperation> registry = new Dictionary<string, SpatialOperation>();
private static readonly IList<SpatialOperation> list = new JCG.List<SpatialOperation>();
// Geometry Operations
/// <summary>
/// Bounding box of the *indexed* shape.
/// </summary>
public static readonly SpatialOperation BBoxIntersects = new BBoxIntersectsSpatialOperation();
private sealed class BBoxIntersectsSpatialOperation : SpatialOperation
{
internal BBoxIntersectsSpatialOperation()
: base("BBoxIntersects", true, false, false)
{ }
public override bool Evaluate(IShape indexedShape, IShape queryShape)
{
return indexedShape.BoundingBox.Relate(queryShape).Intersects();
}
}
/// <summary>
/// Bounding box of the *indexed* shape.
/// </summary>
public static readonly SpatialOperation BBoxWithin = new BBoxWithinSpatialOperation();
private sealed class BBoxWithinSpatialOperation : SpatialOperation
{
internal BBoxWithinSpatialOperation()
: base("BBoxWithin", true, false, false)
{ }
public override bool Evaluate(IShape indexedShape, IShape queryShape)
{
IRectangle bbox = indexedShape.BoundingBox;
return bbox.Relate(queryShape) == SpatialRelation.WITHIN || bbox.Equals(queryShape);
}
}
public static readonly SpatialOperation Contains = new ContainsSpatialOperation();
private sealed class ContainsSpatialOperation : SpatialOperation
{
internal ContainsSpatialOperation()
: base("Contains", true, true, false)
{ }
public override bool Evaluate(IShape indexedShape, IShape queryShape)
{
return indexedShape.HasArea && indexedShape.Relate(queryShape) == SpatialRelation.CONTAINS || indexedShape.Equals(queryShape);
}
}
public static readonly SpatialOperation Intersects = new IntersectsSpatialOperation();
private sealed class IntersectsSpatialOperation : SpatialOperation
{
internal IntersectsSpatialOperation()
: base("Intersects", true, false, false)
{ }
public override bool Evaluate(IShape indexedShape, IShape queryShape)
{
return indexedShape.Relate(queryShape).Intersects();
}
}
public static readonly SpatialOperation IsEqualTo = new IsEqualToSpatialOperation();
private sealed class IsEqualToSpatialOperation : SpatialOperation
{
internal IsEqualToSpatialOperation()
: base("IsEqualTo", false, false, false)
{ }
public override bool Evaluate(IShape indexedShape, IShape queryShape)
{
return indexedShape.Equals(queryShape);
}
}
public static readonly SpatialOperation IsDisjointTo = new IsDisjointToSpatialOperation();
private sealed class IsDisjointToSpatialOperation : SpatialOperation
{
internal IsDisjointToSpatialOperation()
: base("IsDisjointTo", false, false, false)
{ }
public override bool Evaluate(IShape indexedShape, IShape queryShape)
{
return !indexedShape.Relate(queryShape).Intersects();
}
}
public static readonly SpatialOperation IsWithin = new IsWithinSpatialOperation();
private sealed class IsWithinSpatialOperation : SpatialOperation
{
internal IsWithinSpatialOperation()
: base("IsWithin", true, false, true)
{ }
public override bool Evaluate(IShape indexedShape, IShape queryShape)
{
return queryShape.HasArea && (indexedShape.Relate(queryShape) == SpatialRelation.WITHIN || indexedShape.Equals(queryShape));
}
}
public static readonly SpatialOperation Overlaps = new OverlapsSpatialOperation();
private sealed class OverlapsSpatialOperation : SpatialOperation
{
internal OverlapsSpatialOperation()
: base("Overlaps", true, false, true)
{ }
public override bool Evaluate(IShape indexedShape, IShape queryShape)
{
return queryShape.HasArea && indexedShape.Relate(queryShape).Intersects();
}
}
// Member variables
private readonly bool scoreIsMeaningful;
private readonly bool sourceNeedsArea;
private readonly bool targetNeedsArea;
private readonly string name;
protected SpatialOperation(string name, bool scoreIsMeaningful, bool sourceNeedsArea, bool targetNeedsArea)
{
this.name = name;
this.scoreIsMeaningful = scoreIsMeaningful;
this.sourceNeedsArea = sourceNeedsArea;
this.targetNeedsArea = targetNeedsArea;
registry[name] = this;
registry[CultureInfo.InvariantCulture.TextInfo.ToUpper(name)] = this;
list.Add(this);
}
public static SpatialOperation Get(string v)
{
if (!registry.TryGetValue(v, out SpatialOperation op) || op == null)
{
if (!registry.TryGetValue(CultureInfo.InvariantCulture.TextInfo.ToUpper(v), out op) || op == null)
throw new ArgumentException($"Unknown Operation: {v}", nameof(v));
}
return op;
}
public static IList<SpatialOperation> Values => list;
public static bool Is(SpatialOperation op, params SpatialOperation[] tst)
{
foreach (SpatialOperation t in tst)
{
if (op == t)
return true;
}
return false;
}
/// <summary>
/// Returns whether the relationship between <paramref name="indexedShape"/> and <paramref name="queryShape"/> is
/// satisfied by this operation.
/// </summary>
public abstract bool Evaluate(IShape indexedShape, IShape queryShape);
// ================================================= Getters / Setters =============================================
public virtual bool IsScoreMeaningful => scoreIsMeaningful;
public virtual bool SourceNeedsArea => sourceNeedsArea;
public virtual bool IsTargetNeedsArea => targetNeedsArea;
public virtual string Name => name;
public override string ToString()
{
return name;
}
}
}