function createGeoJSON($layer_tablename,$column_names,$file='',$Fixed_Columns='',$projection='900913'){ require_once 'json/include_geom.php'; require_once 'json/geojson.php'; require_once 'json/wkt.php'; if ($Fixed_Columns == '') { $Fixed_Columns = AUTO_DBCOL_PREFIX."id, astext(ST_Transform(".AUTO_DBCOL_PREFIX."topology,". $projection .")) as topology, "; } $total_column_names = $Fixed_Columns . $column_names; $sql = 'select '.$total_column_names.' from "'. $layer_tablename . '"'; $query_args = array($total_column_names, $layer_tablename); $strgeoJSON='{"type": "FeatureCollection", "features": ['; $str=''; $arr = explode(",",$column_names); $cnt = count($arr); $data = db_query($sql); while($layer_obj = db_fetch_object($data)){ $geom_wkt = $layer_obj->topology; $wkt = new WKT(); $geom = $wkt->read($geom_wkt); $strgeoJSON .= '{"geometry": ' . json_encode($geom->getGeoInterface()) . ', '; $strgeoJSON .= '"type": "Feature", '; $strgeoJSON .= '"id": '.$layer_obj->{AUTO_DBCOL_PREFIX."id"}.', '; $strgeoJSON .= '"properties": {'; for($i=0;$i<$cnt;$i++){ $strgeoJSON .= '"'. trim($arr[$i]) .'": "' . $layer_obj->$arr[$i] .'",'; } $strgeoJSON = substr_replace($strgeoJSON,"",-1); $strgeoJSON .= '}},'; } $strgeoJSON = substr_replace($strgeoJSON,"",-1); $strgeoJSON .= ']}'; if ($file != '') { $myFile = $file; }else{ $myFile = "json/".$layer_tablename .".json"; } $fh = fopen($myFile, 'w') or die("can't open file"); fwrite($fh, $strgeoJSON); fclose($fh); return $myFile; }
public function testCollapsePoints() { //simple array $test = array(1, 2); WKT::collapse_points($test, 0); $this->assertEquals("2,1", $test); //multidimensional array $test = array(array(1, 3), array(2, 4)); WKT::collapse_points($test, 0); $this->assertEquals(array("3,1", "4,2"), $test); }
/** * Read WKT string into geometry objects. * * @param string $text A WKT string. * * @return Geometry|GeometryCollection. */ function treasurehunt_wkt_to_object($text) { $wkt = new WKT(); return $wkt->read($text); }
//$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; } } $qval .= '&sensor=false" />'; } else { // If it's not a location, break the array into a string if (is_array($qval)) { $qval = implode(', ', $qval); } } $qualifier_string .= '<strong>' . $qkey . '</strong>: ' . $qval . '<br/>'; } $response_string = ''; foreach ($response_vars as $rkey => $rval) {
/** * returns a GeoJSON instance build from $object through $adapter * * @param mixed $object The data to load * @param GeoJSON_Adapter The adapter through which data will be extracted * * @return GeoJSON A GeoJSON instance */ static protected function loadFeatureFrom($object, GeoJSON_Adapter $adapter) { $geometry = WKT::load($adapter->getObjectGeometry($object)); $feature = new Feature( $adapter->getObjectId($object), $geometry, $adapter->getObjectProperties($object) ); return $feature; }
/** * Logs a user's location. * * @param WP $wp Wordpress object. * @return boolean True if the request was handled. */ private static function location($wp) { if (isset($wp->query_vars['lat']) && isset($wp->query_vars['lon']) && isset($wp->query_vars['acc']) && isset($wp->query_vars['user-id']) && isset($wp->query_vars['collection-id'])) { $collection = get_post($wp->query_vars['collection-id']); if (!$collection) { return false; } $user = XMapsUser::get_user_by_api_key($wp->query_vars['key']); if ($user->id != $wp->query_vars['user-id']) { return false; } $geom = new Point($wp->query_vars['lon'], $wp->query_vars['lat']); $geom->setSRID(XMAPS_SRID); $wkt = new WKT(); $location = $wkt->write($geom); XMapsDatabase::log_location($user->id, $wp->query_vars['key'], $wp->query_vars['collection-id'], $location, floatval($wp->query_vars['acc'])); return true; } return false; }
/** * States if a geometry is valid or not an if not valid, a reason why * Available since PostGIS 1.4 (more dependenices??) * * @param string $geoJSON JSON object with geometry * @return string the reason */ public function ST_IsValidReason($geoJSON) { $registry = Zend_Registry::getInstance(); $db = $registry->get('db'); $autoloader = Zend_Loader_Autoloader::getInstance(); $autoloader->pushAutoloader(array('GeoJSON', 'autoload')); try { $geometry = GeoJSON::load($geoJSON); $geoWKT = WKT::dump($geometry); } catch (Exception $e) { return $e->getMessage(); } $result = $db->fetchOne("SELECT ST_IsValidReason( :geoWKT ) ;", array('geoWKT' => $geoWKT)); return (string)$result; }