繁体   English   中英

MongoDB Geospatial-搜索指向项目和geoIntersects

[英]MongoDB Geospatial - searching to point items AND geoIntersects

我正在开发一个在特定点附近搜索业务的应用程序:

当前文档结构如下:

{
        "_id" : ObjectId("52dbcf155b67b9f80f000021"),
        "name" : "Some Business Name",
        "town" : "Bournemouth",
        "postcode" : "BH7 4XK",
        "country_code" : "UK",
        "country_name" : "United Kingdom",

        "geo" : {
                "type" : "Point",
                "coordinates" : [
                        -1.8185951,
                        50.7429735
                ]
        }

}

这代表业务。

“ geo”代表纬度/经度

然后,我可以使用$near搜索点数为x英里的商户

但是,某些企业提供“移动”服务。
-即,距离给定点x英里。 在这种情况下,我考虑过向文档添加另一个元素:

    "mobileGeo" : {
            "type" : "Polygon", //I'm guessing?
            "coordinates" : [] // not sure how to represent...
    }

如何更改查询以覆盖“ geo”项和与“ mobileGeo”元素相交的项?

例如:

在此处输入图片说明

A和B是“固定”点C代表距给定点10英里的区域。 由于此半径与“搜索”区域重叠,因此将其返回到结果中。

我看过`$ geoIntersects',但是不确定这是否正确?

$ geoIntersects是运营商将同时包含完全覆盖点(你的A和B),也一切相交(C)。 但是,您只能使用一个运算符,因此,如果要使用$geoIntersects ,则需要确保搜索几何形状属于同一字段。 因此,我建议您存储与显示字段(我想是点)不同的搜索字段(点或多边形)。 然后仅在搜索字段上设置索引。 如果搜索字段包含一个点,那么您当然可以选择不存储显示字段,因为它将是重复的信息。

第二个问题涉及计算点C +半径的多边形。 我认为您无法直接通过GeoJSON做到这一点,因此您需要计算适当的点来组成您的圈子。 在PHP中,您可以执行以下操作:

<?php
$lat = 51.5;
$lon = -0.5;

$latRad = deg2rad( $lat );
$lonRad = deg2rad( $lon );

$diameter = 6371; // radius of earth
$distance =    5; // 5 km

$radius = $distance / $diameter;

$points = [];

// create 16 points.
for ($i = 0; $i <= M_PI * 2 + 0.1; $i += M_PI / 8)
{
    $latNew = $latRad + ( sin( $i ) * $radius );
    $lonNew = $lonRad + ( ( cos( $i ) * $radius ) / cos( $latNew ) );

    $points[] = [ rad2deg( $lonNew ), rad2deg( $latNew ) ];

}

$json =json_encode( [
    'type' => 'Polygon',
    'coordinates' => [[ $points ]]
], JSON_PRETTY_PRINT );

echo $json;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM