Geospatial Queries with Python MongoDB

Prerequisites: MongoDB and Python

GeoJSON is an open-source format containing simple geographical features and is based on JavaScript Object Notation. It is used to format shapes in a coordinate space and multiple types are supported by MongoDB to permit storing geospatial data. This article will cover the various ways of using geospatial in MongoDB and explain the GeoJSON polygon and point types.

 
GeoJSON format

  • a field named type that specifies the GeoJSON object type and
  • a field named coordinates that specifies the object’s coordinates.

Note: If specifying latitude and longitude coordinates, list the longitude first and then latitude.



Modules Needed:

  • Pymomgo: This module is used to interact with the MongoDB. To install it type the below command in the terminal.
    pip install pymongo
    OR
    condo install pymongo
  • Matplotlib: This library is used for plotting graphs
  • Basemap: This module is used for plotting maps using Python. To install this module type the below command in the terminal.
    conda install basemap

Steps to use MongoDB Atlas:

  • Open the MongoDB Atlas Cloud from here.
  • Create the account by choosing the package suitable for you (You may also choose the free version which will be enough for this article and for learning purpose).
  • Click on the Cluster view positioned at the left menu bar.
  • Click on the Ellipses button(...) and select Load Sample Dataset.
  • After the sample dataset is added then click on the connect button.
    python-mongodb-cluster-1

  • Then whitelist the IP address (choose your current IP address or type the 0.0.0.0/0 IP for allowing it to access from everywhere. Click the button shown in the below image.
    python-mongodb-cluster-connect-2

  • Then click connect to applications button.
  • Copy the cluster_uri and paste it to a “course_cluster_uri”.

Below is the implementation.

filter_none

edit
close

play_arrow

link
brightness_4
code

import pymongo
import pprint
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
   
      
course_cluster_uri = 'your_connection_string'
   
course_client = pymongo.MongoClient(course_cluster_uri)
  
# sample Database
db = course_client['sample_geospatial']
  
# sample Collection
shipwrecks = db['shipwrecks']
   
l = list(shipwrecks.find({}))
   
lngs = [x['londec'] for x in l]
lats = [x['latdec'] for x in l]
   
# Clear the figure (this is nice if you
# execute the cell multiple times)
plt.clf()
   
# Set the size of our figure
plt.figure(figsize =(14, 8))
   
# Set the center of our map with our 
# first pair of coordinates and set
# the projection
m = Basemap(lat_0 = lats[0],
            lon_0 = lngs[0],
            projection ='cyl')
   
# Draw the coastlines and the states
m.drawcoastlines()
m.drawstates()
   
# Convert our coordinates to the system
# that the projection uses
x, y = m(lngs, lats)
   
# Plot our converted coordinates
plt.scatter(x, y)
   
# Display our beautiful map
plt.show()

chevron_right


Output:

shipwrecks plot

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.