MongoDB error code 16755 - Unable to retrieve geo-keys and duplicate vertices

When I try to create an index on geometry db.polygons.createIndex({"geometry":"2dsphere"})

, it stops at a specific polygon with error code 16755. It says this Can't extract geo keys

and Duplicate vertices: 18 and 20

.

Thus, upon further inspection, it seems that this happens when 2 nodes in a polygon are close to each other or even duplicated.

Then I manually delete that node in QGIS and try the process again, only to find another polygon there with the same problem.

How can I fix this problem without repeating the whole process of committing polygon> loading into MongoDB> creating index? Is there a way to find out how many polygons this problem has?

+3


source to share


2 answers


I faced a similar problem. I just needed to find valid records in my dataset (I canceled records with Duplicate Vertices).

I renamed the collection -

db.myCollection.renameCollection('myCollectionBk')

      

Then I added one record from the original collection to the 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 valid entries to the new collection.



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

      

Invalid entries are simply ignored.

In your case, you probably want to get the WriteResult from insert , and see if you succeed. 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});
    }
})

      

Alternatively, check out this question (I couldn't get this to work)

+4


source


So what I did was I first created a collection with an index and then tried to insert using mongoimport which gave me how many were inserted successfully.

> db.someNewCollection.createIndex({"geometry":"2dsphere"})

      



To insert GeoJSON into MongoDB I did the following:

$ jq --compact-output ".features" yourGeoJSON > output.geojson
$ mongoimport --db someDB -c someNewCollection --file "output.geojson" --jsonArray

      

+1


source







All Articles