You are here

public function WKB::write in geoPHP 7

Same name and namespace in other branches
  1. 8 geoPHP/lib/adapters/WKB.class.php \WKB::write()

Serialize geometries into WKB string.

Parameters

Geometry $geometry:

Return value

string The WKB string representation of the input geometries

Overrides GeoAdapter::write

1 call to WKB::write()
WKB::writeMulti in geoPHP/lib/adapters/WKB.class.php
1 method overrides WKB::write()
EWKB::write in geoPHP/lib/adapters/EWKB.class.php
Serialize geometries into an EWKB binary string.

File

geoPHP/lib/adapters/WKB.class.php, line 160

Class

WKB
PHP Geometry/WKB encoder/decoder

Code

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;
  }
}