예제 #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()
 {
     header('Content-Type: text/plain');
     #print_r(new Sourcemap_Proj_Projection('EPSG:900913'));
     #print_r(new Sourcemap_Proj_Projection('EPSG:4326'));
     #print_r(Sourcemap_Proj_Point::parse_coords('x=1,y=2'));
     $x = isset($_GET['x']) ? $_GET['x'] : null;
     $y = isset($_GET['y']) ? $_GET['y'] : null;
     $src = isset($_GET['src']) ? $_GET['src'] : 'WGS84';
     $dest = isset($_GET['dest']) ? $_GET['dest'] : 'WGS84';
     if ($x == null || $y == null) {
         return $this->_bad_request('Invalid x/y combination.');
     }
     #$this->response = Sourcemap_Proj::transform('WGS84', 'EPSG:900913', new Sourcemap_Proj_Point(-76.640625, 49.921875));
     $this->response = Sourcemap_Proj::transform($src, $dest, new Sourcemap_Proj_Point($x, $y));
 }
예제 #3
0
 public static function get_sc_bbox($raw_sc)
 {
     $min_lat = $max_lat = $min_lon = $max_lon = null;
     foreach ($raw_sc->stops as $i => $stop) {
         if ($pt = Sourcemap_Proj_Point::fromGeometry($stop->geometry)) {
             $pt = Sourcemap_Proj::transform('EPSG:900913', 'WGS84', $pt);
             if ($min_lat === null || $pt->y < $min_lat) {
                 $min_lat = $pt->y;
             }
             if ($max_lat === null || $pt->y > $max_lat) {
                 $max_lat = $pt->y;
             }
             if ($min_lon === null || $pt->x < $min_lon) {
                 $min_lon = $pt->x;
             }
             if ($max_lon === null || $pt->x > $max_lon) {
                 $max_lon = $pt->x;
             }
         }
     }
     $bbox = array($min_lat, $min_lon, $max_lat, $max_lon);
     return $bbox;
 }
예제 #4
0
 public static function make($supplychain)
 {
     $geojson = array("type" => "FeatureCollection", "features" => array());
     foreach ($supplychain->attributes as $k => $v) {
         $geojson["properties"][$k] = $v;
     }
     foreach (array_merge($supplychain->stops, $supplychain->hops) as $item) {
         $geom = Sourcemap_Wkt::read($item->geometry);
         switch ($geom[0]) {
             case "point":
                 $pt = new Sourcemap_Proj_Point($geom[1][0], $geom[1][1]);
                 $pt = Sourcemap_Proj::transform('EPSG:900913 ', 'WGS84', $pt);
                 $geometry = array("type" => "Point", "coordinates" => array($pt->x, $pt->y));
                 break;
             case "multilinestring":
                 $return = array();
                 array_walk_recursive($geom[1], function ($a) use(&$return) {
                     $return[] = $a;
                 });
                 $pt1 = new Sourcemap_Proj_Point($return[2], $return[3]);
                 $pt2 = new Sourcemap_Proj_Point($return[5], $return[6]);
                 $pt1 = Sourcemap_Proj::transform('EPSG:900913 ', 'WGS84', $pt1);
                 $pt2 = Sourcemap_Proj::transform('EPSG:900913 ', 'WGS84', $pt2);
                 $geometry = array("type" => "LineString", "coordinates" => array(array($pt1->x, $pt1->y), array($pt2->x, $pt2->y)));
                 break;
             default:
                 break;
         }
         $props = array();
         foreach ($item->attributes as $k => $v) {
             $props[$k] = $v;
         }
         array_push($geojson["features"], array("type" => "Feature", "geometry" => $geometry, "properties" => $props));
     }
     return json_encode($geojson);
 }
