Guest User

Untitled

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