Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongoDB Geospatial Query $geoWithin got error 'Loop is not closed'

i am newbie for Geospatial Query of mongodb and i try search for point within polygon which have code like below :

db.properties.find({
  location:{
    $geoWithin : {
      $geometry : {
        type:"Polygon",
        coordinates: [[
            [104.9172382, 11.57934524],
            [104.9146632, 11.5724502],
            [104.92753788, 11.57976566]
        ]]
        } 
      } 
    }
  })

and my data table look like below : enter image description here

i want to get result of all point is inside polygon.but i really have no clue

this error alway popup

|____/ Mongo Server error (MongoQueryException): Query failed with error code 2 and error message 'Loop is not closed: [ [ 104.9172382, 11.57934524 ], [ 104.9146632, 11.5724502 ], [ 104.92753788, 11.57976566 ] ]' on server

thank for any idea !

like image 474
chea sotheara Avatar asked Sep 06 '25 11:09

chea sotheara


1 Answers

i found answer that we need to start coordinate and close coordinates are the same value.

db.properties.find({
  location:{
    $geoWithin : {
      $geometry : {
        type:"Polygon",
        coordinates:[
            [
                [ 104.9212999921292 , 11.575955591122819 ] , // start loop
                [ 104.92129194550216 , 11.575198826419006 ] ,
                [ 104.92298978380859 , 11.575238241297862 ] ,
                [ 104.92291736416519 , 11.576023910057827 ] ,
                [ 104.9212999921292 , 11.575955591122819 ] // close loop
            ]   
        ]
       }
     } 
   }
})
like image 184
chea sotheara Avatar answered Sep 09 '25 20:09

chea sotheara