Changes after rebase on master

This commit is contained in:
Christoph Büscher 2015-12-04 13:33:30 +01:00
parent f07e61c05b
commit 081e0e9d61
15 changed files with 291 additions and 105 deletions

View File

@ -148,7 +148,7 @@ public class LineStringBuilder extends PointCollection<LineStringBuilder> {
@Override
public int hashCode() {
return Objects.hash(points, translated());
return Objects.hash(points);
}
@Override
@ -160,8 +160,7 @@ public class LineStringBuilder extends PointCollection<LineStringBuilder> {
return false;
}
LineStringBuilder other = (LineStringBuilder) obj;
return Objects.equals(points, other.points) &&
(translated() == other.translated());
return Objects.equals(points, other.points);
}
@Override
@ -170,7 +169,6 @@ public class LineStringBuilder extends PointCollection<LineStringBuilder> {
for (Coordinate point : points) {
writeCoordinateTo(point, out);
}
out.writeBoolean(translated());
}
@Override
@ -180,7 +178,6 @@ public class LineStringBuilder extends PointCollection<LineStringBuilder> {
for (int i=0; i < size; i++) {
lineStringBuilder.point(readCoordinateFrom(in));
}
lineStringBuilder.translated(in.readBoolean());
return lineStringBuilder;
}
}

View File

