/** * Get Geometry JSON * @param int $incident_id * @param string $incident_title * @param int $incident_date * @return array $geometry */ private static function _get_geometry($incident_id, $incident_title, $incident_date, $on_the_back_end, $color, $link_target = "_self") { $geometry = array(); if ($incident_id) { $db = new Database(); // Get Incident Geometries via SQL query as ORM can't handle Spatial Data $sql = "SELECT id, AsText(geometry) as geometry, geometry_label, \n\t\t\t\tgeometry_comment, geometry_color, geometry_strokewidth FROM " . self::$table_prefix . "geometry \n\t\t\t\tWHERE incident_id=" . $incident_id; $query = $db->query($sql); $wkt = new Wkt(); foreach ($query as $item) { $geom = $wkt->read($item->geometry); $geom_array = $geom->getGeoInterface(); $json_item = "{"; $json_item .= "\"type\":\"Feature\","; $json_item .= "\"properties\": {"; $json_item .= "\"id\": \"" . $incident_id . "\", "; $json_item .= "\"feature_id\": \"" . $item->id . "\", "; $title = $item->geometry_label ? utf8tohtml::convert($item->geometry_label, TRUE) : utf8tohtml::convert($incident_title, TRUE); $fillcolor = $item->geometry_color ? utf8tohtml::convert($item->geometry_color, TRUE) : $color; $strokecolor = $item->geometry_color ? utf8tohtml::convert($item->geometry_color, TRUE) : $color; $strokewidth = $item->geometry_strokewidth ? $item->geometry_strokewidth : "3"; if ($on_the_back_end) { $json_item .= "\"name\":\"" . str_replace(chr(10), ' ', str_replace(chr(13), ' ', "<a href='" . url::base() . "admin/reports/edit/" . $incident_id . "'>" . $title . "</a>")) . "\","; } else { $json_item .= "\"name\":\"" . str_replace(chr(10), ' ', str_replace(chr(13), ' ', "<a target='" . $link_target . "' href='" . url::base() . "reports/view/" . $incident_id . "'>" . $title . "</a>")) . "\","; } $json_item .= "\"description\": \"" . utf8tohtml::convert($item->geometry_comment, TRUE) . "\", "; $json_item .= "\"color\": \"" . $fillcolor . "\", "; $json_item .= "\"strokecolor\": \"" . $strokecolor . "\", "; $json_item .= "\"strokewidth\": \"" . $strokewidth . "\", "; $json_item .= "\"link\": \"" . url::base() . "reports/view/" . $incident_id . "\", "; $json_item .= "\"category\":[0], "; $json_item .= "\"timestamp\": \"" . strtotime($incident_date) . "\""; $json_item .= "},\"geometry\":" . json_encode($geom_array) . "}"; $geometry[] = $json_item; } } return $geometry; }
/** * Get Geometry JSON * @param int $incident_id * @param string $incident_title * @param int $incident_date * @param string $incident_link * @return array $geometry */ protected function get_geometry($incident_id, $incident_title, $incident_date, $incident_link) { $geometry = array(); if ($incident_id) { $geom_data = $this->_get_geometry_data_for_incident($incident_id); $wkt = new Wkt(); foreach ($geom_data as $item) { $geom = $wkt->read($item->geometry); $geom_array = $geom->getGeoInterface(); $title = $item->geometry_label ? $item->geometry_label : $incident_title; $item_name = $this->get_title($title, $incident_link); $fillcolor = $item->geometry_color ? $item->geometry_color : "ffcc66"; $strokecolor = $item->geometry_color ? $item->geometry_color : "CC0000"; $strokewidth = $item->geometry_strokewidth ? $item->geometry_strokewidth : "3"; $json_item = array(); $json_item['type'] = 'Feature'; $json_item['properties'] = array('id' => $incident_id, 'feature_id' => $item->id, 'name' => $item_name, 'description' => $item->geometry_comment, 'color' => $fillcolor, 'icon' => '', 'strokecolor' => $strokecolor, 'strokewidth' => $strokewidth, 'link' => $incident_link, 'category' => array(0), 'timestamp' => strtotime($incident_date)); $json_item['geometry'] = $geom_array; $geometry[] = $json_item; } } return $geometry; }
/** * Get Geometry JSON * @param int $incident_id * @param string $incident_title * @param int $incident_date * @return array $geometry */ private function _get_geometry($incident_id, $incident_title, $incident_date) { $geometry = array(); if ($incident_id) { $geom_data = $this->_get_geometry_data_for_incident($incident_id); $wkt = new Wkt(); foreach ($geom_data as $item) { $geom = $wkt->read($item->geometry); $geom_array = $geom->getGeoInterface(); $json_item = "{"; $json_item .= "\"type\":\"Feature\","; $json_item .= "\"properties\": {"; $json_item .= "\"id\": \"" . $incident_id . "\", "; $json_item .= "\"feature_id\": \"" . $item->id . "\", "; $title = $item->geometry_label ? utf8tohtml::convert($item->geometry_label, TRUE) : utf8tohtml::convert($incident_title, TRUE); $fillcolor = $item->geometry_color ? utf8tohtml::convert($item->geometry_color, TRUE) : "ffcc66"; $strokecolor = $item->geometry_color ? utf8tohtml::convert($item->geometry_color, TRUE) : "CC0000"; $strokewidth = $item->geometry_strokewidth ? $item->geometry_strokewidth : "3"; $json_item .= "\"name\":\"" . str_replace(chr(10), ' ', str_replace(chr(13), ' ', "<a href='" . url::base() . "reports/view/" . $incident_id . "'>" . $title . "</a>")) . "\","; $json_item .= "\"description\": \"" . utf8tohtml::convert($item->geometry_comment, TRUE) . "\", "; $json_item .= "\"color\": \"" . $fillcolor . "\", "; $json_item .= "\"strokecolor\": \"" . $strokecolor . "\", "; $json_item .= "\"strokewidth\": \"" . $strokewidth . "\", "; $json_item .= "\"link\": \"" . url::base() . "reports/view/" . $incident_id . "\", "; $json_item .= "\"category\":[0], "; $json_item .= "\"timestamp\": \"" . strtotime($incident_date) . "\""; $json_item .= "},\"geometry\":" . json_encode($geom_array) . "}"; $geometry[] = $json_item; } } return $geometry; }
} // If there's nothing there, don't show it if ($qval === '' or $qval === 0 or $qval === '0') { continue; } // If it's really long and not an exempted key, chop off the end if (is_string($qval) and strlen($qval) > 150 and $qkey != 'location' and $qkey != 'days_of_the_week') { $qval = substr($qval, 0, 150) . '…'; } // If it's a specific location, show the polygon on a static map if ($qkey == 'location' and $qval == 'specific') { // TODO: Find some more intuitive way to illustrate where this is. //$qval = print_r($qualifiers['geometry'],true);; $qval = 'Geofenced<br/>'; $qval .= '<img src="' . Kohana::config('core.site_protocol') . '://maps.googleapis.com/maps/api/staticmap?size=275x200'; $wkt = new Wkt(); foreach ($qualifiers['geometry'] as $geom_key => $geom) { $geom = json_decode($geom); // Decode in qualifiers array too, so it gets passed to edit as an array $qualifiers['geometry'][$geom_key] = $geom; // Decode polygon with WKT $polygon = $wkt->read($geom->geometry); $coordinates = $polygon->getCoordinates(); WKT::collapse_points($coordinates, 0); // for polygons if (is_array($coordinates)) { $qval .= "&path=color:0xff0000ff|weight:2|fillcolor:0xFFFF0033|"; $qval .= implode('|', WKT::flatten($coordinates)); } else { $qval .= '&markers=' . $coordinates; }
/** * Retrieve Single Marker */ public function single($incident_id = 0) { $json = ""; $json_item = ""; $json_array = array(); $marker = ORM::factory('incident') ->where('id', $incident_id) ->find(); if ($marker->loaded) { /* First We'll get all neighboring reports */ $incident_date = date('Y-m', strtotime($marker->incident_date)); $latitude = $marker->location->latitude; $longitude = $marker->location->longitude; $filter = ""; // Uncomment this to display markers from this month alone // $filter .= " AND i.incident_date LIKE '$incident_date%' "; $filter .= " AND i.id <>".$marker->id; // Database $db = new Database(); // Get Neighboring Markers Within 50 Kms (31 Miles) $query = $db->query("SELECT DISTINCT i.*, l.`latitude`, l.`longitude`, ((ACOS(SIN($latitude * PI() / 180) * SIN(l.`latitude` * PI() / 180) + COS($latitude * PI() / 180) * COS(l.`latitude` * PI() / 180) * COS(($longitude - l.`longitude`) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS distance FROM `".$this->table_prefix."incident` AS i INNER JOIN `".$this->table_prefix."location` AS l ON (l.`id` = i.`location_id`) INNER JOIN `".$this->table_prefix."incident_category` AS ic ON (i.`id` = ic.`incident_id`) INNER JOIN `".$this->table_prefix."category` AS c ON (ic.`category_id` = c.`id`) WHERE i.incident_active=1 $filter HAVING distance<='20' ORDER BY i.`incident_date` DESC LIMIT 100 "); foreach ($query as $row) { $json_item = "{"; $json_item .= "\"type\":\"Feature\","; $json_item .= "\"properties\": {"; $json_item .= "\"id\": \"".$row->id."\", "; $encoded_title = utf8tohtml::convert($row->incident_title,TRUE); $encoded_title = str_ireplace('"','"',$encoded_title); $encoded_title = json_encode($encoded_title); $encoded_title = str_ireplace('"','',$encoded_title); $json_item .= "\"name\":\"" . str_replace(chr(10), ' ', str_replace(chr(13), ' ', "<a href='" . url::base() . "reports/view/" . $row->id . "'>".$encoded_title."</a>")) . "\","; $json_item .= "\"link\": \"".url::base()."reports/view/".$row->id."\", "; $json_item .= "\"category\":[0], "; $json_item .= "\"timestamp\": \"" . strtotime($row->incident_date) . "\""; $json_item .= "},"; $json_item .= "\"geometry\": {"; $json_item .= "\"type\":\"Point\", "; $json_item .= "\"coordinates\":[" . $row->longitude . ", " . $row->latitude . "]"; $json_item .= "}"; $json_item .= "}"; array_push($json_array, $json_item); } // Single Main Incident $json_single = "{"; $json_single .= "\"type\":\"Feature\","; $json_single .= "\"properties\": {"; $json_single .= "\"id\": \"".$marker->id."\", "; $encoded_title = utf8tohtml::convert($marker->incident_title,TRUE); $json_single .= "\"name\":\"" . str_replace(chr(10), ' ', str_replace(chr(13), ' ', "<a href='" . url::base() . "reports/view/" . $marker->id . "'>".$encoded_title."</a>")) . "\","; $json_single .= "\"link\": \"".url::base()."reports/view/".$marker->id."\", "; $json_single .= "\"category\":[0], "; $json_single .= "\"timestamp\": \"" . strtotime($marker->incident_date) . "\""; $json_single .= "},"; $json_single .= "\"geometry\":"; // Get Incident Geometries via SQL query as ORM can't handle Spatial Data $sql = "SELECT AsText(geometry) as geometry FROM ".$this->table_prefix."geometry WHERE incident_id=".$marker->id; $query = $db->query($sql); $geometry = array(); $wkt = new Wkt(); foreach ( $query as $item ) { $geom = $wkt->read($item->geometry); $geom_array = $geom->getGeoInterface(); $geometry[] = $json_single.json_encode($geom_array)."}"; } // If there are no geometries, use Single Incident Marker if ( ! count($geometry)) { $json_item = $json_single; $json_item .= "{\"type\":\"Point\", "; $json_item .= "\"coordinates\":[" . $marker->location->longitude . ", " . $marker->location->latitude . "]"; $json_item .= "}"; $json_item .= "}"; } else { $json_item = implode(",", $geometry); } array_push($json_array, $json_item); } $json = implode(",", $json_array); header('Content-type: application/json; charset=utf-8'); $this->template->json = $json; }