利用mongodb查询某坐标是否在规定多边形区域内的方法

2023-06-07 0 1,016

数据库教程-ER0编程网

前言

大家都知道MongoDB是一个基于分布式文件存储的数据库,并提供创建基于地理空间的索引的能力,本文将使用MongoDB 基于地理空间索引进行坐标所在区域的判断及使用。

1.使用百度拾取坐标工具,在地图上定义多边形的坐标点,并把每个点的坐标保存。

百度拾取坐标工具:http://api.map.baidu.com/lbsapi/getpoint/

多边形的坐标点如下:

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.使用百度地图开放平台地图JS Demo,把多边形坐标输入,看看多边形是否合适。

百度地图开放平台地图JS Demo:http://developer.baidu.com/map/jsdemo.htm#c2_9

把以下代码替换源码编辑器中的内容,然后点击运行

<!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.0&ak=您的密钥\"></script>
 <title>设置线、面可编辑</title>
</head>
<body>
 <div id=\"allmap\"></div>
 <div id=\"control\">
 <button onclick = \"polyline.enableEditing();polygon.enableEditing();\">开启线、面编辑功能</button>
 <button onclick = \"polyline.disableEditing();polygon.disableEditing();\">关闭线、面编辑功能</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}); //创建多边形
 map.addOverlay(polygon); //增加多边形
</script>

多边形区域

利用mongodb查询某坐标是否在规定多边形区域内的方法

3.定义测试坐标

广州东站坐标:113.330908,23.155678 (多边形内)

利用mongodb查询某坐标是否在规定多边形区域内的方法

宏发大厦:113.33831,23.137335 (多边形外)

利用mongodb查询某坐标是否在规定多边形区域内的方法

4.在mongodb测试

1.创建数据库

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.判断坐标是否在多边形区域

广州东站坐标: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 ] ] ] } }

表示坐标 113.330908,23.155678 在多边形区域内

宏发大厦:113.33831,23.137335

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

输出:

表示坐标 113.33831,23.137335 在多边形区域外

总结

以上就是利用mongodb判断坐标是否在指定多边形区域内的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。

1. 本站所有资源来源于用户上传和网络,因此不包含技术服务请大家谅解!如有侵权请邮件联系客服!

2. 本站不保证所提供下载的资源的准确性、安全性和完整性,资源仅供下载学习之用!如有链接无法下载、失效或广告,请联系客服处理,有奖励!

3. 您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容资源!如用于商业或者非法用途,与本站无关,一切后果请用户自负!

4. 如果您也有好的资源或教程,您可以投稿发布,成功分享后有猫币奖励和额外收入!

5. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!

ER0学院 数据库 利用mongodb查询某坐标是否在规定多边形区域内的方法 https://www.er0xy.com/26800.html

常见问题

相关文章

发表评论
暂无评论
  • 0 +

    访问总数

  • 0 +

    会员总数

  • 0 +

    文章总数

  • 0 +

    今日发布

  • 0 +

    本周发布

  • 0 +

    运行天数

你的前景,远超我们想象