English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
MongoDB is a database based on distributed file storage and provides the ability to create geospatial indexes. This article will provide an example of using PHP to connect to MongoDB and determine if the coordinates are within the specified polygonal area.
1.Define the polygonal area
The coordinates 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 polygonal data and determine if the coordinates are within the area
MongoDBPolygons.class.php
<?php /** * MongoDB Polygonal Area Class, determines if the coordinates are within the polygonal area * Date: 2016-09-30 * Author: fdipzone * Ver: 1.0 * * Func: * public add Create a polygonal area * public checkInArea Determine if the coordinates are within the polygonal area * private connect Connect to MongoDB */ class MongoDBPolygons { // class start // MongoDB connection private $_conn = null; // mongo db private $_db = null; /** * Initialization * @param String $host MongoDB address * @param String $user Username * @param String $passwd Password * @param String $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; } /** * Determine if within polygonal area * @param String $collname Table Name * @param Decimal $longitude Longitude * @param Decimal $latitude Latitude * @return Array */ public function checkInArea($collname, $longitude, $latitude){ $filter = array( 'polygons' => array( '$geoIntersects' => array( '$geometry' => array( 'type' => '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#63; $result[0] : $result; } /** * Connect to 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(); } 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 demonstration of whether coordinates are within polygon area: </strong><br><br>'; // Invoke MongoDB polygon area class $oMongoDBPolygons = new MongoDBPolygons('localhost','root','123456','testdb'); // Index $index = array('polygons'=>'2dsphere'); // Insert polygon area data $data = array( array( 'polygons' => array( 'type' => 'Polygon' 'coordinates' => array( array( array(doubleval(113.314882),doubleval(23.163055)) array(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.140858)) array(doubleval(113.323865),doubleval(23.158204)) array(doubleval(113.314882),doubleval(23.163055)) ) ) ), ) ); $inserted = $oMongoDBPolygons->add('geo', $data, $index); if($inserted){ echo '1.Successfully inserted polygon data<br><br>'; } // Judge whether the coordinates are within the polygonal area echo '2.Judge Guangzhou East Railway Station Coordinates (113.330908, 23.155678) is within the area<br>'; $result = $oMongoDBPolygons->checkInArea('geo', 113.330908, 23.155678); echo 'Result: Guangzhou East Railway Station Coordinates (113.330908, 23.155678)'.( $result? 'in the area' : 'out of the area'); echo '<br><br>'; echo '3.Judge Hongfa Building Coordinates (113.33831, 23.137335) is within the area<br>'; $result = $oMongoDBPolygons->checkInArea('geo', 113.33831, 23.137335); echo 'Result: Hongfa Building Coordinates (113.33831, 23.137335)'.( $result? 'in the area' : 'out of the area'); echo '<br><br>'; ?>
Output:
php MongoDB judge whether the coordinates are within the polygonal area demonstration:
1.Successfully inserted polygon data
2.Judge Guangzhou East Railway Station Coordinates (113.330908, 23.155678) is within the area
Result: Guangzhou East Railway Station Coordinates (113.330908, 23.155678) is within the area
3.Judge Hongfa Building Coordinates (113.33831, 23.137335) is within the area
Result: Hongfa Building Coordinates (113.33831, 23.137335Out of the area
Guangzhou East Railway Station Coordinates
Hongfa Building Coordinates
That's all for this article. Hope it helps your learning and also hope everyone will support the Shouting Tutorial more.
Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (When reporting, please replace # with @) for complaints, and provide relevant evidence. Once verified, this site will immediately delete the suspected infringing content.