コード例 #1
0
 /**
  * @param id
  * - NULL: update all records with missing coordinates only
  * - otherwise: specific update
  * @deprecated but seems to have better lat/lng for countries...
  */
 public function updateCoordinates($id = null)
 {
     $Geocoder = new GeocodeLib();
     //$Geocoder->setup();
     $override = false;
     if ($id == -1) {
         $id = '';
         $override = true;
     }
     $conditions = [];
     if (!$override) {
         $conditions = [$this->alias() . '.lat' => 0, $this->alias() . '.lng' => 0];
     }
     if (!empty($id)) {
         $res = $this->find('first', ['fields' => [$this->alias() . '.id', $this->alias() . '.name', $this->alias() . '.ori_name'], 'conditions' => [$this->alias() . '.id' => $id], 'contain' => []]);
         if (!empty($res['ori_name']) && ($data = $Geocoder->geocode($res['ori_name'])) || $res['name'] != $res['ori_name'] && ($data = $Geocoder->geocode($res['name']))) {
             //echo returns($data); echo returns($Geocoder->debug()); die();
             $this->id = $id;
             $this->save($data, true, ['lat', 'lng']);
             return true;
         }
     } else {
         $results = $this->find('all', ['fields' => [$this->alias() . '.id', $this->alias() . '.name', $this->alias() . '.ori_name'], 'conditions' => $conditions, 'contain' => []]);
         $count = 0;
         foreach ($results as $res) {
             if (!empty($res['ori_name']) && ($data = $Geocoder->geocode($res['ori_name'])) || $res['name'] != $res['ori_name'] && ($data = $Geocoder->geocode($res['name']))) {
                 //echo returns($data); echo returns($Geocoder->debug()); die();
                 $this->id = $res['id'];
                 if ($this->save($data, true, ['lat', 'lng'])) {
                     $count++;
                 } else {
                     //echo returns($data); echo returns($Geocoder->debug()); die();
                 }
                 $Geocoder->pause();
             }
         }
         return $count;
     }
     //$this->save($data, true, array('city','id','lat','lng'));
     return false;
 }
コード例 #2
0
 /**
  * Lat and lng + abbr if available!
  *
  * @param id
  * - NULL: update all records with missing coordinates only
  * - otherwise: specific update
  */
 public function updateCoordinates($id = null)
 {
     $geocoder = new GeocodeLib();
     $override = false;
     if ($id == -1) {
         $id = '';
         $override = true;
     }
     if (!empty($id)) {
         $res = $this->find('first', ['conditions' => [$this->alias() . '.id' => $id], 'contain' => ['Country.name']]);
         if (!empty($res['name']) && !empty($res[$this->Country->alias]['name']) && $geocoder->geocode($res['name'] . ', ' . $res[$this->Country->alias]['name'])) {
             $data = $geocoder->getResult();
             //pr($data); die();
             $saveArray = ['id' => $id, 'lat' => $data['lat'], 'lng' => $data['lng'], 'country_id' => $res['country_id']];
             if (!empty($data['country_province_code']) && mb_strlen($data['country_province_code']) <= 3 && preg_match('/^([A-Z])*$/', $data['country_province_code'])) {
                 $saveArray['abbr'] = $data['country_province_code'];
             }
             $this->id = $id;
             if (!$this->save($saveArray, true, ['id', 'lat', 'lng', 'abbr', 'country_id'])) {
                 if ($data['country_province_code'] !== 'DC') {
                     /*
                     echo returns($this->id);
                     pr($res);
                     pr($data);
                     pr($saveArray);
                     die(returns($this->validationErrors));
                     */
                     throw new \Exception('Update Error');
                 }
             }
             return true;
         }
     } else {
         $conditions = [];
         if (!$override) {
             $conditions = [$this->alias() . '.lat' => 0, $this->alias() . '.lng' => 0];
         }
         $results = $this->find('all', ['conditions' => $conditions, 'contain' => ['Country.name'], 'order' => ['CountryProvince.modified' => 'ASC']]);
         $count = 0;
         foreach ($results as $res) {
             if (!empty($res['name']) && !empty($res[$this->Country->alias]['name']) && $geocoder->geocode($res['name'] . ', ' . $res[$this->Country->alias]['name'])) {
                 $data = $geocoder->getResult();
                 //pr($data); die();
                 //pr ($geocoder->debug());
                 $saveArray = ['id' => $res['id'], 'country_id' => $res['country_id']];
                 if (isset($data['lat']) && isset($data['lng'])) {
                     $saveArray = array_merge($saveArray, ['lat' => $data['lat'], 'lng' => $data['lng']]);
                 }
                 if (!empty($data['country_province_code']) && mb_strlen($data['country_province_code']) <= 3 && preg_match('/^([A-Z])*$/', $data['country_province_code'])) {
                     $saveArray['abbr'] = $data['country_province_code'];
                 }
                 $this->id = $res['id'];
                 if ($this->save($saveArray, true, ['lat', 'lng', 'abbr', 'country_id'])) {
                     $count++;
                     if (!empty($saveArray['abbr']) && $saveArray['abbr'] != $res['abbr']) {
                         $this->log('Abbr for country province \'' . $data['country_province'] . '\' changed from \'' . $res['abbr'] . '\' to \'' . $saveArray['abbr'] . '\'', LOG_NOTICE);
                     }
                 } else {
                     //pr($data); pr($geocoder->debug()); die();
                     if ($data['country_province_code'] !== 'DC') {
                         echo returns($this->id);
                         pr($res);
                         pr($data);
                         pr($saveArray);
                         die(returns($this->validationErrors));
                     }
                 }
                 $geocoder->pause();
             }
         }
         return $count;
     }
     return false;
 }