diff --git a/src/Command/ImportKMLFileCommand.php b/src/Command/ImportKMLFileCommand.php new file mode 100644 index 00000000..4d2e71a2 --- /dev/null +++ b/src/Command/ImportKMLFileCommand.php @@ -0,0 +1,39 @@ +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'); + } + + public function __construct(KMLFileImporter $importer) + { + $this->importer = $importer; + + parent::__construct(); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $kml_file = $input->getArgument('file'); + + $this->importer->getMapData($kml_file); + } +} diff --git a/src/Command/TestGeofenceCommand.php b/src/Command/TestGeofenceCommand.php new file mode 100644 index 00000000..1397f08f --- /dev/null +++ b/src/Command/TestGeofenceCommand.php @@ -0,0 +1,44 @@ +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"; + } +} diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index d5fa3b3a..a9a23caf 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -25,6 +25,7 @@ use App\Ramcar\JOEventType; use App\Service\InvoiceCreator; use App\Service\RisingTideGateway; use App\Service\MQTTClient; +use App\Service\GeofenceTracker; use App\Entity\MobileSession; use App\Entity\Customer; @@ -766,7 +767,7 @@ class APIController extends Controller return $res->getReturnResponse(); } - public function requestJobOrder(Request $req, InvoiceCreator $ic) + public function requestJobOrder(Request $req, InvoiceCreator $ic, GeofenceTracker $geo) { // check required parameters and api key $required_params = [ @@ -793,6 +794,19 @@ class APIController extends Controller // instructions $instructions = $req->request->get('delivery_instructions', ''); + // longitude and latitude + $long = $req->request->get('long'); + $lat = $req->request->get('lat'); + + // geofence + $is_covered = $geo->isCovered($long, $lat); + if (!$is_covered) + { + $res->setError(true) + ->setErrorMessage('Location is not covered by our service.'); + return $res->getReturnResponse(); + } + $jo = new JobOrder(); $jo->setSource(TransactionOrigin::MOBILE_APP) @@ -836,9 +850,7 @@ class APIController extends Controller } $jo->setWarrantyClass($warr); - // longitude and latitude - $long = $req->request->get('long'); - $lat = $req->request->get('lat'); + // set coordinates $point = new Point($long, $lat); $jo->setCoordinates($point); diff --git a/src/Entity/SupportedArea.php b/src/Entity/SupportedArea.php new file mode 100644 index 00000000..0f70c39e --- /dev/null +++ b/src/Entity/SupportedArea.php @@ -0,0 +1,86 @@ +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/GeofenceTracker.php b/src/Service/GeofenceTracker.php new file mode 100644 index 00000000..b2052e02 --- /dev/null +++ b/src/Service/GeofenceTracker.php @@ -0,0 +1,35 @@ +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; + } +} diff --git a/src/Service/KMLFileImporter.php b/src/Service/KMLFileImporter.php new file mode 100644 index 00000000..60bcfa54 --- /dev/null +++ b/src/Service/KMLFileImporter.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 + $supported_area = new SupportedArea(); + $supported_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)]); + $supported_area->setCoverageArea($area); + + // add supported area + $this->em->persist($supported_area); + } + } + } + } + $reader->close(); + + $this->em->flush(); + } +}