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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With