[76] | 1 | <?php |
---|
| 2 | |
---|
[80] | 3 | // Paramétrage de la connexion à la base de données |
---|
[76] | 4 | define("PG_DB" , "routing"); |
---|
| 5 | define("PG_HOST", "localhost"); |
---|
| 6 | define("PG_USER", "postgres"); |
---|
| 7 | define("PG_PORT", "5432"); |
---|
| 8 | define("TABLE", "ways"); |
---|
| 9 | |
---|
[80] | 10 | // Récupérer le point de départ |
---|
[76] | 11 | $start = split(' ',$_REQUEST['startpoint']); |
---|
| 12 | $startPoint = array($start[0], $start[1]); |
---|
| 13 | |
---|
[80] | 14 | // Récupérer le point d'arrivée |
---|
[76] | 15 | $end = split(' ',$_REQUEST['finalpoint']); |
---|
| 16 | $endPoint = array($end[0], $end[1]); |
---|
| 17 | |
---|
| 18 | ?> |
---|
| 19 | |
---|
| 20 | <?php |
---|
| 21 | |
---|
[80] | 22 | // Trouver le tronçon le plus proche |
---|
[76] | 23 | $startEdge = findNearestEdge($startPoint); |
---|
| 24 | $endEdge = findNearestEdge($endPoint); |
---|
| 25 | |
---|
[80] | 26 | // FONCTION findNearestEdge |
---|
[76] | 27 | function findNearestEdge($lonlat) { |
---|
| 28 | |
---|
[80] | 29 | // Connexion à la base de données |
---|
[76] | 30 | $con = pg_connect("dbname=".PG_DB." host=".PG_HOST." user=".PG_USER); |
---|
| 31 | |
---|
| 32 | $sql = "SELECT gid, source, target, the_geom, |
---|
| 33 | distance(the_geom, GeometryFromText( |
---|
| 34 | 'POINT(".$lonlat[0]." ".$lonlat[1].")', 4326)) AS dist |
---|
| 35 | FROM ".TABLE." |
---|
| 36 | WHERE the_geom && setsrid( |
---|
| 37 | 'BOX3D(".($lonlat[0]-0.1)." |
---|
| 38 | ".($lonlat[1]-0.1).", |
---|
| 39 | ".($lonlat[0]+0.1)." |
---|
| 40 | ".($lonlat[1]+0.1).")'::box3d, 4326) |
---|
| 41 | ORDER BY dist LIMIT 1"; |
---|
| 42 | |
---|
| 43 | $query = pg_query($con,$sql); |
---|
| 44 | |
---|
| 45 | $edge['gid'] = pg_fetch_result($query, 0, 0); |
---|
| 46 | $edge['source'] = pg_fetch_result($query, 0, 1); |
---|
| 47 | $edge['target'] = pg_fetch_result($query, 0, 2); |
---|
| 48 | $edge['the_geom'] = pg_fetch_result($query, 0, 3); |
---|
| 49 | |
---|
[80] | 50 | // Fermer la connexion |
---|
[76] | 51 | pg_close($con); |
---|
| 52 | |
---|
| 53 | return $edge; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | ?> |
---|
| 57 | |
---|
| 58 | <?php |
---|
| 59 | |
---|
[80] | 60 | // Choisir un algorithme de parcours |
---|
[76] | 61 | switch($_REQUEST['method']) { |
---|
| 62 | |
---|
| 63 | case 'SPD' : // Shortest Path Dijkstra |
---|
| 64 | |
---|
| 65 | $sql = "SELECT rt.gid, ST_AsGeoJSON(rt.the_geom) AS geojson, |
---|
| 66 | length(rt.the_geom) AS length, ".TABLE.".gid |
---|
| 67 | FROM ".TABLE.", |
---|
| 68 | (SELECT gid, the_geom |
---|
| 69 | FROM dijkstra_sp_delta( |
---|
| 70 | '".TABLE."', |
---|
| 71 | ".$startEdge['source'].", |
---|
| 72 | ".$endEdge['target'].", |
---|
| 73 | 0.1) |
---|
| 74 | ) as rt |
---|
| 75 | WHERE ".TABLE.".gid=rt.gid;"; |
---|
| 76 | break; |
---|
| 77 | |
---|
| 78 | case 'SPA' : // Shortest Path A* |
---|
| 79 | |
---|
| 80 | $sql = "SELECT rt.gid, ST_AsGeoJSON(rt.the_geom) AS geojson, |
---|
| 81 | length(rt.the_geom) AS length, ".TABLE.".gid |
---|
| 82 | FROM ".TABLE.", |
---|
| 83 | (SELECT gid, the_geom |
---|
| 84 | FROM astar_sp_delta( |
---|
| 85 | '".TABLE."', |
---|
| 86 | ".$startEdge['source'].", |
---|
| 87 | ".$endEdge['target'].", |
---|
| 88 | 0.1) |
---|
| 89 | ) as rt |
---|
| 90 | WHERE ".TABLE.".gid=rt.gid;"; |
---|
| 91 | break; |
---|
| 92 | |
---|
| 93 | case 'SPS' : // Shortest Path Shooting* |
---|
| 94 | |
---|
| 95 | $sql = "SELECT rt.gid, ST_AsGeoJSON(rt.the_geom) AS geojson, |
---|
| 96 | length(rt.the_geom) AS length, ".TABLE.".gid |
---|
| 97 | FROM ".TABLE.", |
---|
| 98 | (SELECT gid, the_geom |
---|
| 99 | FROM shootingstar_sp( |
---|
| 100 | '".TABLE."', |
---|
| 101 | ".$startEdge['gid'].", |
---|
| 102 | ".$endEdge['gid'].", |
---|
| 103 | 0.1, 'length', true, true) |
---|
| 104 | ) as rt |
---|
| 105 | WHERE ".TABLE.".gid=rt.gid;"; |
---|
| 106 | break; |
---|
| 107 | |
---|
[80] | 108 | } // fin switch |
---|
[76] | 109 | |
---|
[80] | 110 | // Connexion à la base de données |
---|
[76] | 111 | $dbcon = pg_connect("dbname=".PG_DB." host=".PG_HOST." user=".PG_USER); |
---|
| 112 | |
---|
[80] | 113 | // Exécuter une requête |
---|
[76] | 114 | $query = pg_query($dbcon,$sql); |
---|
| 115 | |
---|
| 116 | ?> |
---|
| 117 | |
---|
| 118 | <?php |
---|
| 119 | |
---|
[80] | 120 | // Renvoit un chemin au format GeoJSON |
---|
[76] | 121 | $geojson = array( |
---|
| 122 | 'type' => 'FeatureCollection', |
---|
| 123 | 'features' => array() |
---|
| 124 | ); |
---|
| 125 | |
---|
[80] | 126 | // Ajouter un tronçon au tableau GeoJSON |
---|
[76] | 127 | while($edge=pg_fetch_assoc($query)) { |
---|
| 128 | |
---|
| 129 | $feature = array( |
---|
| 130 | 'type' => 'Feature', |
---|
| 131 | 'geometry' => json_decode($edge['geojson'], true), |
---|
| 132 | 'crs' => array( |
---|
| 133 | 'type' => 'EPSG', |
---|
| 134 | 'properties' => array('code' => '4326') |
---|
| 135 | ), |
---|
| 136 | 'properties' => array( |
---|
| 137 | 'id' => $edge['id'], |
---|
| 138 | 'length' => $edge['length'] |
---|
| 139 | ) |
---|
| 140 | ); |
---|
| 141 | |
---|
[80] | 142 | // Ajouter un tableau d'éléments au tableau de collection d'éléments |
---|
[76] | 143 | array_push($geojson['features'], $feature); |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | |
---|
[80] | 147 | // Fermeture de la connexion |
---|
[76] | 148 | pg_close($dbcon); |
---|
| 149 | |
---|
[80] | 150 | // Renvoyer le résultat |
---|
[76] | 151 | header('Content-type: application/json',true); |
---|
| 152 | echo json_encode($geojson); |
---|
| 153 | |
---|
| 154 | ?> |
---|
| 155 | |
---|