|
@@ -0,0 +1,109 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace FTTHBundle\Command;
|
|
|
+
|
|
|
+use FTTHBundle\Entity\NAP;
|
|
|
+use MapBundle\Entity\Location;
|
|
|
+use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
|
|
|
+use Symfony\Component\Console\Input\InputOption;
|
|
|
+use Symfony\Component\Console\Input\InputInterface;
|
|
|
+use Symfony\Component\Console\Output\OutputInterface;
|
|
|
+
|
|
|
+class MigrateAddressToLatLngCommand extends ContainerAwareCommand
|
|
|
+{
|
|
|
+ protected function configure()
|
|
|
+ {
|
|
|
+ $this
|
|
|
+ ->setName('migrate:data:NAPAddress')
|
|
|
+ ->setAliases(array("ik:mg:NAPAddress"))
|
|
|
+ ->setDescription('Migrate address to Lat/Lng.')
|
|
|
+ ->setHelp('Migrate address to Lat/Lng.')
|
|
|
+ ->addOption('city', null, InputOption::VALUE_REQUIRED, 'City name')
|
|
|
+ ->addOption('state', null, InputOption::VALUE_REQUIRED, 'State name')
|
|
|
+ ->addOption('country', null, InputOption::VALUE_REQUIRED, 'Country name')
|
|
|
+ ->addOption('id', null, InputOption::VALUE_OPTIONAL, 'Id to migrate.')
|
|
|
+ ->addOption('from', null, InputOption::VALUE_OPTIONAL, 'Id from.')
|
|
|
+ ->addOption('to', null, InputOption::VALUE_OPTIONAL, 'Id to.');
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
+ {
|
|
|
+ $em = $this->getContainer()->get('doctrine')->getManager();
|
|
|
+ $city = $input->getOption('city');
|
|
|
+ $state = $input->getOption('state');
|
|
|
+ $country = $input->getOption('country');
|
|
|
+
|
|
|
+ $where = "u.location is NULL";
|
|
|
+ $id = $input->getOption('id');
|
|
|
+ if ($id) {
|
|
|
+ $where .= " and u.id = " . $id;
|
|
|
+ }
|
|
|
+ $from = $input->getOption('from');
|
|
|
+ if ($from) {
|
|
|
+ $where .= " and u.id >= " . $from;
|
|
|
+ }
|
|
|
+ $to = $input->getOption('to');
|
|
|
+ if ($to) {
|
|
|
+ $where .= " and u.id <= " . $to;
|
|
|
+ }
|
|
|
+
|
|
|
+ $q = $em->createQuery("select u from " . NAP::class . " u where $where");
|
|
|
+ $objects = $q->getResult();
|
|
|
+ foreach ($objects as $nap) {
|
|
|
+ $extra = json_decode($nap->getExtraData(), true);
|
|
|
+ $arr = $this->searchLatLng($extra['Address'], $city, $state, $country);
|
|
|
+ $show = $nap->getId() . " - " . $nap->getName() . ". ";
|
|
|
+ if ($arr != null) {
|
|
|
+ $show .= "Ok";
|
|
|
+ // se encontro la direccion
|
|
|
+ $location = new Location();
|
|
|
+ $location->setExtraData("{\"lat\":" . $arr['lat'] . "," .
|
|
|
+ "\"lng\":" . $arr['lng'] . ",\"zoom\":18}");
|
|
|
+ $em->persist($location);
|
|
|
+ $em->flush();
|
|
|
+ $nap->setLocation($location);
|
|
|
+ $em->persist($nap);
|
|
|
+ $em->flush();
|
|
|
+ } else {
|
|
|
+ $show .= "Error: Address=" . $extra['Address'] . " - City=$city - State=$state - Country=$country";
|
|
|
+ }
|
|
|
+ $output->writeln($show);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Busca la latitud y longitud de acuerdo a la direccion que se pasa como parametro.
|
|
|
+ * @param string $address Contiene la direccion.
|
|
|
+ * @param string $city Contiene la ciudad.
|
|
|
+ * @param string $state Contiene la provincia.
|
|
|
+ * @param string $country Contiene el pais.
|
|
|
+ * @return array Retorna un array con la latitud y longitud.
|
|
|
+ */
|
|
|
+ private function searchLatLng($address, $city, $state, $country)
|
|
|
+ {
|
|
|
+ $url = "http://maps.google.com/maps/api/geocode/json?address=";
|
|
|
+ $arrAddress = explode(" ", $address . "," . $city . "," . $state . "," . $country);
|
|
|
+ $address = implode("+", $arrAddress);
|
|
|
+ $url .= $address;
|
|
|
+
|
|
|
+ $ch = curl_init();
|
|
|
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
+ curl_setopt($ch, CURLOPT_URL, $url);
|
|
|
+ $result = curl_exec($ch);
|
|
|
+
|
|
|
+ $json = json_decode($result, true);
|
|
|
+ if (isset($json['results']) &&
|
|
|
+ isset($json['results'][0]) &&
|
|
|
+ isset($json['results'][0]['geometry']) &&
|
|
|
+ isset($json['results'][0]['geometry']['location']) &&
|
|
|
+ isset($json['results'][0]['geometry']['location']['lat']) &&
|
|
|
+ isset($json['results'][0]['geometry']['location']['lng'])
|
|
|
+ ) {
|
|
|
+ return ['address' => $address,
|
|
|
+ 'lat' => json_decode($result, true)['results'][0]['geometry']['location']['lat'],
|
|
|
+ 'lng' => json_decode($result, true)['results'][0]['geometry']['location']['lng']];
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|