_geo_wkb_get_data

geo/includes/geo.wkb.inc, line 16

Versions
6
_geo_wkb_get_data($wkb = NULL, $format = 'text', $fp = NULL, $type = NULL)

Code

<?php
function _geo_wkb_get_data($wkb = NULL, $format = 'text', $fp = NULL, $type = NULL) {
  $data = array();

  if (!$fp) {
    if (empty($wkb)) return;
    // Put our WKB data into memory so we can cruise around with fread.
    $fp = fopen("php://memory", 'r+');
    fputs($fp, $wkb);
    fseek($fp, 0);

    // Fetch the byte order (0 = Big Endian, 1 = Little Endian) and geo type.
    $data = unpack('cbyte_order/Ltype', fread($fp, 5));

    // Set the geometry type
    $data['type'] = geo_wkb_types($data['type']);
  }

  $func = 'geo_wkb_get_'. $format;

  if (!isset($type)) $type = $data['type'];
 
  switch($type) {

    case 'point': // Contains x, y decimal values.
      $data = array_merge($data, unpack('dx/dy', fread($fp, 16)));
      $data['value'] = $func($data);
      if (function_exists($post = $func .'_post')) $data = $post($data);
      return $data;

    case 'linestring': // Contains count * (point) values.
      $data['count'] = current(unpack('L', fread($fp, 4)));
      for ($i = 1; $i <= $data['count']; $i++) {
        $point = geo_wkb_get_data(NULL, $format, $fp, 'point');
        $data['value'] = $func($point, $data['value']);
      }
      if (function_exists($post = $func .'_post')) $data = $post($data);
      return $data;

    case 'polygon': // Contains count * (linestring) items.
      $data['count'] = current(unpack('L', fread($fp, 4)));
      for ($i = 1; $i <= $data['count']; $i++) {
        $line = geo_wkb_get_data(NULL, $format, $fp, 'linestring');
        $data['value'] = $func($line, $data['value']);
      }
      if (function_exists($post = $func .'_post')) $data = $post($data);
      return $data;
  }
}
?>