简体   繁体   中英

How to check if point is inside polygon MongoDb?

I store geometry polygons in MongoDb. It represents a delivery zones. The input parameter is coordinates point. How to check if this point is inside one of polygons?

I know ray-casting algorithm, but how to do that using MnogoDb engine? Is it opmimal way or better make calculations in script?

You can test if a point is within a polygon quite easily in PHP. This is likely to be easier than with Mongo

$vertx[10, 100, 150, 20]; // all x coordinates
$verty[10, 20, 100, 90]; // all y coordinates
$nvert = count($vertx);
$x = 50;
$y = 50;

$test = inpoly($nvert, $vertx, $verty, $x, $y); // true

function inpoly($nvert, $vertx, $verty, $testx, $testy) {
  $i = $j = $c = 0;
  for ($i = 0, $j = $nvert - 1; $i < $nvert; $j = $i++) {
    if ((($verty[$i] > $testy) != ($verty[$j] > $testy)) && ($testx < ($vertx[$j] - $vertx[$i]) * ($testy - $verty[$i]) / ($verty[$j] - $verty[$i]) + $vertx[$i])) {
      $c = !$c;
    }
  }
  return $c;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM