lat = $lat; $this->lon = $lon; } public function toGeoJson(): string { $array = $this->toArrayGeoJson(); return \json_encode($array); } public function jsonSerialize(): array { return $this->toArrayGeoJson(); } public function toArrayGeoJson(): array { return [ "type" => "Point", "coordinates" => [ $this->lon, $this->lat ] ]; } /** * * @return string */ public function toWKT(): string { return 'SRID='.self::$SRID.';POINT('.$this->lon.' '.$this->lat.')'; } /** * * @param type $geojson * @return Point */ public static function fromGeoJson(string $geojson): Point { $a = json_decode($geojson); //check if the geojson string is correct if (NULL === $a or !isset($a->type) or !isset($a->coordinates)){ throw PointException::badJsonString($geojson); } if ($a->type != 'Point'){ throw PointException::badGeoType(); } $lat = $a->coordinates[1]; $lon = $a->coordinates[0]; return Point::fromLonLat($lon, $lat); } public static function fromLonLat(float $lon, float $lat): Point { 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 $array): Point { if ($array['type'] == 'Point' && isset($array['coordinates'])) { return self::fromLonLat($array['coordinates'][0], $array['coordinates'][1]); } } public function getLat(): float { return $this->lat; } public function getLon(): float { return $this->lon; } }