mjaschen / phpgeo

Simple Yet Powerful Geo Library for PHP
https://phpgeo.marcusjaschen.de
MIT License
1.56k stars 195 forks source link

Given a point and a distance, is it possible to compute the corresponding extent ? #30

Closed felixveysseyre closed 7 years ago

felixveysseyre commented 7 years ago

Hello,

I am trying to compute the extent ((latMin, latMax), (lngMin, lngMax)), from a point (Coordinate object) and a distance.

Is there an easy way to do that ? I am not familiar with GPS coordinates yet.

Thank for your time.

mjaschen commented 7 years ago

What do you mean with “extend”? Does the following sketch show the wanted values of ((latMin, latMax), (lngMin, lngMax))?

felixveysseyre commented 7 years ago

Yes, you correctly understood what I meant.

Here is another schema if needed:  extent

mjaschen commented 7 years ago

It should be easy to do using phpgeo's destination point calculation:

<?php

use Location\Bearing\BearingEllipsoidal;
use Location\Coordinate;

require_once __DIR__ . '/vendor/autoload.php';

$berlin   = new Coordinate(52.5, 13.5);
$distance = 10000; // 10 km

$bearingEllipsoidal = new BearingEllipsoidal();

$northernMost = $bearingEllipsoidal->calculateDestination($berlin, 0, $distance);
$easternMost  = $bearingEllipsoidal->calculateDestination($berlin, 90, $distance);
$southernMost = $bearingEllipsoidal->calculateDestination($berlin, 180, $distance);
$westernMost  = $bearingEllipsoidal->calculateDestination($berlin, 270, $distance);

printf(
    'latMax=%f, latMin=%f, lngMax=%f, lngMin=%f%s',
    $northernMost->getLat(),
    $southernMost->getLat(),
    $easternMost->getLng(),
    $westernMost->getLng(),
    PHP_EOL
);

// verify the results:

$distCalc = new \Location\Distance\Vincenty();

printf(
    'dN=%f, dS=%f, dE=%f, dW=%f%s',
    $berlin->getDistance($northernMost, $distCalc),
    $berlin->getDistance($southernMost, $distCalc),
    $berlin->getDistance($easternMost, $distCalc),
    $berlin->getDistance($westernMost, $distCalc),
    PHP_EOL
);

The code above will produce the output below:

latMax=52.589865, latMin=52.410133, lngMax=13.647253, lngMin=13.352747
dN=10000.000000, dS=10000.000000, dE=10000.000000, dW=10000.000000
felixveysseyre commented 7 years ago

Thanks @mjaschen, It works like a charm !