2018-01-02 06:01:01 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2018-02-21 12:33:09 +08:00
|
|
|
use GeoJson\Feature\Feature;
|
|
|
|
use GeoJson\Feature\FeatureCollection;
|
|
|
|
use GeoJson\Geometry\LineString;
|
|
|
|
use GeoJson\Geometry\Point;
|
2018-01-02 06:01:01 +08:00
|
|
|
|
|
|
|
/**
|
2018-01-02 09:02:22 +08:00
|
|
|
* Return different points/features in GeoJSON format
|
|
|
|
* https://tools.ietf.org/html/rfc7946
|
2018-01-02 06:01:01 +08:00
|
|
|
*/
|
|
|
|
class GeoJson
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $counter;
|
|
|
|
/**
|
|
|
|
* @var array [lon, lat] pairs
|
|
|
|
*/
|
|
|
|
protected $line_coords = [];
|
|
|
|
/**
|
|
|
|
* @var Feature[]
|
|
|
|
*/
|
|
|
|
protected $point_coords = [];
|
|
|
|
|
|
|
|
/**
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param $lat
|
|
|
|
* @param $lon
|
|
|
|
* @param array $attrs Attributes of the Feature
|
2018-01-02 06:01:01 +08:00
|
|
|
*/
|
|
|
|
public function addPoint($lat, $lon, array $attrs)
|
|
|
|
{
|
2018-05-03 04:14:18 +08:00
|
|
|
$point = [$lon, $lat];
|
2018-01-02 06:01:01 +08:00
|
|
|
$this->line_coords[] = [$lon, $lat];
|
2018-05-03 04:14:18 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
if (array_key_exists('alt', $attrs)) {
|
2018-05-03 04:14:18 +08:00
|
|
|
$point[] = $attrs['alt'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->point_coords[] = new Feature(new Point($point), $attrs);
|
2018-08-27 00:40:04 +08:00
|
|
|
$this->counter++;
|
2018-01-02 06:01:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the FeatureCollection for the line
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-02 06:01:01 +08:00
|
|
|
* @return FeatureCollection
|
|
|
|
*/
|
|
|
|
public function getLine(): FeatureCollection
|
|
|
|
{
|
2018-03-20 09:50:40 +08:00
|
|
|
if (empty($this->line_coords) || \count($this->line_coords) < 2) {
|
2018-01-02 06:01:01 +08:00
|
|
|
return new FeatureCollection([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new FeatureCollection([
|
2018-08-27 00:40:04 +08:00
|
|
|
new Feature(new LineString($this->line_coords)),
|
2018-01-02 06:01:01 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the feature collection of all the points
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-02 06:01:01 +08:00
|
|
|
* @return FeatureCollection
|
|
|
|
*/
|
|
|
|
public function getPoints(): FeatureCollection
|
|
|
|
{
|
|
|
|
return new FeatureCollection($this->point_coords);
|
|
|
|
}
|
|
|
|
}
|