82 lines
2.4 KiB
PHP
82 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Service;
|
|
|
|
use XMLReader;
|
|
|
|
use App\Entity\SupportedArea;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use CrEOF\Spatial\PHP\Types\Geometry\Polygon;
|
|
use CrEOF\Spatial\PHP\Types\Geometry\Point;
|
|
use CrEOF\Spatial\PHP\Types\Geometry\LineString;
|
|
|
|
class KMLFileImporter
|
|
{
|
|
protected $em;
|
|
|
|
public function __construct(EntityManagerInterface $em)
|
|
{
|
|
$this->em = $em;
|
|
}
|
|
|
|
public function getMapData($fh)
|
|
{
|
|
$placemark_name = '';
|
|
|
|
$reader = new XMLReader();
|
|
$reader->open($fh);
|
|
|
|
while($reader->read())
|
|
{
|
|
if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "Placemark")
|
|
{
|
|
while($reader->read())
|
|
{
|
|
if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "name")
|
|
{
|
|
$placemark_name = $reader->readInnerXML();
|
|
}
|
|
|
|
if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "coordinates")
|
|
{
|
|
// each polygon is a new area
|
|
$supported_area = new SupportedArea();
|
|
$supported_area->setName($placemark_name);
|
|
|
|
$point_array = [];
|
|
$coordinates = $reader->readInnerXML();
|
|
|
|
// get each line
|
|
$coord_split = explode("\n", $coordinates);
|
|
|
|
// go through all the coordinates
|
|
foreach ($coord_split as $coord)
|
|
{
|
|
// skip blank lines
|
|
$coord_trim = trim($coord);
|
|
if (strlen($coord_trim) <= 0)
|
|
continue;
|
|
|
|
// echo "$coord_trim\n";
|
|
|
|
$point_split = explode(',', $coord_trim);
|
|
|
|
$point_array[] = new Point($point_split[0], $point_split[1]);
|
|
}
|
|
|
|
$area = new Polygon([new LineString($point_array)]);
|
|
$supported_area->setCoverageArea($area);
|
|
|
|
// add supported area
|
|
$this->em->persist($supported_area);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
$reader->close();
|
|
|
|
$this->em->flush();
|
|
}
|
|
}
|