karomap/laravel-geo

Laravel 5.* 的空间 OGC 对象集成

2.0.1 2019-08-28 17:51 UTC

This package is auto-updated.

Last update: 2024-09-25 15:02:15 UTC


README

Latest Version on Packagist StyleCI Total Downloads CircleCI

Geo Laravel

这是一个基于 elevenlab/laravel-geo 的分支,经过一些修改以兼容 Laravel 5.4+

注意:在此版本中,您可以使用具有 SRID 的 geometry 或 geography 列类型使用 PostGIS。

特性

  • Laravel 5.4+ 上的 GeoSpatial 集成
    • 使用 Schema 和迁移创建地理空间列
    • 使用直接 OpenGeoConsortium 空间对象 (此包依赖 PHP-OGC) 保存和检索地理空间属性
    • 使用 Laravel fluent 查询构建器直接构建空间查询
    • 支持类型:Point、MultiPoint、Linestring、MultiLinestring、Polygon、MultiPolygon、GeometryCollection
  • 支持驱动程序
    • Postgres:PostGIS 扩展 (几何类型)
    • MySql:空间数据扩展 (几何类型)

感谢 laravel-postgis 的原始工作。

安装 & 配置

  1. 使用 composer 安装
$ composer require karomap/laravel-geo
  1. config/app.php 的服务提供者部分 ('providers' 数组) 中替换以下行
Illuminate\Database\DatabaseServiceProvider::class,

为以下行

Karomap\GeoLaravel\DatabaseServiceProvider::class
  1. 如果需要,在 config/app.php 的别名部分 ('aliases' 数组) 中添加以下行
'GeoModel'      => Karomap\GeoLaravel\Eloquent\Model::class,

快速文档

创建具有空间引用的表

要向迁移中添加地理空间字段,您可以使用以下方法

  • point、multipoint、linestring、multilinestring、polygon、multipolygon、geometrycollection

示例(注意:该方案过于简化)

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Migrations\Migration;
use Karomap\GeoLaravel\Database\Schema\Blueprint; // Replace "Illuminate\Database\Schema\Blueprint"

class CreateNationsTable extends Migration {
    public function up() {
        Schema::create('nations', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->polygon('national_bounds');
            $table->point('capital');
            $table->multipolygon('regions_bounds');
            $table->multipoint('regions_capitals');
            $table->linestring('highway');
        });
    }
}

向模型添加空间属性

为了在 CRUD 操作期间动态处理地理空间属性,您需要

  • 用自定义模型替换 Eloquent 模型抽象对象
  • 定义哪些属性属于哪些地理空间类型,定义 $geometries 属性(您可以在 这里 找到可用的类型)
<?php namespace App;

use Karomap\GeoLaravel\Eloquent\Model as GeoModel;

class Country extends GeoModel
{
    protected $table = "countries";

    protected $geometries = [
        "polygons" =>   ['national_bounds'],
        "points" => ['capital'],
        "multipolygons" => ['regions_bounds'],
        "multipoints" => ['regions_capitals'],
        "linestrings" => ['highway']
    ];
}

操作模型的地理空间属性

<?php
use Karomap\PHPOGC\DataTypes\Point as Point;
use Karomap\PHPOGC\DataTypes\Linestring as Linestring;
use Karomap\PHPOGC\DataTypes\Polygon as Polygon;

$rome = new Point(41.9102415,12.3959149);
$milan = new Point(45.4628328,9.1076927);
$naples = new Point(40.8540943,14.1765626);
$regions_capital = new MultiPoint([$rome, $milan, $naples, ....]);
$italy_bounds = new Polygon([new LineString(getPointArrayOfItalianBounds())]);
$lazio = new LineString(getPointArrayOfLazioBounds());
$campania = new LineString(getPointArrayOfCampaniaBounds());
$lombardia = new LineString(getPointArrayOfLombardiaBounds());
$molise = new LineString(getPointArrayOfMoliseBounds()); # raise MoliseNotFoundException
$regions_bounds = new MultiPolygon([$lazio, $campania, $lombardia, ....]);
$a1 = new LineString(getPointArrayOfA1());

$italy = Country::create([
    'name' => 'Italy',
    'capital' => $rome,
    'national_bounds' => $italy_bounds,
    'regions_bounds' => $regions_bounds,
    'regions_capitals' => $regions_capital,
    'highway' => $a1
]);

$italy = Country::whereName('Italy')->first();
echo get_class($italy->capital); // Karomap\PHPOGC\DataTypes\Point
echo get_class($italy->national_bounds); // Karomap\PHPOGC\DataTypes\Polygon
echo get_class($italy->regions_bounds); // Karomap\PHPOGC\DataTypes\Polygon
echo get_class($italy->regions_capitals); // Karomap\PHPOGC\DataTypes\MultiPoint
echo get_class($italy->highway); // Karomap\PHPOGC\DataTypes\LineString

构建查询

有两种不同的方法组可用,一种用于使用底层数据库引擎在现有对象上执行空间操作,另一种用于构建流畅查询并在数据库驻留数据上执行操作。

给定两个 OGCObjects,您可以执行以下操作

  • 交集

  • 差集

  • 包含

  • 相交

  • 接触

  • 重叠

  • 质心

  • 距离

  • 等于

给定一个 Illuminate Query Builder 对象,您可以使用

  • whereEquals

  • whereNotEquals

  • orWhereEquals

  • orWhereNotEquals

  • whereContains

  • whereNotContains

  • orWhereContains

  • orWhereNotContains

  • whereIntersects

  • whereNotIntersects

  • orWhereIntersects

  • orWhereNotIntersects

  • whereTouches

  • whereNotTouches

  • orWhereTouches

  • orWhereNotTouches

  • whereOverlaps

  • whereNotOverlaps

  • orWhereOverlaps

  • orWhereNotOverlaps

待办事项

  • 改进文档
    • 为“构建查询”部分添加示例
    • 添加手动安装指南
  • 添加缺失的 ST_functions