Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bumped into a shrink call on a BoundingBox that yielded a bbox that goes around the globe #19

Open
victusfate opened this issue Mar 9, 2021 · 0 comments

Comments

@victusfate
Copy link

victusfate commented Mar 9, 2021

believe the issue is with BoundingBox::shrink + BoundingBox::transformBoundingBox

public function shrink(Distance $distance): BoundingBox

private static function transformBoundingBox(BoundingBox $bbox, float $distanceInMeters): BoundingBox

example input to recreate:

$oBBox = BoundingBox::fromCoordinates([-118.45982654727163,33.858738223292775,-118.27313345274219,34.19377442763222]);
$fShrinkDistance = Distance::fromString('12km');
$oShrinkBBox = $oBBox->shrink($fShrinkDistance);

$oShrinkBox coords (note NE x is < SW x)
    [$oShrinkBox] => Geokit\BoundingBox Object
        (
            [southWest:Geokit\BoundingBox:private] => Geokit\Position Object
                (
                    [x:Geokit\Position:private] => -118.32986907162
                    [y:Geokit\Position:private] => 33.96665666694
                )

            [northEast:Geokit\BoundingBox:private] => Geokit\Position Object
                (
                    [x:Geokit\Position:private] => -118.40360502789
                    [y:Geokit\Position:private] => 34.085855983985
                )
        )

Screen Shot 2021-03-09 at 2 08 52 PM

the original method when passed with a positive shrink distance can result in minLon > maxLon. I added the additional check on lat just to be clear. Maybe there's something more elegant :D

below is my local fix

        /**
         * @see http://janmatuschek.de/LatitudeLongitudeBoundingCoordinates
         */
        private static function transformBoundingBox(BoundingBox $bbox, float $distanceInMeters): BoundingBox
        {
            $latSW = deg2rad($bbox->southWest()->latitude());
            $lngSW = deg2rad($bbox->southWest()->longitude());

            $latNE = deg2rad($bbox->northEast()->latitude());
            $lngNE = deg2rad($bbox->northEast()->longitude());

            $angularDistance = $distanceInMeters / Earth::RADIUS;

            $minLat = $latSW - $angularDistance;
            $maxLat = $latNE + $angularDistance;
            $fMinLat = min($minLat,$maxLat);
            $fMaxLat = max($minLat,$maxLat);
            $minLat = $fMinLat;
            $maxLat = $fMaxLat;

            $deltaLonSW = asin(sin($angularDistance) / cos($latSW));
            $deltaLonNE = asin(sin($angularDistance) / cos($latNE));

            $minLon = $lngSW - $deltaLonSW;
            $maxLon = $lngNE + $deltaLonNE;

            // asin can be neg Mark E.
            $fMinLon = min($minLon,$maxLon);
            $fMaxLon = max($minLon,$maxLon);
            $minLon = $fMinLon;
            $maxLon = $fMaxLon;

            $positionSW = Position::fromXY(rad2deg($minLon), rad2deg($minLat));
            $positionNE = Position::fromXY(rad2deg($maxLon), rad2deg($maxLat));

            // Check if we're shrinking too much
            if ($positionSW->latitude() > $positionNE->latitude()) {
                $center = $bbox->center();

                return BoundingBox::fromCornerPositions($center, $center);
            }

            return BoundingBox::fromCornerPositions($positionSW, $positionNE);
        }

final result calling above vs shrink:
shrunken bbox coords -> [-118.40360502789,33.96665666694,-118.32986907162,34.085855983985];
Screen Shot 2021-03-09 at 1 55 53 PM

@victusfate victusfate changed the title bumped into a shrink call on a BoundingBox that yielded the bbox that goes around the globe bumped into a shrink call on a BoundingBox that yielded a bbox that goes around the globe Mar 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant