phaza / laravel-postgis
Requires
- php: >=7.1
- bosnadev/database: ^0.21|dev-master
- 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
- dev-master
- 6.1
- 6.0
- 5.4.2
- 5.4.1
- 5.4
- 5.3
- 5.2.1
- 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-04-10 09:51:14 UTC
README
已弃用
考虑使用: https://github.com/clickbar/laravel-magellan
特性
- 使用几何类而不是数组。
$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 新的 Package Discovery。
如果您使用的是 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