简体   繁体   English

如何获得geojson对象的边界框?

[英]How to get bounding box of geojson object?

I have several polygons and multi polygons. 我有几个多边形和多边形。 How to get bounding boxes of them? 如何获得它们的边界框? I need only offline solution. 我只需要离线解决方案。

BBOX

This is my solution: 这是我的解决方案:

function mymax($a, $b)
{
    if ($a === null) return $b;
    if ($b === null) return $a; 

    if($a > $b) return $a;
    return $b;
}

function mymin($a, $b)
{
    if ($a === null) return $b;
    if ($b === null) return $a; 

    if($a < $b) return $a;
    return $b;
}

function bbox($g, $bounds=array(null, null, null, null))    
{
    foreach($g as $iter)
    {
        if(is_array($iter))
        {
                $bounds = bbox($iter, $bounds);
        }else{
            $lon = $g[0];
            $lat = $g[1];

            $n = $bounds[0];
            $s = $bounds[1];
            $w = $bounds[2];
            $e = $bounds[3];

            $n = mymin($lat, $n);
            $s = mymax($lat, $s);
            $w = mymin($lon, $w);
            $e = mymax($lon, $e);

            return array($n, $s, $w, $e);
        }
    }

    return $bounds; 
}

But there is a problem with 180/-180 longitude crossing in it. 但是180 / -180经度交叉存在问题。

Does anybody read this? 有人看过这个吗? The problem of 180/-180 longitude crossing is that the maximum value is 180 and the minimum value is - 180. That means the whole world, but this is wrong. 180 / -180经度交叉的问题是最大值是180,最小值是-180。这意味着整个世界,但这是错误的。 See this picture: 看这张图:

世界

I found geoPHP library and there is the same bug in it. 我找到了geoPHP库,其中也有相同的bug。

Here I found the right answer (Global Gotchas). 在这里,我找到了正确的答案(Global Gotchas)。

Unfortunately, no simple and elegant solution exists to solving the Global Gotchas. 不幸的是,解决Global Gotchas并不存在简单而优雅的解决方案。 Multipart bounding boxes are a possible alternative, but they add complexity to the database and search process, defeating the simplicity of the bounding box. 多部分边界框是一种可能的替代方案,但它们增加了数据库和搜索过程的复杂性,从而破坏了边界框的简单性。

And here is the correct code: 这是正确的代码:

function mymax($a, $b)
{
    if ($a === null) return $b;
    if ($b === null) return $a; 

    if($a > $b) return $a;
    return $b;
}

function mymin($a, $b)
{
    if ($a === null) return $b;
    if ($b === null) return $a; 

    if($a < $b) return $a;
    return $b;
}

function bbox($g, $bounds=array(array(null, null, null, null), array(null, null, null, null)))  
{
    foreach($g as $iter)
    {
        if(is_array($iter))
        {
                $bounds = bbox($iter, $bounds);
        }else{
            $pBounds = $bounds[0];
            $nBounds = $bounds[1];

            $lon = $g[0];
            $lat = $g[1];

            $curBounds = $pBounds;
            if($lon < 0)
            {
                $curBounds = $nBounds;
            }

            $n = $curBounds[0];
            $s = $curBounds[1];
            $w = $curBounds[2];
            $e = $curBounds[3];

            $n = mymin($lat, $n);
            $s = mymax($lat, $s);
            $w = mymin($lon, $w);
            $e = mymax($lon, $e);

            if($lon < 0)
            {
                return array($pBounds, array($n, $s, $w, $e));
            }

            return array(array($n, $s, $w, $e), $nBounds);
        }
    }

    return $bounds; 
}

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

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