Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"php": ">=5.5",
"illuminate/database": "~5.0",
"bosnadev/database": "~0.1",
"geo-io/wkb-parser": "~1.0"
"geo-io/wkb-parser": "~1.0",
"jmikola/geojson": "~1.0"
},
"require-dev": {
"phpunit/phpunit": "~4.5",
Expand Down
2 changes: 1 addition & 1 deletion src/Geometries/Geometry.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use GeoIO\WKB\Parser\Parser;
use Phaza\LaravelPostgis\Exceptions\UnknownWKTTypeException;

abstract class Geometry implements GeometryInterface
abstract class Geometry implements GeometryInterface, \JsonSerializable
{
protected static $wkb_types = [
1 => Point::class,
Expand Down
16 changes: 16 additions & 0 deletions src/Geometries/GeometryCollection.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace Phaza\LaravelPostgis\Geometries;

use Countable;
use GeoJson\GeoJson;
use InvalidArgumentException;

class GeometryCollection extends Geometry implements Countable
Expand Down Expand Up @@ -71,4 +72,19 @@ public function count()
{
return count($this->geometries);
}

/**
* Convert to GeoJson GeometryCollection that is jsonable to GeoJSON
*
* @return \GeoJson\Geometry\GeometryCollection
*/
public function jsonSerialize()
{
$geometries = [];
foreach ($this->geometries as $geometry) {
$geometries[] = $geometry->jsonSerialize();
}

return new \GeoJson\Geometry\GeometryCollection($geometries);
}
}
15 changes: 15 additions & 0 deletions src/Geometries/LineString.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,19 @@ public function __toString()
{
return $this->toPairList();
}

/**
* Convert to GeoJson LineString that is jsonable to GeoJSON
*
* @return \GeoJson\Geometry\LineString
*/
public function jsonSerialize()
{
$points = [];
foreach ($this->points as $point) {
$points[] = $point->jsonSerialize();
}

return new \GeoJson\Geometry\LineString($points);
}
}
16 changes: 16 additions & 0 deletions src/Geometries/MultiLineString.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,20 @@ public function count()
{
return count($this->linestrings);
}

/**
* Convert to GeoJson Point that is jsonable to GeoJSON
*
* @return \GeoJson\Geometry\MultiLineString
*/
public function jsonSerialize()
{
$linestrings = [];

foreach ($this->linestrings as $linestring) {
$linestrings[] = $linestring->jsonSerialize();
}

return new \GeoJson\Geometry\MultiLineString($linestrings);
}
}
17 changes: 16 additions & 1 deletion src/Geometries/MultiPoint.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php namespace Phaza\LaravelPostgis\Geometries;

class MultiPoint extends PointCollection implements GeometryInterface
class MultiPoint extends PointCollection implements GeometryInterface, \JsonSerializable
{
public function toWKT()
{
Expand Down Expand Up @@ -36,4 +36,19 @@ public function __toString()
return sprintf('(%s)', $point->toPair());
}, $this->points));
}

/**
* Convert to GeoJson MultiPoint that is jsonable to GeoJSON
*
* @return \GeoJson\Geometry\MultiPoint
*/
public function jsonSerialize()
{
$points = [];
foreach ($this->points as $point) {
$points[] = $point->jsonSerialize();
}

return new \GeoJson\Geometry\MultiPoint($points);
}
}
15 changes: 15 additions & 0 deletions src/Geometries/MultiPolygon.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,19 @@ protected static function assembleParts(array $parts)

return $polygons;
}

/**
* Convert to GeoJson MultiPolygon that is jsonable to GeoJSON
*
* @return \GeoJson\Geometry\MultiPolygon
*/
public function jsonSerialize()
{
$polygons = [];
foreach ($this->polygons as $polygon) {
$polygons[] = $polygon->jsonSerialize();
}

return new \GeoJson\Geometry\MultiPolygon($polygons);
}
}
13 changes: 13 additions & 0 deletions src/Geometries/Point.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php namespace Phaza\LaravelPostgis\Geometries;

use GeoJson\GeoJson;

class Point extends Geometry
{
protected $lat;
Expand Down Expand Up @@ -57,4 +59,15 @@ public function __toString()
{
return $this->getLng() . ' ' . $this->getLat();
}

/**
* Convert to GeoJson Point that is jsonable to GeoJSON
*
* @return \GeoJson\Geometry\Point
*/
public function jsonSerialize()
{
// !!! This will convert LngLat from PostGIS to ISO 6709 LatLng !!!
return new \GeoJson\Geometry\Point([$this->getLat(), $this->getLng()]);
}
}
3 changes: 2 additions & 1 deletion src/Geometries/PointCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
use Illuminate\Contracts\Support\Arrayable;
use InvalidArgumentException;
use IteratorAggregate;
use JsonSerializable;

class PointCollection implements IteratorAggregate, Arrayable, ArrayAccess, Countable
abstract class PointCollection implements IteratorAggregate, Arrayable, ArrayAccess, Countable, JsonSerializable
{
/**
* @var Point[]
Expand Down
15 changes: 15 additions & 0 deletions src/Geometries/Polygon.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,19 @@ public function toWKT()
{
return sprintf('POLYGON(%s)', (string)$this);
}

/**
* Convert to GeoJson Polygon that is jsonable to GeoJSON
*
* @return \GeoJson\Geometry\Polygon
*/
public function jsonSerialize()
{
$linearrings = [];
foreach ($this->linestrings as $linestring) {
$linearrings[] = new \GeoJson\Geometry\LinearRing($linestring->jsonSerialize()->getCoordinates());
}

return new \GeoJson\Geometry\Polygon($linearrings);
}
}
48 changes: 37 additions & 11 deletions tests/Geometries/GeometryCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,29 @@

