Create function to retrieve the enclosed area from a KML file
This commit is contained in:
parent
87a8770bb4
commit
84eb00f9e5
1 changed files with 70 additions and 0 deletions
70
src/Service/KMLFileImporter.php
Normal file
70
src/Service/KMLFileImporter.php
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Entity\SupportedArea;
|
||||
|
||||
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||||
|
||||
use CrEOF\Spatial\PHP\Types\Geometry\Polygon;
|
||||
use CrEOF\Spatial\PHP\Types\Geometry\Point;
|
||||
use CrEOF\Spatial\PHP\Types\Geometry\LineString;
|
||||
|
||||
class KMLFileImporter
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$this->supported_area = new SupportedArea();
|
||||
}
|
||||
|
||||
public function getMapData(UploadedFile $file)
|
||||
{
|
||||
$coordinate_array = array();
|
||||
$point_array = array();
|
||||
$reader = new XMLReader();
|
||||
$reader->open($file);
|
||||
|
||||
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();
|
||||
echo $placemark_name."\n";
|
||||
$supported_area->setName($placemark_name);
|
||||
|
||||
}
|
||||
if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "coordinates")
|
||||
{
|
||||
$coordinates = $reader->readInnerXML();
|
||||
// clean and parse the coordinates data from KML
|
||||
$coordinates = preg_replace('/\s\s+/', ',', $coordinates);
|
||||
$parsed_coordinates = explode(',', $coordinates);
|
||||
|
||||
for ($x = 0; $x < sizeof($parsed_coordinates); $x++)
|
||||
{
|
||||
if (($x%3 != 0) && ($parsed_coordinates[$x] != ""))
|
||||
{
|
||||
$coordinate_array[] = $parsed_coordinates[$x];
|
||||
}
|
||||
}
|
||||
// create an array of Points for the Polygon constructor
|
||||
$i = 0;
|
||||
while($i < sizeof($coordinate_array))
|
||||
{
|
||||
$point_array[] = new Point($coordinate_array[$i], $coordinate_array[$++i]);
|
||||
$i++;
|
||||
}
|
||||
$area = new Polygon(array(new LineString($point_array)));
|
||||
$supported_area->setSupportedArea($area);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
$reader->close();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue