| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | // Paramétrage de la connexion à la base de données |
|---|
| 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 | |
|---|
| 10 | // Récupérer le point de départ |
|---|
| 11 | $start = split(' ',$_REQUEST['startpoint']); |
|---|
| 12 | $startPoint = array($start[0], $start[1]); |
|---|
| 13 | |
|---|
| 14 | // Récupérer le point d'arrivée |
|---|
| 15 | $end = split(' ',$_REQUEST['finalpoint']); |
|---|
| 16 | $endPoint = array($end[0], $end[1]); |
|---|
| 17 | |
|---|
| 18 | ?> |
|---|
| 19 | |
|---|
| 20 | <?php |
|---|
| 21 | |
|---|
| 22 | // Trouver le tronçon le plus proche |
|---|
| 23 | $startEdge = findNearestEdge($startPoint); |
|---|
| 24 | $endEdge = findNearestEdge($endPoint); |
|---|
| 25 | |
|---|
| 26 | // FONCTION findNearestEdge |
|---|
| 27 | function findNearestEdge($lonlat) { |
|---|
| 28 | |
|---|
| 29 | // Connexion à la base de données |
|---|
| 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 | |
|---|
| 50 | // Fermer la connexion |
|---|
| 51 | pg_close($con); |
|---|
| 52 | |
|---|
| 53 | return $edge; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | ?> |
|---|
| 57 | |
|---|
| 58 | <?php |
|---|
| 59 | |
|---|
| 60 | // Choisir un algorithme de parcours |
|---|
| 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 | |
|---|
| 108 | } // fin switch |
|---|
| 109 | |
|---|
| 110 | // Connexion à la base de données |
|---|
| 111 | $dbcon = pg_connect("dbname=".PG_DB." host=".PG_HOST." user=".PG_USER); |
|---|
| 112 | |
|---|
| 113 | // Exécuter une requête |
|---|
| 114 | $query = pg_query($dbcon,$sql); |
|---|
| 115 | |
|---|
| 116 | ?> |
|---|
| 117 | |
|---|
| 118 | <?php |
|---|
| 119 | |
|---|
| 120 | // Renvoit un chemin au format GeoJSON |
|---|
| 121 | $geojson = array( |
|---|
| 122 | 'type' => 'FeatureCollection', |
|---|
| 123 | 'features' => array() |
|---|
| 124 | ); |
|---|
| 125 | |
|---|
| 126 | // Ajouter un tronçon au tableau GeoJSON |
|---|
| 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 | |
|---|
| 142 | // Ajouter un tableau d'éléments au tableau de collection d'éléments |
|---|
| 143 | array_push($geojson['features'], $feature); |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | // Fermeture de la connexion |
|---|
| 148 | pg_close($dbcon); |
|---|
| 149 | |
|---|
| 150 | // Renvoyer le résultat |
|---|
| 151 | header('Content-type: application/json',true); |
|---|
| 152 | echo json_encode($geojson); |
|---|
| 153 | |
|---|
| 154 | ?> |
|---|
| 155 | |
|---|