Skip to content

Commit

Permalink
Merge pull request #72 from Chrissi2812/mysql-5.6
Browse files Browse the repository at this point in the history
Unserialize from GeoJSON for MySQL 5.6 Branch
  • Loading branch information
grimzy authored Jan 21, 2019
2 parents 6337d15 + df0ded9 commit 78ebd48
Show file tree
Hide file tree
Showing 22 changed files with 494 additions and 10 deletions.
7 changes: 7 additions & 0 deletions src/Exceptions/InvalidGeoJsonException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Grimzy\LaravelMysqlSpatial\Exceptions;

class InvalidGeoJsonException extends \RuntimeException
{
}
7 changes: 4 additions & 3 deletions src/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ public function geometry($column)
/**
* Add a point column on the table.
*
* @param $column
* @param string $column
* @param null|int $srid
*
* @return \Illuminate\Support\Fluent
*/
public function point($column)
public function point($column, $srid = null)
{
return $this->addColumn('point', $column);
return $this->addColumn('point', $column, compact('srid'));
}

/**
Expand Down
20 changes: 20 additions & 0 deletions src/Types/Geometry.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Grimzy\LaravelMysqlSpatial\Types;

use GeoIO\WKB\Parser\Parser;
use GeoJson\GeoJson;
use Grimzy\LaravelMysqlSpatial\Exceptions\UnknownWKTTypeException;
use Illuminate\Contracts\Support\Jsonable;

Expand Down Expand Up @@ -71,6 +72,25 @@ public static function fromWKT($wkt)
return static::fromString($wktArgument);
}

public static function fromJson($geoJson)
{
if (is_string($geoJson)) {
$geoJson = GeoJson::jsonUnserialize(json_decode($geoJson));
}

if ($geoJson->getType() === 'FeatureCollection') {
return GeometryCollection::fromJson($geoJson);
}

if ($geoJson->getType() === 'Feature') {
$geoJson = $geoJson->getGeometry();
}

$type = '\Grimzy\LaravelMysqlSpatial\Types\\'.$geoJson->getType();

return $type::fromJson($geoJson);
}

public function toJson($options = 0)
{
return json_encode($this, $options);
Expand Down
21 changes: 21 additions & 0 deletions src/Types/GeometryCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use ArrayAccess;
use ArrayIterator;
use Countable;
use GeoJson\Feature\FeatureCollection;
use GeoJson\GeoJson;
use Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException;
use Illuminate\Contracts\Support\Arrayable;
use InvalidArgumentException;
use IteratorAggregate;
Expand Down Expand Up @@ -107,6 +110,24 @@ public function count()
return count($this->items);
}

public static function fromJson($geoJson)
{
if (is_string($geoJson)) {
$geoJson = GeoJson::jsonUnserialize(json_decode($geoJson));
}

if (!is_a($geoJson, FeatureCollection::class)) {
throw new InvalidGeoJsonException('Expected '.FeatureCollection::class.', got '.get_class($geoJson));
}

$set = [];
foreach ($geoJson->getFeatures() as $feature) {
$set[] = parent::fromJson($feature);
}

return new self($set);
}

/**
* Convert to GeoJson GeometryCollection that is jsonable to GeoJSON.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Types/GeometryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public static function fromWKT($wkt);
public function __toString();

public static function fromString($wktArgument);

public static function fromJson($geoJson);
}
24 changes: 23 additions & 1 deletion src/Types/LineString.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace Grimzy\LaravelMysqlSpatial\Types;

use GeoJson\GeoJson;
use GeoJson\Geometry\LineString as GeoJsonLineString;
use Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException;

class LineString extends PointCollection
{
public function toWKT()
Expand Down Expand Up @@ -31,6 +35,24 @@ public function __toString()
return $this->toPairList();
}

public static function fromJson($geoJson)
{
if (is_string($geoJson)) {
$geoJson = GeoJson::jsonUnserialize(json_decode($geoJson));
}

if (!is_a($geoJson, GeoJsonLineString::class)) {
throw new InvalidGeoJsonException('Expected '.GeoJsonLineString::class.', got '.get_class($geoJson));
}

$set = [];
foreach ($geoJson->getCoordinates() as $coordinate) {
$set[] = new Point($coordinate[1], $coordinate[0]);
}

return new self($set);
}

/**
* Convert to GeoJson LineString that is jsonable to GeoJSON.
*
Expand All @@ -43,6 +65,6 @@ public function jsonSerialize()
$points[] = $point->jsonSerialize();
}

return new \GeoJson\Geometry\LineString($points);
return new GeoJsonLineString($points);
}
}
27 changes: 26 additions & 1 deletion src/Types/MultiLineString.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Grimzy\LaravelMysqlSpatial\Types;

use GeoJson\GeoJson;
use GeoJson\Geometry\MultiLineString as GeoJsonMultiLineString;
use Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException;
use InvalidArgumentException;

class MultiLineString extends GeometryCollection
Expand Down Expand Up @@ -62,6 +65,28 @@ public function offsetSet($offset, $value)
parent::offsetSet($offset, $value);
}

public static function fromJson($geoJson)
{
if (is_string($geoJson)) {
$geoJson = GeoJson::jsonUnserialize(json_decode($geoJson));
}

if (!is_a($geoJson, GeoJsonMultiLineString::class)) {
throw new InvalidGeoJsonException('Expected '.GeoJsonMultiLineString::class.', got '.get_class($geoJson));
}

$set = [];
foreach ($geoJson->getCoordinates() as $coordinates) {
$points = [];
foreach ($coordinates as $coordinate) {
$points[] = new Point($coordinate[1], $coordinate[0]);
}
$set[] = new LineString($points);
}

return new self($set);
}

/**
* Convert to GeoJson Point that is jsonable to GeoJSON.
*
Expand All @@ -75,6 +100,6 @@ public function jsonSerialize()
$lineStrings[] = $lineString->jsonSerialize();
}

return new \GeoJson\Geometry\MultiLineString($lineStrings);
return new GeoJsonMultiLineString($lineStrings);
}
}
24 changes: 23 additions & 1 deletion src/Types/MultiPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace Grimzy\LaravelMysqlSpatial\Types;

use GeoJson\GeoJson;
use GeoJson\Geometry\MultiPoint as GeoJsonMultiPoint;
use Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException;

class MultiPoint extends PointCollection
{
public function toWKT()
Expand Down Expand Up @@ -35,6 +39,24 @@ public function __toString()
}, $this->items));
}

public static function fromJson($geoJson)
{
if (is_string($geoJson)) {
$geoJson = GeoJson::jsonUnserialize(json_decode($geoJson));
}

if (!is_a($geoJson, GeoJsonMultiPoint::class)) {
throw new InvalidGeoJsonException('Expected '.GeoJsonMultiPoint::class.', got '.get_class($geoJson));
}

$set = [];
foreach ($geoJson->getCoordinates() as $coordinate) {
$set[] = new Point($coordinate[1], $coordinate[0]);
}

return new self($set);
}

/**
* Convert to GeoJson MultiPoint that is jsonable to GeoJSON.
*
Expand All @@ -47,6 +69,6 @@ public function jsonSerialize()
$points[] = $point->jsonSerialize();
}

return new \GeoJson\Geometry\MultiPoint($points);
return new GeoJsonMultiPoint($points);
}
}
31 changes: 30 additions & 1 deletion src/Types/MultiPolygon.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Grimzy\LaravelMysqlSpatial\Types;

use GeoJson\GeoJson;
use GeoJson\Geometry\MultiPolygon as GeoJsonMultiPolygon;
use Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException;
use InvalidArgumentException;

class MultiPolygon extends GeometryCollection
Expand Down Expand Up @@ -97,6 +100,32 @@ public function offsetSet($offset, $value)
parent::offsetSet($offset, $value);
}

public static function fromJson($geoJson)
{
if (is_string($geoJson)) {
$geoJson = GeoJson::jsonUnserialize(json_decode($geoJson));
}

if (!is_a($geoJson, GeoJsonMultiPolygon::class)) {
throw new InvalidGeoJsonException('Expected '.GeoJsonMultiPolygon::class.', got '.get_class($geoJson));
}

$set = [];
foreach ($geoJson->getCoordinates() as $polygonCoordinates) {
$lineStrings = [];
foreach ($polygonCoordinates as $lineStringCoordinates) {
$points = [];
foreach ($lineStringCoordinates as $lineStringCoordinate) {
$points[] = new Point($lineStringCoordinate[1], $lineStringCoordinate[0]);
}
$lineStrings[] = new LineString($points);
}
$set[] = new Polygon($lineStrings);
}

return new self($set);
}

/**
* Convert to GeoJson MultiPolygon that is jsonable to GeoJSON.
*
Expand All @@ -109,6 +138,6 @@ public function jsonSerialize()
$polygons[] = $polygon->jsonSerialize();
}

return new \GeoJson\Geometry\MultiPolygon($polygons);
return new GeoJsonMultiPolygon($polygons);
}
}
26 changes: 25 additions & 1 deletion src/Types/Point.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace Grimzy\LaravelMysqlSpatial\Types;

use GeoJson\GeoJson;
use GeoJson\Geometry\Point as GeoJsonPoint;
use Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException;

class Point extends Geometry
{
protected $lat;
Expand Down Expand Up @@ -61,13 +65,33 @@ public function __toString()
return $this->getLng().' '.$this->getLat();
}

/**
* @param $geoJson \GeoJson\Feature\Feature|string
*
* @return \Grimzy\LaravelMysqlSpatial\Types\Point
*/
public static function fromJson($geoJson)
{
if (is_string($geoJson)) {
$geoJson = GeoJson::jsonUnserialize(json_decode($geoJson));
}

if (!is_a($geoJson, GeoJsonPoint::class)) {
throw new InvalidGeoJsonException('Expected '.GeoJsonPoint::class.', got '.get_class($geoJson));
}

$coordinates = $geoJson->getCoordinates();

return new self($coordinates[1], $coordinates[0]);
}

