Schema for GeoJSON Point/Polygon
Hi,
I am currently trying out the Point/Polygon schema as below, while Point seem to be working fine, Polygon seem to have issue. I'm not sure if I'm doing it right but here's what I roughly did
// Point schema
const Place = new Schema({
name: string;
location: {
type: 'Point',
coordinates: {
lon: 4.56,
lat: 1.23,
alt: 3.33
}
},
});
schema.index({ location: '2dsphere' });
// I can run query with it using like
.find({
location: {
$near: { $geometry: { type: 'Point', coordinates: [4.55, 1.22] },
$minDistance: 1000,
$maxDistance: 10000
}
})
However, with Polygon, I don't seem to be able to create index for it. I reference https://github.com/Automattic/mongoose/issues/6175#issuecomment-374805181 for the Polygon example as I am unable to find any sample using object
// Polygon schema
const Place = new Schema({
name: string;
location: {
type: 'Polygon',
coordinates: [
{
lon: 4.56,
lat: 1.23,
alt: 3.33
},
{
lon: 5.56,
lat: 2.23
},
{
lon: 6.56,
lat: 3.23
},
{
lon: 4.56,
lat: 1.23,
alt: 3.33
},
]
},
});
schema.index({ location: '2dsphere' });
When creating the index, it will complain: Can't extract geo keys ..... GeoJSON coordinates must be an array of coordinates
So I switch to using double array for coordinates as such
// Polygon schema
const Place = new Schema({
name: string;
location: {
type: 'Polygon',
coordinates: [[
{
lon: 4.56,
lat: 1.23,
alt: 3.33
},
{
lon: 5.56,
lat: 2.23
},
{
lon: 6.56,
lat: 3.23
},
{
lon: 4.56,
lat: 1.23,
alt: 3.33
},
]]
},
});
schema.index({ location: '2dsphere' });
When creating the index, it will complain: Can't extract geo keys ..... GeoJSON coordinates must be an array
So my question is
- Does that mean that I cannot create
Polygon(in fact, any other GeoJSON type likeMultiPoint, LineString, etc) usingobject?objectas in specifying thelon, latorlon, lat, altfields. This does work onPointthough - For
Point(because this is the only working one), is there any different between using[number] vs { lon, lat }?
Thanks
node: v12.18.4 mongodb: 4.2.0
There are quite a few issues here.
First, it looks like you're creating a new schema with concrete geojson data embedded in it? Schemas don't store data, they describe the structure of data.
Second, coordinates: [[{ lat, lon, alt }]] is not valid GeoJSON, so that's why you're getting Can't extract geo keys ..... GeoJSON coordinates must be an array. GeoJSON coordinates are always expressed as arrays of numbers, not objects.
Also please check out our GeoJSON docs and this blog post about GeoJSON.
Sorry about that, I was mixing up the sample.
const Coordinate = new Schema({
lon: number,
lat: number,
alt: number,
})
// Point schema
const Place = new Schema({
name: string;
location: {
type: 'Point',
coordinates: Coordinate,
},
});
// Polygon schema
const Place = new Schema({
name: string;
location: {
type: 'Polygon',
coordinates: [[Coordinate]]
},
});
I was confused as of why it would work with Point defining as object and it does not work for other GeoJSON types like Polygon and so on.
Does that mean that mongoose does not support defining object? I was looking at mongodb-geojson and mongodb-geospatial-queries where it supports defining both [lon, lat] or as embedded docs { lon, lat }. Or am I understanding the docs wrongly?
I did read through a number of blogs, forums, and official docs but I do see that almost every example given are using (array) [lon, lat] instead of object but I also see from mongodb docs that embedded objects are also supported. So when I test using Point where it works, and then other type and it doesn't work, it made me more confused.
Thanks!
The { lat, lon } syntax you mentioned is for what MongoDB calls legacy coordinate pairs. That is a distinct MongoDB-specific alternative to GeoJSON that they're trying to move people away from. So you would be able to build a 2dsphere index on location.coordinates for points, but { location: { type: 'Point', coordinates: { lat: 42, lon: 42 } } } is most definitely not valid GeoJSON. For GeoJSON, you must use an array for coordinates.