public static Multimap<String, Object> getRandomDocument() { final StringBuilder s = new StringBuilder(); for (int i = 0; i < 3; i++) s.append(RandomGenerator.randomString(5, 8)).append(" "); final Multimap<String, Object> values = HashMultimap.create(); values.put(TEXT, s.toString()); values.put(NAME, s.toString()); values.put(TIME, Math.abs(random.nextLong())); values.put(WEIGHT, random.nextDouble()); values.put(LOCATION, Geoshape.point(random.nextDouble() * 180 - 90, random.nextDouble() * 360 - 180)); return values; }
@Override public Geoshape createObject(Map<String, Object> data) { final Geoshape shape; if (data.containsKey(FIELD_COORDINATES) && data.get(FIELD_COORDINATES) instanceof List) { final List<Number> coordinates = (List<Number>) data.get(FIELD_COORDINATES); if (coordinates.size() < 2) throw new RuntimeException("Expecting two coordinates when reading point"); shape = Geoshape.point(coordinates.get(1).doubleValue(), coordinates.get(0).doubleValue()); } else { try { final String json = mapWriter.writeValueAsString(data.get("geometry")); shape = new Geoshape(HELPER.getGeojsonReader().read(new StringReader(json))); } catch (IOException | ParseException e) { throw new RuntimeException("I/O exception reading geoshape", e); } } return shape; } }
switch (len) { case 2: return point(arr[0], arr[1]); case 3: return circle(arr[0], arr[1], arr[2]);
@Test public void clearStorageTest() throws Exception { final String store = "vertex"; initialize(store); final Multimap<String, Object> doc1 = getDocument("Hello world", 1001, 5.2, Geoshape.point(48.0, 0.0), Geoshape.polygon(Arrays.asList(new double[][] {{-0.1,47.9},{0.1,47.9},{0.1,48.1},{-0.1,48.1},{-0.1,47.9}})),Arrays.asList("1", "2", "3"), Sets.newHashSet("1", "2"), Instant.ofEpochSecond(1)); add(store, "doc1", doc1, true); clopen(); assertTrue(index.exists()); tearDown(); setUp(); assertFalse(index.exists()); }
@Override public Geoshape read(Kryo kryo, Input input, Class<Geoshape> aClass) { final long l = input.readLong(); assert l>0 && l<Integer.MAX_VALUE; final int length = (int) l; try { final InputStream inputStream = new ByteArrayInputStream(input.readBytes(length)); return GeoshapeBinarySerializer.read(inputStream); } catch (IOException | KryoException e) { // retry using legacy point deserialization try { input.setPosition(0); input.readLong(); final float lat = input.readFloat(); final float lon = input.readFloat(); return point(lat, lon); } catch (KryoException ignored) { } // throw original exception throw new RuntimeException("I/O exception reading geoshape", e); } } }
@Override public Geoshape deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { jsonParser.nextToken(); if (jsonParser.getCurrentName().equals(FIELD_COORDINATES)) { double[] f = jsonParser.readValueAs(double[].class); jsonParser.nextToken(); return Geoshape.point(f[1], f[0]); } else { try { HashMap map = jsonParser.readValueAs(LinkedHashMap.class); jsonParser.nextToken(); String json = mapWriter.writeValueAsString(map); return new Geoshape(HELPER.getGeojsonReader().read(new StringReader(json))); } catch (ParseException e) { throw new IOException("Unable to read and parse geojson", e); } } } }
case "Point": { double[] parsedCoordinates = convertCollection(coordinates); return point(parsedCoordinates[1], parsedCoordinates[0]);
@Override public Geoshape read(ScanBuffer buffer) { long l = VariableLong.readPositive(buffer); assert l>0 && l<Integer.MAX_VALUE; int length = (int)l; int position = ((ReadArrayBuffer) buffer).getPosition(); InputStream inputStream = new ByteArrayInputStream(buffer.getBytes(length)); try { return GeoshapeBinarySerializer.read(inputStream); } catch (IOException e) { // retry using legacy point deserialization try { ((ReadArrayBuffer) buffer).movePositionTo(position); final float lat = buffer.getFloat(); final float lon = buffer.getFloat(); return point(lat, lon); } catch (Exception ignored) { } // throw original exception throw new RuntimeException("I/O exception reading geoshape", e); } }
private void storeTest(String... stores) throws Exception { final Multimap<String, Object> doc1 = getDocument("Hello world", 1001, 5.2, Geoshape.point(48.0, 0.0), Geoshape.polygon(Arrays.asList(new double[][] {{-0.1,47.9},{0.1,47.9},{0.1,48.1},{-0.1,48.1},{-0.1,47.9}})),Arrays.asList("1", "2", "3"), Sets.newHashSet("1", "2"), Instant.ofEpochSecond(1)); final Multimap<String, Object> doc2 = getDocument("Tomorrow is the world", 1010, 8.5, Geoshape.point(49.0, 1.0), Geoshape.line(Arrays.asList(new double[][] {{0.9,48.9},{0.9,49.1},{1.1,49.1},{1.1,48.9}})), Arrays.asList("4", "5", "6"), Sets.newHashSet("4", "5"), Instant.ofEpochSecond(2)); final Multimap<String, Object> doc3 = getDocument("Hello Bob, are you there?", -500, 10.1, Geoshape.point(47.0, 10.0), Geoshape.box(46.9, 9.9, 47.1, 10.1), Arrays.asList("7", "8", "9"), Sets.newHashSet("7", "8"), Instant.ofEpochSecond(3)); result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(BOUNDARY, Geo.CONTAINS, Geoshape.point(47, 10)))).collect(Collectors.toList()); assertEquals(1, result.size()); assertEquals(ImmutableSet.of("doc3"), ImmutableSet.copyOf(result)); add(store, "doc4", getDocument("I'ts all a big Bob", -100, 11.2, Geoshape.point(-48.0, 8.0), Geoshape.point(-48.0, 8.0), Arrays.asList("10", "11", "12"), Sets.newHashSet("10", "11"), Instant.ofEpochSecond(4)), true); remove(store, "doc2", doc2, true); remove(store, "doc3", ImmutableMultimap.of(WEIGHT, 10.1), false); add(store, "doc3", ImmutableMultimap.of(TIME, 2000, TEXT, "Bob owns the world"), false); remove(store, "doc1", ImmutableMultimap.of(TIME, 1001), false); add(store, "doc1", ImmutableMultimap.of(TIME, 1005, WEIGHT, 11.1, LOCATION, Geoshape.point(-48.0, 0.0), BOUNDARY, Geoshape.circle(-48.0, 0.0, 1.0)), false); final Geoshape multiPoint = Geoshape.geoshape(Geoshape.getShapeFactory().multiPoint().pointXY(60.0, 60.0).pointXY(120.0, 60.0).build()); add(store, "doc5", getDocument("A Full Yes", -100, -11.2, Geoshape.point(48.0, 8.0), multiPoint, Arrays.asList("10", "11", "12"), Sets.newHashSet("10", "11"), Instant.ofEpochSecond(400)), true); final Geoshape multiLine = Geoshape.geoshape(Geoshape.getShapeFactory().multiLineString().add(Geoshape.getShapeFactory().lineString().pointXY(59.0, 60.0).pointXY(61.0, 60.0)) .add(Geoshape.getShapeFactory().lineString().pointXY(119.0, 60.0).pointXY(121.0, 60.0)).build()); add(store, "doc6", getDocument("A Full Yes", -100, -11.2, Geoshape.point(48.0, 8.0), multiLine, Arrays.asList("10", "11", "12"), Sets.newHashSet("10", "11"), Instant.ofEpochSecond(400)), true); final Geoshape multiPolygon = Geoshape.geoshape(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()); add(store, "doc7", getDocument("A Full Yes", -100, -11.2, Geoshape.point(48.0, 8.0), multiPolygon, Arrays.asList("10", "11", "12"), Sets.newHashSet("10", "11"), Instant.ofEpochSecond(400)), true); final Geoshape geometryCollection = Geoshape.geoshape(Geoshape.getGeometryCollectionBuilder().add(Geoshape.getShapeFactory().pointXY(60.0, 60.0)) .add(Geoshape.getShapeFactory().lineString().pointXY(119.0, 60.0).pointXY(121.0, 60.0).build()).build()); add(store, "doc8", getDocument("A Full Yes", -100, -11.2, Geoshape.point(48.0, 8.0), geometryCollection, Arrays.asList("10", "11", "12"), Sets.newHashSet("10", "11"), Instant.ofEpochSecond(400)), true);
private void testIndexing(Cardinality cardinality) { if (supportsCollections()) { final PropertyKey stringProperty = mgmt.makePropertyKey("name").dataType(String.class).cardinality(cardinality).make(); final PropertyKey intProperty = mgmt.makePropertyKey("age").dataType(Integer.class).cardinality(cardinality).make(); final PropertyKey longProperty = mgmt.makePropertyKey("long").dataType(Long.class).cardinality(cardinality).make(); final PropertyKey uuidProperty = mgmt.makePropertyKey("uuid").dataType(UUID.class).cardinality(cardinality).make(); final PropertyKey geopointProperty = mgmt.makePropertyKey("geopoint").dataType(Geoshape.class).cardinality(cardinality).make(); mgmt.buildIndex("collectionIndex", Vertex.class).addKey(stringProperty, getStringMapping()).addKey(intProperty).addKey(longProperty).addKey(uuidProperty).addKey(geopointProperty).buildMixedIndex(INDEX); finishSchema(); testCollection(cardinality, "name", "Totoro", "Hiro"); testCollection(cardinality, "age", 1, 2); testCollection(cardinality, "long", 1L, 2L); testCollection(cardinality, "geopoint", Geoshape.point(1.0, 1.0), Geoshape.point(2.0, 2.0)); final String backend = readConfig.get(INDEX_BACKEND, INDEX); // Solr 6 has issues processing UUIDs with Multivalues // https://issues.apache.org/jira/browse/SOLR-11264 if (!"solr".equals(backend)) { testCollection(cardinality, "uuid", UUID.randomUUID(), UUID.randomUUID()); } } else { try { final PropertyKey stringProperty = mgmt.makePropertyKey("name").dataType(String.class).cardinality(cardinality).make(); //This should throw an exception mgmt.buildIndex("collectionIndex", Vertex.class).addKey(stringProperty, getStringMapping()).buildMixedIndex(INDEX); Assert.fail("Should have thrown an exception"); } catch (final JanusGraphException ignored) { } } }
v.property(VertexProperty.Cardinality.single, "time", i); offset = (i % 2 == 0 ? 1 : -1) * (i * 50.0 / numV); v.property(VertexProperty.Cardinality.single, "location", Geoshape.point(0.0 + offset, 0.0 + offset)); if (i % 2 == 0) { v.property(VertexProperty.Cardinality.single, "boundary", Geoshape.line(Arrays.asList(new double[][] { e.property("category", i % numCategories); e.property("group", i % numGroups); e.property("location", Geoshape.point(0.0 + offset, 0.0 + offset)); if (i % 2 == 0) { e.property("boundary", Geoshape.line(Arrays.asList(new double[][] {
final Geoshape point = Geoshape.point(10.0, 10.0); final Geoshape shape = Geoshape.box(10.0, 10.0, 20.0, 20.0);
private void testGeo(int i, int origNumV, int numV) { final double offset = (i * 50.0 / origNumV); final double bufferKm = 20; final double distance = Geoshape.point(0.0, 0.0).getPoint().distance(Geoshape.point(offset, offset).getPoint()) + bufferKm; 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());
final double distance = Geoshape.point(0.0, 0.0).getPoint().distance(Geoshape.point(offset, offset).getPoint()) + 20; assertCount(5, tx.query().has("location", Geo.INTERSECT, Geoshape.circle(0.0, 0.0, distance)).has("text", Text.CONTAINS, words[0]).vertices()); assertCount(5, tx.query().has("boundary", Geo.INTERSECT, Geoshape.circle(0.0, 0.0, distance)).has("text", Text.CONTAINS, words[0]).vertices());