postgis.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?
  2. // Uncomment to test
  3. # run_test();
  4. function run_test() {
  5. header("Content-type: text");
  6. include_once('../geoPHP.inc');
  7. // Your database test table should contain 3 columns: name (text), type (text), geom (geometry)
  8. $host = 'localhost';
  9. $database = 'phayes';
  10. $table = 'test';
  11. $column = 'geom';
  12. $user = 'phayes';
  13. $pass = 'supersecret';
  14. $connection = pg_connect("host=$host dbname=$database user=$user password=$pass");
  15. // Truncate
  16. pg_query($connection, "DELETE FROM $table");
  17. // Working with PostGIS and EWKB
  18. // ----------------------------
  19. foreach (scandir('./input') as $file) {
  20. $parts = explode('.',$file);
  21. if ($parts[0]) {
  22. $name = $parts[0];
  23. $format = $parts[1];
  24. $value = file_get_contents('./input/'.$file);
  25. print '---- Testing '.$file."\n";
  26. flush();
  27. $geometry = geoPHP::load($value, $format);
  28. test_postgis($name, $format, $geometry, $connection, 'wkb');
  29. $geometry->setSRID(4326);
  30. test_postgis($name, $format, $geometry, $connection, 'ewkb');
  31. }
  32. }
  33. print "Testing Done!";
  34. }
  35. function test_postgis($name, $type, $geom, $connection, $format) {
  36. global $table;
  37. // Let's insert into the database using GeomFromWKB
  38. $insert_string = pg_escape_bytea($geom->out($format));
  39. pg_query($connection, "INSERT INTO $table (name, type, geom) values ('$name', '$type', GeomFromWKB('$insert_string'))");
  40. // SELECT using asBinary PostGIS
  41. $result = pg_fetch_all(pg_query($connection, "SELECT asBinary(geom) as geom FROM $table WHERE name='$name'"));
  42. foreach ($result as $item) {
  43. $wkb = pg_unescape_bytea($item['geom']); // Make sure to unescape the hex blob
  44. $geom = geoPHP::load($wkb, $format); // We now a full geoPHP Geometry object
  45. }
  46. // SELECT and INSERT directly, with no wrapping functions
  47. $result = pg_fetch_all(pg_query($connection, "SELECT geom as geom FROM $table WHERE name='$name'"));
  48. foreach ($result as $item) {
  49. $wkb = pack('H*',$item['geom']); // Unpacking the hex blob
  50. $geom = geoPHP::load($wkb, $format); // We now have a geoPHP Geometry
  51. // Let's re-insert directly into postGIS
  52. // We need to unpack the WKB
  53. $unpacked = unpack('H*', $geom->out($format));
  54. $insert_string = $unpacked[1];
  55. pg_query($connection, "INSERT INTO $table (name, type, geom) values ('$name', '$type', '$insert_string')");
  56. }
  57. // SELECT and INSERT using as EWKT (ST_GeomFromEWKT and ST_AsEWKT)
  58. $result = pg_fetch_all(pg_query($connection, "SELECT ST_AsEWKT(geom) as geom FROM $table WHERE name='$name'"));
  59. foreach ($result as $item) {
  60. $wkt = $item['geom']; // Make sure to unescape the hex blob
  61. $geom = geoPHP::load($wkt, 'ewkt'); // We now a full geoPHP Geometry object
  62. // Let's re-insert directly into postGIS
  63. $insert_string = $geom->out('ewkt');
  64. pg_query($connection, "INSERT INTO $table (name, type, geom) values ('$name', '$type', ST_GeomFromEWKT('$insert_string'))");
  65. }
  66. }