Wallogit.com
2017 © Pedro Peláez
Representations of spatial data
Representations of spatial data., (*1)
composer require aeris/spatial
The GeometryConverter component may be used to convert geometry data between different formats. The following conversions are currently supported:, (*2)
Additional conversions can be made by using Geometry objects directly, via the ConvertibleGeometryInterface::FromArray() and ConvertibleGeometryInterface::toWkt() methods (see Geometry component documentation), (*3)
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 components are object representations of spatial data structures. Objects implementing \Aeris\Spatial\Geometry\ConvertibleGeometryInterface may be easily converted between array and WKT (string) formats using the FromArray(array $data) and toWkt() methods, (*4)
Some Geometry components may implement additional conversion methods. For example, \Aeris\Spatial\Geometry\MultiPolygon implements a FromFeatureCollection() method, which makes it easy to convert a GeoJson feature collection to a MultiPolygon object., (*5)
The following example demonstrates converting a GeoJson FeatureCollection into a MultiPolygon object., (*6)
$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());
You could then use the MultiPolygon object to execute a MySql spatial query:, (*7)
$myQuery = 'SELECT * FROM `places` ' .
'WHERE ST_CONTAINS(GeomFromText(' . $mPoly->toWKT() . '), `places`.`point`)'
The Aeris\Spatial\Util namespace contains some utility function for working with spatial components., (*8)
Util\bearingReturns the direction (in degrees) between two coordinates., (*9)
$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\compassDirectionReturns the compass rose direction between two coordinates., (*10)
$minneapolis = new Coordinate(-93.251953125, 44.9336963896947); $edenPrarie = new Coordinate(-93.4768295288086, 44.85148787683413); Util\compassDirection($minneapolis, $edenPrarie); // 'SW' Util\compassDirection($edenPrarie, $minneapolis); // 'NE'
Possible return values:, (*11)