khama_zaspyan / laravel-postgis
为 Laravel 提供的 PostGIS 扩展。旨在使从 Laravel 模型中处理几何形状变得容易
Requires
- php: >=7.1
- geo-io/wkb-parser: ^1.0
- illuminate/database: ^9.0|^10.0
- jmikola/geojson: ^1.0
Requires (Dev)
- illuminate/pagination: ^9.0
- mockery/mockery: ^1.3
- phpunit/phpunit: ^9.0.0
This package is not auto-updated.
Last update: 2024-10-03 11:28:05 UTC
README
仅适用于 Laravel 9+ 和 PHP 8+
特性
- 使用几何类而不是数组。
$model->myPoint = new Point(1,2); //lat, long
- 在迁移中添加辅助函数。
$table->polygon('myColumn');
安装
composer require mstaack/laravel-postgis
使用
首先,请确保您的数据库已启用 PostGIS - 您可以在 Laravel 迁移或手动通过 SQL 执行此操作。
通过 Laravel 迁移启用 PostGIS
您需要发布迁移以轻松启用 PostGIS
php artisan vendor:publish --provider="Ajthinking\LaravelPostgis\DatabaseServiceProvider" --tag="migrations"
然后运行迁移
php artisan migrate
这些方法可以使用,并且只有在相关的情况下才会启用/禁用 PostGIS 扩展 - 如果 PostGIS 已经启用或未启用,它们不会导致错误。
如果您愿意,可以使用 enablePostgis()
方法,如果 PostGIS 已启用,它将引发错误,以及 disablePostgis()
方法,如果 PostGIS 未启用,它将引发错误。
手动启用 PostGIS
使用 SQL 客户端连接到您的数据库并运行以下命令
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 Ajthinking\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 Ajthinking\LaravelPostgis\Eloquent\PostgisTrait; use Ajthinking\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
致谢
这是从 mstaack/laravel-postgis 的伟大工作中派生出来的。感谢