/** * Convert the geometry to geohash. * @return string the geohash or null when the $geometry is not a Point * @param Point $geometry * @see GeoAdapter::write() */ public function write(Geometry $geometry, $precision = NULL) { if ($geometry->isEmpty()) { return ''; } if ($geometry->geometryType() === 'Point') { return $this->encodePoint($geometry, $precision); } else { // The geohash is the hash grid ID that fits the envelope $envelope = $geometry->envelope(); $geohashes = array(); $geohash = ''; foreach ($envelope->getPoints() as $point) { $geohashes[] = $this->encodePoint($point, 1.0E-7); } $i = 0; while ($i < strlen($geohashes[0])) { $char = $geohashes[0][$i]; foreach ($geohashes as $hash) { if ($hash[$i] != $char) { return $geohash; } } $geohash .= $char; $i++; } return $geohash; } }
/** * Extract geometry to a WKT string * * @param Geometry $geometry A Geometry object * * @return string */ public function extractData($geometry) { $parts = array(); switch ($geometry->geometryType()) { case 'Point': return $geometry->getX() . ' ' . $geometry->getY(); case 'LineString': foreach ($geometry->getComponents() as $component) { $parts[] = $this->extractData($component); } return implode(', ', $parts); case 'Polygon': case 'MultiPoint': case 'MultiLineString': case 'MultiPolygon': foreach ($geometry->getComponents() as $component) { $parts[] = '(' . $this->extractData($component) . ')'; } return implode(', ', $parts); case 'GeometryCollection': foreach ($geometry->getComponents() as $component) { $parts[] = strtoupper($component->geometryType()) . ' (' . $this->extractData($component) . ')'; } return implode(', ', $parts); } }