Skip to content

Commit

Permalink
Updating readme to use ewkb for postgis example
Browse files Browse the repository at this point in the history
  • Loading branch information
phayes committed Apr 5, 2012
1 parent 095beb0 commit cd3b473
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,28 +106,28 @@ $pass = 'supersecret';

$connection = pg_connect("host=$host dbname=$database user=$user password=$pass");

// Working with PostGIS and WKB
// Working with PostGIS and Extended-WKB
// ----------------------------

// Using asBinary and GeomFromWKB in PostGIS
$result = pg_fetch_all(pg_query($connection, "SELECT asBinary($column) as geom FROM $table"));
foreach ($result as $item) {
$wkb = pg_unescape_bytea($item['geom']); // Make sure to unescape the hex blob
$geom = geoPHP::load($wkb, 'wkb'); // We now a full geoPHP Geometry object
$geom = geoPHP::load($wkb, 'ewkb'); // We now a full geoPHP Geometry object

// Let's insert it back into the database
$insert_string = pg_escape_bytea($geom->out('wkb'));
$insert_string = pg_escape_bytea($geom->out('ewkb'));
pg_query($connection, "INSERT INTO $table ($column) values (GeomFromWKB('$insert_string'))");
}

// Using a direct SELECT and INSERTs in PostGIS without using wrapping functions
$result = pg_fetch_all(pg_query($connection, "SELECT $column as geom FROM $table"));
foreach ($result as $item) {
$wkb = pack('H*',$item['geom']); // Unpacking the hex blob
$geom = geoPHP::load($wkb, 'wkb'); // We now have a geoPHP Geometry
$geom = geoPHP::load($wkb, 'ewkb'); // We now have a geoPHP Geometry

// To insert directly into postGIS we need to unpack the WKB
$unpacked = unpack('H*', $geom->out('wkb'));
$unpacked = unpack('H*', $geom->out('ewkb'));
$insert_string = $unpacked[1];
pg_query($connection, "INSERT INTO $table ($column) values ('$insert_string')");
}
Expand Down

0 comments on commit cd3b473

Please sign in to comment.