Giter VIP home page Giter VIP logo

geojson-spring-data's Introduction

Build Status

Support for GeoJSON classes in Spring Data for MongoDB

Currently Spring Data for MongoDB has support for geospatial geometries, but they do not use the GeoJSON representation. Also, not all geometries covered by GeoJSON are included by default in Spring Data.

This library provides support for all geometries supported by MongoDB.

Configuring the GeoJSON converters

Register the providers in your Spring context's MongoTemplate. If you're using Spring Boot, this means you'll have to create a configuration that extends AbstractMongoConfiguration and override the customConversions bean.

...
@Override
public CustomConversions customConversions() {
    return new CustomConversions(GeoJsonConverters.getConvertersToRegister());
}
...

If you're not using Spring Boot, you'll have to manually configure your MongoTemplate.

MongoDbFactory factory = new SimpleMongoDbFactory(mongo, "DatabaseName");
MappingMongoConverter converter = new MappingMongoConverter(new DefaultDbRefResolver(factory), new MongoMappingContext());
CustomConversions customConversions = new CustomConversions(GeoJsonConverters.getConvertersToRegister());
converter.setCustomConversions(customConversions);
customConversions.registerConvertersIn((GenericConversionService) converter.getConversionService());
mongoTemplate = new MongoTemplate(factory, converter);

Using the GeoJSON classes

You can use any of the geometries in your document classes. For example to use a point you can create a document like

@Document
public static class GeoLocation {
    private String name;
    @GeoSpatialIndexed(type = GeoSpatialIndexType.GEO_2DSPHERE)
    private Point location;

    public GeoLocation(String name, Point point) {
        this.name = name;
        this.location = point;
    }

    public GeoLocation() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Point getLocation() {
        return location;
    }

    public void setLocation(Point location) {
        this.location = location;
    }
}

Querying

To query for documents using the GeoJSON geometries a custom criteria class GeoJsonCriteria has been built. This class support all the geospatial methods supported by MongoDB. For example, to query for locations near a point, you can use the following query:

Query query = Query.query(GeoJsonCriteria.where("location").near(new Point(0., 0.)));
List<GeoLocation> nearestLocations = mongoTemplate.find(query, GeoLocation.class);

geojson-spring-data's People

Contributors

lievendoclo avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.