@ -131,7 +131,7 @@ public class MultiLineStringBuilder extends ShapeBuilder {
public MultiLineStringBuilder readFrom(StreamInput in) throws IOException {
MultiLineStringBuilder multiLineStringBuilder = new MultiLineStringBuilder();
int size = in.readVInt();
for (int i=0; i < size; i++) {
for (int i = 0; i < size; i++) {
multiLineStringBuilder.linestring(LineStringBuilder.PROTOTYPE.readFrom(in));
}
return multiLineStringBuilder;

View File

@ -56,7 +56,7 @@ public class MultiPointBuilder extends PointCollection<MultiPointBuilder> {
for (Coordinate coord : points) {
shapes.add(SPATIAL_CONTEXT.makePoint(coord.x, coord.y));
}
XShapeCollection multiPoints = new XShapeCollection<>(shapes, SPATIAL_CONTEXT);
XShapeCollection<Point> multiPoints = new XShapeCollection<>(shapes, SPATIAL_CONTEXT);
multiPoints.setPointsOnly(true);
return multiPoints;
}

View File

@ -541,6 +541,150 @@ public class PolygonBuilder extends ShapeBuilder {
return points.length-1;
}
/**
* Create a connected list of a list of coordinates
*
* @param points
* array of point
* @param offset
* index of the first point
* @param length
* number of points
* @return Array of edges
*/
private static Edge[] ring(int component, boolean direction, boolean handedness, LineStringBuilder shell,
Coordinate[] points, int offset, Edge[] edges, int toffset, int length, final AtomicBoolean translated) {
// calculate the direction of the points:
// find the point a the top of the set and check its
// neighbors orientation. So direction is equivalent
// to clockwise/counterclockwise
final int top = top(points, offset, length);
final int prev = (offset + ((top + length - 1) % length));
final int next = (offset + ((top + 1) % length));
boolean orientation = points[offset + prev].x > points[offset + next].x;
// OGC requires shell as ccw (Right-Handedness) and holes as cw (Left-Handedness)
// since GeoJSON doesn't specify (and doesn't need to) GEO core will assume OGC standards
// thus if orientation is computed as cw, the logic will translate points across dateline
// and convert to a right handed system
// compute the bounding box and calculate range
double[] range = range(points, offset, length);
final double rng = range[1] - range[0];
// translate the points if the following is true
// 1. shell orientation is cw and range is greater than a hemisphere (180 degrees) but not spanning 2 hemispheres
// (translation would result in a collapsed poly)
// 2. the shell of the candidate hole has been translated (to preserve the coordinate system)
boolean incorrectOrientation = component == 0 && handedness != orientation;
if ( (incorrectOrientation && (rng > DATELINE && rng != 2*DATELINE)) || (translated.get() && component != 0)) {
translate(points);
// flip the translation bit if the shell is being translated
if (component == 0) {
translated.set(true);
}
// correct the orientation post translation (ccw for shell, cw for holes)
if (component == 0 || (component != 0 && handedness == orientation)) {
orientation = !orientation;
}
}
return concat(component, direction ^ orientation, points, offset, edges, toffset, length);
}
private static final int top(Coordinate[] points, int offset, int length) {
int top = 0; // we start at 1 here since top points to 0
for (int i = 1; i < length; i++) {
if (points[offset + i].y < points[offset + top].y) {
top = i;
} else if (points[offset + i].y == points[offset + top].y) {
if (points[offset + i].x < points[offset + top].x) {
top = i;
}
}
}
return top;
}
private static final double[] range(Coordinate[] points, int offset, int length) {
double minX = points[0].x;
double maxX = points[0].x;
double minY = points[0].y;
double maxY = points[0].y;
// compute the bounding coordinates (@todo: cleanup brute force)
for (int i = 1; i < length; ++i) {
if (points[offset + i].x < minX) {
minX = points[offset + i].x;
}
if (points[offset + i].x > maxX) {
maxX = points[offset + i].x;
}
if (points[offset + i].y < minY) {
minY = points[offset + i].y;
}
if (points[offset + i].y > maxY) {
maxY = points[offset + i].y;
}
}
return new double[] {minX, maxX, minY, maxY};
}
/**
* Concatenate a set of points to a polygon
*
* @param component
* component id of the polygon
* @param direction
* direction of the ring
* @param points
* list of points to concatenate
* @param pointOffset
* index of the first point
* @param edges
* Array of edges to write the result to
* @param edgeOffset
* index of the first edge in the result
* @param length
* number of points to use
* @return the edges creates
*/
private static Edge[] concat(int component, boolean direction, Coordinate[] points, final int pointOffset, Edge[] edges, final int edgeOffset,
int length) {
assert edges.length >= length+edgeOffset;
assert points.length >= length+pointOffset;
edges[edgeOffset] = new Edge(points[pointOffset], null);
for (int i = 1; i < length; i++) {
if (direction) {
edges[edgeOffset + i] = new Edge(points[pointOffset + i], edges[edgeOffset + i - 1]);
edges[edgeOffset + i].component = component;
} else if(!edges[edgeOffset + i - 1].coordinate.equals(points[pointOffset + i])) {
edges[edgeOffset + i - 1].next = edges[edgeOffset + i] = new Edge(points[pointOffset + i], null);
edges[edgeOffset + i - 1].component = component;
} else {
throw new InvalidShapeException("Provided shape has duplicate consecutive coordinates at: " + points[pointOffset + i]);
}
}
if (direction) {
edges[edgeOffset].setNext(edges[edgeOffset + length - 1]);
edges[edgeOffset].component = component;
} else {
edges[edgeOffset + length - 1].setNext(edges[edgeOffset]);
edges[edgeOffset + length - 1].component = component;
}
return edges;
}
/**
* Transforms coordinates in the eastern hemisphere (-180:0) to a (180:360) range
*/
private static void translate(Coordinate[] points) {
for (Coordinate c : points) {
if (c.x < 0) {
c.x += 2*DATELINE;
}
}
}
@Override
public int hashCode() {
return Objects.hash(shell, holes, orientation);

View File

@ -69,7 +69,7 @@ public abstract class AbstractShapeBuilderTestCase<SB extends ShapeBuilder> exte
/**
* mutate the given shape so the returned shape is different
*/
protected abstract SB mutate(SB original) throws IOException;
protected abstract SB createMutation(SB original) throws IOException;
/**
* Test that creates new shape from a random test shape and checks both for equality
@ -95,10 +95,11 @@ public abstract class AbstractShapeBuilderTestCase<SB extends ShapeBuilder> exte
/**
* Test serialization and deserialization of the test shape.
*/
@SuppressWarnings("unchecked")
public void testSerialization() throws IOException {
for (int runs = 0; runs < NUMBER_OF_TESTBUILDERS; runs++) {
SB testShape = createTestShapeBuilder();
SB deserializedShape = copyShape(testShape);
SB deserializedShape = (SB) copyShape(testShape);
assertEquals(testShape, deserializedShape);
assertEquals(testShape.hashCode(), deserializedShape.hashCode());
assertNotSame(testShape, deserializedShape);
@ -108,6 +109,7 @@ public abstract class AbstractShapeBuilderTestCase<SB extends ShapeBuilder> exte
/**
* Test equality and hashCode properties
*/
@SuppressWarnings("unchecked")
public void testEqualsAndHashcode() throws IOException {
for (int runs = 0; runs < NUMBER_OF_TESTBUILDERS; runs++) {
SB firstShape = createTestShapeBuilder();
@ -116,15 +118,15 @@ public abstract class AbstractShapeBuilderTestCase<SB extends ShapeBuilder> exte
assertTrue("shape is not equal to self", firstShape.equals(firstShape));
assertThat("same shape's hashcode returns different values if called multiple times", firstShape.hashCode(),
equalTo(firstShape.hashCode()));
assertThat("different shapes should not be equal", mutate(firstShape), not(equalTo(firstShape)));
assertThat("different shapes should not be equal", createMutation(firstShape), not(equalTo(firstShape)));
SB secondShape = copyShape(firstShape);
SB secondShape = (SB) copyShape(firstShape);
assertTrue("shape is not equal to self", secondShape.equals(secondShape));
assertTrue("shape is not equal to its copy", firstShape.equals(secondShape));
assertTrue("equals is not symmetric", secondShape.equals(firstShape));
assertThat("shape copy's hashcode is different from original hashcode", secondShape.hashCode(), equalTo(firstShape.hashCode()));
SB thirdShape = copyShape(secondShape);
SB thirdShape = (SB) copyShape(secondShape);
assertTrue("shape is not equal to self", thirdShape.equals(thirdShape));
assertTrue("shape is not equal to its copy", secondShape.equals(thirdShape));
assertThat("shape copy's hashcode is different from original hashcode", secondShape.hashCode(), equalTo(thirdShape.hashCode()));
@ -135,14 +137,12 @@ public abstract class AbstractShapeBuilderTestCase<SB extends ShapeBuilder> exte
}
}
protected SB copyShape(SB original) throws IOException {
static ShapeBuilder copyShape(ShapeBuilder original) throws IOException {
try (BytesStreamOutput output = new BytesStreamOutput()) {
original.writeTo(output);
try (StreamInput in = new NamedWriteableAwareStreamInput(StreamInput.wrap(output.bytes()), namedWriteableRegistry)) {
ShapeBuilder prototype = (ShapeBuilder) namedWriteableRegistry.getPrototype(ShapeBuilder.class, original.getWriteableName());
@SuppressWarnings("unchecked")
SB copy = (SB) prototype.readFrom(in);
return copy;
return prototype.readFrom(in);
}
}
}

View File

@ -27,20 +27,18 @@ import java.io.IOException;
public class CircleBuilderTests extends AbstractShapeBuilderTestCase<CircleBuilder> {
final static CircleBuilderTests PROTOTYPE = new CircleBuilderTests();
@Override
protected CircleBuilder createTestShapeBuilder() {
double centerX = randomDoubleBetween(-180, 180, false);
double centerY = randomDoubleBetween(-90, 90, false);
return new CircleBuilder()
.center(new Coordinate(centerX, centerY))
.radius(randomDoubleBetween(0.1, 10.0, false), randomFrom(DistanceUnit.values()));
return createRandomShape();
}
@Override
protected CircleBuilder mutate(CircleBuilder original) throws IOException {
CircleBuilder mutation = copyShape(original);
protected CircleBuilder createMutation(CircleBuilder original) throws IOException {
return mutate(original);
}
static CircleBuilder mutate(CircleBuilder original) throws IOException {
CircleBuilder mutation = (CircleBuilder) copyShape(original);
double radius = original.radius();
DistanceUnit unit = original.unit();
@ -57,4 +55,12 @@ public class CircleBuilderTests extends AbstractShapeBuilderTestCase<CircleBuild
}
return mutation.radius(radius, unit);
}
static CircleBuilder createRandomShape() {
double centerX = randomDoubleBetween(-180, 180, false);
double centerY = randomDoubleBetween(-90, 90, false);
return new CircleBuilder()
.center(new Coordinate(centerX, centerY))
.radius(randomDoubleBetween(0.1, 10.0, false), randomFrom(DistanceUnit.values()));
}
}

View File

@ -29,20 +29,18 @@ import java.io.IOException;
public class EnvelopeBuilderTests extends AbstractShapeBuilderTestCase<EnvelopeBuilder> {
static final EnvelopeBuilderTests PROTOTYPE = new EnvelopeBuilderTests();
@Override
protected EnvelopeBuilder createTestShapeBuilder() {
EnvelopeBuilder envelope = new EnvelopeBuilder(randomFrom(Orientation.values()));
Rectangle box = RandomShapeGenerator.xRandomRectangle(getRandom(), RandomShapeGenerator.xRandomPoint(getRandom()));
envelope.topLeft(box.getMinX(), box.getMaxY())
.bottomRight(box.getMaxX(), box.getMinY());
return envelope;
return createRandomShape();
}
@Override
protected EnvelopeBuilder mutate(EnvelopeBuilder original) throws IOException {
EnvelopeBuilder mutation = copyShape(original);
protected EnvelopeBuilder createMutation(EnvelopeBuilder original) throws IOException {
return mutate(original);
}
static EnvelopeBuilder mutate(EnvelopeBuilder original) throws IOException {
EnvelopeBuilder mutation = (EnvelopeBuilder) copyShape(original);
if (randomBoolean()) {
// toggle orientation
mutation.orientation = (original.orientation == Orientation.LEFT ? Orientation.RIGHT : Orientation.LEFT);
@ -65,4 +63,12 @@ public class EnvelopeBuilderTests extends AbstractShapeBuilderTestCase<EnvelopeB
}
return mutation;
}
static EnvelopeBuilder createRandomShape() {
EnvelopeBuilder envelope = new EnvelopeBuilder(randomFrom(Orientation.values()));
Rectangle box = RandomShapeGenerator.xRandomRectangle(getRandom(), RandomShapeGenerator.xRandomPoint(getRandom()));
envelope.topLeft(box.getMinX(), box.getMaxY())
.bottomRight(box.getMaxX(), box.getMinY());
return envelope;
}
}

View File

@ -33,28 +33,28 @@ public class GeometryCollectionBuilderTests extends AbstractShapeBuilderTestCase
for (int i = 0; i < shapes; i++) {
switch (randomIntBetween(0, 7)) {
case 0:
geometryCollection.shape(PointBuilderTests.PROTOTYPE.createTestShapeBuilder());
geometryCollection.shape(PointBuilderTests.createRandomShape());
break;
case 1:
geometryCollection.shape(CircleBuilderTests.PROTOTYPE.createTestShapeBuilder());
geometryCollection.shape(CircleBuilderTests.createRandomShape());
break;
case 2:
geometryCollection.shape(EnvelopeBuilderTests.PROTOTYPE.createTestShapeBuilder());
geometryCollection.shape(EnvelopeBuilderTests.createRandomShape());
break;
case 3:
geometryCollection.shape(LineStringBuilderTests.PROTOTYPE.createTestShapeBuilder());
geometryCollection.shape(LineStringBuilderTests.createRandomShape());
break;
case 4:
geometryCollection.shape(MultiLineStringBuilderTests.PROTOTYPE.createTestShapeBuilder());
geometryCollection.shape(MultiLineStringBuilderTests.createRandomShape());
break;
case 5:
geometryCollection.shape(MultiPolygonBuilderTests.PROTOTYPE.createTestShapeBuilder());
geometryCollection.shape(MultiPolygonBuilderTests.createRandomShape());
break;
case 6:
geometryCollection.shape(MultiPointBuilderTests.PROTOTYPE.createTestShapeBuilder());
geometryCollection.shape(MultiPointBuilderTests.createRandomShape());
break;
case 7:
geometryCollection.shape(PolygonBuilderTests.PROTOTYPE.createTestShapeBuilder());
geometryCollection.shape(PolygonBuilderTests.createRandomShape());
break;
}
}
@ -62,8 +62,12 @@ public class GeometryCollectionBuilderTests extends AbstractShapeBuilderTestCase
}
@Override
protected GeometryCollectionBuilder mutate(GeometryCollectionBuilder original) throws IOException {
GeometryCollectionBuilder mutation = copyShape(original);
protected GeometryCollectionBuilder createMutation(GeometryCollectionBuilder original) throws IOException {
return mutate(original);
}
static GeometryCollectionBuilder mutate(GeometryCollectionBuilder original) throws IOException {
GeometryCollectionBuilder mutation = (GeometryCollectionBuilder) copyShape(original);
// NORELEASE check of GeometryCollectionBuilder should parse maintain orientation
// if (randomBoolean()) {
// // toggle orientation
@ -75,28 +79,28 @@ public class GeometryCollectionBuilderTests extends AbstractShapeBuilderTestCase
ShapeBuilder shapeToChange = mutation.shapes.get(shapePosition);
switch (shapeToChange.type()) {
case POINT:
shapeToChange = PointBuilderTests.PROTOTYPE.mutate((PointBuilder) shapeToChange);
shapeToChange = PointBuilderTests.mutate((PointBuilder) shapeToChange);
break;
case CIRCLE:
shapeToChange = CircleBuilderTests.PROTOTYPE.mutate((CircleBuilder) shapeToChange);
shapeToChange = CircleBuilderTests.mutate((CircleBuilder) shapeToChange);
break;
case ENVELOPE:
shapeToChange = EnvelopeBuilderTests.PROTOTYPE.mutate((EnvelopeBuilder) shapeToChange);
shapeToChange = EnvelopeBuilderTests.mutate((EnvelopeBuilder) shapeToChange);
break;
case LINESTRING:
shapeToChange = LineStringBuilderTests.PROTOTYPE.mutate((LineStringBuilder) shapeToChange);
shapeToChange = LineStringBuilderTests.mutate((LineStringBuilder) shapeToChange);
break;
case MULTILINESTRING:
shapeToChange = MultiLineStringBuilderTests.PROTOTYPE.mutate((MultiLineStringBuilder) shapeToChange);
shapeToChange = MultiLineStringBuilderTests.mutate((MultiLineStringBuilder) shapeToChange);
break;
case MULTIPOLYGON:
shapeToChange = MultiPolygonBuilderTests.PROTOTYPE.mutate((MultiPolygonBuilder) shapeToChange);
shapeToChange = MultiPolygonBuilderTests.mutate((MultiPolygonBuilder) shapeToChange);
break;
case MULTIPOINT:
shapeToChange = MultiPointBuilderTests.PROTOTYPE.mutate((MultiPointBuilder) shapeToChange);
shapeToChange = MultiPointBuilderTests.mutate((MultiPointBuilder) shapeToChange);
break;
case POLYGON:
shapeToChange = PolygonBuilderTests.PROTOTYPE.mutate((PolygonBuilder) shapeToChange);
shapeToChange = PolygonBuilderTests.mutate((PolygonBuilder) shapeToChange);
break;
case GEOMETRYCOLLECTION:
throw new UnsupportedOperationException("GeometryCollection should not be nested inside each other");

View File

@ -28,20 +28,18 @@ import java.io.IOException;
public class LineStringBuilderTests extends AbstractShapeBuilderTestCase<LineStringBuilder> {
static final LineStringBuilderTests PROTOTYPE = new LineStringBuilderTests();
@Override
protected LineStringBuilder createTestShapeBuilder() {
LineStringBuilder lsb = (LineStringBuilder) RandomShapeGenerator.createShape(getRandom(), ShapeType.LINESTRING);
if (randomBoolean()) {
lsb.close();
}
return lsb;
return createRandomShape();
}
@Override
protected LineStringBuilder mutate(LineStringBuilder original) throws IOException {
LineStringBuilder mutation = copyShape(original);
protected LineStringBuilder createMutation(LineStringBuilder original) throws IOException {
return mutate(original);
}
static LineStringBuilder mutate(LineStringBuilder original) throws IOException {
LineStringBuilder mutation = (LineStringBuilder) copyShape(original);
Coordinate[] coordinates = original.coordinates(false);
Coordinate coordinate = randomFrom(coordinates);
if (randomBoolean()) {
@ -59,4 +57,12 @@ public class LineStringBuilderTests extends AbstractShapeBuilderTestCase<LineStr
}
return mutation.points(coordinates);
}
static LineStringBuilder createRandomShape() {
LineStringBuilder lsb = (LineStringBuilder) RandomShapeGenerator.createShape(getRandom(), ShapeType.LINESTRING);
if (randomBoolean()) {
lsb.close();
}
return lsb;
}
}

View File

@ -28,16 +28,18 @@ import java.io.IOException;
public class MultiLineStringBuilderTests extends AbstractShapeBuilderTestCase<MultiLineStringBuilder> {
static final MultiLineStringBuilderTests PROTOTYPE = new MultiLineStringBuilderTests();
@Override
protected MultiLineStringBuilder createTestShapeBuilder() {
return (MultiLineStringBuilder) RandomShapeGenerator.createShape(getRandom(), ShapeType.MULTILINESTRING);
return createRandomShape();
}
@Override
protected MultiLineStringBuilder mutate(MultiLineStringBuilder original) throws IOException {
MultiLineStringBuilder mutation = copyShape(original);
protected MultiLineStringBuilder createMutation(MultiLineStringBuilder original) throws IOException {
return mutate(original);
}
static MultiLineStringBuilder mutate(MultiLineStringBuilder original) throws IOException {
MultiLineStringBuilder mutation = (MultiLineStringBuilder) copyShape(original);
Coordinate[][] coordinates = mutation.coordinates();
int lineToChange = randomInt(coordinates.length - 1);
for (int i = 0; i < coordinates.length; i++) {
@ -61,4 +63,8 @@ public class MultiLineStringBuilderTests extends AbstractShapeBuilderTestCase<Mu
}
return mutation;
}
static MultiLineStringBuilder createRandomShape() {
return (MultiLineStringBuilder) RandomShapeGenerator.createShape(getRandom(), ShapeType.MULTILINESTRING);
}
}

View File

@ -28,16 +28,18 @@ import java.io.IOException;
public class MultiPointBuilderTests extends AbstractShapeBuilderTestCase<MultiPointBuilder> {
static final MultiPointBuilderTests PROTOTYPE = new MultiPointBuilderTests();
@Override
protected MultiPointBuilder createTestShapeBuilder() {
return (MultiPointBuilder) RandomShapeGenerator.createShape(getRandom(), ShapeType.MULTIPOINT);
return createRandomShape();
}
@Override
protected MultiPointBuilder mutate(MultiPointBuilder original) throws IOException {
MultiPointBuilder mutation = copyShape(original);
protected MultiPointBuilder createMutation(MultiPointBuilder original) throws IOException {
return mutate(original);
}
static MultiPointBuilder mutate(MultiPointBuilder original) throws IOException {
MultiPointBuilder mutation = (MultiPointBuilder) copyShape(original);
Coordinate[] coordinates = original.coordinates(false);
Coordinate coordinate = randomFrom(coordinates);
if (randomBoolean()) {
@ -55,4 +57,8 @@ public class MultiPointBuilderTests extends AbstractShapeBuilderTestCase<MultiPo
}
return mutation.points(coordinates);
}
static MultiPointBuilder createRandomShape() {
return (MultiPointBuilder) RandomShapeGenerator.createShape(getRandom(), ShapeType.MULTIPOINT);
}
}

View File

@ -27,25 +27,18 @@ import java.io.IOException;
public class MultiPolygonBuilderTests extends AbstractShapeBuilderTestCase<MultiPolygonBuilder> {
static final MultiPolygonBuilderTests PROTOTYPE = new MultiPolygonBuilderTests();
@Override
protected MultiPolygonBuilder createTestShapeBuilder() {
MultiPolygonBuilder mpb = new MultiPolygonBuilder(randomFrom(Orientation.values()));
int polys = randomIntBetween(1, 10);
for (int i = 0; i < polys; i++) {
PolygonBuilder pgb = (PolygonBuilder) RandomShapeGenerator.createShape(getRandom(), ShapeType.POLYGON);
pgb.orientation = mpb.orientation;
// NORELEASE translated might have been changed by createShape, but won't survive xContent->Parse roundtrip
pgb.shell().translated(false);
mpb.polygon(pgb);
}
return mpb;
return createRandomShape();
}
@Override
protected MultiPolygonBuilder mutate(MultiPolygonBuilder original) throws IOException {
MultiPolygonBuilder mutation = copyShape(original);
protected MultiPolygonBuilder createMutation(MultiPolygonBuilder original) throws IOException {
return mutate(original);
}
static MultiPolygonBuilder mutate(MultiPolygonBuilder original) throws IOException {
MultiPolygonBuilder mutation = (MultiPolygonBuilder) copyShape(original);
if (randomBoolean()) {
// toggle orientation
mutation.orientation = (original.orientation == Orientation.LEFT ? Orientation.RIGHT : Orientation.LEFT);
@ -55,4 +48,15 @@ public class MultiPolygonBuilderTests extends AbstractShapeBuilderTestCase<Multi
}
return mutation;
}
static MultiPolygonBuilder createRandomShape() {
MultiPolygonBuilder mpb = new MultiPolygonBuilder(randomFrom(Orientation.values()));
int polys = randomIntBetween(1, 10);
for (int i = 0; i < polys; i++) {
PolygonBuilder pgb = (PolygonBuilder) RandomShapeGenerator.createShape(getRandom(), ShapeType.POLYGON);
pgb.orientation = mpb.orientation;
mpb.polygon(pgb);
}
return mpb;
}
}

View File

@ -24,17 +24,27 @@ import com.vividsolutions.jts.geom.Coordinate;
import org.elasticsearch.test.geo.RandomShapeGenerator;
import org.elasticsearch.test.geo.RandomShapeGenerator.ShapeType;
public class PointBuilderTests extends AbstractShapeBuilderTestCase<PointBuilder> {
import java.io.IOException;
final static PointBuilderTests PROTOTYPE = new PointBuilderTests();
public class PointBuilderTests extends AbstractShapeBuilderTestCase<PointBuilder> {
@Override
protected PointBuilder createTestShapeBuilder() {
return (PointBuilder) RandomShapeGenerator.createShape(getRandom(), ShapeType.POINT);
return createRandomShape();
}
@Override
protected PointBuilder mutate(PointBuilder original) {
protected PointBuilder createMutation(PointBuilder original) throws IOException {
return mutate(original);
}
static PointBuilder mutate(PointBuilder original) {
return new PointBuilder().coordinate(new Coordinate(original.longitude() / 2, original.latitude() / 2));
}
static PointBuilder createRandomShape() {
return (PointBuilder) RandomShapeGenerator.createShape(getRandom(), ShapeType.POINT);
}
}

View File

@ -29,20 +29,18 @@ import java.io.IOException;
public class PolygonBuilderTests extends AbstractShapeBuilderTestCase<PolygonBuilder> {
static final PolygonBuilderTests PROTOTYPE = new PolygonBuilderTests();
@Override
protected PolygonBuilder createTestShapeBuilder() {
PolygonBuilder pgb = (PolygonBuilder) RandomShapeGenerator.createShape(getRandom(), ShapeType.POLYGON);
pgb.orientation = randomFrom(Orientation.values());
// NORELEASE translated might have been changed by createShape, but won't survive xContent->Parse roundtrip
pgb.shell().translated(false);
return pgb;
return createRandomShape();
}
@Override
protected PolygonBuilder mutate(PolygonBuilder original) throws IOException {
PolygonBuilder mutation = copyShape(original);
protected PolygonBuilder createMutation(PolygonBuilder original) throws IOException {
return mutate(original);
}
static PolygonBuilder mutate(PolygonBuilder original) throws IOException {
PolygonBuilder mutation = (PolygonBuilder) copyShape(original);
return mutatePolygonBuilder(mutation);
}
@ -75,4 +73,10 @@ public class PolygonBuilderTests extends AbstractShapeBuilderTestCase<PolygonBui
}
return pb;
}
static PolygonBuilder createRandomShape() {
PolygonBuilder pgb = (PolygonBuilder) RandomShapeGenerator.createShape(getRandom(), ShapeType.POLYGON);
pgb.orientation = randomFrom(Orientation.values());
return pgb;
}
}

View File

@ -29,7 +29,6 @@ import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.geo.ShapeRelation;
import org.elasticsearch.common.geo.SpatialStrategy;
import org.elasticsearch.common.geo.builders.EnvelopeBuilder;
import org.elasticsearch.common.geo.builders.PolygonBuilder;
import org.elasticsearch.common.geo.builders.ShapeBuilder;
import org.elasticsearch.common.geo.builders.ShapeBuilders;
import org.elasticsearch.common.xcontent.XContentBuilder;
@ -60,12 +59,6 @@ public class GeoShapeQueryBuilderTests extends AbstractQueryTestCase<GeoShapeQue
protected GeoShapeQueryBuilder doCreateTestQueryBuilder() {
ShapeType shapeType = ShapeType.randomType(getRandom());
ShapeBuilder shape = RandomShapeGenerator.createShapeWithin(getRandom(), null, shapeType);
// NORELEASE PolygonBuilder gets validated on creation, so the shells 'translate' might get set
// this causes problems in the test, because we can't parse this property in fromXContent
// so we switch it to false again.
if (shape instanceof PolygonBuilder) {
((PolygonBuilder) shape).shell().translated(false);
}
GeoShapeQueryBuilder builder;
clearShapeFields();