lscortesc / laravel-postgis
laravel 的 Postgis 扩展。旨在简化从 laravel 模型处理几何数据的工作
Requires
- php: ^7.3
- geo-io/wkb-parser: ^1.0
- illuminate/database: ^8.0
- jmikola/geojson: ^1.0
- lscortesc/database: ^0.21
Requires (Dev)
- illuminate/pagination: ^8.0
- mockery/mockery: ^1.3
- phpunit/phpunit: ^8.5
- dev-master
- 5.2
- 5.1.x-dev
- 5.1
- 5.0
- 4.0.1
- 4.0
- 3.6
- 3.5
- 3.4
- 3.3.2
- 3.3.1
- 3.3
- 3.2
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1
- 3.0
- 2.19
- 2.18
- 2.17
- 2.16
- 2.15
- 2.14
- 2.13
- 2.12
- 2.11
- 2.10
- 2.9
- 2.8
- 2.7
- 2.6
- 2.5
- 2.4
- 2.3
- 2.2
- 2.1
- 2.0
- 1.2
- 1.1
- 1.0
- 0.2
- 0.1
- dev-bugfix/multipoint-one-point
- dev-revert-90-patch-1
- dev-add-query-scopes
- dev-fix-for-5.1-connection
This package is auto-updated.
Last update: 2024-09-29 05:22:32 UTC
README
特性
- 使用几何类而不是数组。
$model->myPoint = new Point(1,2); //lat, long
- 在迁移中添加辅助工具。
$table->polygon('myColumn');
警告
此包已移交给新所有者,并很快将支持 Laravel 6/7/8 和 PHP 7!
替换所有对新命名空间的新引用
MStaack\LaravelPostgis
感谢
如果你精通 Laravel 包和 Postgres/Postgis,请考虑贡献!我们正在寻找任何想要帮忙的人!
安装
- 使用 3.* 版本适用于 Laravel 5
composer require "mstaack/laravel-postgis:3.*"
- 使用 5.* 版本适用于 Laravel 6/7/8
composer require mstaack/laravel-postgis
对于 Laravel >=5.5,以上步骤即可。此包支持 Laravel 5.5 新的 包发现。
如果你使用的是 Laravel < 5.5,你还需要在 config/app.php
文件中添加 DatabaseServiceProvider。
'MStaack\LaravelPostgis\DatabaseServiceProvider',
使用方法
首先,确保已启用 postgis。
CREATE EXTENSION postgis;
为了验证 postgis 是否已启用
SELECT postgis_full_version();
迁移
现在通过运行以下命令创建一个带有迁移的模型
php artisan make:model Location
如果你不想创建模型,只想创建迁移,则运行
php artisan make:migration create_locations_table
使用你的编辑器打开创建的迁移文件。
use Illuminate\Database\Migrations\Migration; use MStaack\LaravelPostgis\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'); // GEOGRAPHY POINT column with SRID of 4326 (these are the default values). $table->point('location2', 'GEOGRAPHY', 4326); // GEOGRAPHY POINT column with SRID of 4326 with optional parameters. $table->point('location3', 'GEOMETRY', 27700); // GEOMETRY column with SRID of 27700. $table->polygon('polygon'); // GEOGRAPHY POLYGON column with SRID of 4326. $table->polygon('polygon2', 'GEOMETRY', 27700); // GEOMETRY POLYGON column with SRID of 27700. $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::drop('locations'); } }
可用的蓝图几何形状
- point
- multipoint
- linestring
- multilinestring
- polygon
- multipolygon
- geometrycollection
其他方法
- enablePostgis
- disablePostgis
模型
所有需要启用 PostGis 的模型 必须 使用 PostgisTrait。
你还必须定义一个名为 $postgisFields
的数组,该数组定义了你的模型中哪些属性/列应该被视为几何对象。默认情况下,所有属性都是 geography
类型。如果你想使用带有自定义 SRID 的 geometry
,你必须定义一个名为 $postgisTypes
的数组。此关联数组的键必须与 $postgisFields
中的条目匹配(所有缺失的键默认为 geography
),值也是关联数组。它们必须有两个键:geomtype
,可以是 geography
或 geometry
,和 srid
,这是所需的 SRID。 注意:自定义 SRID 仅支持 geometry
,不支持 geography
。
use Illuminate\Database\Eloquent\Model; use MStaack\LaravelPostgis\Eloquent\PostgisTrait; use MStaack\LaravelPostgis\Geometries\Point; class Location extends Model { use PostgisTrait; protected $fillable = [ 'name', 'address' ]; protected $postgisFields = [ 'location', 'location2', 'location3', 'polygon', 'polygon2' ]; protected $postgisTypes = [ 'location' => [ 'geomtype' => 'geography', 'srid' => 4326 ], 'location2' => [ 'geomtype' => 'geography', 'srid' => 4326 ], 'location3' => [ 'geomtype' => 'geometry', 'srid' => 27700 ], 'polygon' => [ 'geomtype' => 'geography', 'srid' => 4326 ], 'polygon2' => [ 'geomtype' => 'geometry', 'srid' => 27700 ] ] } $linestring = new LineString( [ new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 0), new Point(0, 0) ] ); $location1 = new Location(); $location1->name = 'Googleplex'; $location1->address = '1600 Amphitheatre Pkwy Mountain View, CA 94043'; $location1->location = new Point(37.422009, -122.084047); $location1->location2 = new Point(37.422009, -122.084047); $location1->location3 = new Point(37.422009, -122.084047); $location1->polygon = new Polygon([$linestring]); $location1->polygon2 = new Polygon([$linestring]); $location1->save(); $location2 = Location::first(); $location2->location instanceof Point // true
可用的几何类
- Point
- MultiPoint
- LineString
- MultiLineString
- Polygon
- MultiPolygon
- GeometryCollection