English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Introduction
As everyone knows, MongoDB is a database based on distributed file storage and provides the ability to create geospatial indexes. This article will use MongoDB's geospatial index to judge the area of coordinates and use it.
1Use the Baidu Coordinate Acquisition Tool to define the coordinates of the polygon on the map and save the coordinates of each point.
Baidu Coordinate Acquisition Tool:http://api.map.baidu.com/lbsapi/getpoint/
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
Note:The start and end coordinates must be the same to ensure that the polygon is closed.
2Use the Baidu Map Open Platform Map JS Demo to input the coordinates of the polygon and see if the polygon is suitable.
Baidu Map Open Platform Map JS Demo:http://developer.baidu.com/map/jsdemo.htm#c2_9
Replace the following code in the source code editor and then click to run
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> <style type="text/css"> body, html{width: 100%;height: 100%;margin:0;font-family:"Microsoft YaHei";} #allmap {height:100%; width: 100%;} #control{width:100%;} </style> <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=Your key"></script> <title>Set line and area editable</title> </head> <body> <div id="allmap"></div> <div id="control"> <button onclick = "polyline.enableEditing();polygon.enableEditing();">Open line and area editing features</button> <button onclick = "polyline.disableEditing();polygon.disableEditing();">Close line and area editing features</button> </div> </body> </html> <script type="text/javascript"> // Baidu Map API features var map = new BMap.Map("allmap"); map.centerAndZoom(new BMap.Point(113.330764,23.155878), 15); map.enableScrollWheelZoom(); var polygon = new BMap.Polygon([ new BMap.Point(113.314882,23.163055), new BMap.Point(113.355845,23.167042), new BMap.Point(113.370289,23.149564), new BMap.Point(113.356779,23.129758), new BMap.Point(113.338238,23.13913), new BMap.Point(113.330979,23.124706), new BMap.Point(113.313588,23.140858), new BMap.Point(113.323865,23.158204) ], {strokeColor:"blue", strokeWeight:2, strokeOpacity:0.5}); //Create polygon map.addOverlay(polygon); //Add polygon </script>
Polygon area
3.Define test coordinates
Guangzhou East Station coordinates:113.330908,23.155678 (Inside polygon)
Mafang Building:113.33831,23.137335 (Outside polygon)
4.In MongoDB test
1.Create database
use testdb; db.createUser( { "user":"root", "pwd":"123456", "roles":[{"role" : "readWrite", "db":"testdb"}] } ); db.auth( { "user":"root", "pwd":"123456" } );
2.Insert polygonal range and add index
db.geo.ensureIndex( { polygons: "2dsphere" } ); db.geo.insert( { polygons: { type:"Polygon", coordinates:[[ [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, ]] } } );
3.Judge whether the coordinates are in the polygonal area
Guangzhou East Station coordinates:113.330908,23.155678
db.geo.find( { polygons: { $geoIntersects: { $geometry:{ "type" : "Point", "coordinates" : [113.330908,23.155678}] } } } );
Output:
{ "_id" : ObjectId("57c2b1895fb7fd4790f9f099"), "polygons" : { "type" : "Polygon", "coordinates" : [ [ [ 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 ]] } }
represents the coordinates 113.330908,23.155678 Within the polygonal area
Mafang Building:113.33831,23.137335
db.geo.find( { polygons: { $geoIntersects: { $geometry:{ "type" : "Point", "coordinates" : [113.33831,23.137335}] } } } );
Output:empty
represents the coordinates 113.33831,23.137335 Outside the polygonal area
Summary
That's all about using mongodb to judge whether the coordinates are within the specified polygonal area. I hope the content of this article can bring you some help in learning or work. If you have any questions, you can leave a message for communication, thank you for your support of the Yell tutorial.
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, does not edit the content manually, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)