Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Retrieve start point
- $start = split(' ',$_REQUEST['startpoint']);
- $startPoint = array($start[0], $start[1]);
- // Retrieve end point
- $end = split(' ',$_REQUEST['finalpoint']);
- $endPoint = array($end[0], $end[1]);
- ?>
- <!--determinar inicio e fim da rota-->
- <?php
- // Find the nearest point
- $startpoint = findNearestpoint($startPoint);
- $endpoint = findNearestpoint($endPoint);
- // FUNCTION findNearestpoint
- function findNearestpoint($lonlat) {
- // Connect to database
- $con = pg_connect("host=localhost dbname=****** port =5432 user=**** password=****");
- $sql = "SELECT id, the_geom,
- distance(the_geom, GeometryFromText('POINT(".$lonlat[0]." ".$lonlat[1].")', 27492)) AS dist
- FROM vertices_tmp
- ORDER BY dist LIMIT 1";
- $query = pg_query($con,$sql);
- $edge['id'] = pg_fetch_result($query, 0, 0);
- $edge['the_geom'] = pg_fetch_result($query, 0, 1);
- // Close database connection
- pg_close($con);
- return $edge;
- }
- ?>
- <!--query de custo-->
- <?php
- // Select the routing algorithm
- switch($_REQUEST['method']) {
- case 'SP' : // Shortest Path
- $sql = "SELECT arcos.id, ST_AsGeoJSON(arcos.the_geom) AS geojson, length(arcos.the_geom) AS length, ".TABLE.".id
- FROM ".TABLE."
- JOIN
- (SELECT * FROM shortest_path('SELECT id,source::int4 AS source,target::int4 AS target,length::double precision AS cost FROM ".TABLE."',
- ".$startpoint['id'].",".$endpoint['id'].",false,false)) AS rota
- ON
- ".TABLE.".id = rota.edge_id;";
- break;
- case 'SP2' : // Shortest Path2
- $sql = "SELECT arcos.id, ST_AsGeoJSON(arcos.the_geom) AS geojson, length(arcos.the_geom) AS length, ".TABLE.".id
- FROM ".TABLE."
- JOIN
- (SELECT * FROM shortest_path('SELECT id,source::int4 AS source,target::int4 AS target,length::double precision AS cost FROM ".TABLE."',
- ".$startpoint['id'].",".$endpoint['id'].",false,false)) AS rota
- ON
- ".TABLE.".id = rota.edge_id;";
- break;
- } // close switch
- // Connect to database
- $dbcon = pg_connect("host=localhost dbname=***** port =5432 user=**** password=*****");
- // Perform database query
- $query = pg_query($dbcon,$sql);
- ?>
- <!--retornar resultado como geojson-->
- <?php
- // Return route as GeoJSON
- $geojson = array(
- 'type' => 'FeatureCollection',
- 'features' => array()
- );
- // Add edges to GeoJSON array
- while($edge=pg_fetch_assoc($query)) {
- $feature = array(
- 'type' => 'Feature',
- 'geometry' => json_decode($edge['geojson'], true),
- 'crs' => array(
- 'type' => 'EPSG',
- 'properties' => array('code' => '27492')
- ),
- 'properties' => array(
- 'id' => $edge['id'],
- 'length' => $edge['length']
- )
- );
- // Add feature array to feature collection array
- array_push($geojson['features'], $feature);
- }
- // Close database connection
- pg_close($dbcon);
- // Return routing result
- header('Content-type: application/json',true);
- echo json_encode($geojson);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement