MongoDB for Java and geospatial space Operations

Source: Internet
Author: User
Tags mongodb
A 2d Sphere Index supports queries that calculate geometries on A earth-like sphere. The index supports data stored as both Geojson objects and as legacy coordinate. The index supports legacy coordinate pairs by converting the ' data to the ' Geojson point type.
Translation: 2d sphere index supports spherical geometry query. Data storage is dominated by Geojson objects.

Store your location data as Geojson objects with this coordinate-axis order:longitude, latitude. The coordinate reference system for Geojson uses the WGS84 datum.
/** * Convert to Geojson object/public String Getgeojson (String lon,string lat) throws jsonexception{
        Jsonobject point = new Jsonobject ();
        Point.put ("type", "point");
        Jsonarray coord = new Jsonarray ("[" +lon+ "," +lat+ "]");
        Point.put ("coordinates", coord);
      return point.tostring (); /** * Insert space Data * @param collection * @throws jsonexception/public void I Nsert (Dbcollection collection) throws jsonexception{dbobject point1 = new Basicdbobj
            ECT ();
            Point1.put ("name", "001");
            
            Point1.put ("Geo", Getgeojson ("116.342176", "39.995376"));
            DBObject Point2 = new Basicdbobject ();
            Point2.put ("name", "002");
            
            Point2.put ("Geo", Getgeojson ("116.348694", "39.990965"));
            DBObject point3 = new Basicdbobject ();
            Point3.put ("Name", "003");Point3.put ("Geo", Getgeojson ("116.343318", "39.991184"));
            DBObject point4 = new Basicdbobject ();
            Point4.put ("name", "004");
            Point4.put ("Geo", Getgeojson ("116.359590", "39.982762"));
            list<dbobject> list = new arraylist<dbobject> ();
            List.add (POINT1);
            List.add (Point2);
            List.add (POINT3);
            List.add (POINT4);
      Collection.insert (list). GETN (); /** * Create spatial Index * @param collection */public void Makespatialindexs (Dbcollection c
      ollection) {Collection.ensureindex (New Basicdbobject ("Geo", "2dsphere"), "Geospatialidx"); /** * All elements within the query rectangle * @param collection/public void query Dbcollection Collectio
            N) {list<double[]> box = new arraylist<double[]> (); Box.add (new double[] {116.341795,39.992277}); Top left corner coordinate BoX.add (New double[]{116.350122,39.989251}); Lower right corner coordinate basicdbobject query = new Basicdbobject ("Loc", New Basicdbobject ("$within", New BASICDBOBJEC
            T ("$box", Box));
             Dbcursor cur1 = collection.find (query);
                  while (Cur1.hasnext ()) {Basicdbobject o = (basicdbobject) cur1.next (); System.
                  Out.println (O.get ("name")); System.
            Out.println (O.get ("geometry"));
 }
      }


More Space Operations Reference:
Http://java.dzone.com/articles/writing-geospatial-queries
References: geojson:http://www.geojson.org/geojson-spec.htmlgeospatial
Indexes and queries:http://docs.mongodb.org/manual/applications/geospatial-indexes/
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.