2017 © Pedro Peláez
 

yii2-extension yii2-spatial

Yii2 ActiveRecord supporting MySQL spatial data

image

sjaakp/yii2-spatial

Yii2 ActiveRecord supporting MySQL spatial data

  • Sunday, February 25, 2018
  • by sjaakp
  • Repository
  • 2 Watchers
  • 8 Stars
  • 3,314 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 3 Forks
  • 1 Open issues
  • 3 Versions
  • 7 % Grown

The README.md

Yii2-spatial

Latest Stable Version Total Downloads License, (*1)

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

Yii2-spatial can also be used to find the model or models which are nearest to a given location., (*3)

Notice that this extension is intended to be used with a MySQL or MariaDB database exclusively., (*4)

Version 1.1.0 is compatible with MySQL 5.7 and MariaDB 10.3., (*5)

Installation

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

"sjaakp/yii2-spatial": "*", (*7)

Or run:, (*8)

composer require sjaakp/yii2-spatial "*", (*9)

You can manually install Yii2-spatial by downloading the source in ZIP-format., (*10)

Usage

Simply use a sjaakp\spatial\ActiveRecord as base class for your models, like so:, (*11)

<?php

use sjaakp\spatial\ActiveRecord;

class MySpatialModel extends ActiveRecord
{
    // ...
}

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

ActiveRecord method

featureProperties()

public function featureProperties($field, $geometry)

Override this function to add properties to the GeoJSON encoded attribute., (*13)

  • $field is the attribute name.
  • $geometry is an array with the GeoJSON-information, like decoded JSON.
  • Return: array of property => value.

The default implementation adds the ActiveRecord's primary key as the property 'id'., (*14)

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., (*15)

  • $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.
  • Return: $this.

Example usages:, (*16)

$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.

The Versions

25/02 2018

dev-master

9999999-dev

Yii2 ActiveRecord supporting MySQL spatial data

  Sources   Download

MIT

The Requires

 

extension yii2 geo yii mysql data db map spatial

25/02 2018

1.0.1

1.0.1.0

Yii2 ActiveRecord supporting MySQL spatial data

  Sources   Download

MIT

The Requires

 

extension yii2 geo yii mysql data db map spatial

21/11 2015

v1.0.0

1.0.0.0

Yii2 ActiveRecord supporting MySQL spatial data

  Sources   Download

MIT

The Requires

 

extension yii2 geo yii mysql data db map spatial