/** * 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(FOX_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; } }
/** * Serialize geometries into a GPX string. * * @param Geometry $geometry * * @return string The GPX string representation of the input geometries */ public function write(FOX_geometry $geometry, $namespace = false) { if ($geometry->isEmpty()) { return null; } if ($namespace) { $this->namespace = $namespace; $this->nss = $namespace . ':'; } return '<' . $this->nss . 'gpx creator="geoPHP" version="1.0">' . $this->geometryToGPX($geometry) . '</' . $this->nss . 'gpx>'; }
/** * Serialize geometries into an EWKT string. * * @param Geometry $geometry * * @return string The Extended-WKT string representation of the input geometries */ public function write(FOX_geometry $geometry) { $srid = $geometry->SRID(); $wkt = ''; if ($srid) { $wkt = 'SRID=' . $srid . ';'; $wkt .= $geometry->out('wkt'); return $wkt; } else { return $geometry->out('wkt'); } }
/** * Serialize geometries into an EWKB binary string. * * @param Geometry $geometry * * @return string The Extended-WKB binary string representation of the input geometries */ public function write(FOX_geometry $geometry, $write_as_hex = false) { // NOTE: Isn't this supposed to write the SRID to the binary? // We always write into NDR (little endian) $wkb = pack('c', 1); $type = strtolower($geometry->getGeomType()); switch ($type) { case "point": $wkb .= pack('L', 1); $wkb .= $this->writePoint($geometry); break; case "linestring": $wkb .= pack('L', 2); $wkb .= $this->writeLineString($geometry); break; case "polygon": $wkb .= pack('L', 3); $wkb .= $this->writePolygon($geometry); break; case "multipoint": $wkb .= pack('L', 4); $wkb .= $this->writeMulti($geometry); break; case "multilinestring": $wkb .= pack('L', 5); $wkb .= $this->writeMulti($geometry); break; case "multipolygon": $wkb .= pack('L', 6); $wkb .= $this->writeMulti($geometry); break; case "geometrycollection": $wkb .= pack('L', 7); $wkb .= $this->writeMulti($geometry); 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)); } if ($write_as_hex) { $unpacked = unpack('H*', $wkb); return $unpacked[1]; } else { return $wkb; } }
/** * Serialize geometries into a WKT string. * * @param Geometry $geometry * @param string $return_type Should be either 'string' or 'array' * * @return string Does a reverse geocode of the geometry */ public function write(FOX_geometry $geometry, $return_type = 'string') { $centroid = $geometry->getCentroid(); $lat = $centroid->getY(); $lon = $centroid->getX(); $url = "http://maps.googleapis.com/maps/api/geocode/json"; $url .= '?latlng=' . $lat . ',' . $lon; $url .= '&sensor=false'; $this->result = json_decode(@file_get_contents($url)); if ($this->result->status == 'OK') { if ($return_type == 'string') { return $this->result->results[0]->formatted_address; } elseif ($return_type == 'array') { return $this->result->results[0]->address_components; } } else { throw new FOX_exception(array('numeric' => 1, 'text' => "Error in Google reverse-geocoder API", 'data' => array('geometry' => $geometry, 'return_type' => $return_type, 'result' => $this->result), 'file' => __FILE__, 'class' => __CLASS__, 'function' => __FUNCTION__, 'line' => __LINE__, 'child' => null)); } }
public function project(FOX_geometry $point, $normalized = null) { if ($this->geos()) { return $this->geos()->project($point->geos(), $normalized); } }
/** * Serialize geometries into a WKT string. * * @param Geometry $geometry * * @return string The WKT string representation of the input geometries */ public function write(FOX_geometry $geometry) { // If geos is installed, then we take a shortcut and let it write the WKT if (FOX_geo::geosInstalled()) { $writer = new GEOSWKTWriter(); $writer->setTrim(true); return $writer->write($geometry->geos()); } if ($geometry->isEmpty()) { return strtoupper($geometry->geometryType()) . ' EMPTY'; } else { $data = $this->extractData($geometry); if ($data) { return strtoupper($geometry->geometryType()) . ' (' . $data . ')'; } else { return null; } } }