mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

Schema for GeoJSON Point/Polygon

Open josephstgh opened this issue 6 years ago • 3 comments

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

  1. Does that mean that I cannot create Polygon (in fact, any other GeoJSON type like MultiPoint, LineString, etc) using object? object as in specifying the lon, lat or lon, lat, alt fields. This does work on Point though
  2. 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

josephstgh avatar Oct 05 '20 12:10 josephstgh

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.

vkarpov15 avatar Oct 06 '20 19:10 vkarpov15

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!

josephstgh avatar Oct 07 '20 02:10 josephstgh

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.

vkarpov15 avatar Oct 09 '20 15:10 vkarpov15