예제 #5
0
 public static function get_image($raw_sc)
 {
     $params = array('size' => '1024x768', 'styleid' => 4993, 'format' => 'png32');
     $markers = array();
     $paths = array();
     $stop_dict = array();
     $min_lat = $max_lat = $min_lon = $max_lon = null;
     foreach ($raw_sc->stops as $i => $stop) {
         # TODO: address hard limit of 100 stops.
         if ($pt = Sourcemap_Proj_Point::fromGeometry($stop->geometry)) {
             $pt = Sourcemap_Proj::transform('EPSG:900913', 'WGS84', $pt);
             if ($min_lat === null || $pt->y < $min_lat) {
                 $min_lat = $pt->y;
             }
             if ($max_lat === null || $pt->y > $max_lat) {
                 $max_lat = $pt->y;
             }
             if ($min_lon === null || $pt->x < $min_lon) {
                 $min_lon = $pt->x;
             }
             if ($max_lon === null || $pt->x > $max_lon) {
                 $max_lon = $pt->x;
             }
             $color = '008000';
             if (isset($stop->attributes, $stop->attributes->color)) {
                 $stcolor = $stop->attributes->color;
                 if (preg_match('/^#?([A-Fa-f0-9]{3}|[A-Fa-f0-9]{6})$/', $stcolor)) {
                     $color = ltrim($stcolor, '#');
                 }
             }
             $sz = 16;
             if (isset($stop->attributes, $stop->attributes->size) && is_numeric($stop->attributes->size)) {
                 $sz = (int) $stop->attributes->size;
                 $sz += 16;
             }
             #$markerimg = "http://chart.apis.google.com/chart?cht=it&chs={$sz}x{$sz}&chco=$color&chx=ffffff,8&chf=bg,s,00000000&ext=.png";
             $markerimg = "http://chart.apis.google.com/chart?cht=it&chs={$sz}x{$sz}&chco={$color}&chx=ffffff,8&chf=bg,s,00000000&ext=.png";
             $markers[] = 'url:' . urlencode($markerimg) . '|opacity:0.85|' . $pt->y . ',' . $pt->x;
         }
         $stop_dict[$stop->local_stop_id] = $stop;
     }
     $bbox = array($min_lat, $min_lon, $max_lat, $max_lon);
     foreach ($raw_sc->hops as $i => $hop) {
         $geom = Sourcemap_Wkt::read($hop->geometry);
         $fromst = Sourcemap_Proj_Point::fromGeometry($stop_dict[$hop->from_stop_id]->geometry);
         $fromst = Sourcemap_Proj::transform('EPSG:900913', 'WGS84', $fromst);
         $tost = Sourcemap_Proj_Point::fromGeometry($stop_dict[$hop->to_stop_id]->geometry);
         $tost = Sourcemap_Proj::transform('EPSG:900913', 'WGS84', $tost);
         $bentpts = self::make_bent_line($fromst, $tost);
         $pts = array();
         foreach ($bentpts as $bpi => $bp) {
             $pts[] = sprintf("%f,%f", $bp->y, $bp->x);
         }
         $paths[] = 'color:green|weight:3|opacity:1|' . join('|', $pts);
     }
     $ps = array();
     foreach ($params as $k => $v) {
         $ps[] = "{$k}={$v}";
     }
     foreach ($markers as $i => $m) {
         $ps[] = 'marker=' . $m;
     }
     foreach ($paths as $i => $p) {
         $ps[] = 'path=' . $p;
     }
     if ($bbox[0] || $bbox[1] || $bbox[2]) {
         $ps[] = sprintf("bbox=%s", join(',', $bbox));
     } else {
         $ps[] = 'center=0,0';
         $ps[] = 'zoom=2';
     }
     $ps = join($ps, '&');
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, self::get_base_url());
     curl_setopt($ch, CURLOPT_POST, true);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $ps);
     return curl_exec($ch);
 }
예제 #6
0
function geo_proj_transform($pt, $from, $to)
{
    $src_pt = new Sourcemap_Proj_Point($pt['longitude'], $pt['latitude']);
    $new_pt = Sourcemap_Proj::transform($from, $to, $src_pt);
    return array('latitude' => $new_pt->y, 'longitude' => $new_pt->x);
}
예제 #7
0
 public function render()
 {
     $this->stitch_tiles();
     list($nwxt, $nwyt, $throwaway) = $this->tile_numbers[0][0];
     #$se = new Sourcemap_Proj_Point($this->tiles_bounds[3], $this->tiles_bounds[2]);
     #list($sext,$seyt) = Cloudmade_Tiles::get_tile_number($se->y, $se->x, $this->zoom);
     $stops = array();
     foreach ($this->raw_sc->stops as $stop) {
         $pt = Sourcemap_Proj_Point::fromGeometry($stop->geometry);
         $pt = Sourcemap_Proj::transform('EPSG:900913', 'WGS84', $pt);
         $lon = $pt->x;
         $lat = $pt->y;
         list($xt, $yt) = Cloudmade_Tiles::get_tile_number($lat, $lon, $this->zoom);
         list($xto, $yto) = Cloudmade_Tiles::get_tile_offset($lat, $lon, $this->zoom);
         $x = ($xt - $nwxt) * 256 + $xto;
         $y = ($yt - $nwyt) * 256 + $yto;
         $stops[$stop->local_stop_id] = (object) array('stop' => $stop, 'x' => $x, 'y' => $y);
     }
     foreach ($this->raw_sc->hops as $i => $hop) {
         $from = $stops[$hop->from_stop_id];
         $to = $stops[$hop->to_stop_id];
         $this->draw_hop2($hop, $from, $to);
         //$this->draw_hop($hop, $from, $to);
     }
     foreach ($stops as $sid => $st) {
         $this->draw_stop($st->stop, $st->x, $st->y);
     }
     /*ob_start();
       imagepng($this->tiles_img);
       $imgdata = ob_get_contents();
       ob_end_clean();
       return $imgdata;*/
     return $this->tiles_img;
 }
예제 #8
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;
 }