Geolocation APIs in MongoDB

Authored byHarsh R.on Jun 06, 2017 in Topic Technology
Keyword Cloud

Subscribe to email updates

zymr-mongodb-geospatial-apis

MongoDB is the NoSQL database known around the world for its clever document based structure, ease of use, and flexibility. When some of the biggest companies in the world like Forbes and Bosch use MongoDB for their systems, you know that you are in good hands.

Unlike most other NoSQL databases, MongoDB comes with built-in geospatial indexing and search functionality, which makes it perfect for developers needing simple location based querying and map work.

Support for geospatial data is in the form of GeoJSON objects, which if you are familiar with JSON isn’t too difficult to wrap your head around. To initialize a geospatial index in MongoDB, we use a ‘2dsphere’. You also have the option to use just a simple ‘2d’ flat geometry. However this is not as precise as using the 2dsphere. These two types of indexes are designed specifically to return queries on the geospatial data contained efficiently.

To create a 2dsphere index, you need to create the index, as such:

db.mycollection.createIndex( { <location field in collection> : "2dsphere" } )

You can do this before or after populating your collection of GeoJSON objects, either is acceptable and works.

The MongoDB 2dsphere index includes support for all the following GeoJSON objects: Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, and GeometryCollection. To add a GeoJSON object to the database, you need to specify in the following manner:

{ type: “<Type>”,  coordinates: <coordinates> }

In GeoJSON we must always remember that when inserting coordinates, longitude is written before latitude. This can cause many headaches if you mess it up!

For example,

{ type: “Point”, coordinates: [ 55.2, 7.5 ] }

So, for instance, we might choose to add public playgrounds to a collection, which would be done as such:

db.playgrounds.insert(

               {

                              location : { type: “Point”, coordinates: [33.6, 23.5] }

                              name : “Shelley Playgrounds”

               }

db.playgrounds.insert(

               {

                              location : { type: “Point”, coordinates: [46.9, 37.3] }

                              name : “Wintergardens”

               }

And if we haven’t already, create the 2dsphere index on our collection:

db.playgrounds.createIndex( “location” : “2dsphere” )

If you then have a collection of geospatial data in your database, there are some really neat queries that you can do on the data within it.

$geoWithin returns all GeoJSON objects within a given specified polygon. For instance, this call would return both of our playgrounds:

db.playgrounds.find( { “location” :

               { $geoWithin :

                              { $geometry : { type : "Polygon" ,  coordinates : [0, 0] , [0, 100], [100, 100], [100,0]  } }

               }

} )

$geoIntersects checks to see if a GeoJSON object is intersected by a given geometry, this would return our “Wintergardens” playground:

db.playgrounds.find( { “location” :

               { $geoIntersects:

                              { $geometry : { type : "Polygon" ,  coordinates : [0, 0] , [0, 37.3], [100, 37.3], [100,0]  } }

               }

} )

geoNear is a very handy feature that returns objects near to a given point. This command would return Wintergardens, followed by Shelley Playgrounds.

db.runCommand( { geoNear : “playgrounds” ,

              near : { type : "Point" , coordinates: [ 46, 36 ] } ,

spherical : true } )

There’s a lot that you can do with MongoDB and GeoJSON spatial data, we’ve only just touched on the basics today. For tutorials, you can check out the MongoDB site itself to get started.

This article was authored by Harsh Raval, who is a Lead Software Engineer at Zymr.

Categories

0 comments

Leave a Reply

Contact Us

Request a Consultation

Please prove you are human by selecting the Star.

Smartsourcing: A guide to selecting an Agile Development Partner

Smartsourcing is a brief guide to the world of modern technology partnerships. It was developed through a collaborative effort of top Zymr executives as we uncovered a gap in the market between the perception of what outsourcing used to be, and how leading technology innovators are leveraging this globalized approach to value generation. Read this guide to learn...

  • Key factors to consider for your development strategy
  • Popular destinations with a track record of high quality services
  • Selection criteria to narrow your shortlisted vendors

Get access to Smartsourcing eBook

 30 days 3 Months 1 year Still exploring

Register below to download your free eBook

Register below to download your free White Paper

Register below to download your free Guide

Register below to download your full Case Study

Please prove you are human by selecting the Heart.

Register below to download your Healthcare Cloud Stack

Register below to download your Microservices eBook