You are here

public function EWKB::read in geoPHP 7

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

Read WKB binary string into geometry objects

Parameters

string $wkb An Extended-WKB binary string:

Return value

Geometry

Overrides WKB::read

File

geoPHP/lib/adapters/EWKB.class.php, line 15

Class

EWKB
EWKB (Extended Well Known Binary) Adapter

Code

public function read($wkb, $is_hex_string = FALSE) {
  if ($is_hex_string) {
    $wkb = pack('H*', $wkb);
  }

  // Open the wkb up in memory so we can examine the SRID
  $mem = fopen('php://memory', 'r+');
  fwrite($mem, $wkb);
  fseek($mem, 0);
  $base_info = unpack("corder/ctype/cz/cm/cs", fread($mem, 5));
  if ($base_info['s']) {
    $srid = current(unpack("Lsrid", fread($mem, 4)));
  }
  else {
    $srid = NULL;
  }
  fclose($mem);

  // Run the wkb through the normal WKB reader to get the geometry
  $wkb_reader = new WKB();
  $geom = $wkb_reader
    ->read($wkb);

  // If there is an SRID, add it to the geometry
  if ($srid) {
    $geom
      ->setSRID($srid);
  }
  return $geom;
}