Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
protezione-civile-bot/src/GP/ProtezioneCivileBot/Model/Map.php
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
75 lines (60 sloc)
1.83 KB
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
<?php | |
/** | |
* This file is part of the ProtezioneCivileBot package. | |
* For the full copyright and license information, please view the LICENSE | |
* file that was distributed with this source code. | |
* | |
* @license MIT License | |
*/ | |
namespace GP\ProtezioneCivileBot\Model; | |
/** | |
* @author Giovanni Pirrotta <giovanni.pirrotta@gmail.com> | |
*/ | |
class Map | |
{ | |
private $aree; | |
public function __construct() | |
{ | |
$this->aree = []; | |
} | |
public function addArea(Area $area) | |
{ | |
$this->aree[] = $area; | |
} | |
public function loadFromGeoJSONFilename($filename) | |
{ | |
$json = file_get_contents($filename); | |
$json = json_decode($json); | |
foreach($json->features as $feature) { | |
$properties = $feature->properties; | |
$area = new Area(); | |
if (property_exists($properties, 'name')) { | |
$area->setName($properties->name); | |
} | |
if (property_exists($properties, 'description')) { | |
$area->setDescription($properties->description); | |
} | |
foreach($feature->geometry->coordinates as $polygon) { | |
foreach($polygon as $point) { | |
$area->addVertex(new Vertex($point[1], $point[0])); | |
} | |
} | |
$this->addArea($area); | |
unset($area); | |
} | |
} | |
public function getNearestArea(Vertex $vertex) | |
{ | |
$areaNearest = null; | |
$distanceNearest = PHP_INT_MAX; | |
foreach($this->aree as $areaAccoglienza) { | |
$areaAccoglienza->calculateVertexNearest($vertex); | |
$v = $areaAccoglienza->getVertexNearest(); | |
if ($v->getDistance() < $distanceNearest) { | |
$distanceNearest = $v->getDistance(); | |
$areaNearest = $areaAccoglienza; | |
} | |
} | |
return $areaNearest; | |
} | |
} | |