#64 - Polishing.
Removed the additional GeoJsonModule and only register the mixin for GeoJsonPoint to be able to read the data from the source JSON file. Removed custom logging. A bit of polish in the README, JavaDoc.
This commit is contained in:
@@ -1,17 +1,17 @@
|
||||
# Spring Data MongoDB - GeoJSON examples
|
||||
|
||||
This project contains samples of [GeoJSON](http://geojeson.org) specific features of Spring Data (MongoDB).
|
||||
This project contains samples of [GeoJSON](http://geojson.org) specific features of Spring Data (MongoDB).
|
||||
|
||||
## Support for GeoJSON types in domain classes
|
||||
|
||||
Using [GeoJSON](http://geojeson.org) types in domain classes is straight forward. The `org.springframework.data.mongodb.core.geo` package contains types like `GeoJsonPoint` or `GeoJsonPolygon` which are extensions to the existing `org.springframework.data.geo` types.
|
||||
Please read the [MongoDB manual on GeoJSON support](http://docs.mongodb.org/manual/core/2dsphere/#geospatial-indexes-store-geojson) to learn about requirements and restrictions.
|
||||
Using [GeoJSON](http://geojson.org) types in domain classes is straight forward. The `org.springframework.data.mongodb.core.geo` package contains types like `GeoJsonPoint` or `GeoJsonPolygon` which are extensions to the existing `org.springframework.data.geo` types.
|
||||
Find more information in the [MongoDB manual on GeoJSON support](http://docs.mongodb.org/manual/core/2dsphere/#geospatial-indexes-store-geojson) to learn about requirements and restrictions.
|
||||
|
||||
```java
|
||||
public class Store {
|
||||
|
||||
String id;
|
||||
|
||||
|
||||
/**
|
||||
* location is stored in GeoJSON format.
|
||||
* {
|
||||
@@ -23,7 +23,6 @@ public class Store {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
## Support for GeoJSON types in repository methods
|
||||
|
||||
Using GeoJson types as repository query parameters forces usage of the `$geometry` operator when creating the query.
|
||||
@@ -32,14 +31,11 @@ Using GeoJson types as repository query parameters forces usage of the `$geometr
|
||||
public interface StoreRepository extends CrudRepository<Store, String> {
|
||||
|
||||
List<Store> findByLocationWithin(Polygon polygon);
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
```java
|
||||
|
||||
/*
|
||||
* {
|
||||
/* {
|
||||
* "location": {
|
||||
* "$geoWithin": {
|
||||
* "$geometry": {
|
||||
@@ -59,14 +55,13 @@ public interface StoreRepository extends CrudRepository<Store, String> {
|
||||
*/
|
||||
repo.findByLocationWithin(
|
||||
new GeoJsonPolygon(
|
||||
new Point(-73.992514, 40.758934),
|
||||
new Point(-73.961138, 40.760348),
|
||||
new Point(-73.991658, 40.730006),
|
||||
new Point(-73.992514, 40.758934),
|
||||
new Point(-73.961138, 40.760348),
|
||||
new Point(-73.991658, 40.730006),
|
||||
new Point(-73.992514, 40.758934)));
|
||||
|
||||
/*
|
||||
* {
|
||||
* "location" : {
|
||||
|
||||
/* {
|
||||
* "location" : {
|
||||
* "$geoWithin" : {
|
||||
* "$polygon" : [ [ -73.992514, 40.758934 ] , [ -73.961138, 40.760348 ] , [ -73.991658, 40.730006 ] ]
|
||||
* }
|
||||
@@ -75,7 +70,7 @@ repo.findByLocationWithin(
|
||||
*/
|
||||
repo.findByLocationWithin(
|
||||
new Polygon(
|
||||
new Point(-73.992514, 40.758934),
|
||||
new Point(-73.961138, 40.760348),
|
||||
new Point(-73.991658, 40.730006));
|
||||
new Point(-73.992514, 40.758934),
|
||||
new Point(-73.961138, 40.760348),
|
||||
new Point(-73.991658, 40.730006));
|
||||
```
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
</parent>
|
||||
|
||||
<artifactId>spring-data-mongodb-geojson</artifactId>
|
||||
<name>Spring Data MongoDB - GeoJson specific features</name>
|
||||
<name>Spring Data MongoDB - GeoJson support</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
@@ -18,7 +18,7 @@
|
||||
<version>${project.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
|
||||
@@ -22,7 +22,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.data.geo.GeoModule;
|
||||
import org.springframework.data.mongodb.core.geo.GeoJson;
|
||||
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
|
||||
import org.springframework.data.repository.init.AbstractRepositoryPopulatorFactoryBean;
|
||||
import org.springframework.data.repository.init.Jackson2RepositoryPopulatorFactoryBean;
|
||||
@@ -31,13 +31,16 @@ import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
/**
|
||||
* Spring Boot sample application to show the usage of {@link GeoJson} types with Spring Data MongoDB.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class App {
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(App.class, args);
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -48,7 +51,7 @@ public class App {
|
||||
public @Bean AbstractRepositoryPopulatorFactoryBean repositoryPopulator() {
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.registerModule(new GeoJsonModule());
|
||||
mapper.addMixIn(GeoJsonPoint.class, GeoJsonPointMixin.class);
|
||||
mapper.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
|
||||
Jackson2RepositoryPopulatorFactoryBean factoryBean = new Jackson2RepositoryPopulatorFactoryBean();
|
||||
@@ -56,22 +59,6 @@ public class App {
|
||||
factoryBean.setMapper(mapper);
|
||||
|
||||
return factoryBean;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link GeoJsonModule} registering mixins for common and MongoDB geo-spatial types. <br />
|
||||
* This should be part of Spring Data MongoDB.
|
||||
*/
|
||||
static class GeoJsonModule extends GeoModule {
|
||||
|
||||
private static final long serialVersionUID = 6239912797617786302L;
|
||||
|
||||
public GeoJsonModule() {
|
||||
super();
|
||||
|
||||
setMixInAnnotation(GeoJsonPoint.class, GeoJsonPointMixin.class);
|
||||
}
|
||||
}
|
||||
|
||||
static abstract class GeoJsonPointMixin {
|
||||
|
||||
@@ -33,7 +33,7 @@ public class Store {
|
||||
String city;
|
||||
|
||||
/**
|
||||
* location is stored in GeoJSON format.
|
||||
* {@code location} is stored in GeoJSON format.
|
||||
*
|
||||
* <pre>
|
||||
* <code>
|
||||
|
||||
@@ -21,10 +21,18 @@ import org.springframework.data.geo.Polygon;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
/**
|
||||
* Spring Data repository interface to manage {@link Store} instances.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public interface StoreRepository extends CrudRepository<Store, String> {
|
||||
interface StoreRepository extends CrudRepository<Store, String> {
|
||||
|
||||
/**
|
||||
* Returns all {@link Store}s located withing the given {@link Polygon}.
|
||||
*
|
||||
* @param polygon must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
List<Store> findByLocationWithin(Polygon polygon);
|
||||
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.data.geo.Point;
|
||||
import org.springframework.data.geo.Polygon;
|
||||
import org.springframework.data.mongodb.core.MongoOperations;
|
||||
@@ -28,7 +29,6 @@ import org.springframework.data.mongodb.core.geo.GeoJsonPolygon;
|
||||
import org.springframework.data.mongodb.core.query.BasicQuery;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.data.util.Version;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.mongodb.BasicDBObject;
|
||||
@@ -37,19 +37,25 @@ import com.mongodb.DBObject;
|
||||
import example.springdata.mongodb.util.RequiresMongoDB;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link StoreRepository}.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { App.class })
|
||||
@SpringApplicationConfiguration(classes = { Application.class })
|
||||
public class StoreRepositoryTests {
|
||||
|
||||
private static final GeoJsonPolygon GEO_JSON_POLYGON = new GeoJsonPolygon(new Point(-73.992514, 40.758934),
|
||||
new Point(-73.961138, 40.760348), new Point(-73.991658, 40.730006), new Point(-73.992514, 40.758934));
|
||||
|
||||
public static @ClassRule RequiresMongoDB requiresMongoDB_2_6 = RequiresMongoDB.atLeast(new Version(2, 6, 0));
|
||||
|
||||
@Autowired StoreRepository repo;
|
||||
@Autowired MongoOperations mongoOps;
|
||||
@Autowired StoreRepository repository;
|
||||
@Autowired MongoOperations operations;
|
||||
|
||||
/**
|
||||
* Get all the Starbucks stores within the triange defined by
|
||||
* Get all the Starbucks stores within the triangle defined by
|
||||
*
|
||||
* <pre>
|
||||
* <ol>
|
||||
@@ -64,7 +70,7 @@ public class StoreRepositoryTests {
|
||||
* {
|
||||
* "location": {
|
||||
* "$geoWithin": {
|
||||
* "$geometry": {
|
||||
* "geometry": {
|
||||
* "type": "Polygon",
|
||||
* "coordinates": [
|
||||
* [
|
||||
@@ -84,9 +90,7 @@ public class StoreRepositoryTests {
|
||||
*/
|
||||
@Test
|
||||
public void findWithinGeoJsonPolygon() {
|
||||
repo.findByLocationWithin(
|
||||
new GeoJsonPolygon(new Point(-73.992514, 40.758934), new Point(-73.961138, 40.760348), new Point(-73.991658,
|
||||
40.730006), new Point(-73.992514, 40.758934))).forEach(System.out::println);
|
||||
repository.findByLocationWithin(GEO_JSON_POLYGON).forEach(System.out::println);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -107,7 +111,7 @@ public class StoreRepositoryTests {
|
||||
*/
|
||||
@Test
|
||||
public void findWithinLegacyPolygon() {
|
||||
repo.findByLocationWithin(
|
||||
repository.findByLocationWithin(
|
||||
new Polygon(new Point(-73.992514, 40.758934), new Point(-73.961138, 40.760348),
|
||||
new Point(-73.991658, 40.730006))).forEach(System.out::println);
|
||||
}
|
||||
@@ -121,14 +125,11 @@ public class StoreRepositoryTests {
|
||||
|
||||
DBObject geoJsonDbo = new BasicDBObject();
|
||||
|
||||
mongoOps.getConverter().write(
|
||||
new GeoJsonPolygon(new Point(-73.992514, 40.758934), new Point(-73.961138, 40.760348), new Point(-73.991658,
|
||||
40.730006), new Point(-73.992514, 40.758934)), geoJsonDbo);
|
||||
operations.getConverter().write(GEO_JSON_POLYGON, geoJsonDbo);
|
||||
|
||||
BasicQuery bq = new BasicQuery(new BasicDBObject("location", new BasicDBObject("$geoIntersects", new BasicDBObject(
|
||||
"$geometry", geoJsonDbo))));
|
||||
|
||||
mongoOps.find(bq, Store.class).forEach(System.out::println);
|
||||
operations.find(bq, Store.class).forEach(System.out::println);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
|
||||
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d %5p %40.40c:%4L - %m%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<!-- enable debug to print out query used -->
|
||||
<logger name="org.springframework.data.mongodb.core.MongoTemplate" level="debug" />
|
||||
|
||||
<root level="info">
|
||||
<appender-ref ref="console" />
|
||||
</root>
|
||||
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user