mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 07:03:49 +00:00
add point type + Address: add Point field : fix dependency injection of the Point type
This commit is contained in:
96
src/Bundle/ChillMainBundle/Doctrine/Model/Point.php
Normal file
96
src/Bundle/ChillMainBundle/Doctrine/Model/Point.php
Normal file
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\MainBundle\Doctrine\Model;
|
||||
|
||||
use \JsonSerializable;
|
||||
|
||||
/**
|
||||
* Description of Point
|
||||
*
|
||||
*/
|
||||
class Point implements JsonSerializable {
|
||||
private $lat;
|
||||
private $lon;
|
||||
public static $SRID = '4326';
|
||||
|
||||
private function __construct($lon, $lat) {
|
||||
$this->lat = $lat;
|
||||
$this->lon = $lon;
|
||||
}
|
||||
|
||||
public function toGeoJson() {
|
||||
$array = $this->toArrayGeoJson();
|
||||
return \json_encode($array);
|
||||
}
|
||||
|
||||
public function jsonSerialize() {
|
||||
return $this->toArrayGeoJson();
|
||||
}
|
||||
|
||||
public function toArrayGeoJson() {
|
||||
return array("type" => "Point",
|
||||
"coordinates" => array ($this->lon, $this->lat));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function toWKT() {
|
||||
return 'SRID='.self::$SRID.';POINT('.$this->lon.' '.$this->lat.')';
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param type $geojson
|
||||
* @return Point
|
||||
*/
|
||||
public static function fromGeoJson($geojson)
|
||||
{
|
||||
$a = json_decode($geojson);
|
||||
//check if the geojson string is correct
|
||||
if ($a == null or !isset($a->type) or !isset($a->coordinates)){
|
||||
throw PointException::badJsonString($geojson);
|
||||
}
|
||||
|
||||
if ($a->type != "Point"){
|
||||
throw PointException::badGeoType();
|
||||
} else {
|
||||
$lat = $a->coordinates[1];
|
||||
$lon = $a->coordinates[0];
|
||||
return Point::fromLonLat($lon, $lat);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static function fromLonLat($lon, $lat)
|
||||
{
|
||||
if (($lon > -180 && $lon < 180) && ($lat > -90 && $lat < 90))
|
||||
{
|
||||
return new Point($lon, $lat);
|
||||
} else {
|
||||
throw PointException::badCoordinates($lon, $lat);
|
||||
}
|
||||
}
|
||||
|
||||
public static function fromArrayGeoJson($array)
|
||||
{
|
||||
if ($array['type'] == 'Point' &&
|
||||
isset($array['coordinates']))
|
||||
{
|
||||
return self::fromLonLat($array['coordinates'][0], $array['coordinates'][1]);
|
||||
}
|
||||
}
|
||||
|
||||
public function getLat()
|
||||
{
|
||||
return $this->lat;
|
||||
}
|
||||
|
||||
public function getLon()
|
||||
{
|
||||
return $this->lon;
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user