diff --git a/.env.dist b/.env.dist index 0262d765..a0693430 100644 --- a/.env.dist +++ b/.env.dist @@ -83,3 +83,6 @@ SSL_ENABLE=set_to_true_or_false # for hub filtering round robin HUB_JO_KEY=hub_jo_count + +# hub geofence +HUB_GEOFENCE_ENABLE=set_to_true_or_false diff --git a/config/services.yaml b/config/services.yaml index 91e1996b..c4da052b 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -295,4 +295,7 @@ services: arguments: $em: "@doctrine.orm.entity_manager" - # TODO: add the HubFilteringGeoChecker here + # hub filter geofence checking + App\Service\HubFilteringGeoChecker: + arguments: + $geofence_flag: "%env(HUB_GEOFENCE_ENABLE)%" diff --git a/src/Command/ImportHubFilterKMLFileCommand.php b/src/Command/ImportHubFilterKMLFileCommand.php new file mode 100644 index 00000000..21450a67 --- /dev/null +++ b/src/Command/ImportHubFilterKMLFileCommand.php @@ -0,0 +1,41 @@ +setName('hubfilterarea:import') + ->setDescription('Extracts map data of the hub filter area from the KML file and saves to database') + ->setHelp('Gets the coordinates of the hub filter area and saves to the database') + ->addArgument('file', InputArgument::REQUIRED, 'Path to the KML file'); + } + + public function __construct(HubFilterKMLFileImporter $importer) + { + $this->importer = $importer; + + parent::__construct(); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $kml_file = $input->getArgument('file'); + + $this->importer->getMapData($kml_file); + + return 0; + } +} diff --git a/src/Entity/HubFilterArea.php b/src/Entity/HubFilterArea.php new file mode 100644 index 00000000..4b2f5ae8 --- /dev/null +++ b/src/Entity/HubFilterArea.php @@ -0,0 +1,85 @@ +date_create = new DateTime(); + } + + public function getID() + { + return $this->id; + } + + public function setDateCreate(DateTime $date_create) + { + $this->date_create = $date_create; + return $this; + } + + public function getDateCreate() + { + return $this->date_Create; + } + + public function setName($name) + { + $this->name = $name; + return $this; + } + + public function getName() + { + return $this->name; + } + + public function setCoverageArea(Polygon $polygon) + { + $this->coverage_area = $polygon; + + return $this; + } + + public function getCoverageArea() + { + return $this->coverage_area; + } +} diff --git a/src/Service/HubFilterKMLFileImporter.php b/src/Service/HubFilterKMLFileImporter.php new file mode 100644 index 00000000..93c851ad --- /dev/null +++ b/src/Service/HubFilterKMLFileImporter.php @@ -0,0 +1,82 @@ +em = $em; + } + + public function getMapData($fh) + { + $placemark_name = ''; + + $reader = new XMLReader(); + $reader->open($fh); + + 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(); + } + + if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "coordinates") + { + // each polygon is a new area + $hub_filter_area = new HubFilterArea(); + $hub_filter_area->setName($placemark_name); + + $point_array = []; + $coordinates = $reader->readInnerXML(); + + // get each line + $coord_split = explode("\n", $coordinates); + + // go through all the coordinates + foreach ($coord_split as $coord) + { + // 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]); + } + + $area = new Polygon([new LineString($point_array)]); + $hub_filter_area->setCoverageArea($area); + + // add hub filter area + $this->em->persist($hub_filter_area); + } + } + } + } + $reader->close(); + + $this->em->flush(); + } +} diff --git a/src/Service/HubFilteringGeoChecker.php b/src/Service/HubFilteringGeoChecker.php new file mode 100644 index 00000000..8db7ac66 --- /dev/null +++ b/src/Service/HubFilteringGeoChecker.php @@ -0,0 +1,44 @@ +em = $em; + $this->geofence_flag = $geofence_flag; + } + + public function isCovered($long, $lat) + { + // check if geofence is enabled + if ($this->geofence_flag == 'true') + { + // see if the point is in any of the polygons + $query = $this->em->createQuery('SELECT count(s) from App\Entity\HubFilterArea s where st_contains(s.coverage_area, point(:long, :lat)) = true') + ->setParameter('long', $long) + ->setParameter('lat', $lat); + + // number of polygons that contain the point + $count = $query->getSingleScalarResult(); + + if ($count > 0) + return true; + + return false; + } + + return true; + } + +}