From 5def677267779450f189ce29d03b737f27a2a069 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 6 Mar 2019 19:52:05 -0500 Subject: [PATCH] Create command that takes a kml file as an argument. Fix errors with the SupportedArea entity file and the KMLImporter service. --- src/Command/ImportKMLFileCommand.php | 45 ++++++++++++++++++++++++++++ src/Entity/SupportedArea.php | 15 +++++----- src/Service/KMLFileImporter.php | 26 +++++++++++----- 3 files changed, 71 insertions(+), 15 deletions(-) create mode 100644 src/Command/ImportKMLFileCommand.php diff --git a/src/Command/ImportKMLFileCommand.php b/src/Command/ImportKMLFileCommand.php new file mode 100644 index 00000000..9695059b --- /dev/null +++ b/src/Command/ImportKMLFileCommand.php @@ -0,0 +1,45 @@ +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); + } +} diff --git a/src/Entity/SupportedArea.php b/src/Entity/SupportedArea.php index c8914998..0f70c39e 100644 --- a/src/Entity/SupportedArea.php +++ b/src/Entity/SupportedArea.php @@ -29,7 +29,7 @@ class SupportedArea // name of the supported area /** - * @ORM\Colume(type="string", length=25) + * @ORM\Column(type="string", length=80) */ protected $name; @@ -37,7 +37,7 @@ class SupportedArea /** * @ORM\Column(type="polygon") */ - protected $supported_area; + protected $coverage_area; public function __construct() { @@ -57,7 +57,7 @@ class SupportedArea public function getDateCreate() { - returh $this->date_Create; + return $this->date_Create; } public function setName($name) @@ -71,15 +71,16 @@ class SupportedArea return $this->name; } - public function setSupportedArea(Polygon $polygon) + public function setCoverageArea(Polygon $polygon) { - $this->supported_area = $polygon; + $this->coverage_area = $polygon; return $this; } - public function getSupportedArea() + public function getCoverageArea() { - return $this->supported_area; + return $this->coverage_area; } } + diff --git a/src/Service/KMLFileImporter.php b/src/Service/KMLFileImporter.php index 627e63d7..9e996230 100644 --- a/src/Service/KMLFileImporter.php +++ b/src/Service/KMLFileImporter.php @@ -2,9 +2,11 @@ namespace App\Service; +use XMLReader; + 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\Point; @@ -12,17 +14,21 @@ use CrEOF\Spatial\PHP\Types\Geometry\LineString; 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(); $point_array = array(); + $supported_area = new SupportedArea(); + $reader = new XMLReader(); - $reader->open($file); + $reader->open($fh); while($reader->read()) { @@ -33,7 +39,6 @@ class KMLFileImporter if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "name") { $placemark_name = $reader->readInnerXML(); - echo $placemark_name."\n"; $supported_area->setName($placemark_name); } @@ -55,16 +60,21 @@ class KMLFileImporter $i = 0; 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++; } $area = new Polygon(array(new LineString($point_array))); - $supported_area->setSupportedArea($area); + $supported_area->setCoverageArea($area); } } break; } } $reader->close(); + + // add supported area + $em = $this->em; + $em->persist($supported_area); + $em->flush(); } }