Advertisement
akiasmoro

Untitled

Jan 17th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. <?php
  2. ini_set('display_errors', 1);
  3.  
  4. //database login info
  5. $host = 'localhost';
  6. $port = '5432';
  7. $dbname = 'WebGIS_db';
  8. $user = 'akiasmoro';
  9. $password = 'asmoro14';
  10.  
  11. $conn = pg_connect("host=$host port=$port dbname=$dbname user=$user password=$password");
  12. if (!$conn) {
  13. echo "Not connected : " . pg_error();
  14. exit;
  15. }
  16.  
  17. //get the table and fields data
  18. $table = $_GET['table'];
  19. $fields = $_GET['fields'];
  20.  
  21. //turn fields array into formatted string
  22. $fieldstr = "";
  23. foreach ($fields as $i => $field){
  24. $fieldstr = $fieldstr . "l.$field, ";
  25. }
  26.  
  27. //get the geometry as geojson in WGS84
  28. $fieldstr = $fieldstr . "ST_AsGeoJSON(ST_Transform(l.geom,4326))";
  29.  
  30. //create basic sql statement
  31. $sql = "SELECT $fieldstr FROM $table l";
  32.  
  33. //if a query, add those to the sql statement
  34. if (isset($_GET['featname'])){
  35. $featname = $_GET['featname'];
  36. $distance = $_GET['distance'] * 1000; //change km to meters
  37.  
  38. //join for spatial query - table geom is in EPSG:26916
  39. $sql = $sql . " LEFT JOIN $table r ON ST_DWithin(l.geom, r.geom, $distance) WHERE r.featname = '$featname';";
  40. }
  41.  
  42. // echo $sql;
  43.  
  44. //send the query
  45. if (!$response = pg_query($conn, $sql)) {
  46. echo "A query error occured.\n";
  47. exit;
  48. }
  49.  
  50. //echo the data back to the DOM
  51. while ($row = pg_fetch_row($response)) {
  52. foreach ($row as $i => $attr){
  53. echo $attr.", ";
  54. }
  55. echo ";";
  56. }
  57.  
  58. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement