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

Commit

Permalink
Merge pull request #101 from mypharmabr/master
Browse files Browse the repository at this point in the history
Add geometry update support
  • Loading branch information
njbarrett authored Jul 4, 2018
2 parents bab919c + 6261e1c commit 7c2b8cf
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
19 changes: 8 additions & 11 deletions src/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,21 @@ public function update(array $values)
{
foreach ($values as $key => &$value) {
if ($value instanceof GeometryInterface) {
$value = $this->asWKT($value);
if (is_null($this->model)) {
$value = $this->asWKT($value);
} else {
$attrs = $this->model->getPostgisType($key);
$value = $this->model->asWKT($value, $attrs);
}
}
}

return parent::update($values);
}

protected function getPostgisFields()
{
return $this->getModel()->getPostgisFields();
}


protected function asWKT(GeometryInterface $geometry)
{
return $this->getQuery()->raw(
sprintf("%s.ST_GeogFromText('%s')", function_exists('config') ? config('postgis.schema') : 'public', $geometry->toWKT())
);

return $this->getQuery()->raw(sprintf("%s.ST_GeogFromText('%s')",
function_exists('config') ? config('postgis.schema') : 'public', $geometry->toWKT()));
}
}
46 changes: 27 additions & 19 deletions src/Eloquent/PostgisTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,41 @@ public function newEloquentBuilder($query)
return new Builder($query);
}

protected function geogFromText(GeometryInterface $geometry)
{
return $this->getConnection()->raw(sprintf("%s.ST_GeogFromText('%s')",
function_exists('config') ? config('postgis.schema') : 'public', $geometry->toWKT()));
}

protected function geomFromText(GeometryInterface $geometry, $srid = 4326)
{
return $this->getConnection()->raw(sprintf("%s.ST_GeomFromText('%s', '%d')",
function_exists('config') ? config('postgis.schema') : 'public', $geometry->toWKT(), $srid));
}

public function asWKT(GeometryInterface $geometry, $attrs)
{
switch (strtoupper($attrs['geomtype'])) {
case 'GEOMETRY':
return $this->geomFromText($geometry, $attrs['srid']);
break;
case 'GEOGRAPHY':
default:
return $this->geogFromText($geometry);
break;
}
}

protected function performInsert(EloquentBuilder $query, array $options = [])
{
foreach ($this->attributes as $key => $value) {
if ($value instanceof GeometryInterface) {
$this->geometries[$key] = $value; //Preserve the geometry objects prior to the insert
if (! $value instanceof GeometryCollection) {
$attrs = $this->getPostgisType($key);
switch (strtoupper($attrs['geomtype'])) {
case 'GEOMETRY':
$this->attributes[$key] = $this->getConnection()->raw(
sprintf("%s.ST_GeomFromText('%s', '%d')",
function_exists('config') ? config('postgis.schema') : 'public', $value->toWKT(), $attrs['srid'])
);
break;
case 'GEOGRAPHY':
default:
$this->attributes[$key] = $this->getConnection()->raw(
sprintf("%s.ST_GeogFromText('%s')",
function_exists('config') ? config('postgis.schema') : 'public', $value->toWKT())
);
break;
}
$this->attributes[$key] = $this->asWKT($value, $attrs);
} else {
$this->attributes[$key] = $this->getConnection()->raw(
sprintf("%s.ST_GeomFromText('%s', 4326)",
function_exists('config') ? config('postgis.schema') : 'public', $value->toWKT())
);
$this->attributes[$key] = $this->geomFromText($value);
}
}
}
Expand Down

0 comments on commit 7c2b8cf

Please sign in to comment.