Java Code Examples for org.springframework.data.geo.Metrics#KILOMETERS

The following examples show how to use org.springframework.data.geo.Metrics#KILOMETERS . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ArangoResultConverter.java    From spring-data with Apache License 2.0 6 votes vote down vote up
/**
 * Build a GeoResult from the given ArangoCursor
 *
 * @param cursor
 *            query result from driver
 * @return GeoResult object
 */
private GeoResult<?> buildGeoResult(final ArangoCursor<?> cursor) {
	GeoResult<?> geoResult = null;
	while (cursor.hasNext() && geoResult == null) {
		final Object object = cursor.next();
		if (!(object instanceof VPackSlice)) {
			continue;
		}

		final VPackSlice slice = (VPackSlice) object;
		final VPackSlice distSlice = slice.get("_distance");
		final Double distanceInMeters = distSlice.isDouble() ? distSlice.getAsDouble() : null;
		if (distanceInMeters == null) {
			continue;
		}

		final Object entity = operations.getConverter().read(domainClass, slice);
		final Distance distance = new Distance(distanceInMeters / 1000, Metrics.KILOMETERS);
		geoResult = new GeoResult<>(entity, distance);
	}
	return geoResult;
}
 
Example 2
Source File: ArangoResultConverter.java    From spring-data with Apache License 2.0 6 votes vote down vote up
/**
 * Construct a GeoResult from the given object
 *
 * @param object
 *            object representing one document in the result
 * @return GeoResult object
 */
private GeoResult<?> buildGeoResult(final Object object) {
	if (object == null || !(object instanceof VPackSlice)) {
		return null;
	}

	final VPackSlice slice = (VPackSlice) object;
	final VPackSlice distSlice = slice.get("_distance");
	final Double distanceInMeters = distSlice.isDouble() ? distSlice.getAsDouble() : null;
	if (distanceInMeters == null) {
		return null;
	}

	final Object entity = operations.getConverter().read(domainClass, slice);
	final Distance distance = new Distance(distanceInMeters / 1000, Metrics.KILOMETERS);
	return new GeoResult<>(entity, distance);
}
 
Example 3
Source File: DerivedQueryCreatorTest.java    From spring-data with Apache License 2.0 6 votes vote down vote up
@Test
public void findWithinOrNearTest() {
	final List<Customer> toBeRetrieved = new LinkedList<>();
	final Customer customer1 = new Customer("---", "", 0);
	customer1.setLocation(new int[] { 45, 2 });
	toBeRetrieved.add(customer1);
	final Customer customer2 = new Customer("+++", "", 0);
	customer2.setLocation(new int[] { 60, 1 });
	toBeRetrieved.add(customer2);
	repository.saveAll(toBeRetrieved);
	final Customer customer3 = new Customer("---", "", 0);
	customer3.setLocation(new int[] { 0, 180 });
	repository.save(customer3);
	final double distanceInMeters = convertAngleToDistance(30);
	final Distance distance = new Distance(distanceInMeters / 1000, Metrics.KILOMETERS);
	final Circle circle = new Circle(new Point(0, 20), distance);
	final Iterable<Customer> retrieved = repository.findByLocationWithinOrNameAndLocationNear(circle, "+++",
		new Point(0, 0));
	assertTrue(equals(toBeRetrieved, retrieved, cmp, eq, false));
}
 
Example 4
Source File: CustomerRepositoryIntegrationTest.java    From spring-data-examples with Apache License 2.0 6 votes vote down vote up
/**
 * Test case to show the usage of the geo-spatial APIs to lookup people within a given distance of a reference point.
 */
