Stay home. Save Lives
MongoDB GeoJSON Objects

MongoDB GeoJSON Objects

  • July 01, 2019
  • by

Introduction to Geospatial Queries

In this post, we feature a comprehensive Example on MongoDB Geospatial Query Operators. MongoDB is a JSON based document storage database. MongoDB stores the records in the form of unstructured JSON documents. It allows the flexibility of having varying number of columns for every record. This feature of MongoDB has been put to action by the Geospatial queries. Geospatial queries, as the name indicates, are queries used to query data using the geological data. These queries help you find places within a specific geospatial area. In addition to querying places, MongoDB is powerful and smart enough to calculate minimum or maximum distance, a geometry around a specific area and also able to find the centre

About GeoJSON

All the Geospatial queries supported by MongoDB are supported using GeoJSON. GeoJSON is a geography related data available in JSON format. A GeoJSON basically contains the details about the coordinates of various places. It is a defined standard to store details of any location. With the maps becoming a need for the day, GeoJSON is being used extensively in applications where location feature is being leveraged.

There are various types of GeoJSON objects possible. These are as listed below:

  • Point
  • LineString
  • Polygon
  • MultiLine
  • MultiLine String
  • MultiPolygon
  • GeometryCollection

1. Point

As the name suggests, a point is used to store the exact location or an address. A GeoJSON Point can be defined as shown below.

{ type: "Point", coordinates: [ 1, 2 ] }

As it can be seen from the above example, here the attribute type stands for the type of GeoJSON object while the coordinates confirm which is the object.

2. LineString

A LineString is a combination of two points forming a line. A LineString could be used to define boundaries, walls, barriers as well as roads. The possible use cases of LineString can be to find intersections in roads. A GeoJSON LineString can be defined as shown below.

{ type: "LineString", coordinates: [ [ 4, 5 ], [ 5, 6 ] ] }

The array of two points indicate a line geometrically. A GeoJSON LineString can be stored in this manner. A query can be executed to detect the entries crossing this line or overlapping a certain area.

3. Polygon

A Polygon is a combination of LineStrings forming a closed area. A Polygon can be defined using multiple points forming a closed loop. For instance, a pentagon can have coordinates. A Polygon is necessarily used to define an area covered by a city, state or a country. A Polygon string will help us locate areas within a specific boundary. A GeoJSON Polgon can be defined as shown below.

{
type: "Polygon",
coordinates: [ [ [ 0 , 0 ] , [ 3 , 4 ] , [ 4 , 1 ] , [ 0 , 0 ] ] ]
}

 

4. Multipoint

A GeoJSON multipoint object is used to store a collection of points. This is generally used to locate similar places in maps in the form of coordinates. A GeoJSON query can be used to find out which collection is located within a specific region. A Multipoint GeoJSON can be defined as shown below.

{
type: "MultiPoint",
coordinates: [
[ -73.980, 40.8503 ],
[ -73.998, 40.4968 ],
[ -73.737, 40.2648 ],
[ -73.940, 40.7281 ]
]
}

 

5. MultiLineString

A GeoJSON multipoint object is used to store a collection of points. This is generally used to locate similar places in maps in the form of coordinates. A GeoJSON query can be used to find out which collection is located within a specific region. A Multipoint GeoJSON can be defined as shown below.

{
type: "MultiLineString",
coordinates: [
[ [ -73.9643, 40.7859 ], [ -73.9608, 40.7805 ] ],
[ [ -73.9415, 40.7929 ], [ -73.9554, 40.7884 ] ],
[ [ -73.9162, 40.7825 ], [ -73.9634, 40.7775 ] ],
[ [ -73.9780, 40.7724 ], [ -73.9706, 40.7681 ] ]
]
}

6. MultiPolygon

A MultiPolygon generally defines various group of areas distributed over certain regions. A MultiPolygon can be defined as shown below.

{
type: "MultiPolygon",
coordinates: [
[ [ [ -73.958, 40.8003 ], [ -73.9498, 40.7968 ], [ -73.9737, 40.7648 ], [ -73.9814, 40.7681 ], [ -73.958, 40.8003 ] ] ],
[ [ [ -73.958, 40.8003 ], [ -73.9498, 40.7968 ], [ -73.9737, 40.7648 ], [ -73.958, 40.8003 ] ] ]
]
}​

7. GeometryCollection

A GeometryCollection GeoJSON object, as the name suggests, is an object containing multiple GeoJSON objects in the form of a single GeoJSON GeometryCollection object. A possible GeometryCollection object is shown below.

{
type: "GeometryCollection",
geometries: [
{
type: "MultiPoint",
coordinates: [
[ -3.9580, 4.803 ],
[ -3.9498, 4.768 ],
[ -3.9737, 3.748 ],
[ -7.9814, 0.761 ]
]
},
{
type: "MultiLineString",
coordinates: [
[ [ -7.9943, 0.519 ], [ -7.96082, 40.785 ] ],
[ [ -3.9415, 0.799 ], [ -3.95544, 40.854 ] ],
[ [ -7.7162, 4.205 ], [ -3.96374, 40.715 ] ],
[ [ -3.9880, 0.747 ], [ -3.97036, 40.761 ] ]
]
}
]
}

You may also Like

MongoDB Indexes

MongoDB Indexes

April 30, 2020

Leave a reply

Your email address will not be published. Required fields are marked *

About Me

I am a dad of two wonderful boys, Utku Efe and Omer Eren, and married with the love of my life, Elif. In addition, I am an academician and AI/ML scientist because I worked more than 15 years in universities, have M.S and Ph.D. thesis and more than 20 scientific papers/presentations and 100 citations. Now people call me as a Principal Developer in my last company :).
I am really hungry on learning new technologies and get more fun while developing new softwares.

Stay Connected

Recent Posts


MongoDB Indexes


What’s Big Query?


Categories

×