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); }
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); }
public static function csv2hops($csv, $stops, $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); $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 ($headers as $i => $h) { if (is_null($fromcol) && preg_match('/^from(_?stop)?$/i', $h)) { $fromcol = $h; } elseif (is_null($tocol) && preg_match('/^to(_?stop)?$/i', $h)) { $tocol = $h; } } } if (!$fromcol || !$tocol) { throw new Exception('To and from columns required.'); } $data = array(); 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; } } $stops_by_id = array(); foreach ($stops as $sti => $st) { $stops_by_id[(int) $st->local_stop_id] = $st; } $hops = array(); foreach ($data as $i => $record) { if (!isset($record[$fromcol]) || !is_numeric($record[$fromcol])) { throw new Exception('Missing or invalid from field at record #' . ($i + 1) . '.'); } if (!isset($record[$tocol]) || !is_numeric($record[$tocol])) { throw new Exception('Missing or invalid to field at record #' . ($i + 1) . '.'); } $from = $record[$fromcol]; $to = $record[$tocol]; if (!isset($stops_by_id[(int) $from])) { throw new Exception('From stop in hop does not exist in record #' . ($i + 1) . '.'); } if (!isset($stops_by_id[(int) $to])) { throw new Exception('To stop in hop does not exist in record #' . ($i + 1) . '.'); } list($type, $fromcoords) = Sourcemap_Wkt::read($stops_by_id[$from]->geometry); list($type, $tocoords) = Sourcemap_Wkt::read($stops_by_id[$to]->geometry); $frompt = new Sourcemap_Proj_Point($fromcoords); $topt = new Sourcemap_Proj_Point($tocoords); $geometry = Sourcemap_Wkt::write(Sourcemap_Wkt::MULTILINESTRING, array($frompt, $topt)); $new_hop = (object) array('from_stop_id' => $from, 'to_stop_id' => $to, 'geometry' => $geometry, 'attributes' => new stdClass()); foreach ($record as $k => $v) { if ($k !== $fromcol && $k !== $tocol) { $new_hop->attributes->{$k} = $v; } } $hops[] = $new_hop; } return $hops; }