This article mainly introduces the PHP+MONGODB to determine whether the coordinates in the specified polygon area of the example, has a certain reference value, interested in the small partners can refer to.
MongoDB is a database based on distributed file storage and provides the ability to create geo-spatial-based indexes, this article will provide an instance of using PHP to connect MongoDB to determine whether coordinates are within a specified polygon area.
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 "}] });d B.auth ( { " user ":" Root ", " pwd ":" 123456 " });
3. Insert polygon Data using PHP and determine if the coordinates are within the region
MongoDBPolygons.class.php
<?php/** * MongoDB Polygon Area class to determine if the coordinates are within the polygon area * date:2016-09-30 * author:fdipzone * ver:1.0 * * Func: * Public add Create Polygon Area * Public Checkinarea determine if coordinates are within the polygon area * Private Connect connection MongoDB */class mongodbpolygons {//class start//mo NGO DB connection Private $_conn = null; MONGO db private $_db = null; /** * Initialize * @param string $host mongodb address * @param string $user user name * @param string $passwd password * @param string $DB Database */Public function __construct ($host, $user, $passwd, $db) {$this->_conn = $this->connect ($host, $u Ser, $passwd); $this->_db = $db; /** * Insert Polygon Data * @param String $collname table name * @param array $data polygon coordinate data * @param array $index index * @retur N Int */Public Function Add ($collname, $data, $index) {//CREATE INDEX $cmd = Array (' createindexes ' + = $collnam E, ' indexes ' = = Array (' name ' = ' = ' index ', ' key ' = = $index, ' ns ' =&G T $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 the polygon 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 ' =& Gt Array (' $geoIntersects ' = = Array (' $geometry ' = = Array (' type ' = ' point ', ' coordinates ' = = Array (doubleval ($longitude), Doubleval ($latitude)))) ); $options = Array (' limit ' =>1); $query = newMongodb\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; /** * Connect MongoDB * @param string $host database Address * @param string $user user name * @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
<?phprequire ' MongoDBPolygons.class.php '; Echo ' <strong>php MongoDB determines whether the coordinates are within the polygon area: </strong><br ><br> ';//Call the MongoDB polygon Region class $omongodbpolygons = new Mongodbpolygons (' localhost ', ' root ', ' 123456 ', ' testdb ');// Index $index = array (' polygons ' = ' 2dsphere ');//Insert polygon area Data $data = Array (' polygons ' = = Array ( ' Type ' = ' Polygon ', ' coordinates ' = = Array (Array (Doubleval (113.314882), do Ubleval (23.163055)), Array (Doubleval (113.355845), Doubleval (23.167042)), Array (Doubleval (113.3702 ), Doubleval (23.149564)), Array (Doubleval (113.356779), Doubleval (23.129758)), Array (Doubleval (11 3.338238), Doubleval (23.13913)), Array (Doubleval (113.330979), Doubleval (23.124706)), Array (Doublev Al (113.313588), Doubleval (23.140858)), Array (Doubleval (113.323865), Doubleval (23.158204)), Array (d Oubleval (113.314882), Doubleval (23.163055))), $inserted = $oMongoDBPolygons->add (' Geo ', $data, $index); if ($inserted {echo ' 1. Insert polygon data successfully <br><br> ';} Determine if the coordinates are in the polygon area echo ' 2. Determine if the coordinates of Guangzhou East station (113.330908, 23.155678) are in the area <br> '; $result = $oMongoDBPolygons->checkinarea (' Geo ', 113.330908, 23.155678); Echo ' Result: Guangzhou East Station coordinates (113.330908, 23.155678) '. ($result? ' Within the region ': ' Outside the region '; Echo ' <br><br> '; Echo ' 3. Determine if Acer building coordinates (113.33831, 23.137335) are within the region <br> '; $result = $ Omongodbpolygons->checkinarea (' Geo ', 113.33831, 23.137335); Echo ' Result: HONGFA building coordinates (113.33831, 23.137335) '. ($result? ' Within the region ': ' Outside the region '); Echo ' <br><br> ';? >
Output:
PHP MongoDB Determines whether coordinates are demonstrated within the polygon area:
1. Insert Polygon Data successfully
2. Determine if Guangzhou East station coordinates (113.330908, 23.155678) are within the region
Result: Guangzhou East Station coordinates (113.330908, 23.155678) within the region
3. Determine if Acer building coordinates (113.33831, 23.137335) are within the region
Results: HONGFA Building Coordinates (113.33831, 23.137335) outside the region
Guangzhou East Station coordinates
Acer FAT Building coordinates
The above is the whole content of this article, I hope that everyone's study has helped.