// Build geojson $wkts = $this->DB->selectAssocs("SELECT mode, AsWKT(spat) AS wkt FROM pl_gminy_spats WHERE gmina_id = {$id}"); if ($wkts[0]['mode'] != 'ADD') { throw new Exception("First geom should be the base with the mode = ADD"); } $geom = geoPHP::load($wkts[0]['wkt'], 'wkt'); for ($i = 1; $i < count($wkts); $i++) { if ($wkts[$i]['mode'] == 'ADD') { $geom = $geom->union(geoPHP::load($wkts[$i]['wkt'], 'wkt')); } else { if ($wkts[$i]['mode'] == 'DIFF') { $geom = $geom->difference(geoPHP::load($wkts[$i]['wkt'], 'wkt')); } else { throw new Exception("Unrecognized mode = " . $wkts[$i]['mode']); } } } if ($simplify) { // w tej postaci bedzie to nierównomiernie robił w pioniie i poziomie ze względu na CRS (ale roznica wymiarow tylko 2x) $geom = $geom->simplify(0.006, true); } $geojsonConverter = new GeoJSON(); $geojson = $geojsonConverter->write($geom, true); MpUtils::transposeCoordinates($geojson); // Put in cache $cacheClient->set($cacheKey, json_encode($geojson)); } $data =& $this->data['data']; $feat = array("type" => "Feature", "id" => $this->data['_id'], "properties" => array('gminy.nazwa' => $data['gminy.nazwa'], 'gminy.teryt' => $data['gminy.teryt'], 'gminy.typ_nazwa' => $data['gminy.typ_nazwa'], 'powiaty.id' => $data['powiaty.id'], 'powiaty.nazwa' => $data['powiaty.nazwa'], 'wojewodztwa.id' => $data['wojewodztwa.id'], 'wojewodztwa.nazwa' => $data['wojewodztwa.nazwa']), "geometry" => $geojson); MpUtils::geoStampCRS($feat); return $feat;
function getData($simplify = true) { App::import('model', 'DB'); // App::import('Vendor', 'geoPHP', array('file' => '/phayes/geophp/geoPHP.inc')); $this->DB = new DB(); $data = $this->DB->selectAssocs("SELECT `sid`, GROUP_CONCAT(AsText(polygon_4) SEPARATOR \"\n\") as 'wkts' FROM `pl_gminy_obszary` GROUP BY `sid`"); $features = array(); foreach ($data as $d) { $wkts = explode("\n", $d['wkts']); $geom = geoPHP::load($wkts[0], 'wkt'); for ($i = 1; $i < count($wkts); $i++) { $geom = $geom->union(geoPHP::load($wkts[$i], 'wkt')); } if ($simplify) { // w tej postaci bedzie to nierównomiernie robił w pioniie i poziomie ze względu na CRS (ale roznica wymiarow tylko 2x) $geom = $geom->simplify(0.006, true); } $geojsonConverter = new GeoJSON(); $geojson = $geojsonConverter->write($geom, true); /// debug( $geojson ); die(); // MpUtils::transposeCoordinates($geojson); // debug( $geojson ); die(); $features[] = array("type" => "Feature", "id" => $d['sid'], "properties" => array(), "geometry" => $geojson); } $featc = array("type" => "FeatureCollection", "features" => $features); MpUtils::geoStampCRS($featc); return $featc; }
/** * Zwraca obiekt GeoJson Feature zawierający obszar województwa (cache w redis) wraz z dynamicznie dociąganymi właściwościami */ App::import('Vendor', 'geoPHP', array('file' => '/phayes/geophp/geoPHP.inc')); App::import('model', 'MPCache'); // Try cache $cacheKey = 'geojson/wojewodztwo/' . $id . ($simplify ? 's' : ''); $cache = new MPCache(); $cacheClient = $cache->getDataSource()->getRedisClient(); if ($cacheClient->exists($cacheKey)) { $geojson = json_decode($cache->get($cacheKey)); } else { // Build geojson $wkt = $this->DB->selectAssoc("SELECT AsWKT(spat) AS wkt FROM wojewodztwa WHERE id = {$id}"); if (!$wkt['wkt']) { return null; } $spat = geoPHP::load($wkt['wkt'], 'wkt'); if ($simplify) { // w tej postaci bedzie to nierównomiernie robił w pioniie i poziomie ze względu na CRS (ale roznica wymiarow tylko 2x) $spat = $spat->simplify(0.005, true); } $geojsonConverter = new GeoJSON(); $geojson = $geojsonConverter->write($spat, true); MpUtils::transposeCoordinates($geojson); // Put in cache $cacheClient->set($cacheKey, json_encode($geojson)); } $feat = array("type" => "Feature", "id" => $this->data['_id'], "properties" => $this->data['data'], "geometry" => $geojson); MpUtils::geoStampCRS($feat); return $feat;
/** * Deserialize a JSON array in a compatible object with the library GeoJSON. * * @param string $array The GeoJSON string. * * @return Feature|FeatureCollection The PHP equivalent object. */ function treasurehunt_geojson_to_object($array) { $geojson = new GeoJSON(); return $geojson->loadArray($array); }
public function getArray($feature) { if ($feature instanceof AbstractFeature) { if ($feature instanceof FeatureCollection) { $feature_array = array(); foreach ($feature->getFeatures() as $f) { $feature_array[] = $this->getArray($f); } return array('type' => 'FeatureCollection', 'id' => $feature->getId(), 'properties' => (array) $feature->getProperties(), 'features' => $feature_array); } elseif ($feature instanceof Feature) { return array('type' => 'Feature', 'id' => $feature->getId(), 'properties' => (array) $feature->getProperties(), 'geometry' => $this->getArray($feature->getGeometry())); } } else { $adapter = new GeoJSON(); return $adapter->write($feature, true); } }
/** * 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; }