5

When I try to create an index on geometry db.polygons.createIndex({"geometry":"2dsphere"}), it stops at a certain polygon with the error code 16755. It says it Can't extract geo keys and then either Duplicate vertices: 18 and 20 or Edges 16 and 18 cross.

It's nice that MongoDB points out where the problem is. I then go manually remove these nodes in QGIS and re-try the process, only to find there's another polygon with the same issue.

How can I fix this issue without having to repeat the entire process of fixing polygons > uploading to MongoDB > and then attempting to create the index?

Is there a way I can find out how many polygons have this issue? How many of them are malformed according to MongoDB?

edit: I should note that I'm dealing with 25,000 polygons. Hence it would be nice to have a list of all the malformed polygons that MongoDB rejects.

kaoscify
  • 981
  • 2
  • 10
  • 22

1 Answers1

4

I hit a similar problem. I just needed to find the valid records in my dataset (I discarded the records with Duplicate Vertices).

I renamed the collection -

db.myCollection.renameCollection('myCollectionBk')

Then I added a single record from the original collection into a new collection and added a geospatial index to the collection

db.myCollection.insert(db.myCollectionBk.findOne()) // recreate the collection
db.myCollection.createIndex({geometry:"2dsphere"}) // create the index (assumes the geometry on the record is valid)
db.myCollection.remove({}) // remove the record

Then I added the valid records into the new collection.

db.myCollectionBk.find().forEach(function(x){
    db.myCollection.insert(x);
})

Invalid records are simply ignored.

In your case you probably want to get the WriteResult from your insert, and look to see if it was successful. Something like

var errors = []
db.myCollectionBk.find().forEach(function(x){
    var result = db.myCollection.insert(x);
    if (result.writeError) {
        errors.push({x._id:result.writeError.errmsg});
    }
})

As another alternative, check out this question (I couldn't get this to work)

Aidan Ewen
  • 156
  • 5
  • This helped me a ton - only tweak is that newer versions of Mongo have a writeConcernError property instead of a writeError - details at https://www.mongodb.com/docs/manual/reference/method/db.collection.insert/#write-concern-errors – Grey Vugrin Apr 24 '23 at 00:23