use of org.janusgraph.core.attribute.Geoshape in project janusgraph by JanusGraph.
the class JanusGraphIndexTest method testCollection.
private void testCollection(Cardinality cardinality, String property, Object value1, Object value2) {
clopen();
Vertex v1 = graph.addVertex();
// Adding properties one at a time
v1.property(property, value1);
// Flush the index
clopen();
assertEquals(v1, getOnlyElement(graph.query().has(property, value1).vertices()));
v1 = getV(graph, v1.id());
v1.property(property, value2);
assertEquals(v1, getOnlyElement(graph.query().has(property, value1).vertices()));
assertEquals(v1, getOnlyElement(graph.query().has(property, value2).vertices()));
// Flush the index
clopen();
assertEquals(v1, getOnlyElement(graph.query().has(property, value1).vertices()));
assertEquals(v1, getOnlyElement(graph.query().has(property, value2).vertices()));
// Remove the properties
v1 = getV(graph, v1.id());
v1.properties(property).forEachRemaining(p -> {
if (p.value().equals(value1)) {
p.remove();
}
});
assertFalse(graph.query().has(property, value1).vertices().iterator().hasNext());
assertEquals(v1, getOnlyElement(graph.query().has(property, value2).vertices()));
// Flush the index
clopen();
assertEquals(v1, getOnlyElement(graph.query().has(property, value2).vertices()));
assertFalse(graph.query().has(property, value1).vertices().iterator().hasNext());
// Re add the properties
v1 = getV(graph, v1.id());
v1.property(property, value1);
assertEquals(v1, getOnlyElement(graph.query().has(property, value1).vertices()));
assertEquals(v1, getOnlyElement(graph.query().has(property, value2).vertices()));
// Flush the index
clopen();
assertEquals(v1, getOnlyElement(graph.query().has(property, value1).vertices()));
assertEquals(v1, getOnlyElement(graph.query().has(property, value2).vertices()));
// Add a duplicate property
v1 = getV(graph, v1.id());
v1.property(property, value1);
assertEquals(Cardinality.SET.equals(cardinality) ? 2 : 3, Iterators.size(getOnlyVertex(graph.query().has(property, value1)).properties(property)));
// Flush the index
clopen();
assertEquals(Cardinality.SET.equals(cardinality) ? 2 : 3, Iterators.size(getOnlyVertex(graph.query().has(property, value1)).properties(property)));
// Add two properties at once to a fresh vertex
graph.vertices().forEachRemaining(Element::remove);
v1 = graph.addVertex();
v1.property(property, value1);
v1.property(property, value2);
assertEquals(v1, getOnlyElement(graph.query().has(property, value1).vertices()));
assertEquals(v1, getOnlyElement(graph.query().has(property, value2).vertices()));
// Flush the index
clopen();
assertEquals(v1, getOnlyElement(graph.query().has(property, value1).vertices()));
assertEquals(v1, getOnlyElement(graph.query().has(property, value2).vertices()));
// If this is a geo test then try a within query
if (value1 instanceof Geoshape) {
assertEquals(v1, getOnlyElement(graph.query().has(property, Geo.WITHIN, Geoshape.circle(1.0, 1.0, 0.1)).vertices()));
assertEquals(v1, getOnlyElement(graph.query().has(property, Geo.WITHIN, Geoshape.circle(2.0, 2.0, 0.1)).vertices()));
}
// Test traversal property drop Issue #408
GraphTraversalSource g = graph.traversal();
g.V().drop().iterate();
// Flush the index
clopen();
g = graph.traversal();
v1 = g.addV().property(property, value1).property(property, value2).next();
Vertex v2 = g.addV().property(property, value1).property(property, value2).next();
// Flush the index
clopen();
g = graph.traversal();
assertEquals(2, g.V().has(property, value1).toList().size());
g.V().properties().drop().iterate();
// Flush the index
clopen();
g = graph.traversal();
assertFalse(g.V().has(property, value1).hasNext());
assertFalse(g.V().has(property, value2).hasNext());
}
use of org.janusgraph.core.attribute.Geoshape in project janusgraph by JanusGraph.
the class JanusGraphIndexTest method testGeo.
private void testGeo(int i, int origNumV, int numV) {
double offset = (i * 50.0 / origNumV);
double bufferKm = 20;
double distance = Geoshape.point(0.0, 0.0).getPoint().distance(Geoshape.point(offset, offset).getPoint()) + bufferKm;
assertCount(i + 1, tx.query().has("location", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance)).vertices());
assertCount(i + 1, tx.query().has("location", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance)).edges());
assertCount(i + 1, tx.query().has("location", Geo.INTERSECT, Geoshape.circle(0.0, 0.0, distance)).vertices());
assertCount(i + 1, tx.query().has("location", Geo.INTERSECT, Geoshape.circle(0.0, 0.0, distance)).edges());
assertCount(numV - (i + 1), tx.query().has("location", Geo.DISJOINT, Geoshape.circle(0.0, 0.0, distance)).vertices());
assertCount(numV - (i + 1), tx.query().has("location", Geo.DISJOINT, Geoshape.circle(0.0, 0.0, distance)).edges());
assertCount(i + 1, tx.query().has("boundary", Geo.INTERSECT, Geoshape.circle(0.0, 0.0, distance)).vertices());
assertCount(i + 1, tx.query().has("boundary", Geo.INTERSECT, Geoshape.circle(0.0, 0.0, distance)).edges());
if (i > 0) {
assertCount(i, tx.query().has("boundary", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance - bufferKm)).vertices());
assertCount(i, tx.query().has("boundary", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance - bufferKm)).edges());
}
assertCount(numV - (i + 1), tx.query().has("boundary", Geo.DISJOINT, Geoshape.circle(0.0, 0.0, distance)).vertices());
assertCount(numV - (i + 1), tx.query().has("boundary", Geo.DISJOINT, Geoshape.circle(0.0, 0.0, distance)).edges());
if (indexFeatures.supportsGeoContains()) {
assertCount(i % 2, tx.query().has("boundary", Geo.CONTAINS, Geoshape.point(-offset, -offset)).vertices());
assertCount(i % 2, tx.query().has("boundary", Geo.CONTAINS, Geoshape.point(-offset, -offset)).edges());
}
double buffer = bufferKm / 111.;
double min = -Math.abs(offset);
double max = Math.abs(offset);
Geoshape bufferedBox = Geoshape.box(min - buffer, min - buffer, max + buffer, max + buffer);
assertCount(i + 1, tx.query().has("location", Geo.WITHIN, bufferedBox).vertices());
assertCount(i + 1, tx.query().has("location", Geo.WITHIN, bufferedBox).edges());
assertCount(i + 1, tx.query().has("location", Geo.INTERSECT, bufferedBox).vertices());
assertCount(i + 1, tx.query().has("location", Geo.INTERSECT, bufferedBox).edges());
assertCount(numV - (i + 1), tx.query().has("location", Geo.DISJOINT, bufferedBox).vertices());
assertCount(numV - (i + 1), tx.query().has("location", Geo.DISJOINT, bufferedBox).edges());
if (i > 0) {
Geoshape exactBox = Geoshape.box(min, min, max, max);
assertCount(i, tx.query().has("boundary", Geo.WITHIN, exactBox).vertices());
assertCount(i, tx.query().has("boundary", Geo.WITHIN, exactBox).edges());
}
assertCount(i + 1, tx.query().has("boundary", Geo.INTERSECT, bufferedBox).vertices());
assertCount(i + 1, tx.query().has("boundary", Geo.INTERSECT, bufferedBox).edges());
assertCount(numV - (i + 1), tx.query().has("boundary", Geo.DISJOINT, bufferedBox).vertices());
assertCount(numV - (i + 1), tx.query().has("boundary", Geo.DISJOINT, bufferedBox).edges());
Geoshape bufferedPoly = Geoshape.polygon(Arrays.asList(new double[][] { { min - buffer, min - buffer }, { max + buffer, min - buffer }, { max + buffer, max + buffer }, { min - buffer, max + buffer }, { min - buffer, min - buffer } }));
if (i > 0) {
Geoshape exactPoly = Geoshape.polygon(Arrays.asList(new double[][] { { min, min }, { max, min }, { max, max }, { min, max }, { min, min } }));
assertCount(i, tx.query().has("boundary", Geo.WITHIN, exactPoly).vertices());
assertCount(i, tx.query().has("boundary", Geo.WITHIN, exactPoly).edges());
}
assertCount(i + 1, tx.query().has("boundary", Geo.INTERSECT, bufferedPoly).vertices());
assertCount(i + 1, tx.query().has("boundary", Geo.INTERSECT, bufferedPoly).edges());
assertCount(numV - (i + 1), tx.query().has("boundary", Geo.DISJOINT, bufferedPoly).vertices());
assertCount(numV - (i + 1), tx.query().has("boundary", Geo.DISJOINT, bufferedPoly).edges());
}
use of org.janusgraph.core.attribute.Geoshape in project janusgraph by JanusGraph.
the class GeoshapeHelperTest method testGetPoint.
@Test
public void testGetPoint() {
Geoshape shape = new Geoshape(getShapeFactory().pointXY(1.0, 2.0));
Point point = getHelper().getPoint(shape, 0);
Assert.assertEquals(1.0, point.getLongitude(), 0.0);
Assert.assertEquals(2.0, point.getLatitude(), 0.0);
shape = new Geoshape(getShapeFactory().circle(1.0, 2.0, 200));
point = getHelper().getPoint(shape, 0);
Assert.assertEquals(1.0, point.getLongitude(), 0.0);
Assert.assertEquals(2.0, point.getLatitude(), 0.0);
shape = new Geoshape(getShapeFactory().lineString().pointXY(1.0, 2.0).pointXY(3.0, 4.0).build());
point = getHelper().getPoint(shape, 0);
Assert.assertEquals(1.0, point.getLongitude(), 0.0);
Assert.assertEquals(2.0, point.getLatitude(), 0.0);
point = getHelper().getPoint(shape, 1);
Assert.assertEquals(3.0, point.getLongitude(), 0.0);
Assert.assertEquals(4.0, point.getLatitude(), 0.0);
shape = new Geoshape(getShapeFactory().rect(-1.0, 2.0, -3.0, 4.0));
point = getHelper().getPoint(shape, 0);
Assert.assertEquals(-1.0, point.getLongitude(), 0.0);
Assert.assertEquals(-3.0, point.getLatitude(), 0.0);
point = getHelper().getPoint(shape, 1);
Assert.assertEquals(2.0, point.getLongitude(), 0.0);
Assert.assertEquals(4.0, point.getLatitude(), 0.0);
shape = new Geoshape(getShapeFactory().multiPoint().pointXY(60.0, 90.0).pointXY(120.0, 60.0).build());
point = getHelper().getPoint(shape, 0);
Assert.assertEquals(60, point.getLongitude(), 0.0);
Assert.assertEquals(90, point.getLatitude(), 0.0);
point = getHelper().getPoint(shape, 1);
Assert.assertEquals(120, point.getLongitude(), 0.0);
Assert.assertEquals(60, point.getLatitude(), 0.0);
shape = new Geoshape(getShapeFactory().multiLineString().add(getShapeFactory().lineString().pointXY(59.0, 60.0).pointXY(61.0, 60.0)).add(getShapeFactory().lineString().pointXY(119.0, 60.0).pointXY(121.0, 60.0)).build());
point = getHelper().getPoint(shape, 0);
Assert.assertEquals(59, point.getLongitude(), 0.0);
Assert.assertEquals(60, point.getLatitude(), 0.0);
point = getHelper().getPoint(shape, 1);
Assert.assertEquals(61, point.getLongitude(), 0.0);
Assert.assertEquals(60, point.getLatitude(), 0.0);
point = getHelper().getPoint(shape, 2);
Assert.assertEquals(119, point.getLongitude(), 0.0);
Assert.assertEquals(60, point.getLatitude(), 0.0);
point = getHelper().getPoint(shape, 3);
Assert.assertEquals(121, point.getLongitude(), 0.0);
Assert.assertEquals(60, point.getLatitude(), 0.0);
shape = new Geoshape(getShapeFactory().multiShape(Shape.class).add(getShapeFactory().pointXY(59.0, 60.0)).add(getShapeFactory().lineString().pointXY(119.0, 60.0).pointXY(121.0, 60.0).build()).build());
point = getHelper().getPoint(shape, 0);
Assert.assertEquals(59, point.getLongitude(), 0.0);
Assert.assertEquals(60, point.getLatitude(), 0.0);
point = getHelper().getPoint(shape, 1);
Assert.assertEquals(119, point.getLongitude(), 0.0);
Assert.assertEquals(60, point.getLatitude(), 0.0);
point = getHelper().getPoint(shape, 2);
Assert.assertEquals(121, point.getLongitude(), 0.0);
Assert.assertEquals(60, point.getLatitude(), 0.0);
if (supportJts()) {
shape = new Geoshape(getShapeFactory().polygon().pointXY(1.0, 2.0).pointXY(3.0, 4.0).pointXY(3.0, -4.0).pointXY(1.0, -4.0).pointXY(1.0, 2.0).build());
point = getHelper().getPoint(shape, 0);
Assert.assertEquals(1.0, point.getLongitude(), 0.0);
Assert.assertEquals(2.0, point.getLatitude(), 0.0);
point = getHelper().getPoint(shape, 1);
Assert.assertEquals(3.0, point.getLongitude(), 0.0);
Assert.assertEquals(4.0, point.getLatitude(), 0.0);
point = getHelper().getPoint(shape, 2);
Assert.assertEquals(3.0, point.getLongitude(), 0.0);
Assert.assertEquals(-4.0, point.getLatitude(), 0.0);
point = getHelper().getPoint(shape, 3);
Assert.assertEquals(1.0, point.getLongitude(), 0.0);
Assert.assertEquals(-4.0, point.getLatitude(), 0.0);
point = getHelper().getPoint(shape, 4);
Assert.assertEquals(1.0, point.getLongitude(), 0.0);
Assert.assertEquals(2.0, point.getLatitude(), 0.0);
shape = new Geoshape(getShapeFactory().multiPolygon().add(Geoshape.getShapeFactory().polygon().pointXY(59.0, 59.0).pointXY(61.0, 59.0).pointXY(61.0, 61.0).pointXY(59.0, 61.0).pointXY(59.0, 59.0)).add(Geoshape.getShapeFactory().polygon().pointXY(119.0, 59.0).pointXY(121.0, 59.0).pointXY(121.0, 61.0).pointXY(119.0, 61.0).pointXY(119.0, 59.0)).build());
point = getHelper().getPoint(shape, 2);
Assert.assertEquals(61.0, point.getLongitude(), 0.0);
Assert.assertEquals(61.0, point.getLatitude(), 0.0);
point = getHelper().getPoint(shape, 7);
Assert.assertEquals(121.0, point.getLongitude(), 0.0);
Assert.assertEquals(61.0, point.getLatitude(), 0.0);
shape = new Geoshape(getShapeFactory().multiShape(Shape.class).add(getShapeFactory().pointXY(60.0, 60.0)).add(getShapeFactory().lineString().pointXY(119.0, 60.0).pointXY(121.0, 60.0).build()).add(getShapeFactory().polygon().pointXY(119.0, 59.0).pointXY(121.0, 59.0).pointXY(121.0, 61.0).pointXY(119.0, 61.0).pointXY(119.0, 59.0).build()).build());
point = getHelper().getPoint(shape, 0);
Assert.assertEquals(60.0, point.getLongitude(), 0.0);
Assert.assertEquals(60.0, point.getLatitude(), 0.0);
point = getHelper().getPoint(shape, 2);
Assert.assertEquals(121.0, point.getLongitude(), 0.0);
Assert.assertEquals(60.0, point.getLatitude(), 0.0);
point = getHelper().getPoint(shape, 6);
Assert.assertEquals(119.0, point.getLongitude(), 0.0);
Assert.assertEquals(61.0, point.getLatitude(), 0.0);
}
}
use of org.janusgraph.core.attribute.Geoshape in project janusgraph by JanusGraph.
the class GeoshapeTest method testIntersection.
@Test
public void testIntersection() {
for (int i = 0; i < 50; i++) {
Geoshape point = Geoshape.point(i, i);
Geoshape line = Geoshape.line(Arrays.asList(new double[][] { { i - 1, i - 1 }, { i, i }, { i + 1, i + 1 } }));
Geoshape polygon = Geoshape.polygon(Arrays.asList(new double[][] { { i - 1, i - 1 }, { i, i - 1 }, { i + 1, 0 }, { i + 1, i + 1 }, { i - 1, i + 1 }, { i - 1, i - 1 } }));
Geoshape circle = Geoshape.circle(i, i, point.getPoint().distance(Geoshape.point(i, i).getPoint()) + 10);
assertTrue(circle.intersect(point));
assertTrue(point.intersect(circle));
assertTrue(circle.intersect(circle));
assertTrue(polygon.intersect(circle));
assertTrue(circle.intersect(polygon));
assertTrue(line.intersect(circle));
assertTrue(circle.intersect(line));
}
}
use of org.janusgraph.core.attribute.Geoshape in project janusgraph by JanusGraph.
the class GeoshapeTest method testDistance.
@Test
public void testDistance() {
Geoshape p1 = Geoshape.point(37.759, -122.536);
Geoshape p2 = Geoshape.point(35.714, -105.938);
double distance = 1496;
assertEquals(distance, p1.getPoint().distance(p2.getPoint()), 5.0);
}
Aggregations