org.springframework.data.geo.GeoResult Java Examples

The following examples show how to use org.springframework.data.geo.GeoResult. 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: RestaurantController.java    From flash-waimai with MIT License 7 votes vote down vote up
@RequestMapping(value = "/v4/restaurants", method = RequestMethod.GET)
public Object restaurants(@RequestParam("geohash") String geoHash, @RequestParam("keyword") String keyWord) {
    String[] geoHashArr = geoHash.split(",");
    String longitude = geoHashArr[1];
    String latitude = geoHashArr[0];
    Map<String, Object> params = Maps.newHashMap("name", keyWord);
    System.out.println(Json.toJson(params));
    GeoResults<Map> geoResults = mongoRepository.near(Double.valueOf(longitude), Double.valueOf(latitude),
            "shops", params);
    List<GeoResult<Map>> geoResultList = geoResults.getContent();
    List<Map> list = Lists.newArrayList();
    for (int i = 0; i < geoResultList.size(); i++) {
        Map map = geoResultList.get(i).getContent();
        Distance distance = new Distance(Double.valueOf(longitude), Double.valueOf(latitude),
                Double.valueOf(map.get("longitude").toString()), Double.valueOf(map.get("latitude").toString()));
        map.put("distance", distance.getDistance());
        list.add(map);
    }
    return Rets.success(list);        
}
 
Example #2
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 #3
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 #4
Source File: DerivedQueryCreatorTest.java    From spring-data with Apache License 2.0 6 votes vote down vote up
@Test
public void geoResultTest() {
	final Customer customer1 = new Customer("", "", 0);
	customer1.setLocation(new int[] { 7, 5 });
	repository.save(customer1);
	final Customer customer2 = new Customer("", "", 0);
	customer2.setLocation(new int[] { 70, 50 });
	repository.save(customer2);
	final double distance = convertAngleToDistance(10);
	final GeoResult<Customer> retrieved = repository.queryByLocationWithin(new Point(1, 2), distance);
	final double expectedDistanceInMeters = getDistanceBetweenPoints(new Point(5, 7), new Point(1, 2));
	final double expectedNormalizedDistance = expectedDistanceInMeters / 1000.0
			/ Metrics.KILOMETERS.getMultiplier();
	assertEquals(customer1, retrieved.getContent());
	assertEquals(expectedNormalizedDistance, retrieved.getDistance().getNormalizedValue(), 0.000000001);
}
 
Example #5
Source File: DerivedQueryCreatorTest.java    From spring-data with Apache License 2.0 6 votes vote down vote up
@Test
public void geoPageTest() {
	final Customer customer1 = new Customer("", "", 0);
	customer1.setLocation(new int[] { 2, 0 });
	repository.save(customer1);
	final Customer customer2 = new Customer("", "", 0);
	customer2.setLocation(new int[] { 3, 0 });
	repository.save(customer2);
	final Customer customer3 = new Customer("", "", 0);
	customer3.setLocation(new int[] { 4, 0 });
	repository.save(customer3);
	final Customer customer4 = new Customer("", "", 0);
	customer4.setLocation(new int[] { 6, 0 });
	repository.save(customer4);
	final GeoPage<Customer> retrieved = repository.findByLocationNear(new Point(0, 0), PageRequest.of(1, 2));
	final List<GeoResult<Customer>> expectedGeoResults = new LinkedList<>();
	expectedGeoResults.add(new GeoResult<>(customer3,
			new Distance(getDistanceBetweenPoints(new Point(0, 0), new Point(0, 4)) / 1000, Metrics.KILOMETERS)));
	expectedGeoResults.add(new GeoResult<>(customer4,
			new Distance(getDistanceBetweenPoints(new Point(0, 0), new Point(0, 6)) / 1000, Metrics.KILOMETERS)));
	assertEquals(4, retrieved.getTotalElements());
	assertEquals(2, retrieved.getTotalPages());
	assertTrue(equals(expectedGeoResults, retrieved, geoCmp, geoEq, true));
}
 
Example #6
Source File: ArangoResultConverter.java    From spring-data with Apache License 2.0 5 votes vote down vote up
/**
 * Build a GeoResults object with the ArangoCursor returned by query execution
 *
 * @param cursor
 *            ArangoCursor containing query results
 * @return GeoResults object with all results
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
private GeoResults<?> buildGeoResults(final ArangoCursor<?> cursor) {
	final List<GeoResult<?>> list = new LinkedList<>();
	cursor.forEachRemaining(o -> {
		final GeoResult<?> geoResult = buildGeoResult(o);
		if (geoResult != null) {
			list.add(geoResult);
		}
	});
	return new GeoResults(list);
}
 
Example #7
Source File: DerivedQueryCreatorTest.java    From spring-data with Apache License 2.0 5 votes vote down vote up
@Test
public void geoResultsTest() {
	final List<Customer> toBeRetrieved = new LinkedList<>();
	final Customer customer1 = new Customer("", "", 0);
	customer1.setLocation(new int[] { 43, 21 });
	toBeRetrieved.add(customer1);
	final Customer customer2 = new Customer("", "", 0);
	customer2.setLocation(new int[] { 21, 43 });
	toBeRetrieved.add(customer2);
	repository.saveAll(toBeRetrieved);
	final Customer customer3 = new Customer("", "", 0);
	customer3.setLocation(new int[] { 70, 50 });
	repository.save(customer3);
	final Customer customer4 = new Customer("", "", 0);
	customer4.setLocation(new int[] { 3, 2 });
	repository.save(customer4);
	final Bound<Double> lowerBound = Bound.inclusive(convertAngleToDistance(30));
	final Bound<Double> upperBound = Bound.inclusive(convertAngleToDistance(50));
	final GeoResults<Customer> retrieved = repository.findByLocationWithin(new Point(1, 0),
		Range.of(lowerBound, upperBound));
	final List<GeoResult<Customer>> expectedGeoResults = new LinkedList<>();
	expectedGeoResults.add(new GeoResult<>(customer1,
			new Distance(getDistanceBetweenPoints(new Point(1, 0), new Point(21, 43)) / 1000, Metrics.KILOMETERS)));
	expectedGeoResults.add(new GeoResult<>(customer2,
			new Distance(getDistanceBetweenPoints(new Point(1, 0), new Point(43, 21)) / 1000, Metrics.KILOMETERS)));
	assertTrue(equals(expectedGeoResults, retrieved, geoCmp, geoEq, false));
}
 
Example #8
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 #9
Source File: TaxiController.java    From Spring-Boot-2.0-Projects with MIT License 4 votes vote down vote up
@GetMapping
public Flux<TaxiAvailableResponseDTO> getAvailableTaxis(@RequestParam("type") TaxiType taxiType, @RequestParam("latitude") Double latitude, @RequestParam("longitude") Double longitude, @RequestParam(value = "radius", defaultValue = "1") Double radius) {
    Flux<GeoResult<RedisGeoCommands.GeoLocation<String>>> availableTaxisFlux = taxiService.getAvailableTaxis(taxiType, latitude, longitude, radius);
    return availableTaxisFlux.map(r -> new TaxiAvailableResponseDTO(r.getContent().getName()));
}
 
Example #10
Source File: ArangoResultConverter.java    From spring-data with Apache License 2.0 4 votes vote down vote up
public GeoResult<?> convertGeoResult() {
	return buildGeoResult(result);
}
 
Example #11
Source File: CustomerRepository.java    From spring-data with Apache License 2.0 votes vote down vote up
GeoResult<Customer> queryByLocationWithin(Point location, double distance);