Guest User

Untitled

a guest
Sep 23rd, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 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.  
  18. # Retrive URL variables
  19. if (empty($_GET['geotable'])) {
  20. echo "missing required parameter: <i>geotable</i>";
  21. exit;
  22. } else
  23. $geotable = $_GET['geotable'];
  24.  
  25. if (empty($_GET['geomfield'])) {
  26. echo "missing required parameter: <i>geomfield</i>";
  27. exit;
  28. } else
  29. $geomfield = $_GET['geomfield'];
  30.  
  31. if (empty($_GET['srid'])) {
  32. $srid = '4326';
  33. } else
  34. $srid = $_GET['srid'];
  35.  
  36. if (empty($_GET['fields'])) {
  37. $fields = '*';
  38. } else
  39. $fields = $_GET['fields'];
  40.  
  41. $parameters = $_GET['parameters'];
  42.  
  43. $orderby = $_GET['orderby'];
  44.  
  45. if (empty($_GET['sort'])) {
  46. $sort = 'ASC';
  47. } else
  48. $sort = $_GET['sort'];
  49.  
  50. $limit = $_GET['limit'];
  51.  
  52. $offset = $_GET['offset'];
  53.  
  54. # Connect to PostgreSQL database
  55. $conn = pg_connect("dbname='mydbname' user='myusername' password='mypassword' host='localhost'");
  56. if (!$conn) {
  57. echo "Not connected : " . pg_error();
  58. exit;
  59. }
  60.  
  61. # Build SQL SELECT statement and return the geometry as a GeoJSON element in EPSG: 4326
  62. $sql = "SELECT " . pg_escape_string($fields) . ", st_asgeojson(transform(" . pg_escape_string($geomfield) . ",$srid)) AS geojson FROM " . pg_escape_string($geotable);
  63. if (strlen(trim($parameters)) > 0) {
  64. $sql .= " WHERE " . pg_escape_string($parameters);
  65. }
  66. if (strlen(trim($orderby)) > 0) {
  67. $sql .= " ORDER BY " . pg_escape_string($orderby) . " " . $sort;
  68. }
  69. if (strlen(trim($limit)) > 0) {
  70. $sql .= " LIMIT " . pg_escape_string($limit);
  71. }
  72. if (strlen(trim($offset)) > 0) {
  73. $sql .= " OFFSET " . pg_escape_string($offset);
  74. }
  75. //echo $sql;
  76.  
  77. # Try query or error
  78. $rs = pg_query($conn, $sql);
  79. if (!$rs) {
  80. echo "An SQL error occured.\n";
  81. exit;
  82. }
  83.  
  84. # Build GeoJSON
  85. $output = '';
  86. $rowOutput = '';
  87.  
  88. while ($row = pg_fetch_assoc($rs)) {
  89. $rowOutput = (strlen($rowOutput) > 0 ? ',' : '') . '{"type": "Feature", "geometry": ' . $row['geojson'] . ', "properties": {';
  90. $props = '';
  91. $id = '';
  92. foreach ($row as $key => $val) {
  93. if ($key != "geojson") {
  94. $props .= (strlen($props) > 0 ? ',' : '') . '"' . $key . '":"' . $val . '"';
  95. }
  96. if ($key == "id") {
  97. $id .= ',"id":"' . $val . '"';
  98. }
  99. }
  100.  
  101. $rowOutput .= $props . '}';
  102. $rowOutput .= $id;
  103. $rowOutput .= '}';
  104. $output .= $rowOutput;
  105. }
  106.  
  107. $output = '{ "type": "FeatureCollection", "features": [ ' . $output . ' ]}';
  108. echo $output;
  109. ?>
Add Comment
Please, Sign In to add comment