Php+mongodb instance _php to determine if coordinates are within a specified polygon region

Source: Internet
Author: User
Tags create index mongodb php mongodb

MongoDB is a database based on distributed file storage and provides the ability to create geo-spatial indexes, and this article will provide an example of whether the coordinates are within a specified polygon region using a PHP connection MongoDB.

1. Defining the Polygon area

The coordinate points of the polygon are as follows:

113.314882,23.163055
113.355845,23.167042
113.370289,23.149564
113.356779,23.129758
113.338238,23.13913
113.330979,23.124706
113.313588,23.140858
113.323865,23.158204
113.314882,23.163055

2. Create a database in MongoDB

Use TestDB;

Db.createuser ( 
  { 
    "user": "Root", " 
    pwd": "123456", 
    "roles": [{"Role": "ReadWrite", "db": "TestDB"}] 
  } 
);

Db.auth ( 
  { 
    "user": "Root", 
    "pwd": "123456" 
  } 
);

3. Use PHP to insert polygon data and determine if the coordinates are in the area

MongoDBPolygons.class.php

<?php/** * MongoDB Polygon Region class to determine whether the coordinates are within the polygon region * date:2016-09-30 * author:fdipzone * ver:1.0 * * Func: * Public  Add Create Polygon Area * Public Checkinarea determine if coordinates are in the polygon area * Private Connect MongoDB/class Mongodbpolygons {//Class

  Start//MONGO DB connection private $_conn = null;

  MONGO db private $_db = null; /** * Initialization * @param string $host mongodb address * @param string $user username * @param string $passwd password * @param S Tring $DB Database */Public function __construct ($host, $user, $passwd, $db) {$this->_conn = $this->connect ($
    Host, $user, $PASSWD);
  $this->_db = $db;  /** * Insert Polygon Data * @param String $collname table name * @param array $data polygon coordinate data * @param Array $index Index * @return Int */Public Function Add ($collname, $data, $index) {//CREATE INDEX $cmd = Array (' createindexes ' =
     > $collname, ' Indexes ' => Array (Array (' name ' => ' index ', ' key ' => $index,     ' NS ' => $this->_db. '.
    $collname)));
    $command = new Mongodb\driver\command ($cmd);

    $this->_conn->executecommand ($this->_db, $command);
    Insert Data $bulk = new Mongodb\driver\bulkwrite ();

    $inserted = 0;
      if ($data) {foreach ($data as $k => $v) {$bulk->insert ($v); $result = $this->_conn->executebulkwrite ($this->_db. '. ')
      $collname, $bulk);
    $inserted = $result->getinsertedcount ();
  return $inserted; /** * Determines whether the @param decimal $longitude longitude * @param decimal in the Polygon region * @param String $collname table name $latitude Latitude * @return Array */Public function Checkinarea ($collname, $longitude, $latitude) {$filter = array (' P Olygons ' => Array (' $geoIntersects ' => array (' $geometry ' => array (' Ty
       PE ' => ' point ', ' coordinates ' => array (doubleval ($longitude), Doubleval ($latitude))       )
          )
      )
    );
    $options = Array (' limit ' =>1);
    $query = new Mongodb\driver\query ($filter, $options); $cursor = $this->_conn->executequery ($this->_db. ')

    $collname, $query);
    $result = Array ();
      if ($cursor) {foreach ($cursor as $v) {$result [] = $v; } return $result?
  $result [0]: $result; /** * Connection MongoDB * @param string $host database Address * @param string $user username * @param string $passwd password * Return dblink * * Private Function Connect ($host, $user, $passwd) {$server = ' mongodb://'. $user. ': $passwd. '
    @ '. $host;
    try{$conn = new Mongodb\driver\manager ();  The catch (Mongodb\driver\exception\connectionexception $e) {throw new errorexception (' Unable to connect to DB server. Error: '.
    $e->getmessage (), 31);
  return $conn;
 }//Class end?>

Demo.php

<?php require ' MongoDBPolygons.class.php ';

Echo ' <strong>php MongoDB determine if the coordinates are in the polygon region to demonstrate:</strong><br><br> ';

Call the MongoDB polygon Area class $oMongoDBPolygons = new Mongodbpolygons (' localhost ', ' root ', ' 123456 ', ' TestDB ');

Index $index = array (' polygons ' => ' 2dsphere '); Insert Polygon Region Data $data = Array (' Polygons ' => array (' type ' => ' Polygon ', ' coor Dinates ' => Array (Array (Doubleval (113.314882), Doubleval (23.163055)), AR
              Ray (Doubleval (113.355845), Doubleval (23.167042)), Array (Doubleval (113.370289), Doubleval (23.149564)),
              Array (Doubleval (113.356779), Doubleval (23.129758)), Array (Doubleval (113.338238), Doubleval (23.13913)), Array (Doubleval (113.330979), Doubleval (23.124706)), Array (Doubleval (113.313588), Doubleval (23.140 858)), Array (Doubleval (113.323865), Doubleval (23.158204)), Array (Doubleval (113).314882), Doubleval (23.163055)));
$inserted = $oMongoDBPolygons->add (' Geo ', $data, $index);

if ($inserted) {echo ' 1. Insert polygon data successfully <br><br> ';}
Determines whether the coordinates are in the polygon area of ECHO ' 2. Judge whether Guangzhou East station coordinate (113.330908, 23.155678) is in the area <br> ';
$result = $oMongoDBPolygons->checkinarea (' Geo ', 113.330908, 23.155678); Echo ' Result: Guangzhou East Station coordinates (113.330908, 23.155678) '. ($result?
' In the area ': ' Outside the area ');

Echo ' <br><br> ';
Echo ' 3. Determine whether the macro-building coordinates (113.33831, 23.137335) are in the area <br> ';
$result = $oMongoDBPolygons->checkinarea (' Geo ', 113.33831, 23.137335); Echo ' Result: macro-hair building coordinates (113.33831, 23.137335) '. ($result?
' In the area ': ' Outside the area ');

Echo ' <br><br> ';

 ?>

Output:
PHP MongoDB Determine whether the coordinates are shown in the polygon region:

1. Insert Polygon Data successfully

2. Judge Guangzhou East Station coordinates (113.330908, 23.155678) whether in the area
Result: Guangzhou East Station coordinates (113.330908, 23.155678) in the area

3. Judge whether the macro-fat building coordinates (113.33831, 23.137335) are in the area
Results: HONGFA Building Coordinates (113.33831, 23.137335) outside the area

Guangzhou East Station coordinates

Hong Fat Building coordinates

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.