Advertisement
Guest User

Untitled

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