Skip to content

Commit

Permalink
Various small improvements to format detection.
Browse files Browse the repository at this point in the history
  • Loading branch information
phayes committed Sep 4, 2012
1 parent 5c18d6e commit d098449
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions geoPHP.inc
Original file line number Diff line number Diff line change
Expand Up @@ -215,18 +215,27 @@ class geoPHP

$bytes = unpack("c*", fread($mem, 11));

// If bytes is empty, then we were passed empty input
if (empty($bytes)) return FALSE;

// First char is a tab, space or carriage-return. trim it and try again
if ($bytes[1] == 9 || $bytes[1] == 10 || $bytes[1] == 32) {
return geoPHP::detectFormat(ltrim($input));
}

// Detect WKB or EWKB -- first byte is 1 (little endian indicator)
if ($bytes[1] == 1) {
// If SRID byte is TRUE (1), it's EWKB
if ($bytes[5]) return 'ewkb';
else return 'wkb';
}

// Detect HEX encoded WKB or EWKB -- first byte is 48, second byte is 49 (hex '01' => first-byte = 1)
// Detect HEX encoded WKB or EWKB (PostGIS format) -- first byte is 48, second byte is 49 (hex '01' => first-byte = 1)
if ($bytes[1] == 48 && $bytes[2] == 49) {
// The shortest possible WKB string (LINESTRING EMPTY) is 9 bytes long (18 hex chars)
// The shortest possible WKB string (LINESTRING EMPTY) is 18 hex-chars (9 encoded bytes) long
// This differentiates it from a geohash, which is always shorter than 18 characters.
if (strlen($input) >= 18) {
//@@TODO: Differentiate between EWKB and WKB -- check bytes 10 or 11 (SRID bool indicator)
//@@TODO: Differentiate between EWKB and WKB -- check hex-char 10 or 11 (SRID bool indicator at encoded byte 5)
return 'ewkb:1';
}
}
Expand Down

0 comments on commit d098449

Please sign in to comment.