@Test
public void exposesGeoSpatialFunctionality() {

	GeospatialIndex indexDefinition = new GeospatialIndex("address.location");
	indexDefinition.getIndexOptions().put("min", -180);
	indexDefinition.getIndexOptions().put("max", 180);

	operations.indexOps(Customer.class).ensureIndex(indexDefinition);

	Customer ollie = new Customer("Oliver", "Gierke");
	ollie.setAddress(new Address(new Point(52.52548, 13.41477)));
	ollie = repository.save(ollie);

	Point referenceLocation = new Point(52.51790, 13.41239);
	Distance oneKilometer = new Distance(1, Metrics.KILOMETERS);

	GeoResults<Customer> result = repository.findByAddressLocationNear(referenceLocation, oneKilometer);

	assertThat(result.getContent(), hasSize(1));

	Distance distanceToFirstStore = result.getContent().get(0).getDistance();
	assertThat(distanceToFirstStore.getMetric(), is(Metrics.KILOMETERS));
	assertThat(distanceToFirstStore.getValue(), closeTo(0.862, 0.001));
}
 
Example 5
Source File: Neo4jQuerySupport.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
private static double calculateDistanceInMeter(Distance distance) {

		if (distance.getMetric() == Metrics.KILOMETERS) {
			double kilometersDivisor = 0.001d;
			return distance.getValue() / kilometersDivisor;

		} else if (distance.getMetric() == Metrics.MILES) {
			double milesDivisor = 0.00062137d;
			return distance.getValue() / milesDivisor;

		} else {
			return distance.getValue();
		}
	}
 
Example 6
Source File: DerivedQueryCreatorTest.java    From spring-data with Apache License 2.0 5 votes vote down vote up
@Test
public void buildPredicateWithDistanceTest() {
	final List<Customer> toBeRetrieved = new LinkedList<>();
	final Customer customer1 = new Customer("+", "", 0);
	customer1.setLocation(new int[] { 89, 89 });
	toBeRetrieved.add(customer1);
	final Customer customer2 = new Customer("", "+", 0);
	customer2.setLocation(new int[] { 5, 0 });
	toBeRetrieved.add(customer2);
	final Customer customer3 = new Customer("", "", 0);
	customer3.setLocation(new int[] { 0, 25 });
	toBeRetrieved.add(customer3);
	repository.saveAll(toBeRetrieved);
	final Customer customer4 = new Customer("", "", 0);
	customer4.setLocation(new int[] { 15, 0 });
	repository.save(customer4);
	final Customer customer5 = new Customer("", "", 0);
	customer5.setLocation(new int[] { 0, 35 });
	repository.save(customer5);
	final double distanceInMeters = convertAngleToDistance(10);
	final Distance distance = new Distance(distanceInMeters / 1000, Metrics.KILOMETERS);
	final Range<Distance> distanceRange = Range.of(
		Bound.inclusive(new Distance(convertAngleToDistance(20) / 1000, Metrics.KILOMETERS)),
		Bound.inclusive(new Distance(convertAngleToDistance(30) / 1000, Metrics.KILOMETERS)));
	final Point location = new Point(0, 0);
	final GeoResults<Customer> retrieved = repository.findByNameOrSurnameAndLocationWithinOrLocationWithin("+", "+",
		location, distance, location, distanceRange);
	final List<GeoResult<Customer>> expectedGeoResults = new LinkedList<>();
	expectedGeoResults.add(new GeoResult<>(customer1,
			new Distance(getDistanceBetweenPoints(location, new Point(89, 89)) / 1000, Metrics.KILOMETERS)));
	expectedGeoResults.add(new GeoResult<>(customer2,
			new Distance(getDistanceBetweenPoints(location, new Point(0, 5)) / 1000, Metrics.KILOMETERS)));
	expectedGeoResults.add(new GeoResult<>(customer3,
			new Distance(getDistanceBetweenPoints(location, new Point(25, 0)) / 1000, Metrics.KILOMETERS)));
	assertTrue(equals(expectedGeoResults, retrieved, geoCmp, geoEq, false));
}
 
Example 7
Source File: PersonRepositoryTests.java    From spring-data-examples with Apache License 2.0 5 votes vote down vote up
/**
 * Find entity by a {@link GeoIndexed} property on an embedded entity.
 */
