Skip to content

Commit

Permalink
fix bug on update geo fields
Browse files Browse the repository at this point in the history
  • Loading branch information
honarkhah committed Feb 4, 2017
1 parent 3e658d5 commit 373cdd6
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 40 deletions.
9 changes: 6 additions & 3 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
70 changes: 45 additions & 25 deletions src/Mammutgroup/Database/Eloquent/GeometryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

trait GeometryTrait
{

public $geometries = [];

/**
* Create a new Eloquent query builder for the model.
*
Expand All @@ -23,58 +23,78 @@ 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()));
}
}

$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
}
}
28 changes: 17 additions & 11 deletions src/Mammutgroup/Database/Geometries/Point.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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()
Expand Down
1 change: 1 addition & 0 deletions src/Mammutgroup/Database/Geometries/PointCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
1 change: 0 additions & 1 deletion src/Mammutgroup/Database/Geometries/Polygon.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public function toWKT()
public function jsonSerialize()
{
$linearrings = [];

foreach ($this->linestrings as $linestring) {
$linearrings[] = new \GeoJson\Geometry\LinearRing($linestring->jsonSerialize()->getCoordinates());
}
Expand Down

0 comments on commit 373cdd6

Please sign in to comment.