Advertisement
Guest User

pgrouting.php

a guest
Nov 11th, 2011
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. <?php
  2.  
  3. // Database connection settings
  4. define("PG_DB" , "routing");
  5. define("PG_HOST", "localhost");
  6. define("PG_USER", "user");
  7. define("PG_PASS", "user")
  8. define("PG_PORT", "5432");
  9. define("TABLE", "ways");
  10.  
  11. // Retrieve start point
  12. $start = split(' ',$_REQUEST['startpoint']);
  13. $startPoint = array($start[0], $start[1]);
  14.  
  15. // Retrieve end point
  16. $end = split(' ',$_REQUEST['finalpoint']);
  17. $endPoint = array($end[0], $end[1]);
  18.  
  19. ?>
  20.  
  21. <?php
  22.  
  23. // Find the nearest edge
  24. $startEdge = findNearestEdge($startPoint);
  25. $endEdge = findNearestEdge($endPoint);
  26.  
  27. // FUNCTION findNearestEdge
  28. function findNearestEdge($lonlat) {
  29.  
  30. // Connect to database
  31. $con = pg_connect("dbname=".PG_DB." host=".PG_HOST." user=".PG_USER. " password=".PG_PASS);
  32.  
  33. $sql = "SELECT gid, source, target, the_geom,
  34. distance(the_geom, GeometryFromText(
  35. 'POINT(".$lonlat[0]." ".$lonlat[1].")', 4326)) AS dist
  36. FROM ".TABLE."
  37. WHERE the_geom && setsrid(
  38. 'BOX3D(".($lonlat[0]-0.1)."
  39. ".($lonlat[1]-0.1).",
  40. ".($lonlat[0]+0.1)."
  41. ".($lonlat[1]+0.1).")'::box3d, 4326)
  42. ORDER BY dist LIMIT 1";
  43.  
  44. $query = pg_query($con,$sql);
  45.  
  46. $edge['gid'] = pg_fetch_result($query, 0, 0);
  47. $edge['source'] = pg_fetch_result($query, 0, 1);
  48. $edge['target'] = pg_fetch_result($query, 0, 2);
  49. $edge['the_geom'] = pg_fetch_result($query, 0, 3);
  50.  
  51. // Close database connection
  52. pg_close($con);
  53.  
  54. return $edge;
  55. }
  56.  
  57. ?>
  58.  
  59. <?php
  60.  
  61. // Select the routing algorithm
  62. switch($_REQUEST['method']) {
  63.  
  64. case 'SPD' : // Shortest Path Dijkstra
  65.  
  66. $sql = "SELECT rt.gid, ST_AsGeoJSON(rt.the_geom) AS geojson,
  67. length(rt.the_geom) AS length, ".TABLE.".gid
  68. FROM ".TABLE.",
  69. (SELECT gid, the_geom
  70. FROM dijkstra_sp_delta(
  71. '".TABLE."',
  72. ".$startEdge['source'].",
  73. ".$endEdge['target'].",
  74. 0.1)
  75. ) as rt
  76. WHERE ".TABLE.".gid=rt.gid;";
  77. break;
  78.  
  79. case 'SPA' : // Shortest Path A*
  80.  
  81. $sql = "SELECT rt.gid, ST_AsGeoJSON(rt.the_geom) AS geojson,
  82. length(rt.the_geom) AS length, ".TABLE.".gid
  83. FROM ".TABLE.",
  84. (SELECT gid, the_geom
  85. FROM astar_sp_delta(
  86. '".TABLE."',
  87. ".$startEdge['source'].",
  88. ".$endEdge['target'].",
  89. 0.1)
  90. ) as rt
  91. WHERE ".TABLE.".gid=rt.gid;";
  92. break;
  93.  
  94. case 'SPS' : // Shortest Path Shooting*
  95.  
  96. $sql = "SELECT rt.gid, ST_AsGeoJSON(rt.the_geom) AS geojson,
  97. length(rt.the_geom) AS length, ".TABLE.".gid
  98. FROM ".TABLE.",
  99. (SELECT gid, the_geom
  100. FROM shootingstar_sp(
  101. '".TABLE."',
  102. ".$startEdge['gid'].",
  103. ".$endEdge['gid'].",
  104. 0.1, 'length', true, true)
  105. ) as rt
  106. WHERE ".TABLE.".gid=rt.gid;";
  107. break;
  108.  
  109. } // close switch
  110.  
  111. // Connect to database
  112. $dbcon = pg_connect("dbname=".PG_DB." host=".PG_HOST." user=".PG_USER. " password=".PG_PASS);
  113.  
  114. // Perform database query
  115. $query = pg_query($dbcon,$sql);
  116.  
  117. ?>
  118.  
  119. <?php
  120.  
  121. // Return route as GeoJSON
  122. $geojson = array(
  123. 'type' => 'FeatureCollection',
  124. 'features' => array()
  125. );
  126.  
  127. // Add edges to GeoJSON array
  128. while($edge=pg_fetch_assoc($query)) {
  129.  
  130. $feature = array(
  131. 'type' => 'Feature',
  132. 'geometry' => json_decode($edge['geojson'], true),
  133. 'crs' => array(
  134. 'type' => 'EPSG',
  135. 'properties' => array('code' => '4326')
  136. ),
  137. 'properties' => array(
  138. 'id' => $edge['id'],
  139. 'length' => $edge['length']
  140. )
  141. );
  142.  
  143. // Add feature array to feature collection array
  144. array_push($geojson['features'], $feature);
  145. }
  146.  
  147.  
  148. // Close database connection
  149. pg_close($dbcon);
  150.  
  151. // Return routing result
  152. header('Content-type: application/json',true);
  153. echo json_encode($geojson);
  154. ?>
  155.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement