本文整理汇总了Java中org.springframework.data.geo.Distance类的典型用法代码示例。如果您正苦于以下问题:Java Distance类的具体用法?Java Distance怎么用?Java Distance使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Distance类属于org.springframework.data.geo包,在下文中一共展示了Distance类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: exposesGeoSpatialFunctionality
import org.springframework.data.geo.Distance; //导入依赖的package包/类
/**
* 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));
}
示例2: extractDistanceString
import org.springframework.data.geo.Distance; //导入依赖的package包/类
/**
* extract the distance string from a {@link org.springframework.data.geo.Distance} object.
*
* @param distance distance object to extract string from
* @param sb StringBuilder to build the distance string
*/
private void extractDistanceString(Distance distance, StringBuilder sb) {
// handle Distance object
sb.append((int) distance.getValue());
Metrics metric = (Metrics) distance.getMetric();
switch (metric) {
case KILOMETERS:
sb.append("km");
break;
case MILES:
sb.append("mi");
break;
}
}
示例3: testGeoLocation
import org.springframework.data.geo.Distance; //导入依赖的package包/类
@Test
public void testGeoLocation() {
ExampleSolrBean searchableBeanInBuffalow = createExampleBeanWithId("1");
searchableBeanInBuffalow.setStore("45.17614,-93.87341");
ExampleSolrBean searchableBeanInNYC = createExampleBeanWithId("2");
searchableBeanInNYC.setStore("40.7143,-74.006");
solrTemplate.saveBeans(Arrays.asList(searchableBeanInBuffalow, searchableBeanInNYC));
solrTemplate.commit();
Page<ExampleSolrBean> result = solrTemplate.queryForPage(
new SimpleQuery(new Criteria("store").near(new Point(45.15, -93.85), new Distance(5))), ExampleSolrBean.class);
Assert.assertEquals(1, result.getContent().size());
}
示例4: testGeoLocationWithDistanceInMiles
import org.springframework.data.geo.Distance; //导入依赖的package包/类
@Test
public void testGeoLocationWithDistanceInMiles() {
ExampleSolrBean searchableBeanInBuffalow = createExampleBeanWithId("1");
searchableBeanInBuffalow.setStore("45.17614,-93.87341");
ExampleSolrBean searchableBeanInNYC = createExampleBeanWithId("2");
searchableBeanInNYC.setStore("40.7143,-74.006");
solrTemplate.saveBeans(Arrays.asList(searchableBeanInBuffalow, searchableBeanInNYC));
solrTemplate.commit();
Page<ExampleSolrBean> result = solrTemplate.queryForPage(
new SimpleQuery(new Criteria("store").near(new Point(45.15, -93.85), new Distance(3.106856, Metrics.MILES))),
ExampleSolrBean.class);
Assert.assertEquals(1, result.getContent().size());
}
示例5: geoRadiusByMember
import org.springframework.data.geo.Distance; //导入依赖的package包/类
/**
* Look up points using a geo-index member as reference.
*/
@Test
public void geoRadiusByMember() {
GeoResults<GeoLocation<String>> byDistance = geoOperations.geoRadiusByMember("Sicily", "Palermo",
new Distance(100, DistanceUnit.KILOMETERS));
assertThat(byDistance).hasSize(2).extracting("content.name").contains("Arigento", "Palermo");
GeoResults<GeoLocation<String>> greaterDistance = geoOperations.geoRadiusByMember("Sicily", "Palermo",
new Distance(200, DistanceUnit.KILOMETERS));
assertThat(greaterDistance).hasSize(3).extracting("content.name").contains("Arigento", "Catania", "Palermo");
}
示例6: geoRadius
import org.springframework.data.geo.Distance; //导入依赖的package包/类
/**
* Lookup points within a circle around coordinates.
*/
@Test
public void geoRadius() {
Circle circle = new Circle(new Point(13.583333, 37.316667), //
new Distance(100, DistanceUnit.KILOMETERS));
GeoResults<GeoLocation<String>> result = geoOperations.geoRadius("Sicily", circle);
assertThat(result).hasSize(2).extracting("content.name").contains("Arigento", "Palermo");
}
示例7: geoDistance
import org.springframework.data.geo.Distance; //导入依赖的package包/类
/**
* Calculate the distance between two geo-index members.
*/
@Test
public void geoDistance() {
Distance distance = geoOperations.geoDist("Sicily", "Catania", "Palermo", DistanceUnit.KILOMETERS);
assertThat(distance.getValue()).isBetween(130d, 140d);
}
示例8: findByGeoLocationProperty
import org.springframework.data.geo.Distance; //导入依赖的package包/类
/**
* 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, hasItem(robb));
assertThat(eddardStark, hasSize(1));
Circle biggerCircle = new Circle(new Point(51.8911912, -0.4979756), new Distance(200, Metrics.KILOMETERS));
List<Person> eddardAndRobbStark = repository.findByAddress_LocationWithin(biggerCircle);
assertThat(eddardAndRobbStark, hasItems(robb, eddard));
assertThat(eddardAndRobbStark, hasSize(2));
}
示例9: index
import org.springframework.data.geo.Distance; //导入依赖的package包/类
/**
* Looks up the stores in the given distance around the given location.
*
* @param model the {@link Model} to populate.
* @param location the optional location, if none is given, no search results will be returned.
* @param distance the distance to use, if none is given the {@link #DEFAULT_DISTANCE} is used.
* @param pageable the pagination information
* @return
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
String index(Model model, @RequestParam Optional<Point> location, @RequestParam Optional<Distance> distance,
Pageable pageable) {
Point point = location.orElse(KNOWN_LOCATIONS.get("Timesquare NY"));
Page<Store> stores = repository.findByAddressLocationNear(point, distance.orElse(DEFAULT_DISTANCE), pageable);
model.addAttribute("stores", stores);
model.addAttribute("distances", DISTANCES);
model.addAttribute("selectedDistance", distance.orElse(DEFAULT_DISTANCE));
model.addAttribute("location", point);
model.addAttribute("locations", KNOWN_LOCATIONS);
model.addAttribute("api", entityLinks.linkToSearchResource(Store.class, "by-location", pageable).getHref());
return "index";
}
示例10: findsStoresByLocation
import org.springframework.data.geo.Distance; //导入依赖的package包/类
@Test
public void findsStoresByLocation() {
Point location = new Point(-73.995146, 40.740337);
Store store = new Store("Foo", new Address("street", "city", "zip", location));
store = repository.save(store);
Page<Store> stores = repository.findByAddressLocationNear(location, new Distance(1.0, Metrics.KILOMETERS),
new PageRequest(0, 10));
assertThat(stores.getContent(), hasSize(1));
assertThat(stores.getContent(), hasItem(store));
}
示例11: getProductsByLocation
import org.springframework.data.geo.Distance; //导入依赖的package包/类
@Override
public List<Product> getProductsByLocation(String LatLng) throws GeoLocationException {
List<Product> found;
try {
Point point = GeoConverters.StringToPointConverter.INSTANCE.convert(LatLng);
found =
customProductRepository.findByLocationNear(new Point(point.getX(), point.getY()), new Distance(30));
} catch (Exception e) {
logger.debug("No location found with coordinates: {}", LatLng);
throw new GeoLocationException("Error in mapping latLng: " + LatLng);
}
return found;
}
示例12: testFindByLocationCriteria
import org.springframework.data.geo.Distance; //导入依赖的package包/类
@Test
public void testFindByLocationCriteria() {
Point location = new Point(22.15, -90.85);
Criteria criteria = new Criteria("store").near(location, new Distance(5));
Page<Product> result =
solrOperations.queryForPage(new SimpleQuery(criteria), Product.class);
Assert.assertEquals(1, result.getTotalElements());
Assert.assertEquals(locatedInYonkers.getId(), result.getContent().get(0).getId());
}
示例13: convert
import org.springframework.data.geo.Distance; //导入依赖的package包/类
private Q convert(Condition condition, String fieldName) {
Object value = condition.getValue();
switch (condition.getKey()) {
case EQUALS:
return queryBuilder.equal(fieldName, value);
case NULL:
return queryBuilder.isNull(fieldName);
case IN:
return queryBuilder.in(fieldName, (Collection<?>) value);
case GREATER:
return queryBuilder.greaterThan(fieldName, value);
case GREATER_EQUAL:
return queryBuilder.greaterThanOrEqual(fieldName, value);
case LESS:
return queryBuilder.lessThan(fieldName, value);
case LESS_EQUAL:
return queryBuilder.lessThanOrEqual(fieldName, value);
case BETWEEN:
Object[] ranges = (Object[]) value;
return queryBuilder.between(fieldName, ranges[0], ranges[1]);
case CONTAINS:
return queryBuilder.contains(fieldName, value);
case STARTS_WITH:
return queryBuilder.startsWith(fieldName, value);
case ENDS_WITH:
return queryBuilder.endsWith(fieldName, value);
case REGEXP:
return queryBuilder.reqexp(fieldName, String.valueOf(value));
case WITHIN:
Object[] params = (Object[]) value;
Double latitude = (Double) params[0];
Double longitude = (Double) params[1];
Distance distance = (Distance) params[2];
double distanceInKm = toKm(distance);
return queryBuilder.spatial(fieldName, latitude, longitude, distanceInKm);
default:
throw new IllegalArgumentException("Unsupported operator " + condition.getKey());
}
}
示例14: toKm
import org.springframework.data.geo.Distance; //导入依赖的package包/类
public static double toKm(Distance distance) {
Metric metric = distance.getMetric();
if (Metrics.KILOMETERS.equals(metric)) {
return distance.getValue();
} else if (Metrics.MILES.equals(metric)) {
return distance.getValue() * 1.609344;
} else {
throw new IllegalArgumentException("Unknown metric: " + metric);
}
}
示例15: createNearCriteria
import org.springframework.data.geo.Distance; //导入依赖的package包/类
private Criteria createNearCriteria(Iterator<?> parameters, Criteria criteria) {
Object value = getBindableValue((BindableSolrParameter) parameters.next());
if (value instanceof Box) {
return criteria.near((Box) value);
} else {
return criteria.near((Point) value, (Distance) getBindableValue((BindableSolrParameter) parameters.next()));
}
}