Last active
December 16, 2019 04:10
-
-
Save chuehnone/c9c5ab4ea226730068ea42338a94b9dd to your computer and use it in GitHub Desktop.
PHP convert wkb point
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DbHelper | |
{ | |
/** | |
* WKB is Well-known binary. | |
* | |
* @param Point $point | |
* | |
* @return string | |
*/ | |
public static function convertPointToWKB(Point $point): string | |
{ | |
return pack('LcLd2', 0, 1, 1, $point->getLongitude(), $point->getLatitude()); | |
} | |
/** | |
* @param string $wkb | |
* | |
* @return Point | |
*/ | |
public static function convertWKBToPoint(string $wkb): Point | |
{ | |
$data = unpack('x/x/x/x/corder/Ltype/dlat/dlon', $wkb); | |
$point = new Point(); | |
$point->setLatitude($data['lat']); | |
$point->setLongitude($data['lon']); | |
return $point; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Point | |
{ | |
/** | |
* @var float | |
*/ | |
private $latitude; | |
/** | |
* @var float | |
*/ | |
private $longitude; | |
/** | |
* @return float | |
*/ | |
public function getLatitude(): float | |
{ | |
return $this->latitude; | |
} | |
/** | |
* @param float $latitude | |
*/ | |
public function setLatitude(float $latitude): void | |
{ | |
$this->latitude = $latitude; | |
} | |
/** | |
* @return float | |
*/ | |
public function getLongitude(): float | |
{ | |
return $this->longitude; | |
} | |
/** | |
* @param float $longitude | |
*/ | |
public function setLongitude(float $longitude): void | |
{ | |
$this->longitude = $longitude; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment