Esempio n. 1
0
 /**
  * Set address
  *
  * @param \Geocoder\Model\Address $address
  * @return Geo
  */
 private function setAddress($address)
 {
     if (!$this->formatter) {
         $this->formatter = new StringFormatter();
     }
     $this->address = trim($this->formatter->format($address, '%S %n %z %L %C'));
     return $this;
 }
 /**
  * @param string $address
  *
  * @return bool|\Geo\Model\Entity\GeocodedAddress
  */
 public function retrieve($address)
 {
     $entity = $this->find()->where(['address' => $address])->first();
     if ($entity) {
         return $entity;
     }
     $result = $this->_execute($address);
     $address = $this->newEntity(['address' => $address]);
     if ($result) {
         $address->lat = $result->getLatitude();
         $address->lng = $result->getLongitude();
         $address->country = $result->getCountry()->getCode();
         $formatter = new StringFormatter();
         $address->formatted_address = $formatter->format($result, '%S %n, %z %L');
         $address->data = $result;
     }
     return $this->save($address);
 }
Esempio n. 3
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $coordinate = new Coordinate($input->getArgument('coordinate'));
     $geocoder = new ProviderAggregator();
     $adapter = $this->getAdapter($input->getOption('adapter'));
     $provider = $this->getProvider($input->getOption('provider'));
     if ($input->getOption('args')) {
         $args = is_array($input->getOption('args')) ? implode(',', $input->getOption('args')) : $input->getOption('args');
         $geocoder->registerProvider(new $provider(new $adapter(), $args));
     } else {
         $geocoder->registerProvider(new $provider(new $adapter()));
     }
     $reversed = $geocoder->reverse($coordinate->getLatitude(), $coordinate->getLongitude());
     $reversed = $reversed->first();
     $formatter = new Formatter();
     if ($input->getOption('raw')) {
         $result = array();
         $result[] = sprintf('<label>Adapter</label>:       <value>%s</value>', $adapter);
         $result[] = sprintf('<label>Provider</label>:      <value>%s</value>', $provider);
         if ($input->getOption('args')) {
             $result[] = sprintf('<label>Arguments</label>:     <value>%s</value>', $args);
         }
         $result[] = '---';
         $result[] = sprintf('<label>Latitude</label>:      <value>%s</value>', $reversed->getLatitude());
         $result[] = sprintf('<label>Longitude</label>:     <value>%s</value>', $reversed->getLongitude());
         if (null !== ($bounds = $reversed->getBounds()->toArray())) {
             $result[] = '<label>Bounds</label>';
             $result[] = sprintf(' - <label>South</label>: <value>%s</value>', $bounds['south']);
             $result[] = sprintf(' - <label>West</label>:  <value>%s</value>', $bounds['west']);
             $result[] = sprintf(' - <label>North</label>: <value>%s</value>', $bounds['north']);
             $result[] = sprintf(' - <label>East</label>:  <value>%s</value>', $bounds['east']);
         }
         $result[] = sprintf('<label>Street Number</label>: <value>%s</value>', $reversed->getStreetNumber());
         $result[] = sprintf('<label>Street Name</label>:   <value>%s</value>', $reversed->getStreetName());
         $result[] = sprintf('<label>Zipcode</label>:       <value>%s</value>', $reversed->getPostalCode());
         $result[] = sprintf('<label>City</label>:          <value>%s</value>', $reversed->getLocality());
         $result[] = sprintf('<label>City District</label>: <value>%s</value>', $reversed->getSubLocality());
         if (NULL !== ($adminLevels = $reversed->getAdminLevels())) {
             $result[] = '<label>Admin Levels</label>';
             foreach ($adminLevels as $adminLevel) {
                 $result[] = sprintf(' - <label>%s</label>: <value>%s</value>', $adminLevel->getCode(), $adminLevel->getName());
             }
         }
         $result[] = sprintf('<label>Country</label>:       <value>%s</value>', $reversed->getCountry()->toString());
         $result[] = sprintf('<label>Country Code</label>:  <value>%s</value>', $reversed->getCountryCode());
         $result[] = sprintf('<label>Timezone</label>:      <value>%s</value>', $reversed->getTimezone());
     } elseif ($input->getOption('json')) {
         $result = sprintf('<value>%s</value>', json_encode($reversed->toArray()));
     } else {
         $result = $formatter->format($reversed, $input->getOption('format'));
     }
     $output->writeln($result);
 }
 /**
  * Uses the Geocode class to query
  *
  * @param string $address
  * @return \Geocoder\Model\Address|null
  */
 protected function _execute($address)
 {
     $this->_Geocoder = new Geocoder($this->_config);
     if ($this->config('cache')) {
         $GeocodedAddresses = TableRegistry::get('Geo.GeocodedAddresses');
         $result = $GeocodedAddresses->find()->where(['address' => $address])->first();
         if ($result) {
             return $result->data ?: null;
         }
     }
     try {
         $addresses = $this->_Geocoder->geocode($address);
     } catch (InconclusiveException $e) {
         $addresses = null;
     } catch (NotAccurateEnoughException $e) {
         $addresses = null;
     }
     $result = null;
     if ($addresses && $addresses->count() > 0) {
         $result = $addresses->first();
     }
     if ($this->config('cache')) {
         $addressEntity = $GeocodedAddresses->newEntity(['address' => $address]);
         if ($result) {
             $formatter = new StringFormatter();
             $addressEntity->formatted_address = $formatter->format($result, '%S %n, %z %L');
             $addressEntity->lat = $result->getLatitude();
             $addressEntity->lng = $result->getLongitude();
             $addressEntity->country = $result->getCountry()->getCode();
             $addressEntity->data = $result;
         }
         if (!$GeocodedAddresses->save($addressEntity, ['atomic' => false])) {
             throw new RuntimeException('Could not store geocoding cache data');
         }
     }
     return $result;
 }