19 using Lucene.Net.Search;
20 using Spatial4n.Core.Context;
21 using Spatial4n.Core.Shapes;
23 namespace Lucene.Net.Spatial.BBox
55 private readonly Rectangle queryExtent;
56 private readonly
double queryArea;
58 private readonly
double targetPower;
59 private readonly
double queryPower;
61 public AreaSimilarity(Rectangle queryExtent,
double queryPower,
double targetPower)
63 this.queryExtent = queryExtent;
64 this.queryArea = queryExtent.GetArea(null);
66 this.queryPower = queryPower;
67 this.targetPower = targetPower;
78 : this(queryExtent, 2.0, 0.5)
82 public String GetDelimiterQueryParameters()
84 return queryExtent +
";" + queryPower +
";" + targetPower;
89 if (target == null || queryArea <= 0)
93 double targetArea = target.GetArea(null);
100 double top = Math.Min(queryExtent.GetMaxY(), target.GetMaxY());
101 double bottom = Math.Max(queryExtent.GetMinY(), target.GetMinY());
102 double height = top - bottom;
106 if (queryExtent.GetCrossesDateLine())
109 if (target.GetCrossesDateLine())
111 double left = Math.Max(queryExtent.GetMinX(), target.GetMinX());
112 double right = Math.Min(queryExtent.GetMaxX(), target.GetMaxX());
113 width = right + 360.0 - left;
117 double qryWestLeft = Math.Max(queryExtent.GetMinX(), target.GetMaxX());
118 double qryWestRight = Math.Min(target.GetMaxX(), 180.0);
119 double qryWestWidth = qryWestRight - qryWestLeft;
120 if (qryWestWidth > 0)
122 width = qryWestWidth;
126 double qryEastLeft = Math.Max(target.GetMaxX(), -180.0);
127 double qryEastRight = Math.Min(queryExtent.GetMaxX(), target.GetMaxX());
128 double qryEastWidth = qryEastRight - qryEastLeft;
129 if (qryEastWidth > 0)
131 width = qryEastWidth;
139 if (target.GetCrossesDateLine())
141 double tgtWestLeft = Math.Max(queryExtent.GetMinX(), target.GetMinX());
142 double tgtWestRight = Math.Min(queryExtent.GetMaxX(), 180.0);
143 double tgtWestWidth = tgtWestRight - tgtWestLeft;
144 if (tgtWestWidth > 0)
146 width = tgtWestWidth;
150 double tgtEastLeft = Math.Max(queryExtent.GetMinX(), -180.0);
151 double tgtEastRight = Math.Min(queryExtent.GetMaxX(), target.GetMaxX());
152 double tgtEastWidth = tgtEastRight - tgtEastLeft;
153 if (tgtEastWidth > 0)
155 width = tgtEastWidth;
161 double left = Math.Max(queryExtent.GetMinX(), target.GetMinX());
162 double right = Math.Min(queryExtent.GetMaxX(), target.GetMaxX());
163 width = right - left;
169 if ((width > 0) && (height > 0))
171 double intersectionArea = width * height;
172 double queryRatio = intersectionArea / queryArea;
173 double targetRatio = intersectionArea / targetArea;
174 double queryFactor = Math.Pow(queryRatio, queryPower);
175 double targetFactor = Math.Pow(targetRatio, targetPower);
176 score = queryFactor * targetFactor * 10000.0;
191 exp.
Value = (float) score;
211 else if (exp != null)
219 public override bool Equals(
object obj)
222 if (other == null)
return false;
223 return GetDelimiterQueryParameters().
Equals(other.GetDelimiterQueryParameters());
226 public override int GetHashCode()
228 return GetDelimiterQueryParameters().GetHashCode();