Add GeofenceTracker service and test command #141

This commit is contained in:
Kendrick Chan 2019-03-10 22:33:35 +08:00
parent d24be90314
commit 2ffc856b10
3 changed files with 81 additions and 0 deletions

View file

@ -13,6 +13,8 @@ use App\Service\KMLFileImporter;
class ImportKMLFileCommand extends Command
{
protected $importer;
protected function configure()
{
$this->setName('supportedarea:import')

View file

@ -0,0 +1,44 @@
<?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\SupportedArea;
use App\Service\GeofenceTracker;
use CrEOF\Spatial\PHP\Types\Geometry\Point;
class TestGeofenceCommand extends Command
{
protected function configure()
{
$this->setName('test:geofence')
->setDescription('Test geofence tracker service.')
->setHelp('Test the geofence tracker service.')
->addArgument('long', InputArgument::REQUIRED, 'Longitude')
->addArgument('lat', InputArgument::REQUIRED, 'Latitude');
}
public function __construct(GeofenceTracker $geo)
{
$this->geo = $geo;
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$long = $input->getArgument('long');
$lat = $input->getArgument('lat');
if ($this->geo->isCovered($long, $lat))
echo "In geofence\n";
else
echo "NOT in geofence\n";
}
}

View file

@ -0,0 +1,35 @@
<?php
namespace App\Service;
use App\Entity\SupportedArea;
use Doctrine\ORM\EntityManagerInterface;
use CrEOF\Spatial\PHP\Types\Geometry\Point;
class GeofenceTracker
{
protected $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
public function isCovered($long, $lat)
{
// see if the point is in any of the polygons
$query = $this->em->createQuery('SELECT count(s) from App\Entity\SupportedArea 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;
}
}