Create command that takes a kml file as an argument. Fix errors with the SupportedArea entity file and the KMLImporter service.

This commit is contained in:
Korina Cordero 2019-03-06 19:52:05 -05:00
parent 84eb00f9e5
commit 5def677267
3 changed files with 71 additions and 15 deletions

View file

@ -0,0 +1,45 @@
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use App\Entity\SupportedArea;
use App\Service\KMLFileImporter;
class ImportKMLFileCommand extends Command
{
protected function configure()
{
$this->setName('supportedarea:add')
->setDescription('Extracts map data of the supported area from the KML file and saves to database')
->setHelp('Gets the coordinates of the supported area and saves to the database')
->addArgument('file', InputArgument::REQUIRED, 'Path to the KML file');
}
public function __construct(KMLFileImporter $importer)
{
$this->importer = $importer;
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$kml_file = $input->getArgument('file');
try
{
$fh = fopen($kml_file, "r");
}
catch (Exception $e)
{
throw new Exception('The file "' . $kml_file . '" could not be read.');
}
$this->importer->getMapData($kml_file);
}
}

View file

@ -29,7 +29,7 @@ class SupportedArea
// name of the supported area // name of the supported area
/** /**
* @ORM\Colume(type="string", length=25) * @ORM\Column(type="string", length=80)
*/ */
protected $name; protected $name;
@ -37,7 +37,7 @@ class SupportedArea
/** /**
* @ORM\Column(type="polygon") * @ORM\Column(type="polygon")
*/ */
protected $supported_area; protected $coverage_area;
public function __construct() public function __construct()
{ {
@ -57,7 +57,7 @@ class SupportedArea
public function getDateCreate() public function getDateCreate()
{ {
returh $this->date_Create; return $this->date_Create;
} }
public function setName($name) public function setName($name)
@ -71,15 +71,16 @@ class SupportedArea
return $this->name; return $this->name;
} }
public function setSupportedArea(Polygon $polygon) public function setCoverageArea(Polygon $polygon)
{ {
$this->supported_area = $polygon; $this->coverage_area = $polygon;
return $this; return $this;
} }
public function getSupportedArea() public function getCoverageArea()
{ {
return $this->supported_area; return $this->coverage_area;
} }
} }

View file

@ -2,9 +2,11 @@
namespace App\Service; namespace App\Service;
use XMLReader;
use App\Entity\SupportedArea; use App\Entity\SupportedArea;
use Symfony\Component\HttpFoundation\File\UploadedFile; use Doctrine\Common\Persistence\ObjectManager;
use CrEOF\Spatial\PHP\Types\Geometry\Polygon; use CrEOF\Spatial\PHP\Types\Geometry\Polygon;
use CrEOF\Spatial\PHP\Types\Geometry\Point; use CrEOF\Spatial\PHP\Types\Geometry\Point;
@ -12,17 +14,21 @@ use CrEOF\Spatial\PHP\Types\Geometry\LineString;
class KMLFileImporter class KMLFileImporter
{ {
public function __construct() protected $em;
public function __construct(ObjectManager $em)
{ {
$this->supported_area = new SupportedArea(); $this->em = $em;
} }
public function getMapData(UploadedFile $file) public function getMapData($fh)
{ {
$coordinate_array = array(); $coordinate_array = array();
$point_array = array(); $point_array = array();
$supported_area = new SupportedArea();
$reader = new XMLReader(); $reader = new XMLReader();
$reader->open($file); $reader->open($fh);
while($reader->read()) while($reader->read())
{ {
@ -33,7 +39,6 @@ class KMLFileImporter
if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "name") if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "name")
{ {
$placemark_name = $reader->readInnerXML(); $placemark_name = $reader->readInnerXML();
echo $placemark_name."\n";
$supported_area->setName($placemark_name); $supported_area->setName($placemark_name);
} }
@ -55,16 +60,21 @@ class KMLFileImporter
$i = 0; $i = 0;
while($i < sizeof($coordinate_array)) while($i < sizeof($coordinate_array))
{ {
$point_array[] = new Point($coordinate_array[$i], $coordinate_array[$++i]); $point_array[] = new Point($coordinate_array[$i], $coordinate_array[++$i]);
$i++; $i++;
} }
$area = new Polygon(array(new LineString($point_array))); $area = new Polygon(array(new LineString($point_array)));
$supported_area->setSupportedArea($area); $supported_area->setCoverageArea($area);
} }
} }
break; break;
} }
} }
$reader->close(); $reader->close();
// add supported area
$em = $this->em;
$em->persist($supported_area);
$em->flush();
} }
} }