Guest User

Untitled

a guest
Sep 23rd, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 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 $bbox Bounding box of request *REQUIRED*
  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.  
  19. $dbconn = pg_connect ("host=localhost port=5432 dbname=osm user=osm password=osm");
  20. if (!$dbconn) {
  21. echo("Error in connection: " . pg_last_error());
  22. }
  23. $bbox = $_GET['bbox'];
  24. list($bbox_west, $bbox_south, $bbox_east, $bbox_north) = split(",", $bbox);
  25.  
  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. # 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. $sql .= " AND (box(point(" . $bbox_west . "," . $bbox_south . "),point(" . $bbox_east . "," . $bbox_north . ")) ~ (". $geomfield ."))";
  66. }
  67. else {
  68. $sql .= " WHERE (box(point(" . $bbox_west . "," . $bbox_south . "),point(" . $bbox_east . "," . $bbox_north . ")) ~ (". $geomfield ."))";
  69. }
  70. if (strlen(trim($orderby)) > 0) {
  71. $sql .= " ORDER BY " . pg_escape_string($orderby) . " " . $sort;
  72. }
  73. if (strlen(trim($limit)) > 0) {
  74. $sql .= " LIMIT " . pg_escape_string($limit);
  75. }
  76. if (strlen(trim($offset)) > 0) {
  77. $sql .= " OFFSET " . pg_escape_string($offset);
  78. }
  79. $sql .= ";";
  80. //echo $sql;
  81.  
  82. # Try query or error
  83. $rs = pg_query($dbconn, $sql);
  84. if (!$rs) {
  85. echo "An SQL error occured.\n";
  86. exit;
  87. }
  88.  
  89. # Build GeoJSON
  90. $output = '';
  91. $rowOutput = '';
  92.  
  93. while ($row = pg_fetch_assoc($rs)) {
  94. $rowOutput = (strlen($rowOutput) > 0 ? ',' : '') . '{"type": "Feature", "geometry": ' . $row['geojson'] . ', "properties": {';
  95. $props = '';
  96. $id = '';
  97. foreach ($row as $key => $val) {
  98. if ($key != "geojson") {
  99. $props .= (strlen($props) > 0 ? ',' : '') . '"' . $key . '":"' . $val . '"';
  100. }
  101. if ($key == "id") {
  102. $id .= ',"id":"' . $val . '"';
  103. }
  104. }
  105.  
  106. $rowOutput .= $props . '}';
  107. $rowOutput .= $id;
  108. $rowOutput .= '}';
  109. $output .= $rowOutput;
  110. }
  111.  
  112. $output = '{ "type": "FeatureCollection", "features": [ ' . $output . ' ]}';
  113. echo $output;
  114.  
  115.  
  116. ?>
Add Comment
Please, Sign In to add comment