Search code examples
pythonmongodbpymongogeo

Pymongo search geo on line


I have collection with geo coordinates. To find objects in circle I use command like this

collection.find({"loc":{"$within":{"$center":[[49.236484,28.472172], 10]}}})

But I need find objects on street(line)? how I can do that? And please tell me if my command will search right. Thanks.


Solution

  • Why not use a $polygon to make what is essentially a thick line?

    collection.find({'loc':{'$within':{'$polygon':[[linestart_x, linestart_y], [linestart_x+jiggle, linestart_y+jiggle], [lineend_x, lineend_y], [lineend_x+jiggle, lineend_y+jiggle]]}}})
    

    You'll have to experiment to find an appropriate number of the 'jiggle'.

    Enjoy!