diff --git a/src/Helpers.php b/src/Helpers.php index dc420c6..e5dd2f2 100644 --- a/src/Helpers.php +++ b/src/Helpers.php @@ -54,19 +54,22 @@ function g_is_polygon($data = null) if (!function_exists('g_point')) { function g_point($lng, $lat = null) { + return new \Mammutgroup\Database\Geometries\Point($lng, $lat); } } if (!function_exists('g_linestring')) { - function g_linestring($points){ + function g_linestring($points) + { return new \Mammutgroup\Database\Geometries\LineString($points); } } if (!function_exists('g_linestrings')) { - function g_linestrings($lineString){ - if(is_array($lineString)){ + function g_linestrings($lineString) + { + if (is_array($lineString)) { return new \Mammutgroup\Database\Geometries\LineString($lineString); } diff --git a/src/Mammutgroup/Database/Eloquent/GeometryTrait.php b/src/Mammutgroup/Database/Eloquent/GeometryTrait.php index 5303874..66cc5cb 100644 --- a/src/Mammutgroup/Database/Eloquent/GeometryTrait.php +++ b/src/Mammutgroup/Database/Eloquent/GeometryTrait.php @@ -8,8 +8,8 @@ trait GeometryTrait { - public $geometries = []; + /** * Create a new Eloquent query builder for the model. * @@ -23,21 +23,46 @@ public function newEloquentBuilder($query) public function newQuery($excludeDeleted = true) { - $raw=''; - foreach($this->geoFields as $column){ - $raw .= ' ST_AsBinary('.$column.') as '.$column.' '; + $raw = ''; + foreach ($this->geoFields as $column) { + $raw .= ' ST_AsBinary(' . $column . ') as ' . $column . ' '; } - return parent::newQuery($excludeDeleted)->addSelect('*',\DB::raw($raw)); + return parent::newQuery($excludeDeleted)->addSelect('*', \DB::raw($raw)); + } + + public function setRawAttributes(array $attributes, $sync = false) + { + $pgfields = $this->getGeoFields(); + + foreach ($attributes as $attribute => &$value) { + if (in_array($attribute, $pgfields) && is_string($value) && strlen($value) >= 15) { + + $value = Geometry::fromWKB($value); + } + } + + parent::setRawAttributes($attributes, $sync); + } + + public function getGeoFields() + { + if (property_exists($this, 'geoFields')) { + return Arr::isAssoc($this->geoFields) ? //Is the array associative? + array_keys($this->geoFields) : //Returns just the keys to preserve compatibility with previous versions + $this->geoFields; //Returns the non-associative array that doesn't define the geometry type. + } else { + throw new GeoFieldsNotDefinedException(__CLASS__ . ' has to define $geoFields'); + } } protected function performInsert(EloquentBuilder $query, array $options = []) { foreach ($this->attributes as $key => $value) { - if ($value instanceof GeometryInterface && ! $value instanceof GeometryCollection) { + if ($value instanceof GeometryInterface && !$value instanceof GeometryCollection) { $this->geometries[$key] = $value; //Preserve the geometry objects prior to the insert $this->attributes[$key] = $this->getConnection()->raw(sprintf("ST_GeomFromText('%s')", $value->toWKT())); - } else if ($value instanceof GeometryInterface && $value instanceof GeometryCollection) { + } else if ($value instanceof GeometryInterface && $value instanceof GeometryCollection) { $this->geometries[$key] = $value; //Preserve the geometry objects prior to the insert $this->attributes[$key] = $this->getConnection()->raw(sprintf("ST_GeomFromText('%s', 4326)", $value->toWKT())); } @@ -45,36 +70,31 @@ protected function performInsert(EloquentBuilder $query, array $options = []) $insert = parent::performInsert($query, $options); - foreach($this->geometries as $key => $value){ + foreach ($this->geometries as $key => $value) { $this->attributes[$key] = $value; //Retrieve the geometry objects so they can be used in the model } return $insert; //Return the result of the parent insert } - public function setRawAttributes(array $attributes, $sync = false) + protected function performUpdate(EloquentBuilder $query, array $options = []) { - $pgfields = $this->getGeoFields(); - - foreach ($attributes as $attribute => &$value) { - if (in_array($attribute, $pgfields) && is_string($value) && strlen($value) >= 15) { - - $value = Geometry::fromWKB($value); + foreach ($this->attributes as $key => $value) { + if ($value instanceof GeometryInterface && !$value instanceof GeometryCollection) { + $this->geometries[$key] = $value; //Preserve the geometry objects prior to the insert + $this->attributes[$key] = $this->getConnection()->raw(sprintf("ST_GeomFromText('%s')", $value->toWKT())); + } else if ($value instanceof GeometryInterface && $value instanceof GeometryCollection) { + $this->geometries[$key] = $value; //Preserve the geometry objects prior to the insert + $this->attributes[$key] = $this->getConnection()->raw(sprintf("ST_GeomFromText('%s', 4326)", $value->toWKT())); } } - parent::setRawAttributes($attributes, $sync); - } + $update = parent::performUpdate($query, $options); - public function getGeoFields() - { - if (property_exists($this, 'geoFields')) { - return Arr::isAssoc($this->geoFields) ? //Is the array associative? - array_keys($this->geoFields) : //Returns just the keys to preserve compatibility with previous versions - $this->geoFields; //Returns the non-associative array that doesn't define the geometry type. - } else { - throw new GeoFieldsNotDefinedException(__CLASS__ . ' has to define $geoFields'); + foreach ($this->geometries as $key => $value) { + $this->attributes[$key] = $value; //Retrieve the geometry objects so they can be used in the model } + return $update; //Return the result of the parent insert } } diff --git a/src/Mammutgroup/Database/Geometries/Point.php b/src/Mammutgroup/Database/Geometries/Point.php index 3bf8b33..9d9d1bb 100644 --- a/src/Mammutgroup/Database/Geometries/Point.php +++ b/src/Mammutgroup/Database/Geometries/Point.php @@ -14,26 +14,32 @@ public function __construct($lng, $lat = null) public function setLngLat($lng, $lat = null) { - if (is_object($lng) && $lng instanceof $this){ + if (is_object($lng) && $lng instanceof $this) { $this->setLng($lng->getLng()); $this->setLat($lng->getLat()); return $this; } if (is_string($lng)) { - $lng=trim($lng); - if (preg_match('~^\-?\d+(\.\d+)?\s*,\s*\-?\d+(\.\d+)?$~', $lng)){ + $lng = trim($lng); + if (preg_match('~^\-?\d+(\.\d+)?\s*,\s*\-?\d+(\.\d+)?$~', $lng)) { $lng = explode(',', $lng); - }elseif ($json = g_is_point($lng)){ - $lat = $json['coordinates'][0]; - $lng = $json['coordinates'][1]; + } elseif ($json = g_is_point($lng)) { + $lat = $json['coordinates'][1]; + $lng = $json['coordinates'][0]; } } if (is_array($lng)) { $lng = array_map('trim', $lng); - $lat = $lng[1]; - $lng = $lng[0]; + if (isset($lng['lat'])) { + $lat = $lng['lat']; + $lng = $lng['lng']; + } else { + + $lat = $lng[1]; + $lng = $lng[0]; + } } $this->setLng($lng); @@ -48,7 +54,7 @@ public function getLat() public function setLat($lat) { - if (g_is_valid_lat($lat)){ + if (g_is_valid_lat($lat)) { return $this->lat = (float)$lat; } @@ -62,7 +68,7 @@ public function getLng() public function setLng($lng) { - if (g_is_valid_lng($lng)){ + if (g_is_valid_lng($lng)) { return $this->lng = (float)$lng; } @@ -78,7 +84,7 @@ public static function fromPair($pair) { list($lng, $lat) = explode(' ', trim($pair)); - return new static((float)$lat, (float)$lng); + return new static((float)$lng, (float)$lat); } public function toWKT() diff --git a/src/Mammutgroup/Database/Geometries/PointCollection.php b/src/Mammutgroup/Database/Geometries/PointCollection.php index d8375ed..af196ce 100644 --- a/src/Mammutgroup/Database/Geometries/PointCollection.php +++ b/src/Mammutgroup/Database/Geometries/PointCollection.php @@ -28,6 +28,7 @@ public function __construct($points) $points = array_map('g_point', $points); } catch (\Exception $e){ + throw new InvalidArgumentException('$points must be an array of Points'); } diff --git a/src/Mammutgroup/Database/Geometries/Polygon.php b/src/Mammutgroup/Database/Geometries/Polygon.php index 1f2800e..455aaa4 100644 --- a/src/Mammutgroup/Database/Geometries/Polygon.php +++ b/src/Mammutgroup/Database/Geometries/Polygon.php @@ -17,7 +17,6 @@ public function toWKT() public function jsonSerialize() { $linearrings = []; - foreach ($this->linestrings as $linestring) { $linearrings[] = new \GeoJson\Geometry\LinearRing($linestring->jsonSerialize()->getCoordinates()); }