/** * 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->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; } }
/** * Serialize geometries into a WKT string. * * @param Geometry $geometry * * @return string The WKT string representation of the input geometries */ public function write(Geometry $geometry) { // If geos is installed, then we take a shortcut and let it write the WKT if (geoPHP::geosInstalled()) { $writer = new GEOSWKTWriter(); return $writer->write($geometry->geos()); } $type = strtolower($geometry->geometryType()); if (is_null($data = $this->extract($geometry))) { return null; } return strtoupper($type) . ' (' . $data . ')'; }
/** * 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); } }
/** * Extract geometry to a WKT string * * @param Geometry $geometry A Geometry object * * @return string */ public function extractData($geometry) { $type = strtolower($geometry->geometryType()); switch ($type) { case "point": return $geometry->getX() . ' ' . $geometry->getY(); break; case "linestring": $parts = array(); foreach ($geometry->getComponents() as $component) { $parts[] = $this->extractData($component); } return implode(', ', $parts); break; case "polygon": case 'multipoint': case 'multilinestring': case 'multipolygon': $parts = array(); foreach ($geometry->getComponents() as $component) { $parts[] = '(' . $this->extractData($component) . ')'; } return implode(', ', $parts); break; case 'geometrycollection': $parts = array(); foreach ($geometry->getComponents() as $component) { $parts[] = strtoupper($component->geometryType()) . ' (' . $this->extractData($component) . ')'; } return implode(', ', $parts); break; default: throw new FOX_exception(array('numeric' => 1, 'text' => "Unknown geometry type", 'data' => $geometry, 'file' => __FILE__, 'class' => __CLASS__, 'function' => __FUNCTION__, 'line' => __LINE__, 'child' => null)); } }