Primeiro deve-se criar um Model. Com a função geojson_output(), observem que a geometria da tabela é convertida para GeoJSON e reprojetada para WGS-84:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
class Rodovias_model extends CI_Model { | |
private $_table_name = 'rodovias'; | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
public function geojson_output() | |
{ | |
$sql = "SELECT nome, codigo, trecho, kminicial, kmfinal, situacao_fisica, | |
st_asgeojson(st_transform(geom, 4326)) AS geojson | |
FROM {$this->_table_name}"; | |
$query = $this->db->query($sql); | |
if($query->num_rows() > 0) | |
{ | |
return $query->result_array(); | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
} | |
/* End of file rodovias_model.php */ | |
/* Location: ./application/models/rodovias_model.php */ |
Em seguida o Controller, que vai receber os dados do Model e enviar para a View:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
class Rodovias extends CI_Controller { | |
public function __construct() | |
{ | |
parent::__construct(); | |
$this->load->model('rodovias_model', 'model'); | |
} | |
public function geojson_link() | |
{ | |
$data['geojson_data'] = $this->model->geojson_output(); | |
$this->load->view('rodovias_geojson', $data); | |
} | |
} | |
/* End of file rodovias.php */ | |
/* Location: ./application/controllers/rodovias.php */ |
Por último a View, que renderiza os dados no formato GeoJSON:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
header('Content-Type: text/javascript; charset=UTF-8'); | |
function escapeJsonString($value) | |
{ | |
$escapers = array("\\", "/", "\"", "\n", "\r", "\t", "\x08", "\x0c"); | |
$replacements = array("\\\\", "\\/", "\\\"", "\\n", "\\r", "\\t", "\\f", "\\b"); | |
$result = str_replace($escapers, $replacements, $value); | |
return $result; | |
} | |
$output = ''; | |
$rowOutput = ''; | |
foreach ($geojson_data as $row) | |
{ | |
$rowOutput = (strlen($rowOutput) > 0 ? ',' : '') . '{"type": "Feature", "geometry": ' . $row['geojson'] . ', "properties": {'; | |
$props = ''; | |
$id = ''; | |
foreach ($row as $key => $val) | |
{ | |
if ($key != "geojson") | |
{ | |
$props .= (strlen($props) > 0 ? ',' : '') . '"' . $key . '":"' . escapeJsonString($val) . '"'; | |
} | |
if ($key == "id") | |
{ | |
$id .= ',"id":"' . escapeJsonString($val) . '"'; | |
} | |
} | |
$rowOutput .= $props . '}'; | |
$rowOutput .= $id; | |
$rowOutput .= '}'; | |
$output .= $rowOutput; | |
} | |
$layer = 'var rodovias = '; | |
$output = $layer . '{ "type": "FeatureCollection", "features": [ ' . $output . ' ]}'; | |
echo $output; |
Para ver os dados da tabela neste formato (Figura 1), é necessário acessar a url que contém o "controller/método" utilizado, no meu caso:
http://localhost/sirh/rodovias/geojson_link
![]() |
Figura 1 - Resultado da conversão |
Seguindo a documentação do Leaflet é bastante simples carregar os dados neste formato. O resultado pode ser conferido na Figura 2.
![]() |
Figura 2 - GeoJson exibido na interface do Leaflet |