Skip to content

Commit b3dad0e

Browse files
committed
Add whereBuffer scope
1 parent 0457fe9 commit b3dad0e

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

API.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ An enum is provided with the following values:
6969
* [withDistanceSphere](#withDistanceSphere)
7070
* [whereDistanceSphere](#whereDistanceSphere)
7171
* [orderByDistanceSphere](#orderByDistanceSphere)
72+
* [whereBuffer](#whereBuffer)
7273
* [whereWithin](#whereWithin)
7374
* [whereNotWithin](#whereNotWithin)
7475
* [whereContains](#whereContains)
@@ -253,6 +254,29 @@ echo $places[1]->name; // first
253254
```
254255
</details>
255256

257+
### whereBuffer
258+
259+
Filters records within a buffer. Uses [ST_Buffer](https://dev.mysql.com/doc/refman/8.0/en/spatial-operator-functions.html#function_st-buffer).
260+
261+
| parameter name | type |
262+
|---------------------|---------------------|
263+
| `$column` | `Geometry \ string` |
264+
| `$geometryOrColumn` | `Geometry \ string` |
265+
| `$value` | `int \ float` |
266+
267+
<details><summary>Example</summary>
268+
269+
```php
270+
Place::create(['location' => new Point(1, 1.5)]);
271+
272+
$placesCountWithinBuffer = Place::query()
273+
->whereBuffer('location', Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}'), 3)
274+
->count();
275+
276+
echo $placesCountWithinBuffer; // 1
277+
```
278+
</details>
279+
256280
### whereWithin
257281

258282
Filters records by the [ST_Within](https://dev.mysql.com/doc/refman/8.0/en/spatial-relation-functions-object-shapes.html#function_st-within) function.

src/Traits/HasSpatial.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,22 @@ public function scopeOrderByDistanceSphere(
160160
);
161161
}
162162

163+
public function scopeWhereBuffer(
164+
Builder $query,
165+
ExpressionContract|Geometry|string $column,
166+
ExpressionContract|Geometry|string $geometryOrColumn,
167+
int|float $value
168+
): void {
169+
$query->whereRaw(
170+
sprintf(
171+
'ST_WITHIN(%s, ST_BUFFER(%s, ?))',
172+
$this->toExpressionString($column),
173+
$this->toExpressionString($geometryOrColumn),
174+
),
175+
[$value],
176+
);
177+
}
178+
163179
public function scopeWhereWithin(
164180
Builder $query,
165181
ExpressionContract|Geometry|string $column,

tests/HasSpatialTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,22 @@
196196
expect($testPlacesOrderedByDistance[0]->id)->toBe($fartherTestPlace->id);
197197
});
198198

199+
it('filters by buffer', function (): void {
200+
$polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}', Srid::WGS84->value);
201+
$pointWithinBuffer = new Point(1, 1.5, Srid::WGS84->value);
202+
$pointOutsideBuffer = new Point(10, 10, Srid::WGS84->value);
203+
TestPlace::factory()->create(['point' => $pointWithinBuffer]);
204+
TestPlace::factory()->create(['point' => $pointOutsideBuffer]);
205+
206+
/** @var TestPlace[] $placesWithinBuffer */
207+
$placesWithinBuffer = TestPlace::query()
208+
->whereBuffer('point', $polygon, 3)
209+
->get();
210+
211+
expect($placesWithinBuffer)->toHaveCount(1);
212+
expect($placesWithinBuffer[0]->point)->toEqual($pointWithinBuffer);
213+
});
214+
199215
it('filters by within', function (): void {
200216
$polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}', Srid::WGS84->value);
201217
$pointWithinPolygon = new Point(0, 0, Srid::WGS84->value);

0 commit comments

Comments
 (0)