mirror of https://github.com/apache/lucene.git
refactor GeoPointInPolygonQuery to rewrite using package private delegate
This commit is contained in:
parent
9f1c4d2a0d
commit
072d44f9e8
|
@ -18,10 +18,11 @@ package org.apache.lucene.spatial.search;
|
|||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.apache.lucene.index.IndexReader;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.apache.lucene.spatial.document.GeoPointField.TermEncoding;
|
||||
import org.apache.lucene.spatial.util.GeoEncodingUtils;
|
||||
import org.apache.lucene.spatial.util.GeoRect;
|
||||
import org.apache.lucene.spatial.util.GeoRelationUtils;
|
||||
import org.apache.lucene.spatial.util.GeoUtils;
|
||||
|
||||
/** Implements a simple point in polygon query on a GeoPoint field. This is based on
|
||||
|
@ -43,11 +44,11 @@ import org.apache.lucene.spatial.util.GeoUtils;
|
|||
*
|
||||
* @lucene.experimental
|
||||
*/
|
||||
public final class GeoPointInPolygonQuery extends GeoPointInBBoxQueryImpl {
|
||||
public final class GeoPointInPolygonQuery extends GeoPointInBBoxQuery {
|
||||
// polygon position arrays - this avoids the use of any objects or
|
||||
// or geo library dependencies
|
||||
private final double[] x;
|
||||
private final double[] y;
|
||||
protected final double[] x;
|
||||
protected final double[] y;
|
||||
|
||||
public GeoPointInPolygonQuery(final String field, final double[] polyLons, final double[] polyLats) {
|
||||
this(field, TermEncoding.PREFIX, GeoUtils.polyToBBox(polyLons, polyLats), polyLons, polyLats);
|
||||
|
@ -83,53 +84,8 @@ public final class GeoPointInPolygonQuery extends GeoPointInBBoxQueryImpl {
|
|||
|
||||
/** throw exception if trying to change rewrite method */
|
||||
@Override
|
||||
public void setRewriteMethod(RewriteMethod method) {
|
||||
throw new UnsupportedOperationException("cannot change rewrite method");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CellComparator newCellComparator() {
|
||||
return new GeoPolygonCellComparator(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom {@code org.apache.lucene.spatial.search.GeoPointMultiTermQuery.CellComparator} that computes morton hash
|
||||
* ranges based on the defined edges of the provided polygon.
|
||||
*/
|
||||
private final class GeoPolygonCellComparator extends CellComparator {
|
||||
GeoPolygonCellComparator(GeoPointMultiTermQuery query) {
|
||||
super(query);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean cellCrosses(final double minLon, final double minLat, final double maxLon, final double maxLat) {
|
||||
return GeoRelationUtils.rectCrossesPolyApprox(minLon, minLat, maxLon, maxLat, x, y, GeoPointInPolygonQuery.this.minLon,
|
||||
GeoPointInPolygonQuery.this.minLat, GeoPointInPolygonQuery.this.maxLon, GeoPointInPolygonQuery.this.maxLat);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean cellWithin(final double minLon, final double minLat, final double maxLon, final double maxLat) {
|
||||
return GeoRelationUtils.rectWithinPolyApprox(minLon, minLat, maxLon, maxLat, x, y, GeoPointInPolygonQuery.this.minLon,
|
||||
GeoPointInPolygonQuery.this.minLat, GeoPointInPolygonQuery.this.maxLon, GeoPointInPolygonQuery.this.maxLat);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean cellIntersectsShape(final double minLon, final double minLat, final double maxLon, final double maxLat) {
|
||||
return cellContains(minLon, minLat, maxLon, maxLat) || cellWithin(minLon, minLat, maxLon, maxLat)
|
||||
|| cellCrosses(minLon, minLat, maxLon, maxLat);
|
||||
}
|
||||
|
||||
/**
|
||||
* The two-phase query approach. The parent
|
||||
* {@link org.apache.lucene.spatial.search.GeoPointTermsEnum#accept} method is called to match
|
||||
* encoded terms that fall within the bounding box of the polygon. Those documents that pass the initial
|
||||
* bounding box filter are then compared to the provided polygon using the
|
||||
* {@link org.apache.lucene.spatial.util.GeoRelationUtils#pointInPolygon} method.
|
||||
*/
|
||||
@Override
|
||||
protected boolean postFilter(final double lon, final double lat) {
|
||||
return GeoRelationUtils.pointInPolygon(x, y, lat, lon);
|
||||
}
|
||||
public Query rewrite(IndexReader reader) {
|
||||
return new GeoPointInPolygonQueryImpl(field, termEncoding, this, this.minLon, this.minLat, this.maxLon, this.maxLat);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -0,0 +1,103 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
package org.apache.lucene.spatial.search;
|
||||
|
||||
import org.apache.lucene.search.MultiTermQuery;
|
||||
import org.apache.lucene.spatial.document.GeoPointField.TermEncoding;
|
||||
import org.apache.lucene.spatial.util.GeoRelationUtils;
|
||||
|
||||
/** Package private implementation for the public facing GeoPointInPolygonQuery delegate class.
|
||||
*
|
||||
* @lucene.experimental
|
||||
*/
|
||||
final class GeoPointInPolygonQueryImpl extends GeoPointInBBoxQueryImpl {
|
||||
private final GeoPointInPolygonQuery polygonQuery;
|
||||
|
||||
GeoPointInPolygonQueryImpl(final String field, final TermEncoding termEncoding, final GeoPointInPolygonQuery q,
|
||||
final double minLon, final double minLat, final double maxLon, final double maxLat) {
|
||||
super(field, termEncoding, minLon, minLat, maxLon, maxLat);
|
||||
polygonQuery = q;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRewriteMethod(MultiTermQuery.RewriteMethod method) {
|
||||
throw new UnsupportedOperationException("cannot change rewrite method");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CellComparator newCellComparator() {
|
||||
return new GeoPolygonCellComparator(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom {@code org.apache.lucene.spatial.search.GeoPointMultiTermQuery.CellComparator} that computes morton hash
|
||||
* ranges based on the defined edges of the provided polygon.
|
||||
*/
|
||||
private final class GeoPolygonCellComparator extends CellComparator {
|
||||
GeoPolygonCellComparator(GeoPointMultiTermQuery query) {
|
||||
super(query);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean cellCrosses(final double minLon, final double minLat, final double maxLon, final double maxLat) {
|
||||
return GeoRelationUtils.rectCrossesPolyApprox(minLon, minLat, maxLon, maxLat, polygonQuery.x, polygonQuery.y,
|
||||
polygonQuery.minLon, polygonQuery.minLat, polygonQuery.maxLon, polygonQuery.maxLat);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean cellWithin(final double minLon, final double minLat, final double maxLon, final double maxLat) {
|
||||
return GeoRelationUtils.rectWithinPolyApprox(minLon, minLat, maxLon, maxLat, polygonQuery.x, polygonQuery.y,
|
||||
polygonQuery.minLon, polygonQuery.minLat, polygonQuery.maxLon, polygonQuery.maxLat);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean cellIntersectsShape(final double minLon, final double minLat, final double maxLon, final double maxLat) {
|
||||
return cellContains(minLon, minLat, maxLon, maxLat) || cellWithin(minLon, minLat, maxLon, maxLat)
|
||||
|| cellCrosses(minLon, minLat, maxLon, maxLat);
|
||||
}
|
||||
|
||||
/**
|
||||
* The two-phase query approach. The parent
|
||||
* {@link org.apache.lucene.spatial.search.GeoPointTermsEnum#accept} method is called to match
|
||||
* encoded terms that fall within the bounding box of the polygon. Those documents that pass the initial
|
||||
* bounding box filter are then compared to the provided polygon using the
|
||||
* {@link org.apache.lucene.spatial.util.GeoRelationUtils#pointInPolygon} method.
|
||||
*/
|
||||
@Override
|
||||
protected boolean postFilter(final double lon, final double lat) {
|
||||
return GeoRelationUtils.pointInPolygon(polygonQuery.x, polygonQuery.y, lat, lon);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
if (!super.equals(o)) return false;
|
||||
|
||||
GeoPointInPolygonQueryImpl that = (GeoPointInPolygonQueryImpl) o;
|
||||
|
||||
return !(polygonQuery != null ? !polygonQuery.equals(that.polygonQuery) : that.polygonQuery != null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = super.hashCode();
|
||||
result = 31 * result + (polygonQuery != null ? polygonQuery.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue