From d24be90314dae75eec943a4f84c2c4977e75e90f Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Sun, 10 Mar 2019 19:47:33 +0800 Subject: [PATCH] Refactor import kml to handle multiple polygons #141 --- src/Command/ImportKMLFileCommand.php | 10 +---- src/Service/KMLFileImporter.php | 56 ++++++++++++++-------------- 2 files changed, 30 insertions(+), 36 deletions(-) diff --git a/src/Command/ImportKMLFileCommand.php b/src/Command/ImportKMLFileCommand.php index 9695059b..4f378498 100644 --- a/src/Command/ImportKMLFileCommand.php +++ b/src/Command/ImportKMLFileCommand.php @@ -15,7 +15,7 @@ class ImportKMLFileCommand extends Command { protected function configure() { - $this->setName('supportedarea:add') + $this->setName('supportedarea:import') ->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'); @@ -31,14 +31,6 @@ class ImportKMLFileCommand extends Command 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/Service/KMLFileImporter.php b/src/Service/KMLFileImporter.php index ac040c35..60bcfa54 100644 --- a/src/Service/KMLFileImporter.php +++ b/src/Service/KMLFileImporter.php @@ -23,9 +23,7 @@ class KMLFileImporter public function getMapData($fh) { - $coordinate_array = array(); - $point_array = array(); - $supported_area = new SupportedArea(); + $placemark_name = ''; $reader = new XMLReader(); $reader->open($fh); @@ -39,42 +37,46 @@ class KMLFileImporter if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "name") { $placemark_name = $reader->readInnerXML(); - $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); + // each polygon is a new area + $supported_area = new SupportedArea(); + $supported_area->setName($placemark_name); - for ($x = 0; $x < sizeof($parsed_coordinates); $x++) + $point_array = []; + $coordinates = $reader->readInnerXML(); + + // get each line + $coord_split = explode("\n", $coordinates); + + // go through all the coordinates + foreach ($coord_split as $coord) { - if (($x%3 != 0) && ($parsed_coordinates[$x] != "")) - { - $coordinate_array[] = $parsed_coordinates[$x]; - } + // 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]); } - // 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))); + + $area = new Polygon([new LineString($point_array)]); $supported_area->setCoverageArea($area); + + // add supported area + $this->em->persist($supported_area); } } - break; } } $reader->close(); - // add supported area - $em = $this->em; - $em->persist($supported_area); - $em->flush(); + $this->em->flush(); } }