Mongodb-mongotemplate Geo-coordinate operations

Source: Internet
Author: User

Because of the Springboot + mongotemplate used in the project, it is still necessary to mongotemplate operation mode

First create a bean:

Package Com.iwhere.easy.travel.entity;import Java.io.serializable;import java.util.arrays;import Org.springframework.data.annotation.id;import Org.springframework.data.annotation.persistenceconstructor;import Org.springframework.data.mongodb.core.index.geospatialindexed;import Org.springframework.data.mongodb.core.index.indexed;import org.springframework.data.mongodb.core.mapping.Document;/** * Fee POI * * @author Wenbronk * @time July 19, 2017 pm 4:46:39*/@Document (Collection="Charge_poi") Public classChargepoi implements Serializable {Private StaticFinalLongSerialversionuid =2653147280472201924L; @IdPrivateString _id; @IndexedPrivateString poi_id; PrivateString Poi_name; @GeoSpatialIndexedPrivatedouble[] location; PrivateString Media_url; PrivateDouble Price;  PublicChargepoi () {super ();  } @PersistenceConstructor Chargepoi (string _id, String poi_id, String poi_name, double[] location, String media_url,        Double price) {super ();  This. _id =_id;  This. poi_id =poi_id;  This. Poi_name =Poi_name;  This. location =Location ;  This. Media_url =Media_url;  This. Price =Price ; }     PublicChargepoi (String _id, String poi_id, String poi_name, Double x, double y, String media_url, double price) {su        Per ();  This. _id =_id;  This. poi_id =poi_id;  This. Poi_name =Poi_name;  This. location =Newdouble[]{x, y};  This. Media_url =Media_url;  This. Price =Price ; }     PublicString get_id () {return_id; }     Public voidset_id (String _id) { This. _id =_id; }     PublicString getpoi_id () {returnpoi_id; }     Public voidsetpoi_id (String poi_id) { This. poi_id =poi_id; }     PublicString Getpoi_name () {returnPoi_name; }     Public voidsetpoi_name (String poi_name) { This. Poi_name =Poi_name; }     Publicdouble[] GetLocation () {returnLocation ; }     Public voidsetlocation (double[] location) { This. location =Location ; }     PublicString Getmedia_url () {returnMedia_url; }     Public voidSetmedia_url (String media_url) { This. Media_url =Media_url; }     PublicDouble GetPrice () {returnPrice ; }     Public voidSetprice (Double price) { This. Price =Price ; } @Override PublicString toString () {return "Chargepoi [_id="+ _id +", poi_id="+ poi_id +", Poi_name="+ Poi_name +", location="+ arrays.tostring (location) +", Media_url="+ Media_url +", price="+ Price +"]"; }}

Note:

@GeoSpatialIndexed  

using MongoDB'S Geospatial indexing feature.
Start without adding this annotation, and then calculate the distance when the error will be

1, prepare the test data:

    /** * Save*/@Test Public voidtest1 () { for(intx = -; X <131; X + +) {             for(inty = -; Y < A; y++) {Double loca[]=Newdouble[]{double.valueof (x), double.valueof (y)}; Chargepoi Chargepoi=NewChargepoi (); CHARGEPOI.SETPOI_ID ("poiid"+x); Chargepoi.setmedia_url ("http://www.baidu.com?params="+x); Chargepoi.setpoi_name ("Vini"+arrays.tostring (Loca)); Chargepoi.setprice (Math.random ()* -);                Chargepoi.setlocation (Loca);            Mongotemplate.insert (Chargepoi); }        }    }

2, circular query

    /** * Circle*/@Test Public voidtest2 () {Circle Circle=NewCircle ( -, -, -); List<ChargePoi> find = Mongotemplate.find (NewQuery (Criteria).where(" Location"). Within (circle)), Chargepoi.class); System. out. println (Find); System. out. println (Find.size ()); }

3, Star Search

    /** * Spherical*/@Test Public voidtest3 () {Circle Circle=NewCircle ( -, -, -); List<ChargePoi> find = Mongotemplate.find (NewQuery (Criteria).where(" Location"). Withinsphere (Circle)), Chargepoi.class); System. out. println (Find.size ()); System. out. println (Find); }

4, Rectangle query, Box

    /** * Box*/@Test Public voidtest4 () {Box box=NewBox (NewPoint (Ten, One),NewPoint (Ten, -)); List<ChargePoi> find =Mongotemplate.find (NewQuery (Criteria).where(" Location"). Within (box), Chargepoi.class); System. out. println (Find.size ()); System. out. println (Find); }

5, by distance from near to Meta query

    /** * near*/@Test Public voidTest5 () { point point=NewPoint ( A, A); List<ChargePoi> venues =Mongotemplate.find (NewQuery (Criteria).where(" Location"). Near (point). MaxDistance ( -)), Chargepoi.class); System. out. println (Venues.size ()); System. out. println (venues); }

6, Spatial distance query

    /** * Nearsphere*/@Test Public voidTest6 () { point point=NewPoint ( A, A); List<ChargePoi> venues =Mongotemplate.find (NewQuery (Criteria).where(" Location"). Nearsphere (Point). MaxDistance ( -)), Chargepoi.class); System. out. println (Venues.size ()); System. out. println (venues); }

7, nearest point query

    @Test    publicvoid  test7 () {        new point (12 );         = Nearquery.near (location). MaxDistance (new Distance (100000, metrics.kilometers));        Georesults<ChargePoi> result = mongotemplate.geonear (query, Chargepoi.  Class);        System.  out . println (result);    }

I am a hard-working porter:->http://docs.spring.io/spring-data/data-mongo/docs/1.5.2.release/reference/html/mongo.core.html# Mongo.geospatial

Mongodb-mongotemplate Geo-coordinate operations

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.