Advertisement
Guest User

Untitled

a guest
Jul 10th, 2012
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.56 KB | None | 0 0
  1.  
  2. <!--conexão à base-->
  3.  
  4.  
  5. <?php
  6.  
  7.    // Database connection settings
  8.    define("PG_DB"  , "pinhel");
  9.    define("PG_HOST", "localhost");
  10.    define("PG_USER", "pedro");
  11.    define (PG_PASS", "pass");
  12.   define("PG_PORT", "5432");
  13.   define("TABLE",   "arcos");
  14.  
  15.   // Retrieve start point
  16.   $start = split(' ',$_REQUEST['startpoint']);
  17.   $startPoint = array($start[0], $start[1]);
  18.  
  19.   // Retrieve end point
  20.   $end = split(' ',$_REQUEST['finalpoint']);
  21.   $endPoint = array($end[0], $end[1]);
  22.  
  23. ?>
  24.  
  25.  
  26.  
  27.  
  28.  
  29. <!--determinar inicio e fim da rota-->
  30.  
  31.  
  32. <?php
  33.  
  34.    // Find the nearest point
  35.    $startpoint = findNearestpoint($startPoint);
  36.    $endpoint   = findNearestpoint($endPoint);
  37.  
  38.    // FUNCTION findNearestpoint
  39.    function findNearestpoint($lonlat) {
  40.  
  41.       // Connect to database
  42.       $con = pg_connect("dbname=".PG_DB." host=".PG_HOST." user=".PG_USER "password=".PG_PASS);
  43.  
  44.       $sql = "SELECT id, the_geom,
  45.                   distance(the_geom, GeometryFromText(
  46.                        'POINT(".$lonlat[0]." ".$lonlat[1].")', 27492)) AS dist
  47.                  FROM vertices_tmp  
  48.                  WHERE the_geom && setsrid(
  49.                        'BOX3D(".($lonlat[0]-0.1)."
  50.                               ".($lonlat[1]-0.1).",
  51.                               ".($lonlat[0]+0.1)."
  52.                               ".($lonlat[1]+0.1).")'::box3d, 27492)
  53.                  ORDER BY dist LIMIT 1";
  54.  
  55.       $query = pg_query($con,$sql);  
  56.  
  57.       $edge['id']      = pg_fetch_result($query, 0, 0);  
  58.       $edge['source']   = pg_fetch_result($query, 0, 1);  
  59.       $edge['target']   = pg_fetch_result($query, 0, 2);  
  60.       $edge['the_geom'] = pg_fetch_result($query, 0, 3);  
  61.  
  62.       // Close database connection
  63.       pg_close($con);
  64.  
  65.       return $edge;
  66.    }
  67.    
  68. ?>
  69.  
  70.  
  71.  
  72. <!--query de custo-->
  73.  
  74. <?php
  75.  
  76.    // Select the routing algorithm
  77.    switch($_REQUEST['method']) {
  78.  
  79.     case 'SP' : // Shortest Path
  80.  
  81.        
  82.             §sql = "SELECT arcos.id, ST_AsGeoJSON(arcos.the_geom) AS geojson, length(arcos.the_geom) AS length, ".TABLE.".id
  83.             FROM ".TABLE."
  84.             JOIN
  85.             (SELECT * FROM shortest_path('
  86.             SELECT id,
  87.             source::int4 AS source,
  88.             target::int4 AS target,
  89.             length::double precision AS cost
  90.             FROM ".TABLE."',
  91.             ".$startpoint['id'].",".$endpoint['id'].",false,false)) AS rota
  92.             ON
  93.             ".TABLE.".id = rota.edge_id;";
  94.            
  95.            
  96.    
  97.     break;
  98.        
  99.      
  100.  
  101.    } // close switch
  102.  
  103.    // Connect to database
  104.    $dbcon = pg_connect("dbname=".PG_DB." host=".PG_HOST." user=".PG_USER "password=".PG_PASS);
  105.  
  106.    // Perform database query
  107.    $query = pg_query($dbcon,$sql);
  108.    
  109. ?>
  110.  
  111.  
  112.  
  113. <!--retornar resultado como geojson-->
  114.  
  115.  
  116.  
  117. <?php
  118.  
  119.    // Return route as GeoJSON
  120.    $geojson = array(
  121.       'type'      => 'FeatureCollection',
  122.       'features'  => array()
  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