定制 roman-franko/yii2-spatial 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

roman-franko/yii2-spatial

最新稳定版本:v1.0.6

Composer 安装命令:

composer require roman-franko/yii2-spatial

包简介

Yii2 ActiveRecord supporting MySQL spatial data

README 文档

README

ActiveRecord with spatial attributes. Those attributes are transformed from the internal MySQL format to GeoJSON format after finding, and vice versa before storing.

Yii-spatial can also be used to find the model or models which are nearest to a given location.

Notice that this extension is intended to be used with a MySQL or MariaDB database exclusively.

Version 1.1.0 is compatible with MySQL 5.7 and MariaDB 10.3.

Installation

Install Yii2-spatial with Composer. Either add the following to the require section of your composer.json file:

"romanfranko/yii2-spatial": "*"

Or run:

composer require romanfranko/yii2-spatial "*"

You can manually install Yii2-spatial by downloading the source in ZIP-format.

Usage

Simply use a romanfranko\spatial\ActiveRecord as base class for your models, like so:

<?php

use romanfranko\spatial\ActiveRecord;

class MySpatialModel extends ActiveRecord
{
    // ...
}

Notice: if you override find() in a romanfranko\spatial\ActiveRecord-derived class, be sure to return a romanfranko\spatial\ActiveQuery and not an 'ordinary' yii\db\ActiveQuery.

ActiveRecord method

featureProperties()

public function featureProperties($field, $geometry)

Override this function to add properties to the GeoJSON encoded attribute.

  • $field is the attribute name.
  • $geometry is an array with the GeoJSON-information, like decoded JSON.

The default implementation adds the ActiveRecord's primary key as the property 'id'.

ActiveQuery method

nearest()

public function nearest($from, $attribute, $radius)

Change the query so that it finds the model(s) nearest to the point given by $from.

  • $from - string|array
    • string: GeoJSON representation of search Point or Feature.
    • array: location in the form [<lng>, <lat>] (two floats).
  • $attribute - string attribute name of Point in the model.
  • $radius - number search radius in kilometers. Default 100.

Example usages:

$here = [4.9, 52.3];     // longitude and latitude of my place

$here2 = '{"type":"Point","coordinates":[4.9,52.3]}';	// another representation
 

$nearestModel = <model>::find()->nearest($here, <attributeName>, 200)->one();    // search radius is 200 km

$fiveNearestModels =  <model>::find()->nearest($here, <attributeName>)->limit(5)->all();	// search radius is 100 km (default)

$dataProvider = new ActiveDataProvider([ 'query' => <model>::find()->nearest($here, <attributeName>) ]);

Thanks

  • fpolito for finding a very subtle bug.

统计信息

  • 总下载量: 40
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 9
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-10-20