Quem programa em PHP já deve conhecer ou ter ouvido falar sobre o Laravel. Uma coisa interessante neste framework é que é possível trabalhar com dados espaciais de forma muito prática.
O código abaixo exemplifica a implementação da classe Obra, que no PostGIS é uma tabela espacial do tipo POINT.
Primeiro criamos a migration:
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 | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Database\Migrations\Migration; | |
class CreateobrasTable extends Migration { | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() { | |
Schema::create('obras', function($table){ | |
$table->increments('id'); | |
$table->string('nome_obra'); | |
$table->timestamps(); | |
}); | |
// Adicionando a coluna que armazenará as geometrias do tipo ponto: | |
DB::unprepared("ALTER TABLE obras ADD COLUMN geom GEOMETRY(POINT, 4326)"); | |
} | |
/** | |
* Reverse the migrations. | |
* | |
* @return void | |
*/ | |
public function down() { | |
Schema::drop('obras'); | |
} | |
} |
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 | |
class Obra extends Eloquent { | |
protected $fillable = array('nome_obra', 'geom'); | |
public static $rules = array( | |
'nome_obra'=>'required|min:3', | |
/* OBS: os campos abaixo servem para validação da coluna geom | |
e não existem na tabela Obra */ | |
'longitude'=>'numeric|required', | |
'latitude'=>'numeric|required' | |
); | |
} |
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 | |
class ObrasController extends BaseController { | |
public function getIndex() { | |
$obras = DB::table('obras') | |
->select(DB::raw('ST_X(geom) as lng, ST_Y(geom) AS lat, nome_obra')) | |
->get(); | |
return View::make('obras.index') | |
->with('title', 'Mapa das Obras') | |
->with('obras', $obras); | |
} | |
public function getCreate() { | |
return View::make('obras.create') | |
->with('title', 'Nova Obra'); | |
} | |
public function postCreate() { | |
$validator = Validator::make(Input::all(), Obra::$rules); | |
if(!$validator->passes()) { | |
return Redirect::to('obras/create') | |
->with('message', 'Ocorreu um erro, tente novamente') | |
->withErrors($validator) | |
->withInput(); | |
} else { | |
$x = Input::get('longitude'); | |
$y = Input::get('latitude'); | |
$check = Municipio::checkCoords($x, $y); | |
if($check == false) { | |
return Redirect::to('obras/create') | |
->with('message', 'Coordenadas Inválidas'); | |
} | |
$obra = new Obra; | |
$obra->nome_obra = Input::get('nome_obra'); | |
$obra->geom = DB::raw("ST_GeomFromText('POINT({$x} {$y})', 4326)"); | |
$obra->save(); | |
return Redirect::to('obras/index') | |
->with('message', 'Obra cadastrada com sucesso'); | |
} | |
} | |
} |
![]() |
Figura 1 - Exemplo de 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 | |
class Municipio extends Eloquent { | |
public static function checkCoords($x, $y) { | |
$query = Municipio::whereRaw( | |
"ST_Contains(geom, ST_GeomFromText('POINT($x $y)', 4326))" | |
)->first(); | |
if($query) { | |
return $query; | |
} | |
return false; | |
} | |
} |
Um abraço e até a próxima o/
Nenhum comentário:
Postar um comentário