Laravel postgis extension
, (*1)
Solemnly declare
修改位置
- 修改了readme.md 关于lumen5.4 安装的注册服务,需要注册两个服务(见下文)。
- 添加了几何数据迁移命令
$table->geometry('geom')
;
- 添加 $location1->location = new GeomPoint(37.422009, -122.084047); 实现gis几何数据直接利用模型插入
Features
- Work with geometry classes instead of arrays. (
$myModel->myPoint = new Point(1,2)
)
- Adds helpers in migrations. (
$table->polygon('myColumn')
)
Future plans
- Geometry functions on the geometry classes (contains(), equals(), distance(), etc… (HELP!))
Versions
- Use for Laravel
5.3.*
;5.4.*
- Use for lumen
5.3.*
;5.4.*
Installation
composer require shaozeming/lumen-postgis 'dev-master'
laravel Next add the DatabaseServiceProvider to your config/app.php
file., (*2)
'Shaozeming\LumenPostgis\DatabaseServiceProvider',
That's all., (*3)
Lumen Next add the DatabaseServiceProvider to your bootstrap/app.php
file., (*4)
$app->register(Bosnadev\Database\DatabaseServiceProvider::class); //多添加这个后,就可以解决问题。
$app->register(Shaozeming\LumenPostgis\DatabaseServiceProvider::class);
That's all., (*5)
Usage
First of all, make sure to enable postgis., (*6)
CREATE EXTENSION postgis;
To verify that postgis is enabled, (*7)
SELECT postgis_full_version();
Migrations
Now create a model with a migration by running, (*8)
php artisan make:model Location
If you don't want a model and just a migration run, (*9)
php artisan make:migration create_locations_table
Open the created migrations with your editor., (*10)
use Illuminate\Database\Migrations\Migration;
use Shaozeming\LumenPostgis\Schema\Blueprint;
class CreateLocationsTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('locations', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->string('address')->unique();
$table->point('location');
$table->geometry('geom');
$table->polygon('polygon');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('locations');
}
}
Available blueprint geometries:, (*11)
- point
- geometry
- multipoint
- linestring
- multilinestring
- polygon
- multipolygon
- geometrycollection
other methods:, (*12)
- enablePostgis
- disablePostgis
Models
All models which are to be PostGis enabled must use the PostgisTrait., (*13)
You must also define an array called $postgisFields
which defines
what attributes/columns on your model are to be considered geometry objects., (*14)
use Illuminate\Database\Eloquent\Model;
use Shaozeming\LumenPostgis\Eloquent\PostgisTrait;
use Shaozeming\LumenPostgis\Geometries\Point;
class Location extends Model
{
use PostgisTrait;
protected $fillable = [
'name',
'address'
];
protected $postgisFields = [
'location',
'polygon',
];
}
$location1 = new Location();
$location1->name = 'Googleplex';
$location1->address = '1600 Amphitheatre Pkwy Mountain View, CA 94043';
$location1->location = new Point(37.422009, -122.084047);
$location1->geom = new GeomPoint(37.422009, -122.084047); //这个可以执行成功实现ORM操作gis几何数据。
$location1->save();
$location2 = Location::first();
$location2->location instanceof Point // true
Available geometry classes:, (*15)
- Point
- Geometry
- MultiPoint
- LineString
- MultiLineString
- Polygon
- MultiPolygon
- GeometryCollection