/**
* Convert to GeoJson Point that is jsonable to GeoJSON.
*
* @return \GeoJson\Geometry\Point
*/
public function jsonSerialize()
{
return new \GeoJson\Geometry\Point([$this->getLng(), $this->getLat()]);
return new GeoJsonPoint([$this->getLng(), $this->getLat()]);
}
}
28 changes: 27 additions & 1 deletion src/Types/Polygon.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,39 @@

namespace Grimzy\LaravelMysqlSpatial\Types;

use GeoJson\GeoJson;
use GeoJson\Geometry\Polygon as GeoJsonPolygon;
use Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException;

class Polygon extends MultiLineString
{
public function toWKT()
{
return sprintf('POLYGON(%s)', (string) $this);
}

public static function fromJson($geoJson)
{
if (is_string($geoJson)) {
$geoJson = GeoJson::jsonUnserialize(json_decode($geoJson));
}

if (!is_a($geoJson, GeoJsonPolygon::class)) {
throw new InvalidGeoJsonException('Expected '.GeoJsonPolygon::class.', got '.get_class($geoJson));
}

$set = [];
foreach ($geoJson->getCoordinates() as $coordinates) {
$points = [];
foreach ($coordinates as $coordinate) {
$points[] = new Point($coordinate[1], $coordinate[0]);
}
$set[] = new LineString($points);
}

return new self($set);
}

/**
* Convert to GeoJson Polygon that is jsonable to GeoJSON.
*
Expand All @@ -21,6 +47,6 @@ public function jsonSerialize()
$linearRings[] = new \GeoJson\Geometry\LinearRing($lineString->jsonSerialize()->getCoordinates());
}

return new \GeoJson\Geometry\Polygon($linearRings);
return new GeoJsonPolygon($linearRings);
}
}
Loading

0 comments on commit 78ebd48

Please sign in to comment.