aeris / spatial
空间数据的表示
v1.3.1
2015-08-31 20:43 UTC
This package is not auto-updated.
Last update: 2024-09-14 17:20:14 UTC
README
空间数据的表示。
安装
composer require aeris/spatial
组件
GeometryConverter
GeometryConverter
组件可用于在不同格式之间转换几何数据。目前支持以下转换:
- GeoJson Polygon 到 WKT
- GeoJson MultiPolygon 到 WKT
可以通过直接使用 Geometry
对象以及通过 ConvertibleGeometryInterface::FromArray()
和 ConvertibleGeometryInterface::toWkt()
方法进行其他转换(参见 Geometry 组件文档)。
示例
use Aeris\Spatial\GeometryConverter; $geoJson = json_encode([ 'type' => 'Polygon', 'coordinates' => [ [ [100, 0], [101, 0], [101, 1], [100, 1], [100, 0], ] ] ]); $wkt = GeometryConverter::geoJsonToWkt($geoJson); $this->assertEquals( 'POLYGON((100 0,101 0,101 1,100 1,100 0))', $wkt );
Geometry
Geometry
组件是空间数据结构的对象表示。实现 \Aeris\Spatial\Geometry\ConvertibleGeometryInterface
的对象可以通过 FromArray(array $data)
和 toWkt()
方法轻松地在数组与 WKT(字符串)格式之间转换。
某些 Geometry
组件可能实现了额外的转换方法。例如,\Aeris\Spatial\Geometry\MultiPolygon
实现了 FromFeatureCollection()
方法,这使得将 GeoJson 特征集合转换为 MultiPolygon
对象变得容易。
示例
以下示例演示了将 GeoJson FeatureCollection 转换为 MultiPolygon 对象的过程。
$geoJson = [ 'type' => 'FeatureCollection', 'features' => [ [ 'type' => 'Feature', 'geometry' => [ 'type' => 'Polygon', 'coordinates' => [ [ [100, 0], [100, 100], [0, 100], [0, 0], [100, 0], ] ] ], ], [ 'type' => 'Feature', 'geometry' => [ 'type' => 'Polygon', 'coordinates' => [ [ [200, 0], [200, 200], [0, 200], [0, 0], [200, 0] ] ] ] ] ], ]; $mPoly = MultiPolygon::FromFeatureCollection($geoJson); $this->assertEquals([ Polygon::FromArray([ [ [100, 0], [100, 100], [0, 100], [0, 0], [100, 0] ] ]), Polygon::FromArray([ [ [200, 0], [200, 200], [0, 200], [0, 0], [200, 0] ] ]) ], $mPoly->getPolygons());
然后您可以使用 MultiPolygon 对象执行 MySQL 空间查询。
$myQuery = 'SELECT * FROM `places` ' . 'WHERE ST_CONTAINS(GeomFromText(' . $mPoly->toWKT() . '), `places`.`point`)'
Util
Aeris\Spatial\Util
命名空间包含一些用于处理空间组件的实用函数。
Util\bearing
返回两个坐标之间的方向(以度为单位)。
$minneapolis = new Coordinate(-93.251953125, 44.9336963896947); $chicago = new Coordinate(-87.71484375, 41.80407814427237); Util\bearing($minneapolis, $chicago); // 125.93766052151 Util\bearing($chicago, $minneapolis); // 309.74293632484
Util\compassDirection
返回两个坐标之间的罗盘方向。
$minneapolis = new Coordinate(-93.251953125, 44.9336963896947); $edenPrarie = new Coordinate(-93.4768295288086, 44.85148787683413); Util\compassDirection($minneapolis, $edenPrarie); // 'SW' Util\compassDirection($edenPrarie, $minneapolis); // 'NE'
可能的返回值
- NNE
- NE
- ENE
- E
- ESE
- SE
- SSE
- S
- SSW
- SW
- WSW
- W
- WNW
- NW
- NNW
- N