Add service to check if customer is within area for hub filtering. Add command to import KML files for hub filtering area. #575

This commit is contained in:
Korina Cordero 2021-06-24 03:01:47 +00:00
parent e61c3f1762
commit 12fd768849
6 changed files with 259 additions and 1 deletions

View file

@ -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

View file

@ -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)%"

View file

@ -0,0 +1,41 @@
<?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\HubFilterArea;
use App\Service\HubFilterKMLFileImporter;
class ImportHubFilterKMLFileCommand extends Command
{
protected $importer;
protected function configure()
{
$this->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;
}
}

View file

@ -0,0 +1,85 @@
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use CrEOF\Spatial\PHP\Types\Geometry\Polygon;
use DateTime;
/**
* @ORM\Entity
* @ORM\Table(name="hub_filter_area")
*/
class HubFilterArea
{
// unique id
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
// date supported area was created
/**
* @ORM\Column(type="datetime")
*/
protected $date_create;
// name of the supported area
/**
* @ORM\Column(type="string", length=80)
*/
protected $name;
// coordinates of the supported area
/**
* @ORM\Column(type="polygon")
*/
protected $coverage_area;
public function __construct()
{
$this->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;
}
}

View file

@ -0,0 +1,82 @@
<?php
namespace App\Service;
use XMLReader;
use App\Entity\HubFilterArea;
use Doctrine\ORM\EntityManagerInterface;
use CrEOF\Spatial\PHP\Types\Geometry\Polygon;
use CrEOF\Spatial\PHP\Types\Geometry\Point;
use CrEOF\Spatial\PHP\Types\Geometry\LineString;
class HubFilterKMLFileImporter
{
protected $em;
public function __construct(EntityManagerInterface $em)
{
$this->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();
}
}

View file

@ -0,0 +1,44 @@
<?php
namespace App\Service;
use Doctrine\ORM\EntityManagerInterface;
use CrEOF\Spatial\PHP\Types\Geometry\Point;
use App\Entity\HubFilterArea;
class HubFilteringGeoChecker
{
protected $em;
protected $geofence_flag;
public function __construct(EntityManagerInterface $em, $geofence_flag)
{
$this->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;
}
}