佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > 利用mongodb查詢某坐標(biāo)是否在規(guī)定多邊形區(qū)域內(nèi)的方法

利用mongodb查詢某坐標(biāo)是否在規(guī)定多邊形區(qū)域內(nèi)的方法

熱門標(biāo)簽:呼叫中心市場需求 地方門戶網(wǎng)站 鐵路電話系統(tǒng) AI電銷 Linux服務(wù)器 服務(wù)外包 百度競價排名 網(wǎng)站排名優(yōu)化

前言

大家都知道MongoDB是一個基于分布式文件存儲的數(shù)據(jù)庫,并提供創(chuàng)建基于地理空間的索引的能力,本文將使用MongoDB 基于地理空間索引進(jìn)行坐標(biāo)所在區(qū)域的判斷及使用。

1.使用百度拾取坐標(biāo)工具,在地圖上定義多邊形的坐標(biāo)點,并把每個點的坐標(biāo)保存。

百度拾取坐標(biāo)工具:http://api.map.baidu.com/lbsapi/getpoint/

多邊形的坐標(biāo)點如下:

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

注意:首尾坐標(biāo)必須一樣,這樣才能使多邊形閉合。

2.使用百度地圖開放平臺地圖JS Demo,把多邊形坐標(biāo)輸入,看看多邊形是否合適。

百度地圖開放平臺地圖JS Demo:http://developer.baidu.com/map/jsdemo.htm#c2_9

把以下代碼替換源碼編輯器中的內(nèi)容,然后點擊運行

!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:"微軟雅黑";}
 #allmap {height:100%; width: 100%;}
 #control{width:100%;}
 /style>
 script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0ak=您的密鑰">/script>
 title>設(shè)置線、面可編輯/title>
/head>
body>
 div id="allmap">/div>
 div id="control">
 button onclick = "polyline.enableEditing();polygon.enableEditing();">開啟線、面編輯功能/button>
 button onclick = "polyline.disableEditing();polygon.disableEditing();">關(guān)閉線、面編輯功能/button>
 /div>
/body>
/html>
script type="text/javascript">
 // 百度地圖API功能
 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}); //創(chuàng)建多邊形
 map.addOverlay(polygon); //增加多邊形
/script>

多邊形區(qū)域

3.定義測試坐標(biāo)

廣州東站坐標(biāo):113.330908,23.155678 (多邊形內(nèi))

宏發(fā)大廈:113.33831,23.137335 (多邊形外)

4.在mongodb測試

1.創(chuàng)建數(shù)據(jù)庫

use testdb;

db.createUser( 
 { 
 "user":"root", 
 "pwd":"123456", 
 "roles":[{"role" : "readWrite", "db":"testdb"}] 
 } 
);

db.auth( 
 { 
 "user":"root", 
 "pwd":"123456" 
 } 
);

2.插入多邊形范圍并添加索引

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.判斷坐標(biāo)是否在多邊形區(qū)域

廣州東站坐標(biāo):113.330908,23.155678

db.geo.find(
 {
 polygons:
 {
 $geoIntersects:
 {
 $geometry:{ 
  "type" : "Point",
  "coordinates" : [113.330908,23.155678] }
 }
 }
 }
);

輸出:

{ "_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 ] ] ] } }

表示坐標(biāo) 113.330908,23.155678 在多邊形區(qū)域內(nèi)

宏發(fā)大廈:113.33831,23.137335

db.geo.find(
 {
 polygons:
 {
 $geoIntersects:
 {
 $geometry:{ 
  "type" : "Point",
  "coordinates" : [113.33831,23.137335] }
 }
 }
 }
);

輸出:

表示坐標(biāo) 113.33831,23.137335 在多邊形區(qū)域外

總結(jié)

以上就是利用mongodb判斷坐標(biāo)是否在指定多邊形區(qū)域內(nèi)的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

您可能感興趣的文章:
  • php+mongodb判斷坐標(biāo)是否在指定多邊形區(qū)域內(nèi)的實例

標(biāo)簽:黃山 蘭州 湖南 仙桃 銅川 湘潭 衡水 崇左

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《利用mongodb查詢某坐標(biāo)是否在規(guī)定多邊形區(qū)域內(nèi)的方法》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    沂南县| 汝阳县| 浙江省| 镇巴县| 望城县| 大足县| 高邮市| 怀仁县| 陇南市| 肃宁县| 独山县| 临泉县| 师宗县| 高邑县| 博兴县| 定兴县| 阜南县| 呈贡县| 宜君县| 江安县| 百色市| 上蔡县| 永修县| 枣庄市| 黄冈市| 江安县| 龙海市| 宜兴市| 安福县| 甘德县| 织金县| 廉江市| 宝清县| 玉树县| 胶州市| 永胜县| 兰坪| 营口市| 涟源市| 邵阳县| 津市市|