예제 #1
0
 public function action_get()
 {
     $get = Validate::factory($_GET);
     $get->rule('latitude', 'numeric')->rule('longitude', 'numeric')->rule('placename', 'max_length', array(128))->rule('placename', 'not_empty')->rule('supplychain_id', 'numeric')->rule('projection', 'regex', array('/epsg:[\\w\\d]+/i'))->filter(true, 'trim');
     if ($get->check()) {
         $get = $get->as_array();
         $proj = 'EPSG:4326';
         // wgs84, by default
         if (isset($_GET['projection'])) {
             $proj = $get['projection'];
         }
         if (isset($_GET['latitude'], $_GET['longitude'])) {
             $pt = new Sourcemap_Proj_Point($get['latitude'], $get['longitude']);
         } elseif (isset($_GET['placename'])) {
             $results = Sourcemap_Geocoder::geocode($get['placename']);
             if ($results) {
                 $r = $results[0];
                 $pt = new Sourcemap_Proj_Point($r->longitude, $r->latitude);
             } else {
                 return $this->_internal_server_error('Could not geocode placename.');
             }
         } else {
             return $this->_bad_request('Coordinates or placename required.');
         }
         $pt = Sourcemap_Proj::transform($proj, 'EPSG:900913', $pt);
     } else {
         return $this->_bad_request('Invalid parameters.');
     }
     $this->response = ORM::factory('stop')->nearby($pt);
 }
예제 #2
0
 public function action_get()
 {
     if (isset($_GET['placename']) && !empty($_GET['placename'])) {
         $q = $_GET['placename'];
     } elseif (isset($_GET['ll'])) {
         // note: parameter 'll' is lat/lng, as the WGS84 datum specifies.
         // should we do something to avoid confusion here? (e.g. lat=0&lng=0)?
         if (!preg_match('/((-|\\+)?[\\d]{1,3}(\\.\\d+)?,?){2}/', $_GET['ll'])) {
             return $this->_bad_request('Invalid lat/lng.');
         }
         list($lat, $lng) = explode(',', $_GET['ll']);
         $q = new Sourcemap_Proj_Point($lng, $lat);
     } else {
         return $this->_bad_request('Placename or ll required.');
     }
     $this->response = (object) array('results' => Sourcemap_Geocoder::geocode($q));
 }
예제 #3
0
 public static function csv2stops($csv, $o = array())
 {
     $options = array();
     foreach (self::$default_options as $k => $v) {
         $options[$k] = isset($o[$k]) ? $o[$k] : $v;
     }
     extract($options);
     $csv = Sourcemap_Csv::parse($csv);
     $data = array();
     $raw_headers = array();
     if ($headers) {
         $raw_headers = array_shift($csv);
         $headers = array();
     }
     for ($i = 0; $i < count($raw_headers); $i++) {
         if (strlen(trim($raw_headers[$i]))) {
             $headers[] = strtolower($raw_headers[$i]);
         }
     }
     foreach ($csv as $ri => $row) {
         if ($headers && is_array($headers)) {
             $record = array();
             foreach ($headers as $hi => $k) {
                 if (isset($row[$hi])) {
                     $record[$k] = $row[$hi];
                 }
             }
         } else {
             $record = $row;
         }
         if ($record) {
             $data[] = $record;
         }
     }
     if ($headers) {
         if (is_null($latcol) || is_null($loncol)) {
             foreach ($headers as $i => $h) {
                 if (is_null($latcol) && preg_match('/^lat(itude)?$/i', $h)) {
                     $latcol = $h;
                 } elseif (is_null($loncol) && preg_match('/^(lng)|(lon(g(itude)?)?)$/i', $h)) {
                     $loncol = $h;
                 } elseif (is_null($addresscol) && preg_match('/place ?name/i', $h) || preg_match('/address/i', $h)) {
                     $addresscol = $h;
                 }
             }
             if (is_null($latcol) || is_null($loncol)) {
                 $latcol = $loncol = null;
                 if (is_null($addresscol)) {
                     return $this->_bad_request('Missing lat/lon or address column index.');
                 }
             }
         }
         if (is_null($idcol)) {
             foreach ($headers as $i => $h) {
                 if (preg_match('/^id$/i', $h)) {
                     $idcol = $h;
                     break;
                 }
             }
         }
     }
     $stops = array();
     foreach ($data as $i => $record) {
         if (is_null($addresscol)) {
             if (!isset($record[$latcol], $record[$loncol])) {
                 throw new Exception('Missing lat/lon field (record #' . ($i + 1) . ').');
             }
         } else {
             if (!isset($record[$addresscol])) {
                 throw new Exception('Missing address field (record #' . ($i + 1) . ').');
             }
         }
         if ($idcol && !isset($record[$idcol])) {
             throw new Exception('Missing id field (record #' . ($i + 1) . ').');
         } elseif ($idcol && !is_numeric($record[$idcol])) {
             throw new Exception('Id value must be an integer.');
         }
         $new_stop = array('local_stop_id' => $idcol ? (int) $record[$idcol] : $i + 1, 'attributes' => array());
         $lat = null;
         $lon = null;
         foreach ($record as $k => $v) {
             if ($k == $latcol || $k == $loncol) {
                 if ($k == $latcol) {
                     $lat = $v;
                 } else {
                     $lon = $v;
                 }
                 continue;
             } elseif ($k == $addresscol) {
                 if ($results = Sourcemap_Geocoder::geocode($v)) {
                     $result = $results[0];
                     $lat = (double) $result->lat;
                     $lon = (double) $result->lng;
                     if (!isset($record['placename'])) {
                         $new_stop['attributes']['placename'] = $result->placename;
                     }
                 } else {
                     throw new Exception('Could not geocode: "' . $v . '".');
                 }
             }
             $new_stop['attributes'][$k] = $v;
         }
         if (!isset($new_stop['attributes']['placename']) && $lat && $lon) {
             $results = Sourcemap_Geocoder::geocode(new Sourcemap_Proj_Point($lon, $lat));
             if ($results) {
                 $result = $results[0];
                 //$lat = $result->lat;
                 //$lon = $result->lng;
                 if (!isset($record['placename'])) {
                     $new_stop['attributes']['placename'] = $result->placename;
                 }
             }
         }
         if (is_null($lon) || is_null($lat)) {
             throw new Exception('No lat/lon.');
         }
         $from_pt = new Sourcemap_Proj_Point($lon, $lat);
         $new_stop['geometry'] = Sourcemap_Proj::transform('WGS84', 'EPSG:900913', $from_pt)->toGeometry();
         $stops[] = (object) $new_stop;
     }
     return $stops;
 }