mohamedhabibwork / laravel-postgis
为 Laravel 提供的 PostGIS 扩展。旨在简化 Laravel 模型中的几何体操作
Requires
- php: >=7.1
- bosnadev/database: *
- geo-io/wkb-parser: ^1.0
- illuminate/database: ^6.0|^7.0|^8.0|^9.0|^10.0
- jmikola/geojson: ^1.0
Requires (Dev)
- illuminate/pagination: ^6.0|^7.0|^8.0|^9.0|^10.0
- mockery/mockery: ^1.3
- phpunit/phpunit: >=8.5.23
This package is auto-updated.
Last update: 2024-09-17 12:00:36 UTC
README
特性
- 使用几何类代替数组。
$model->myPoint = new Point(1,2); //lat, long
- 在迁移中添加助手函数。
$table->polygon('myColumn');
警告
此包已移交给新所有者,并计划很快支持 Laravel 6/7/8/9 和 PHP 7!
替换所有对新的命名空间引用
MStaack\LaravelPostgis
感谢
熟悉 Laravel 包和 Postgres/Postgis?考虑贡献!我们正在寻找任何想要帮助的人!
安装
- 使用 3.* 为 Laravel 5
composer require "mstaack/laravel-postgis:3.*"
- 使用 5.* 为 Laravel 6/7/8/9
composer require mstaack/laravel-postgis
对于 Laravel >=5.5,这就足够了。此包支持 Laravel 新的 包发现。
如果您使用 Laravel < 5.5,您还需要将 DatabaseServiceProvider 添加到您的 config/app.php
文件中。
'MStaack\LaravelPostgis\DatabaseServiceProvider',
使用
首先,请确保您的数据库已启用 PostGIS - 您可以在 Laravel 迁移或通过 SQL 手动完成此操作。
通过 Laravel 迁移启用 PostGIS
您需要发布迁移以轻松启用 PostGIS
php artisan vendor:publish --provider="MStaack\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 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'); } }
可用的蓝图几何体
- 点
- 多重点
- 线字符串
- 多重线字符串
- 多边形
- 多重多边形
- 几何体集合
其他方法
- 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
可用的几何体类
- 点
- 多重点
- 线字符串
- 多重线字符串
- 多边形
- 多重多边形
- 几何体集合
发布配置
存在一个配置文件,可以覆盖默认值。要将它添加到您的项目中,请运行
php artisan vendor:publish --provider="MStaack\LaravelPostgis\DatabaseServiceProvider" --tag="postgis"
坐标精度
存储/显示坐标的精度可以在配置中进行自定义。
# config/postgis.php return [ ... 'precision' => 6, ];
有关更多信息,请参阅 http://wiki.gis.com/wiki/index.php/Decimal_degrees#Accuracy