class GeometryCollectionTest extends BaseTestCase
{
/**
* @var GeometryCollection
*/
private $collection;

protected function setUp()
{
$collection = new LineString(
[
new Point(0, 0),
new Point(0, 1),
new Point(1, 1),
new Point(1, 0),
new Point(0, 0)
]
);

$point = new Point(100, 200);

$this->collection = new GeometryCollection([$collection, $point]);
}


public function testFromWKT()
{
/**
Expand All @@ -21,20 +44,23 @@ public function testFromWKT()

public function testToWKT()
{
$collection = new LineString(
[
new Point(0, 0),
new Point(0, 1),
new Point(1, 1),
new Point(1, 0),
new Point(0, 0)
]
$this->assertEquals(
'GEOMETRYCOLLECTION(LINESTRING(0 0,1 0,1 1,0 1,0 0),POINT(200 100))',
$this->collection->toWKT()
);
}

$point = new Point(100, 200);
public function testJsonSerialize()
{
$this->assertInstanceOf(
\GeoJson\Geometry\GeometryCollection::class,
$this->collection->jsonSerialize()
);

$polygon = new GeometryCollection([$collection, $point]);
$this->assertSame(
'{"type":"GeometryCollection","geometries":[{"type":"LineString","coordinates":[[0,0],[0,1],[1,1],[1,0],[0,0]]},{"type":"Point","coordinates":[100,200]}]}',
json_encode($this->collection->jsonSerialize())
);

$this->assertEquals('GEOMETRYCOLLECTION(LINESTRING(0 0,1 0,1 1,0 1,0 0),POINT(200 100))', $polygon->toWKT());
}
}
30 changes: 21 additions & 9 deletions tests/Geometries/LineStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@

class LineStringTest extends BaseTestCase
{
private $points;

protected function setUp()
{
$this->points = [new Point(0, 0), new Point(1, 1), new Point(2, 2)];
}

public function testToWKT()
{
$linestring = new LineString($this->points);

$this->assertEquals('LINESTRING(0 0,1 1,2 2)', $linestring->toWKT());
}

public function testFromWKT()
{
$linestring = LineString::fromWKT('LINESTRING(0 0, 1 1, 2 2)');
Expand All @@ -13,20 +27,18 @@ public function testFromWKT()
$this->assertEquals(3, $linestring->count());
}

public function testToWKT()
public function testToString()
{
$linestring = new LineString($this->points);

$points = [new Point(0, 0), new Point(1, 1), new Point(2, 2)];
$linestring = new LineString($points);

$this->assertEquals('LINESTRING(0 0,1 1,2 2)', $linestring->toWKT());
$this->assertEquals('0 0,1 1,2 2', (string)$linestring);
}

public function testToString()
public function testJsonSerialize()
{
$points = [new Point(0, 0), new Point(1, 1), new Point(2, 2)];
$linestring = new LineString($points);
$lineString = new LineString($this->points);

$this->assertEquals('0 0,1 1,2 2', (string)$linestring);
$this->assertInstanceOf(\GeoJson\Geometry\LineString::class, $lineString->jsonSerialize());
$this->assertSame('{"type":"LineString","coordinates":[[0,0],[1,1],[2,2]]}', json_encode($lineString));
}
}
15 changes: 13 additions & 2 deletions tests/Geometries/MultiLineStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function testFromWKT()
$multilinestring = MultiLineString::fromWKT('MULTILINESTRING((0 0,1 1,1 2),(2 3,3 2,5 4))');
$this->assertInstanceOf(MultiLineString::class, $multilinestring);

$this->assertEquals(2, $multilinestring->count());
$this->assertSame(2, $multilinestring->count());
}

public function testToWKT()
Expand All @@ -28,6 +28,17 @@ public function testToWKT()

$multilinestring = new MultiLineString([$collection]);

$this->assertEquals('MULTILINESTRING((0 0,1 0,1 1,0 1,0 0))', $multilinestring->toWKT());
$this->assertSame('MULTILINESTRING((0 0,1 0,1 1,0 1,0 0))', $multilinestring->toWKT());
}

public function testJsonSerialize()
{
$multilinestring = MultiLineString::fromWKT('MULTILINESTRING((0 0,1 1,1 2),(2 3,3 2,5 4))');

$this->assertInstanceOf(\GeoJson\Geometry\MultiLineString::class, $multilinestring->jsonSerialize());
$this->assertSame(
'{"type":"MultiLineString","coordinates":[[[0,0],[1,1],[2,1]],[[3,2],[2,3],[4,5]]]}',
json_encode($multilinestring)
);
}
}
10 changes: 10 additions & 0 deletions tests/Geometries/MultiPointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,14 @@ public function testToWKT()

$this->assertEquals('MULTIPOINT((0 0),(1 0),(1 1))', $multipoint->toWKT());
}

public function testJsonSerialize()
{
$collection = [new Point(0, 0), new Point(0, 1), new Point(1, 1)];

$multipoint = new MultiPoint($collection);

$this->assertInstanceOf(\GeoJson\Geometry\MultiPoint::class, $multipoint->jsonSerialize());
$this->assertSame('{"type":"MultiPoint","coordinates":[[0,0],[0,1],[1,1]]}', json_encode($multipoint));
}
}
Loading