From c48622f138f1fc04adb3d585296cbda70152acde Mon Sep 17 00:00:00 2001 From: Patrick Hayes Date: Wed, 4 Apr 2012 17:33:18 -0700 Subject: [PATCH 1/4] Adding postGIS examples --- README.md | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/README.md b/README.md index 894c3d13..6a025e19 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,48 @@ Clearly, more complex analysis is possible. echo $geom2->envelope()->area(); +Working with PostGIS +--------------------- +geoPHP, through it's WKB adapter, has good integration with postGIS. Here's an example of reading and writing postGIS geometries + +```php +$host = 'localhost'; +$database = 'phayes'; +$table = 'test'; +$column = 'geom'; +$user = 'phayes'; +$pass = 'supersecret'; + +$connection = pg_connect("host=$host dbname=$database user=$user password=$pass"); + +// Working with PostGIS and WKB +// ------------- + +// Using asBinary and GeomFromBinary 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 + + // Let's insert it back into the database + $insert_string = pg_escape_bytea($geom->out('wkb')); + pg_query($connection, "INSERT INTO $table ($column) values (GeomFromWKB('$insert_string'))"); +} + +// Using direct selects in PostGIS +$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'); + + // To insert directly into postGIS we need to unpack the WKB + $unpacked = unpack('H*', $geom->out('wkb')); + $insert_string = $unpacked[1]; + pg_query($connection, "INSERT INTO $table ($column) values ('$insert_string')"); +} +``` + + Credit ------------------------------------------------- From aa1dfb0dff9a099b16831103129132d9e412e497 Mon Sep 17 00:00:00 2001 From: Patrick Hayes Date: Wed, 4 Apr 2012 17:34:08 -0700 Subject: [PATCH 2/4] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 6a025e19..b184ff20 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,8 @@ Working with PostGIS geoPHP, through it's WKB adapter, has good integration with postGIS. Here's an example of reading and writing postGIS geometries ```php + Date: Wed, 4 Apr 2012 17:38:37 -0700 Subject: [PATCH 3/4] Update README.md --- README.md | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b184ff20..08026e76 100644 --- a/README.md +++ b/README.md @@ -105,11 +105,10 @@ $user = 'phayes'; $pass = 'supersecret'; $connection = pg_connect("host=$host dbname=$database user=$user password=$pass"); - // Working with PostGIS and WKB -// ------------- +// ---------------------------- -// Using asBinary and GeomFromBinary in PostGIS +// 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 @@ -120,11 +119,11 @@ foreach ($result as $item) { pg_query($connection, "INSERT INTO $table ($column) values (GeomFromWKB('$insert_string'))"); } -// Using direct selects in PostGIS +// 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'); + $wkb = pack('H*',$item['geom']); // Unpacking the hex blob + $geom = geoPHP::load($wkb, 'wkb'); // We now have a geoPHP Geometry // To insert directly into postGIS we need to unpack the WKB $unpacked = unpack('H*', $geom->out('wkb')); From ea9b44e0816c731185e30acf19290055cf1b6739 Mon Sep 17 00:00:00 2001 From: Patrick Hayes Date: Wed, 4 Apr 2012 17:39:44 -0700 Subject: [PATCH 4/4] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 08026e76..fd788f2f 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,7 @@ $user = 'phayes'; $pass = 'supersecret'; $connection = pg_connect("host=$host dbname=$database user=$user password=$pass"); + // Working with PostGIS and WKB // ----------------------------