From 84eb00f9e5164d26f0839ca973905bb09f378f1e Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 5 Mar 2019 03:28:51 -0500 Subject: [PATCH] Create function to retrieve the enclosed area from a KML file --- src/Service/KMLFileImporter.php | 70 +++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/Service/KMLFileImporter.php diff --git a/src/Service/KMLFileImporter.php b/src/Service/KMLFileImporter.php new file mode 100644 index 00000000..627e63d7 --- /dev/null +++ b/src/Service/KMLFileImporter.php @@ -0,0 +1,70 @@ +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(); + } +}