Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Commit a2c316d

Browse files
committed
Merge pull request #10 from MetalMatze/feature/geojson
All geometry objects implement JsonSerializable
2 parents 79e6906 + 68100c7 commit a2c316d

17 files changed

+256
-48
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"php": ">=5.5",
1212
"illuminate/database": "~5.0",
1313
"bosnadev/database": "~0.1",
14-
"geo-io/wkb-parser": "~1.0"
14+
"geo-io/wkb-parser": "~1.0",
15+
"jmikola/geojson": "~1.0"
1516
},
1617
"require-dev": {
1718
"phpunit/phpunit": "~4.5",

src/Geometries/Geometry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use GeoIO\WKB\Parser\Parser;
44
use Phaza\LaravelPostgis\Exceptions\UnknownWKTTypeException;
55

6-
abstract class Geometry implements GeometryInterface
6+
abstract class Geometry implements GeometryInterface, \JsonSerializable
77
{
88
protected static $wkb_types = [
99
1 => Point::class,

src/Geometries/GeometryCollection.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php namespace Phaza\LaravelPostgis\Geometries;
22

33
use Countable;
4+
use GeoJson\GeoJson;
45
use InvalidArgumentException;
56

67
class GeometryCollection extends Geometry implements Countable
@@ -71,4 +72,19 @@ public function count()
7172
{
7273
return count($this->geometries);
7374
}
75+
76+
/**
77+
* Convert to GeoJson GeometryCollection that is jsonable to GeoJSON
78+
*
79+
* @return \GeoJson\Geometry\GeometryCollection
80+
*/
81+
public function jsonSerialize()
82+
{
83+
$geometries = [];
84+
foreach ($this->geometries as $geometry) {
85+
$geometries[] = $geometry->jsonSerialize();
86+
}
87+
88+
return new \GeoJson\Geometry\GeometryCollection($geometries);
89+
}
7490
}

src/Geometries/LineString.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,19 @@ public function __toString()
2828
{
2929
return $this->toPairList();
3030
}
31+
32+
/**
33+
* Convert to GeoJson LineString that is jsonable to GeoJSON
34+
*
35+
* @return \GeoJson\Geometry\LineString
36+
*/
37+
public function jsonSerialize()
38+
{
39+
$points = [];
40+
foreach ($this->points as $point) {
41+
$points[] = $point->jsonSerialize();
42+
}
43+
44+
return new \GeoJson\Geometry\LineString($points);
45+
}
3146
}

src/Geometries/MultiLineString.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,20 @@ public function count()
6262
{
6363
return count($this->linestrings);
6464
}
65+
66+
/**
67+
* Convert to GeoJson Point that is jsonable to GeoJSON
68+
*
69+
* @return \GeoJson\Geometry\MultiLineString
70+
*/
71+
public function jsonSerialize()
72+
{
73+
$linestrings = [];
74+
75+
foreach ($this->linestrings as $linestring) {
76+
$linestrings[] = $linestring->jsonSerialize();
77+
}
78+
79+
return new \GeoJson\Geometry\MultiLineString($linestrings);
80+
}
6581
}

src/Geometries/MultiPoint.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php namespace Phaza\LaravelPostgis\Geometries;
22

3-
class MultiPoint extends PointCollection implements GeometryInterface
3+
class MultiPoint extends PointCollection implements GeometryInterface, \JsonSerializable
44
{
55
public function toWKT()
66
{
@@ -36,4 +36,19 @@ public function __toString()
3636
return sprintf('(%s)', $point->toPair());
3737
}, $this->points));
3838
}
39+
40+
/**
41+
* Convert to GeoJson MultiPoint that is jsonable to GeoJSON
42+
*
43+
* @return \GeoJson\Geometry\MultiPoint
44+
*/
45+
public function jsonSerialize()
46+
{
47+
$points = [];
48+
foreach ($this->points as $point) {
49+
$points[] = $point->jsonSerialize();
50+
}
51+
52+
return new \GeoJson\Geometry\MultiPoint($points);
53+
}
3954
}

src/Geometries/MultiPolygon.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,19 @@ protected static function assembleParts(array $parts)
9999

100100
return $polygons;
101101
}
102+
103+
/**
104+
* Convert to GeoJson MultiPolygon that is jsonable to GeoJSON
105+
*
106+
* @return \GeoJson\Geometry\MultiPolygon
107+
*/
108+
public function jsonSerialize()
109+
{
110+
$polygons = [];
111+
foreach ($this->polygons as $polygon) {
112+
$polygons[] = $polygon->jsonSerialize();
113+
}
114+
115+
return new \GeoJson\Geometry\MultiPolygon($polygons);
116+
}
102117
}

src/Geometries/Point.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php namespace Phaza\LaravelPostgis\Geometries;
22

3+
use GeoJson\GeoJson;
4+
35
class Point extends Geometry
46
{
57
protected $lat;
@@ -57,4 +59,15 @@ public function __toString()
5759
{
5860
return $this->getLng() . ' ' . $this->getLat();
5961
}
62+
63+
/**
64+
* Convert to GeoJson Point that is jsonable to GeoJSON
65+
*
66+
* @return \GeoJson\Geometry\Point
67+
*/
68+
public function jsonSerialize()
69+
{
70+
// !!! This will convert LngLat from PostGIS to ISO 6709 LatLng !!!
71+
return new \GeoJson\Geometry\Point([$this->getLat(), $this->getLng()]);
72+
}
6073
}

src/Geometries/PointCollection.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
use Illuminate\Contracts\Support\Arrayable;
77
use InvalidArgumentException;
88
use IteratorAggregate;
9+
use JsonSerializable;
910

10-
class PointCollection implements IteratorAggregate, Arrayable, ArrayAccess, Countable
11+
abstract class PointCollection implements IteratorAggregate, Arrayable, ArrayAccess, Countable, JsonSerializable
1112
{
1213
/**
1314
* @var Point[]

src/Geometries/Polygon.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,19 @@ public function toWKT()
99
{
1010
return sprintf('POLYGON(%s)', (string)$this);
1111
}
12+
13+
/**
14+
* Convert to GeoJson Polygon that is jsonable to GeoJSON
15+
*
16+
* @return \GeoJson\Geometry\Polygon
17+
*/
18+
public function jsonSerialize()
19+
{
20+
$linearrings = [];
21+
foreach ($this->linestrings as $linestring) {
22+
$linearrings[] = new \GeoJson\Geometry\LinearRing($linestring->jsonSerialize()->getCoordinates());
23+
}
24+
25+
return new \GeoJson\Geometry\Polygon($linearrings);
26+
}
1227
}

0 commit comments

Comments
 (0)