@Test
public void findByGeoLocationProperty() {

	Address winterfell = new Address();
	winterfell.setCountry("the north");
	winterfell.setCity("winterfell");
	winterfell.setLocation(new Point(52.9541053, -1.2401016));

	eddard.setAddress(winterfell);

	Address casterlystein = new Address();
	casterlystein.setCountry("Westerland");
	casterlystein.setCity("Casterlystein");
	casterlystein.setLocation(new Point(51.5287352, -0.3817819));

	robb.setAddress(casterlystein);

	flushTestUsers();

	Circle innerCircle = new Circle(new Point(51.8911912, -0.4979756), new Distance(50, Metrics.KILOMETERS));
	List<Person> eddardStark = repository.findByAddress_LocationWithin(innerCircle);

	assertThat(eddardStark).containsOnly(robb);

	Circle biggerCircle = new Circle(new Point(51.8911912, -0.4979756), new Distance(200, Metrics.KILOMETERS));
	List<Person> eddardAndRobbStark = repository.findByAddress_LocationWithin(biggerCircle);

	assertThat(eddardAndRobbStark).hasSize(2).contains(robb, eddard);
}
 
Example 8
Source File: QueryIT.java    From spring-data-hazelcast with Apache License 2.0 5 votes vote down vote up
@Test
  public void findByLocationNearWithShape() {

      final City mumbai = new City("1001", "Mumbai", new Point(19.0990358,72.9612976));
      final City pune = new City("1002", "Pune", new Point(18.5247663,73.792756));
      final City bangalore = new City("1003", "Bangalore", new Point(12.9542944,77.4905127));

      final Point solapur = new Point(17.661548,75.8835121);
final Circle circle100 = new Circle(solapur, new Distance(100, Metrics.KILOMETERS));
final Circle circle250 = new Circle(solapur, new Distance(250, Metrics.KILOMETERS));
final Circle circle350 = new Circle(solapur, new Distance(350, Metrics.KILOMETERS));

      this.cityMap.put(mumbai.getId(), mumbai);
      this.cityMap.put(pune.getId(), pune);
      this.cityMap.put(bangalore.getId(), bangalore);

      List<City> matches = this.cityRepository.findByLocationWithin(circle100);
      int len = matches.size();
      assertThat("Nothing should returned", len, equalTo(0));

      matches = this.cityRepository.findByLocationWithin(circle250);
      len = matches.size();
      assertThat("Pune should return", len, equalTo(1));
      assertThat("Pune should return", matches.get(0), equalTo(pune));

      matches = this.cityRepository.findByLocationWithin(circle350);
      len = matches.size();
      assertThat("Pune and Mumbai should return", len, equalTo(2));
      assertThat("Pune and Mumbai should return", matches, containsInAnyOrder(pune, mumbai));

      this.cityMap.remove(mumbai.getId());
      this.cityMap.remove(pune.getId());
      this.cityMap.remove(bangalore.getId());
  }
 
Example 9
Source File: QueryIT.java    From spring-data-hazelcast with Apache License 2.0 5 votes vote down vote up
@Test
public void findByLocationNearPageable() {

    final City mumbai = new City("1001", "Mumbai", new Point(19.0990358,72.9612976));
    final City pune = new City("1002", "Pune", new Point(18.5247663,73.792756));
    final City bangalore = new City("1003", "Bangalore", new Point(12.9542944,77.4905127));

    final Point solapur = new Point(17.661548,75.8835121);
    final Distance distance1000 = new Distance(1000, Metrics.KILOMETERS);

    this.cityMap.put(mumbai.getId(), mumbai);
    this.cityMap.put(pune.getId(), pune);
    this.cityMap.put(bangalore.getId(), bangalore);
    final City[] cities = new City[]{bangalore,mumbai, pune};

    for(int i=0; i<3; i++){
        final Pageable pageRequest1 = PageRequest.of(i, SIZE_1);
        final Page<City> firstPage = this.cityRepository.findByLocationNear(solapur, distance1000, pageRequest1);
        assertThat("Page " + i + ", has content", firstPage.hasContent(), equalTo(true));
        List<City> firstPageList = firstPage.getContent();
        assertThat("Page " + i + ", one of three citys", firstPageList.size(), equalTo(1));
        assertThat("Page " + i + ", one of three citys", firstPageList.get(0), equalTo(cities[i]));
    }

    this.cityMap.remove(mumbai.getId());
    this.cityMap.remove(pune.getId());
    this.cityMap.remove(bangalore.getId());
}