Advertisement
Guest User

Untitled

a guest
Jul 26th, 2012
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.91 KB | None | 0 0
  1.  
  2. <?php
  3.  
  4.    
  5.    
  6.    
  7.    
  8.    // obter ponto de ínicio
  9.    $start = split(' ',$_REQUEST['startpoint']);
  10.    $startPoint = array($start[0], $start[1]);
  11.  
  12.    // obter ponto de destino
  13.    $end = split(' ',$_REQUEST['finalpoint']);
  14.    $endPoint = array($end[0], $end[1]);
  15.    
  16.  
  17.  
  18.  
  19.    // encontrar vertice mais próximo
  20.    $startpoint = findNearestpoint($startPoint);
  21.    $endpoint   = findNearestpoint($endPoint);
  22.    
  23.    
  24.  
  25.    // função findNearestpoint
  26.    function findNearestpoint($lonlat) {
  27.  
  28.    
  29.    
  30.     // Connectar à base
  31.    
  32.     $con = pg_connect("host=localhost dbname=****** port =5432 user=***** password=****");
  33.      
  34.      
  35.  
  36.      // query para encontar o ponto mais próximo
  37.      //Uma vez que o mapa esta no EPSG 900913 e a base no EPSG27492 foi necessário converter as coordenadas
  38.      
  39.     $sql = "SELECT id, the_geom,
  40.     distance(the_geom, GeometryFromText(ST_Transform(ST_SetSRID(ST_Point(".$lonlat[0].", ".$lonlat[1]."),900913),27492),27492)) As dist
  41.     FROM vertices_tmp
  42.     ORDER BY dist LIMIT 1";
  43.                
  44.                
  45.                
  46.                
  47.                
  48.        
  49.                
  50.  
  51.       $query = pg_query($con,$sql);  
  52.  
  53.       $edge['id']      = pg_fetch_result($query, 0, 0);  
  54.       $edge['the_geom'] = pg_fetch_result($query, 0, 1);  
  55.      
  56.      
  57.  
  58.       // fechar conexão à base de dados
  59.       pg_close($con);
  60.  
  61.       return $edge;
  62.    }
  63.    
  64.  
  65.  
  66.    
  67.    
  68.    // Selecionar o algoritmo para calcular o percurso
  69.    
  70.    
  71.    switch($_REQUEST['method']) {
  72.  
  73.     case 'SP1' :
  74.  
  75.        
  76.             $sql = "SELECT arcos.n_passeio, ST_AsGeoJSON(ST_Transform(arcos.the_geom,900913)) AS geojson, length(arcos.the_geom) AS length, arcos.n_passeio
  77.                     FROM arcos
  78.                     JOIN
  79.                     (SELECT * FROM shortest_path('SELECT id,source::int4 AS source,target::int4 AS target,length::double precision AS cost FROM arcos',
  80.                    
  81.                 ".$startpoint['id'].",".$endpoint['id'].",false,false)) AS rota
  82.                     ON
  83.                     arcos.n_passeio = rota.edge_id;";
  84.            
  85.            
  86.    
  87.             break;
  88.        
  89.        
  90.        
  91.        
  92.     case 'SP2' :
  93.  
  94.             $sql = "SELECT arcos.n_passeio, ST_AsGeoJSON(ST_Transform(arcos.the_geom,900913)) AS geojson, length(arcos.the_geom) AS length, arcos.n_passeio
  95.                     FROM arcos
  96.                     JOIN
  97.                     (SELECT * FROM shortest_path('SELECT id,source::int4 AS source,target::int4 AS target,length::double precision AS cost FROM arcos',
  98.                    
  99.                 ".$startpoint['id'].",".$endpoint['id'].",false,false)) AS rota
  100.                     ON
  101.                     arcos.n_passeio = rota.edge_id;";
  102.                    
  103.                    
  104.                    
  105.             break;      
  106.            
  107.            
  108.            
  109.            
  110.            
  111.     case 'SP3' :
  112.  
  113.             $sql = "SELECT arcos.n_passeio, ST_AsGeoJSON(ST_Transform(arcos.the_geom,900913)) AS geojson, length(arcos.the_geom) AS length, arcos.n_passeio
  114.                     FROM arcos
  115.                     JOIN
  116.                     (SELECT * FROM shortest_path('SELECT id,source::int4 AS source,target::int4 AS target,length::double precision AS cost FROM arcos',
  117.                    
  118.                 ".$startpoint['id'].",".$endpoint['id'].",false,false)) AS rota
  119.                     ON
  120.                     arcos.n_passeio = rota.edge_id;";
  121.                    
  122.                    
  123.                    
  124.             break;  
  125.        
  126.  
  127.  
  128.    } // fechar switch
  129.    
  130.    
  131.      
  132.    
  133.    // Conectar à base
  134.    $dbcon = pg_connect("host=localhost dbname=****** port =5432 user=******* password=******");
  135.    
  136.    
  137.  
  138.    // Realizar a query
  139.    $query = pg_query($dbcon,$sql);
  140.    
  141.  
  142.    
  143.    
  144.    
  145.  
  146.  
  147.    // Retornar a rota como GeoJSON
  148.    $geojson = array(
  149.       'type'      => 'FeatureCollection',
  150.       'features'  => array()
  151.    );
  152.  
  153.  
  154.  
  155.  
  156.  
  157.    // Adicionar as linhas do percurso ao array geojson
  158.    while($edge=pg_fetch_assoc($query)) {  
  159.  
  160.       $feature = array(
  161.          'type' => 'Feature',
  162.          'geometry' => json_decode($edge['geojson'], true),
  163.        
  164.         'crs' => array(
  165.             'type' => 'EPSG',
  166.             'properties' => array('code' => '900913')
  167.          ),
  168.  
  169.  
  170.  
  171.          'properties' => array(
  172.             'id' => $edge['id'],
  173.             'length' => $edge['length']
  174.          )
  175.       );
  176.      
  177.      
  178.      
  179.      
  180.      
  181.       //adicionar o array dos percursos ao array collection
  182.      array_push($geojson['features'], $feature);
  183.    }
  184.  
  185.    
  186.        
  187.    
  188.  
  189.    // fechar a conexão à base de dados
  190.    pg_close($dbcon);
  191.  
  192.    
  193.    
  194.    // Retornar o resultado
  195.    header('Content-type: application/json',true);
  196.    echo json_encode($geojson);
  197.    
  198.    
  199.    
  200.    
  201.    
  202.    
  203.    
  204.    
  205.    
  206.    //points
  207.    
  208.    
  209.    
  210.    
  211.    $sql_pontos = "SELECT pontos.n_passeio, ST_AsGeoJSON(ST_Transform(pontos.the_geom,900913)) AS geojson_pontos
  212.                     FROM pontos
  213.                     JOIN
  214.                     (SELECT * FROM shortest_path('SELECT id,source::int4 AS source,target::int4 AS target,length::double precision AS cost FROM arcos',
  215.                     ".$startpoint['id'].",".$endpoint['id'].",false,false)) AS rota
  216.                     ON
  217.                     pontos.n_passeio = rota.edge_id";
  218.    
  219.    
  220.    
  221.    
  222.    // Conectar à base para os pontos
  223.    $dbcon = pg_connect("host=localhost dbname=****** port =5432 user=***** password=*****");
  224.    
  225.    
  226.    
  227.     //realizar a query dos pontos
  228.    
  229.     $query_pontos = pg_query($dbcon,$sql_pontos);
  230.    
  231.    
  232.    
  233.     // Retornar os pontos como geojson
  234.    
  235.    $geojson_pontos = array(
  236.       'type'      => 'FeatureCollection',
  237.       'features'  => array()
  238.    );
  239.    
  240.    
  241.    
  242.    
  243.      
  244.      // Adicionar os pontos dos pontos ao array geojson
  245.      
  246.    while($point=pg_fetch_assoc($query_pontos)) {  
  247.  
  248.       $feature_pontos = array(
  249.          'type' => 'Feature',
  250.          'geometry' => json_decode($point['geojson_pontos'], true),
  251.        
  252.         'crs' => array(
  253.             'type' => 'EPSG',
  254.             'properties' => array('code' => '900913')
  255.          ),
  256.  
  257.  
  258.  
  259.          'properties' => array(
  260.             'id' => $point['id']
  261.          )
  262.       );
  263.    
  264.    
  265.     //pontos
  266.      array_push($geojson_pontos['features'], $feature_pontos);
  267.    }
  268.    
  269.    
  270.    
  271.    
  272.      // fechar a conexão à base de dados
  273.     pg_close($dbcon);
  274.    
  275.    
  276.    
  277.    
  278.       //pontos
  279.    header('Content-type: application/json',true);
  280.    echo json_encode($geojson_pontos);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement