Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are coordinates of polygon GeoJSON Objects stored in an array of array?

As seen in the official documentation, structure of the polygon GeoJSON object is as shown below:

db.someCollection.insert({
  type: "Polygon",
  coordinates: [
    [
      [0, 0], [3, 6], [6, 1], [0, 0]
    ]
  ]
});

Why is it not structured as shown below in Type A format?

Type A

db.someCollection.insert({
  type: "Polygon",
  coordinates: [
    [0, 0], [3, 6], [6, 1], [0, 0]
  ]
});

I assume the reason might be to store multiple geofences. Something like this:

Type B

db.someCollection.insert({
  type: "Polygon",
  coordinates: [
    [
      [0, 0], [3, 6], [6, 1], [0, 0]
    ],
    [
      [1, 1], [3, 6], [6, 1], [1, 1]
    ]
  ]
});

The reason why I posted this question is because I guess my assumption is wrong after using some of the operators in MongoDB (like $geoIntersects and $geoWithin) which requires the structure to be in Type A format.

like image 351
Dheemanth Bhat Avatar asked Oct 24 '17 18:10

Dheemanth Bhat


1 Answers

MongoDB does not define the GeoJSON format. Instead, it was defined in a standard: RFC7946

This is the relevant section about polygon in the RFC: https://www.rfc-editor.org/rfc/rfc7946#section-3.1.6 where it stated:

  • For type "Polygon", the "coordinates" member MUST be an array of linear ring coordinate arrays.
  • For Polygons with more than one of these rings, the first MUST be the exterior ring, and any others MUST be interior rings. The exterior ring bounds the surface, and the interior rings (if present) bound holes within the surface.

Where a linear ring is defined as:

  • A linear ring is a closed LineString with four or more positions.

And a LineString is https://www.rfc-editor.org/rfc/rfc7946#section-3.1.4:

  • For type "LineString", the "coordinates" member is an array of two or more positions.

Basically a polygon is defined as a series of closed LineStrings, with the first LineString defining the borders of the polygon, and subsequent LineStrings defining "holes" in the first LineString.

Defined in this manner, it is possible to create a donut shape with polygon.

This type of construction is only possible if it's represented as an array of arrays, hence the standard.

like image 144
kevinadi Avatar answered Sep 29 '22 18:09

kevinadi