org.springframework.data.geo.Point Java Examples
The following examples show how to use
org.springframework.data.geo.Point.
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: MongoDbIntegrationTests.java From spring-tutorials with Apache License 2.0 | 6 votes |
@Test public void test_GeoSpacialQuery() { mongoTemplate.indexOps(Venue.class).ensureIndex(new GeospatialIndex("location")); mongoTemplate.insert(new Venue("Venue 1", new Point(51.10682735591432, -114.11773681640625))); mongoTemplate.insert(new Venue("Venue 2", new Point(51.09144802136697, -114.10400390625))); mongoTemplate.insert(new Venue("Venue 3", new Point(51.08282186160978, -114.10400390625))); mongoTemplate.insert(new Venue("Venue 4", new Point(51.12076493195686, -113.98040771484375))); mongoTemplate.insert(new Venue("Venue 5", new Point(50.93939251390387, -113.98040771484375))); mongoTemplate.insert(new Venue("Venue 6", new Point(150.93939251390387, 113.98040771484375))); List<Venue> venueList = mongoTemplate.find( query(where("location") .near(new Point(51.00, -114.00)) .maxDistance(1.00)), Venue.class); assertThat(venueList).size().isEqualTo(5); assertThat(venueList.get(0).getLocation()).isNotNull(); assertThat(venueList.get(0).getName()).isNotNull(); }
Example #2
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 #3
Source File: DerivedQueryCreatorTest.java From spring-data with Apache License 2.0 | 6 votes |
@Test public void findWithinAndWithinTest() { final List<Customer> toBeRetrieved = new LinkedList<>(); final Customer customer1 = new Customer("+++", "", 0); customer1.setLocation(new int[] { 80, 0 }); toBeRetrieved.add(customer1); final Customer customer2 = new Customer("vvv", "", 0); customer2.setLocation(new int[] { 10, 0 }); toBeRetrieved.add(customer2); repository.saveAll(toBeRetrieved); final Customer customer3 = new Customer("--d", "", 0); customer3.setLocation(new int[] { 19, 0 }); repository.save(customer3); final Customer customer4 = new Customer("--r", "", 0); customer4.setLocation(new int[] { 6, 0 }); repository.save(customer4); final Customer customer5 = new Customer("-!r", "", 0); customer5.setLocation(new int[] { 0, 0 }); repository.save(customer5); final int distance = (int) convertAngleToDistance(11); final Bound<Integer> lowerBound = Bound.inclusive((int) convertAngleToDistance(5)); final Bound<Integer> upperBound = Bound.inclusive((int) convertAngleToDistance(15)); final Collection<Customer> retrieved = repository.findByLocationWithinAndLocationWithinOrName(new Point(0, 20), distance, new Ring<>(new Point(0, 0), Range.of(lowerBound, upperBound)), "+++"); assertTrue(equals(toBeRetrieved, retrieved, cmp, eq, false)); }
Example #4
Source File: BoundingBoxTest.java From sdn-rx with Apache License 2.0 | 6 votes |
private static Stream<Arguments> boxesToTest() { return Stream.of( Arguments.of( new Box(new Point(1, 1), new Point(5, 5)), new Point(1, 1), new Point(5, 5) ), Arguments.of( new Box(new Point(8, 3), new Point(2, 9)), new Point(2, 3), new Point(8, 9) ), Arguments.of( new Box(new Point(3, 4), new Point(10, 8)), new Point(3, 4), new Point(10, 8) ) ); }
Example #5
Source File: BoundingBoxTest.java From sdn-rx with Apache License 2.0 | 6 votes |
private static Stream<Arguments> polygonsToTest() { return Stream.of( Arguments.of( new Polygon(new Point(1, 1), new Point(5, 1), new Point(5, 5), new Point(5, 1)), new Point(1, 1), new Point(5, 5) ), Arguments.of( new Polygon(new Point(3, 6), new Point(6, 2), new Point(8, 3), new Point(8, 6), new Point(2, 9)), new Point(2, 2), new Point(8, 9) ), Arguments.of( new Polygon(new Point(3, 4), new Point(7, 1), new Point(9, 4), new Point(10, 8), new Point(8, 10)), new Point(3, 1), new Point(10, 10) ) ); }
Example #6
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 #7
Source File: StoreInitializer.java From spring-data-examples with Apache License 2.0 | 6 votes |
/** * Reads a file {@code starbucks.csv} from the class path and parses it into {@link Store} instances about to * persisted. * * @return * @throws Exception */ public static List<Store> readStores() throws Exception { ClassPathResource resource = new ClassPathResource("starbucks.csv"); Scanner scanner = new Scanner(resource.getInputStream()); String line = scanner.nextLine(); scanner.close(); FlatFileItemReader<Store> itemReader = new FlatFileItemReader<Store>(); itemReader.setResource(resource); // DelimitedLineTokenizer defaults to comma as its delimiter DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer(); tokenizer.setNames(line.split(",")); tokenizer.setStrict(false); DefaultLineMapper<Store> lineMapper = new DefaultLineMapper<Store>(); lineMapper.setFieldSetMapper(fields -> { Point location = new Point(fields.readDouble("Longitude"), fields.readDouble("Latitude")); Address address = new Address(fields.readString("Street Address"), fields.readString("City"), fields.readString("Zip"), location); return new Store(UUID.randomUUID(), fields.readString("Name"), address); }); lineMapper.setLineTokenizer(tokenizer); itemReader.setLineMapper(lineMapper); itemReader.setRecordSeparatorPolicy(new DefaultRecordSeparatorPolicy()); itemReader.setLinesToSkip(1); itemReader.open(new ExecutionContext()); List<Store> stores = new ArrayList<>(); Store store = null; do { store = itemReader.read(); if (store != null) { stores.add(store); } } while (store != null); return stores; }
Example #8
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 #9
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 #10
Source File: BindParameterBinding.java From spring-data with Apache License 2.0 | 5 votes |
public int bindPoint( final Object value, final boolean shouldIgnoreCase, final UniqueCheck uniqueCheck, final int startIndex) { return bindPoint((Point) ignoreArgumentCase(value, shouldIgnoreCase), uniqueCheck, startIndex); }
Example #11
Source File: RedissonConnection.java From redisson with Apache License 2.0 | 5 votes |
@Override public List<Point> geoPos(byte[] key, byte[]... members) { List<Object> params = new ArrayList<Object>(members.length + 1); params.add(key); params.addAll(Arrays.asList(members)); MultiDecoder<Map<Object, Object>> decoder = new ListMultiDecoder2(new ObjectListReplayDecoder2(), new PointDecoder()); RedisCommand<Map<Object, Object>> command = new RedisCommand<Map<Object, Object>>("GEOPOS", decoder); return read(key, StringCodec.INSTANCE, command, params.toArray()); }
Example #12
Source File: GeoOperationsTests.java From spring-data-examples with Apache License 2.0 | 5 votes |
/** * 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"); }
Example #13
Source File: SpatialTypes.java From sdn-rx with Apache License 2.0 | 5 votes |
static Point[] asPointArray(Value value) { Point[] array = new Point[value.size()]; int i = 0; for (Point v : value.values(SpatialTypes::asSpringDataPoint)) { array[i++] = v; } return array; }
Example #14
Source File: BoundingBoxTest.java From sdn-rx with Apache License 2.0 | 5 votes |
@ParameterizedTest @MethodSource("polygonsToTest") void builderShouldWorkForPolygons(Polygon p, Point ll, Point ur) { BoundingBox boundingBox = BoundingBox.of(p); assertThat(boundingBox.getLowerLeft()).isEqualTo(ll); assertThat(boundingBox.getUpperRight()).isEqualTo(ur); }
Example #15
Source File: UserMapper.java From C4SG-Obsolete with MIT License | 5 votes |
/** * Map user data transfer object into user entity * * @param userDTO User Data Transfer object * @return User */ public User getUserEntityFromDto(UserDTO userDTO){ User user = map(userDTO, User.class); if (userDTO.getLatitude() != null && userDTO.getLongitude() != null){ GeometryFactory gf = new GeometryFactory(new PrecisionModel(PrecisionModel.FLOATING)); Coordinate coordinate = new Coordinate(Double.parseDouble(userDTO.getLongitude()), Double.parseDouble(userDTO.getLatitude())); com.vividsolutions.jts.geom.Point point = gf.createPoint(coordinate); user.setLocation(point); } user.setDisplayFlag(Boolean.valueOf(userDTO.getDisplayFlag())); return user; }
Example #16
Source File: PointToLocationConverter.java From Spring-Boot-2.0-Projects with MIT License | 5 votes |
@Nullable @Override public LocationDTO convert(Point point) { if (point == null) { return null; } return new LocationDTO(point.getY(), point.getX(), null); }
Example #17
Source File: DerivedQueryCreatorTest.java From spring-data with Apache License 2.0 | 5 votes |
@Test public void findByLocationWithinBoxTest() { final List<Customer> toBeRetrieved = new LinkedList<>(); final Customer customer1 = new Customer("", "", 0); customer1.setLocation(new int[] { 10, 10 }); toBeRetrieved.add(customer1); repository.saveAll(toBeRetrieved); final Customer customer2 = new Customer("", "", 0); customer2.setLocation(new int[] { 0, 0 }); repository.save(customer2); final Customer customer3 = new Customer("", "", 0); customer3.setLocation(new int[] { 0, 10 }); repository.save(customer3); final Customer customer4 = new Customer("", "", 0); customer4.setLocation(new int[] { 0, 20 }); repository.save(customer4); final Customer customer5 = new Customer("", "", 0); customer5.setLocation(new int[] { 10, 0 }); repository.save(customer5); final Customer customer6 = new Customer("", "", 0); customer6.setLocation(new int[] { 10, 20 }); repository.save(customer6); final Customer customer7 = new Customer("", "", 0); customer7.setLocation(new int[] { 20, 0 }); repository.save(customer7); final Customer customer8 = new Customer("", "", 0); customer8.setLocation(new int[] { 20, 10 }); repository.save(customer8); final Customer customer9 = new Customer("", "", 0); customer9.setLocation(new int[] { 20, 20 }); repository.save(customer9); final List<Customer> retrieved = repository.findByLocationWithin(new Box(new Point(5, 5), new Point(15, 15))); assertTrue(equals(toBeRetrieved, retrieved, cmp, eq, false)); }
Example #18
Source File: PointDecoder.java From redisson with Apache License 2.0 | 5 votes |
@Override public Point decode(List<Object> parts, State state) { if (parts.isEmpty()) { return null; } Double longitude = (Double)parts.get(0); Double latitude = (Double)parts.get(1); return new Point(longitude, latitude); }
Example #19
Source File: LocationToPointConverter.java From Spring-Boot-2.0-Projects with MIT License | 5 votes |
@Nullable @Override public Point convert(LocationDTO locationDTO) { if (locationDTO == null) { return null; } return new Point(locationDTO.getLongitude(), locationDTO.getLatitude()); }
Example #20
Source File: DerivedQueryCreatorTest.java From spring-data with Apache License 2.0 | 5 votes |
@Test public void findNearTest() { john.setLocation(new int[] { 2, 2 }); bob.setLocation(new int[] { 50, 45 }); repository.saveAll(customers); final Customer[] retrieved = repository.findByLocationNear(new Point(10, 20)); final Customer[] check = { john, bob }; assertTrue(equals(check, retrieved, cmp, eq, true)); }
Example #21
Source File: BindParameterBinding.java From spring-data with Apache License 2.0 | 5 votes |
public int bindBox(final Object value, final boolean shouldIgnoreCase, final int startIndex) { int index = startIndex; final Box box = (Box) ignoreArgumentCase(value, shouldIgnoreCase); final Point first = box.getFirst(); final Point second = box.getSecond(); final double minLatitude = Math.min(first.getY(), second.getY()); final double maxLatitude = Math.max(first.getY(), second.getY()); final double minLongitude = Math.min(first.getX(), second.getX()); final double maxLongitude = Math.max(first.getX(), second.getX()); bind(index++, minLatitude); bind(index++, maxLatitude); bind(index++, minLongitude); bind(index++, maxLongitude); return index; }
Example #22
Source File: RedissonConnection.java From redisson with Apache License 2.0 | 5 votes |
@Override public Long geoAdd(byte[] key, Map<byte[], Point> memberCoordinateMap) { List<Object> params = new ArrayList<Object>(memberCoordinateMap.size()*3 + 1); params.add(key); for (Entry<byte[], Point> entry : memberCoordinateMap.entrySet()) { params.add(entry.getValue().getX()); params.add(entry.getValue().getY()); params.add(entry.getKey()); } return write(key, StringCodec.INSTANCE, RedisCommands.GEOADD, params.toArray()); }
Example #23
Source File: BindParameterBinding.java From spring-data with Apache License 2.0 | 5 votes |
private int bindPoint(final Point point, final UniqueCheck uniqueCheck, final int startIndex) { uniqueCheck.check(point); int index = startIndex; bind(index++, point.getY()); bind(index++, point.getX()); return index; }
Example #24
Source File: BindParameterBinding.java From spring-data with Apache License 2.0 | 5 votes |
public int bindCircle( final Object value, final boolean shouldIgnoreCase, final UniqueCheck uniqueCheck, final int startIndex) { int index = startIndex; final Circle circle = (Circle) ignoreArgumentCase(value, shouldIgnoreCase); final Point center = circle.getCenter(); uniqueCheck.check(center); bind(index++, center.getY()); bind(index++, center.getX()); bind(index++, convertDistanceToMeters(circle.getRadius())); return index; }
Example #25
Source File: DerivedQueryCreator.java From spring-data with Apache License 2.0 | 5 votes |
/** * Ensures that Points used in geospatial parts of non-nested properties are the same in case geospatial return type * is expected * * @param point */ private void checkUniquePoint(final Point point) { final boolean isStillUnique = (uniquePoint == null || uniquePoint.equals(point)); if (!isStillUnique) { isUnique = false; } if (!geoFields.isEmpty()) { Assert.isTrue(uniquePoint == null || uniquePoint.equals(point), "Different Points are used - Distance is ambiguous"); uniquePoint = point; } }
Example #26
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 #27
Source File: RedissonConnection.java From redisson with Apache License 2.0 | 5 votes |
@Override public List<Point> geoPos(byte[] key, byte[]... members) { List<Object> params = new ArrayList<Object>(members.length + 1); params.add(key); params.addAll(Arrays.asList(members)); RedisCommand<Map<Object, Object>> command = new RedisCommand<Map<Object, Object>>("GEOPOS", geoDecoder); return read(key, StringCodec.INSTANCE, command, params.toArray()); }
Example #28
Source File: RedissonConnection.java From redisson with Apache License 2.0 | 5 votes |
@Override public Long geoAdd(byte[] key, Map<byte[], Point> memberCoordinateMap) { List<Object> params = new ArrayList<Object>(memberCoordinateMap.size()*3 + 1); params.add(key); for (Entry<byte[], Point> entry : memberCoordinateMap.entrySet()) { params.add(entry.getValue().getX()); params.add(entry.getValue().getY()); params.add(entry.getKey()); } return write(key, StringCodec.INSTANCE, RedisCommands.GEOADD, params.toArray()); }
Example #29
Source File: GeoOperationsTests.java From spring-data-examples with Apache License 2.0 | 5 votes |
@Before public void before() { geoOperations = operations.opsForGeo(); geoOperations.geoAdd("Sicily", new Point(13.361389, 38.115556), "Arigento"); geoOperations.geoAdd("Sicily", new Point(15.087269, 37.502669), "Catania"); geoOperations.geoAdd("Sicily", new Point(13.583333, 37.316667), "Palermo"); }
Example #30
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)); }