org.springframework.data.geo.Metrics Java Examples
The following examples show how to use
org.springframework.data.geo.Metrics.
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 |
/** * 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 |
/** * 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 |
@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: DerivedQueryCreatorTest.java From spring-data with Apache License 2.0 | 6 votes |
@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 |
@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: QueryIT.java From spring-data-hazelcast with Apache License 2.0 | 6 votes |
@Test public void countByLocationNear() { 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)); Point solapur = new Point(17.661548,75.8835121); this.cityMap.put(mumbai.getId(), mumbai); this.cityMap.put(pune.getId(), pune); this.cityMap.put(bangalore.getId(), bangalore); Long cities = this.cityRepository.countByLocationNear(solapur, new Distance(100, Metrics.KILOMETERS)); assertThat("Nothing should returned", cities, equalTo(0L)); cities = this.cityRepository.countByLocationNear(solapur, new Distance(250, Metrics.KILOMETERS)); assertThat("Pune should return", cities, equalTo(1L)); cities = this.cityRepository.countByLocationNear(solapur, new Distance(350, Metrics.KILOMETERS)); assertThat("Pune and Mumbai should return", cities, equalTo(2L)); this.cityMap.remove(mumbai.getId()); this.cityMap.remove(pune.getId()); this.cityMap.remove(bangalore.getId()); }
Example #7
Source File: GeoPredicate.java From spring-data-hazelcast with Apache License 2.0 | 6 votes |
/** * This method users Haversine formula to calculate the distance between two points * Formula is explained here - https://www.movable-type.co.uk/scripts/gis-faq-5.1.html * Sample Java code is here - https://rosettacode.org/wiki/Haversine_formula#Java * @param lat1 - Latitude of first point. * @param lng1 - Longitude of first point. * @param lat2 - Latitude of second point. * @param lng2 - Longitude of second point. * @param metric - metric to specify where its KILOMETERS, MILES or NEUTRAL * @return */ private double calculateDistance(double lat1, double lng1, double lat2, double lng2, Metric metric) { if ((lat1 == lat2) && (lng1 == lng2)) { return 0; } else { double dLat = Math.toRadians(lat2 - lat1); double dLon = Math.toRadians(lng2 - lng1); double lat1Radians = Math.toRadians(lat1); double lat2Radians = Math.toRadians(lat2); double a = Math.pow(Math.sin(dLat / 2), 2) + Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1Radians) * Math.cos(lat2Radians); double c = 2 * Math.asin(Math.sqrt(a)); double dist = R * c; if (Metrics.MILES.equals(metric)) { dist = dist * KM_TO_MILES; } else if (Metrics.NEUTRAL.equals(metric)) { dist = dist * KM_TO_NEUTRAL; } return dist; } }
Example #8
Source File: CustomerRepositoryIntegrationTest.java From spring-data-examples with Apache License 2.0 | 6 votes |
/** * 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 #9
Source File: GeoConverters.java From dubbox with Apache License 2.0 | 5 votes |
@Override public String convert(org.springframework.data.geo.Distance source) { if (source == null) { return null; } double value = source.getValue(); if (source.getMetric() == Metrics.MILES) { value = source.getValue() * 1.609344D; } return String.format(java.util.Locale.ENGLISH, "%s", value); }
Example #10
Source File: CampusServiceImplLiveTest.java From tutorials with MIT License | 5 votes |
@Test public final void whenFindByLocationNearNewYorkCity_thenResultContainsColumbia() throws Exception { Set<Campus> campuses = campusService.findByLocationNear(NewYorkCity, new Distance(1, Metrics.NEUTRAL)); assertFalse(campuses.isEmpty()); assertTrue(campuses.contains(Columbia)); assertFalse(campuses.contains(Harvard)); }
Example #11
Source File: CampusServiceImplLiveTest.java From tutorials with MIT License | 5 votes |
@Test public final void whenFindByLocationNearBoston_thenResultContainsHarvard() throws Exception { Set<Campus> campuses = campusService.findByLocationNear(Boston, new Distance(1, Metrics.NEUTRAL)); assertFalse(campuses.isEmpty()); assertTrue(campuses.contains(Harvard)); assertFalse(campuses.contains(Columbia)); }
Example #12
Source File: QueryIT.java From spring-data-hazelcast with Apache License 2.0 | 5 votes |
@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()); }
Example #13
Source File: QueryIT.java From spring-data-hazelcast with Apache License 2.0 | 5 votes |
@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 #14
Source File: QueryIT.java From spring-data-hazelcast with Apache License 2.0 | 5 votes |
@Test public void findByLocationNearWithDistance() { 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)); Point solapur = new Point(17.661548,75.8835121); this.cityMap.put(mumbai.getId(), mumbai); this.cityMap.put(pune.getId(), pune); this.cityMap.put(bangalore.getId(), bangalore); List<City> matches = this.cityRepository.findByLocationNear(solapur, new Distance(100, Metrics.KILOMETERS)); int len = matches.size(); assertThat("Nothing should returned", len, equalTo(0)); matches = this.cityRepository.findByLocationNear(solapur, new Distance(250, Metrics.KILOMETERS)); len = matches.size(); assertThat("Pune should return", len, equalTo(1)); assertThat("Pune should return", matches.get(0), equalTo(pune)); matches = this.cityRepository.findByLocationNear(solapur, new Distance(350, Metrics.KILOMETERS)); 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 #15
Source File: StoreRepositoryIntegrationTests.java From spring-data-examples with Apache License 2.0 | 5 votes |
@Test public void findsStoresByLocation() { Point location = new Point(-73.995146, 40.740337); Store store = new Store(UUID.randomUUID(), "Foo", new Address("street", "city", "zip", location)); store = repository.save(store); Page<Store> stores = repository.findByAddressLocationNear(location, new Distance(1.0, Metrics.KILOMETERS), PageRequest.of(0, 10)); assertThat(stores.getContent(), hasSize(1)); assertThat(stores.getContent(), hasItem(store)); }
Example #16
Source File: PersonRepositoryTests.java From spring-data-examples with Apache License 2.0 | 5 votes |
/** * 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 #17
Source File: Neo4jQuerySupport.java From sdn-rx with Apache License 2.0 | 5 votes |
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 #18
Source File: DerivedQueryCreatorTest.java From spring-data with Apache License 2.0 | 5 votes |
@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 #19
Source File: DerivedQueryCreatorTest.java From spring-data with Apache License 2.0 | 5 votes |
@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 #20
Source File: MongoRepository.java From flash-waimai with MIT License | 5 votes |
/** * 查询指定位置附近的商家 * @param x * @param y * @param collectionName * @param params * @param miles 公里数 * @return */ public GeoResults<Map> near(double x, double y, String collectionName, Map<String, Object> params,Integer miles) { Point location = new Point(x, y); NearQuery nearQuery = NearQuery.near(location).maxDistance(new Distance(miles, Metrics.MILES)); if (params != null && !params.isEmpty()) { Query query = Query.query(criteria(params)); nearQuery.query(query); } try { return mongoTemplate.geoNear(nearQuery, Map.class, collectionName); }catch (Exception e){ System.out.println(e.getMessage()); } return null; }
Example #21
Source File: BindParameterBinding.java From spring-data with Apache License 2.0 | 4 votes |
private double convertDistanceToMeters(final Distance distance) { return distance.getNormalizedValue() * Metrics.KILOMETERS.getMultiplier() * 1000; }