Minor LatLonShape cleanup

Reinstate TestLatLonPointShapeQueries.testRandomTiny, remove unused
area calculation of Tessellated triangles. Javadoc cleanup.
This commit is contained in:
Nicholas Knize 2018-11-02 11:06:22 -05:00
parent bbb9f726e0
commit 2b8f577c22
3 changed files with 28 additions and 11 deletions

View File

@ -84,7 +84,6 @@ public class LatLonShape {
// create "flat" triangles
double aLat, bLat, aLon, bLon, temp;
double size;
for (int i = 0, j = 1; j < numPoints; ++i, ++j) {
aLat = line.getLat(i);
aLon = line.getLon(i);
@ -107,15 +106,14 @@ public class LatLonShape {
bLon = temp;
}
}
size = StrictMath.sqrt(StrictMath.pow(aLat - bLat, 2d) + StrictMath.pow(aLon - bLon, 2d));
fields.add(new LatLonTriangle(fieldName, aLat, aLon, bLat, bLon, aLat, aLon, size));
fields.add(new LatLonTriangle(fieldName, aLat, aLon, bLat, bLon, aLat, aLon));
}
return fields.toArray(new Field[fields.size()]);
}
/** create indexable fields for point geometry */
public static Field[] createIndexableFields(String fieldName, double lat, double lon) {
return new Field[] {new LatLonTriangle(fieldName, lat, lon, lat, lon, lat, lon, 0d)};
return new Field[] {new LatLonTriangle(fieldName, lat, lon, lat, lon, lat, lon)};
}
/** create a query to find all polygons that intersect a defined bounding box
@ -126,6 +124,9 @@ public class LatLonShape {
return new LatLonShapeBoundingBoxQuery(field, queryRelation, minLatitude, maxLatitude, minLongitude, maxLongitude);
}
/** create a query to find all polygons that intersect a provided polygon (or array of polygons)
* note: does not support dateline crossing
**/
public static Query newPolygonQuery(String field, QueryRelation queryRelation, Polygon... polygons) {
return new LatLonShapePolygonQuery(field, queryRelation, polygons);
}
@ -135,7 +136,7 @@ public class LatLonShape {
*/
private static class LatLonTriangle extends Field {
LatLonTriangle(String name, double aLat, double aLon, double bLat, double bLon, double cLat, double cLon, double size) {
LatLonTriangle(String name, double aLat, double aLon, double bLat, double bLon, double cLat, double cLon) {
super(name, TYPE);
setTriangleValue(encodeLongitude(aLon), encodeLatitude(aLat), encodeLongitude(bLon), encodeLatitude(bLat), encodeLongitude(cLon), encodeLatitude(cLat));
}
@ -178,6 +179,9 @@ public class LatLonShape {
}
}
/** encodes bounding box value of triangle. Note the encoding uses 64bit encoding, but the bounding box only needs
* 32bits, so we pad w/ zeros to take advantage of prefix compression.
*/
public static void encodeTriangleBoxVal(int encodedVal, byte[] bytes, int offset) {
long val = (long)(encodedVal ^ 0x80000000);
val &= 0x00000000FFFFFFFFL;
@ -185,6 +189,7 @@ public class LatLonShape {
NumericUtils.longToSortableBytes(val, bytes, offset);
}
/** counterpart to {@link #encodeTriangleBoxVal}; decodes encoded triangle bounding box values */
public static int decodeTriangleBoxVal(byte[] encoded, int offset) {
long val = NumericUtils.sortableBytesToLong(encoded, offset);
int result = (int)(val & 0x00000000FFFFFFFF);

View File

@ -59,8 +59,11 @@ import static org.apache.lucene.geo.GeoEncodingUtils.encodeLongitudeCeil;
import static org.apache.lucene.geo.GeoTestUtil.nextLatitude;
import static org.apache.lucene.geo.GeoTestUtil.nextLongitude;
/** base test class for {@link TestLatLonLineShapeQueries}, {@link TestLatLonPointShapeQueries},
* and {@link TestLatLonPolygonShapeQueries} */
public abstract class BaseLatLonShapeTestCase extends LuceneTestCase {
/** name of the LatLonShape indexed field */
protected static final String FIELD_NAME = "shape";
protected abstract ShapeType getShapeType();
@ -69,22 +72,27 @@ public abstract class BaseLatLonShapeTestCase extends LuceneTestCase {
return getShapeType().nextShape();
}
/** quantizes a latitude value to be consistent with index encoding */
protected double quantizeLat(double rawLat) {
return decodeLatitude(encodeLatitude(rawLat));
}
/** quantizes a provided latitude value rounded up to the nearest encoded integer */
protected double quantizeLatCeil(double rawLat) {
return decodeLatitude(encodeLatitudeCeil(rawLat));
}
/** quantizes a longitude value to be consistent with index encoding */
protected double quantizeLon(double rawLon) {
return decodeLongitude(encodeLongitude(rawLon));
}
/** quantizes a provided longitude value rounded up to the nearest encoded integer */
protected double quantizeLonCeil(double rawLon) {
return decodeLongitude(encodeLongitudeCeil(rawLon));
}
/** quantizes a provided polygon to be consistent with the index encoding */
protected Polygon quantizePolygon(Polygon polygon) {
double[] lats = new double[polygon.numPoints()];
double[] lons = new double[polygon.numPoints()];
@ -95,6 +103,7 @@ public abstract class BaseLatLonShapeTestCase extends LuceneTestCase {
return new Polygon(lats, lons);
}
/** quantizes a provided linestring to be consistent with the index encoding */
protected Line quantizeLine(Line line) {
double[] lats = new double[line.numPoints()];
double[] lons = new double[line.numPoints()];
@ -105,8 +114,10 @@ public abstract class BaseLatLonShapeTestCase extends LuceneTestCase {
return new Line(lats, lons);
}
/** creates the array of LatLonShape.Triangle values that are used to index the shape */
protected abstract Field[] createIndexableFields(String field, Object shape);
/** adds a shape to a provided document */
private void addShapeToDoc(String field, Document doc, Object shape) {
Field[] fields = createIndexableFields(field, shape);
for (Field f : fields) {
@ -114,10 +125,12 @@ public abstract class BaseLatLonShapeTestCase extends LuceneTestCase {
}
}
/** factory method to create a new bounding box query */
protected Query newRectQuery(String field, QueryRelation queryRelation, double minLat, double maxLat, double minLon, double maxLon) {
return LatLonShape.newBoxQuery(field, queryRelation, minLat, maxLat, minLon, maxLon);
}
/** factory method to create a new polygon query */
protected Query newPolygonQuery(String field, QueryRelation queryRelation, Polygon... polygons) {
return LatLonShape.newPolygonQuery(field, queryRelation, polygons);
}
@ -212,8 +225,8 @@ public abstract class BaseLatLonShapeTestCase extends LuceneTestCase {
addShapeToDoc(FIELD_NAME, doc, shapes[id]);
}
w.addDocument(doc);
if (id > 0 && randomInt(100) == 42) {
int idToDelete = randomInt(id);
if (id > 0 && random().nextInt(100) == 42) {
int idToDelete = random().nextInt(id);
w.deleteDocuments(new Term("id", ""+idToDelete));
deleted.add(idToDelete);
if (VERBOSE) {
@ -227,6 +240,7 @@ public abstract class BaseLatLonShapeTestCase extends LuceneTestCase {
}
}
/** test random generated bounding boxes */
protected void verifyRandomBBoxQueries(IndexReader reader, Object... shapes) throws Exception {
IndexSearcher s = newSearcher(reader);
@ -323,6 +337,7 @@ public abstract class BaseLatLonShapeTestCase extends LuceneTestCase {
}
}
/** test random generated polygons */
protected void verifyRandomPolygonQueries(IndexReader reader, Object... shapes) throws Exception {
IndexSearcher s = newSearcher(reader);
@ -481,6 +496,7 @@ public abstract class BaseLatLonShapeTestCase extends LuceneTestCase {
}
}
/** validator class used to test query results against "ground truth" */
protected abstract class Validator {
protected QueryRelation queryRelation = QueryRelation.INTERSECTS;
public abstract boolean testBBoxQuery(double minLat, double maxLat, double minLon, double maxLon, Object shape);

View File

@ -75,8 +75,4 @@ public class TestLatLonPointShapeQueries extends BaseLatLonShapeTestCase {
return r != Relation.CELL_OUTSIDE_QUERY;
}
}
@Override
public void testRandomTiny() throws Exception {
}
}