Guest User

Untitled

a guest
May 13th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. <?php
  2. header('Content-Type: application/json; charset=UTF-8');
  3. /**
  4. * PostGIS to GeoJSON
  5. * Query a PostGIS table or view and return the results in GeoJSON format, suitable for use in OpenLayers, Leaflet, etc.
  6. *
  7. * @param string $geotable The PostGIS layer name *REQUIRED*
  8. * @param string $geomfield The PostGIS geometry field *REQUIRED*
  9. * @param string $srid The SRID of the returned GeoJSON *OPTIONAL (If omitted, EPSG: 4326 will be used)*
  10. * @param string $fields Fields to be returned *OPTIONAL (If omitted, all fields will be returned)* NOTE- Uppercase field names should be wrapped in double quotes
  11. * @param string $parameters SQL WHERE clause parameters *OPTIONAL*
  12. * @param string $orderby SQL ORDER BY constraint *OPTIONAL*
  13. * @param string $sort SQL ORDER BY sort order (ASC or DESC) *OPTIONAL*
  14. * @param string $limit Limit number of results returned *OPTIONAL*
  15. * @param string $offset Offset used in conjunction with limit *OPTIONAL*
  16. * @return string resulting geojson string
  17. */
  18. function escapeJsonString($value) { # list from www.json.org: (\b backspace, \f formfeed)
  19. $escapers = array("\\", "/", "\"", "\n", "\r", "\t", "\x08", "\x0c");
  20. $replacements = array("\\\\", "\\/", "\\\"", "\\n", "\\r", "\\t", "\\f", "\\b");
  21. $result = str_replace($escapers, $replacements, $value);
  22. return $result;
  23. }
  24.  
  25. # Retrive URL variables
  26. if (empty($_GET['geotable'])) {
  27. echo "missing required parameter: <i>geotable</i>";
  28. exit;
  29. } else
  30. $geotable = $_GET['geotable'];
  31.  
  32. if (empty($_GET['geomfield'])) {
  33. echo "missing required parameter: <i>geomfield</i>";
  34. exit;
  35. } else
  36. $geomfield = $_GET['geomfield'];
  37.  
  38. if (empty($_GET['srid'])) {
  39. $srid = '4326';
  40. } else
  41. $srid = $_GET['srid'];
  42.  
  43. if (empty($_GET['fields'])) {
  44. $fields = '*';
  45. } else
  46. $fields = $_GET['fields'];
  47.  
  48. $parameters = $_GET['parameters'];
  49.  
  50. $orderby = $_GET['orderby'];
  51.  
  52. if (empty($_GET['sort'])) {
  53. $sort = 'ASC';
  54. } else
  55. $sort = $_GET['sort'];
  56.  
  57. $limit = $_GET['limit'];
  58.  
  59. $offset = $_GET['offset'];
  60.  
  61. # Connect to PostgreSQL database
  62. $conn = pg_connect("dbname='mydbname' user='myusername' password='mypassword' host='localhost'");
  63. if (!$conn) {
  64. echo "Not connected : " . pg_error();
  65. exit;
  66. }
  67.  
  68. # Build SQL SELECT statement and return the geometry as a GeoJSON element in EPSG: 4326
  69. $sql = "SELECT " . pg_escape_string($fields) . ", st_asgeojson(transform(" . pg_escape_string($geomfield) . ",$srid)) AS geojson FROM " . pg_escape_string($geotable);
  70. if (strlen(trim($parameters)) > 0) {
  71. $sql .= " WHERE " . pg_escape_string($parameters);
  72. }
  73. if (strlen(trim($orderby)) > 0) {
  74. $sql .= " ORDER BY " . pg_escape_string($orderby) . " " . $sort;
  75. }
  76. if (strlen(trim($limit)) > 0) {
  77. $sql .= " LIMIT " . pg_escape_string($limit);
  78. }
  79. if (strlen(trim($offset)) > 0) {
  80. $sql .= " OFFSET " . pg_escape_string($offset);
  81. }
  82. //echo $sql;
  83.  
  84. # Try query or error
  85. $rs = pg_query($conn, $sql);
  86. if (!$rs) {
  87. echo "An SQL error occured.\n";
  88. exit;
  89. }
  90.  
  91. # Build GeoJSON
  92. $output = '';
  93. $rowOutput = '';
  94.  
  95. while ($row = pg_fetch_assoc($rs)) {
  96. $rowOutput = (strlen($rowOutput) > 0 ? ',' : '') . '{"type": "Feature", "geometry": ' . $row['geojson'] . ', "properties": {';
  97. $props = '';
  98. $id = '';
  99. foreach ($row as $key => $val) {
  100. if ($key != "geojson") {
  101. $props .= (strlen($props) > 0 ? ',' : '') . '"' . $key . '":"' . escapeJsonString($val) . '"';
  102. }
  103. if ($key == "id") {
  104. $id .= ',"id":"' . escapeJsonString($val) . '"';
  105. }
  106. }
  107.  
  108. $rowOutput .= $props . '}';
  109. $rowOutput .= $id;
  110. $rowOutput .= '}';
  111. $output .= $rowOutput;
  112. }
  113.  
  114. $output = '{ "type": "FeatureCollection", "features": [ ' . $output . ' ]}';
  115. echo $output;
  116. ?>
Add Comment
Please, Sign In to add comment