2

I'm attempting to build a spatial index on a collection of features that I created from:

ogr2ogr -f GeoJSON out.json ... and then imported into MongoDB (2.6.1).

When I attempt to build a spatial index({"features" : "2d"}), I receive this error:

Error creating index
WriteConcern detected an error 'geo values must be 'legacy coordinate pairs' for 2d indexes

The MongoDB glossary has this for legacy coordinate pairs:

The format used for geospatial data prior to MongoDB version 2.4. This format stores geospatial data as points on a planar coordinate system (e.g. [ x, y ]).

I don't see how the GeoJSON doesn't meet this spec, since the coordinate pairs are also [x,y]. Is there something else in the legacy coordinate that a Mongo spatial index is expecting? And if that is the case, does this mean that you can't build a spatial index directly against GeoJSON (meaning that the geometry JSON has to be modified in some way to make it ingestible by MongoDB).

Ref: What is difference between GeoJSON & Legacy coordinate pairs in terms of mongoDb?

3

The GeoJSON geometry has more than just an array of the coordinates, which is all the 2d index is expecting. GeoJSON also contains the type and possibly the coordinate system/projection.

From the docs:

Do not use a 2d index if your location data includes GeoJSON objects. To index on both legacy coordinate pairs and GeoJSON objects, use a 2dsphere index.

So, use a 2dsphere index instead. But, in order to use a 2dsphere index, you will have to project your data to WGS84, since that is what MongoDB expects.

  • Thanks...my coordinates were NOT WGS84 so I wasn't using 2dsphere. – Jay Cummins Jun 10 '14 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.