Example #1
0
 /**
  * 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;
     }
 }
Example #2
0
File: GPX.php Project: kitbs/geoPHP
 /**
  * Serialize geometries into a GPX string.
  *
  * @param Geometry $geometry
  *
  * @return string The GPX string representation of the input geometries
  */
 public function write(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>';
 }
Example #3
0
 /**
  * Serialize geometries into an EWKT string.
  *
  * @param Geometry $geometry
  *
  * @return string The Extended-WKT string representation of the input geometries
  */
 public function write(Geometry $geometry)
 {
     $srid = $geometry->SRID();
     $wkt = '';
     if ($srid) {
         $wkt = 'SRID=' . $srid . ';';
         $wkt .= $geometry->out('wkt');
         return $wkt;
     } else {
         return $geometry->out('wkt');
     }
 }
Example #4
0
 /**
  * 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(Geometry $geometry, $write_as_hex = FALSE)
 {
     // We always write into NDR (little endian)
     $wkb = pack('c', 1);
     switch ($geometry->getGeomType()) {
         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;
     }
     if ($write_as_hex) {
         $unpacked = unpack('H*', $wkb);
         return $unpacked[1];
     } else {
         return $wkb;
     }
 }
Example #5
0
File: WKT.php Project: kitbs/geoPHP
 /**
  * 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);
     }
 }
Example #6
0
 /**
  * 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(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;
         }
         if ($return_type == 'array') {
             return $this->result->results[0]->address_components;
         }
     } elseif ($this->result->status == 'ZERO_RESULTS') {
         if ($return_type == 'string') {
             return '';
         }
         if ($return_type == 'array') {
             return $this->result->results;
         }
     } else {
         if ($this->result->status) {
             throw new Exception('Error in Google Reverse Geocoder: ' . $this->result->status);
         } else {
             throw new Exception('Unknown error in Google Reverse Geocoder');
         }
         return FALSE;
     }
 }
Example #7
0
 public function project(Geometry $point, $normalized = NULL)
 {
     if ($this->geos()) {
         return $this->geos()->project($point->geos(), $normalized);
     }
 }