Skip to content

Commit

Permalink
- doc: updated after arushad comments
Browse files Browse the repository at this point in the history
  • Loading branch information
falahwho committed Jun 2, 2024
1 parent e77e505 commit ee508b1
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 146 deletions.
146 changes: 0 additions & 146 deletions docs/basic-usage/overview.md

This file was deleted.

74 changes: 74 additions & 0 deletions docs/basic-usage/preparing-the-model.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
title: Preparing The Model
sidebar_position: 1
---

### Preparing The Model

This is a sample migration for a `City` model as an example.

```php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class () extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('cities', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->geography('coordinates', 'point')->nullable();
$table->geography('boundary', 'polygon')->nullable();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('cities');
}
};
```

After the package has been installed, you can use the `HasSpatial` Trait in your models that interacts with MySQL geospatial columns. The package supports sqlite testing for Point and Polygon geospatial subtypes.

Then add the `Point` and `Polygon` casts to your model attributes according to the column subtype.

```php
use Illuminate\Database\Eloquent\Model;
use Javaabu\Geospatial\HasSpatial;
use Javaabu\Geospatial\Objects\Point;
use Javaabu\Geospatial\Objects\Polygon;

class City extends Model
{
use HasSpatial;

protected $casts = [
'coordinates' => Point::class,
'boundary' => Polygon::class,
];
}

```

The default column names used by the trait for Point and Polygon columns are `coordinates` and `boundary` respectively. You can override the `getDefaultPointField` and `getDefaultPolygonField` methods to change the model column.

```php
public function getDefaultPointField(): string
{
return 'coordinates';
}

public function getDefaultPolygonField(): string
{
return 'boundary';
}
```
24 changes: 24 additions & 0 deletions docs/basic-usage/search-for-points-within-a-polygon-boundary.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
title: Preparing The Model
sidebar_position: 4
---

### Search For Points Within a Polygon Boundary

The trait provides a `scopeWithinBounds` scope to search for records that fall within a given boundary.

```php
$city = new City();
$city->name = 'Male City';
$city->setPoint(4.175804, 73.509337);
$city->save();

$male_city_boundary_wkt = 'POLYGON ((73.50932514628285 4.175929944808645,73.50954911073559 4.175730219415812,73.50914768804103 4.17570881870468,73.50932514628285 4.175929944808645))';

$cities_within_search_area = City::withinBounds($wkt)->get();
$cities_within_search_area->first()->name; // "Male City"

$uligan_boundary_wkt = '(72.92683934689452 7.0841231111032235,72.92706331134727 7.083924382773967,72.9266618886527 7.083903088896789,72.92683934689452 7.0841231111032235)';
City::withinBounds($in_uligan)->first(); // null
```

20 changes: 20 additions & 0 deletions docs/basic-usage/setting-a-point.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
title: Setting A Point
sidebar_position: 2
---

### Setting a Point Column

Use the `setPoint` method. Method accepts a latitude and longitude. You also have the option to manually pass in the database `column` and `srid` as the third argument and fourth argument.
```php
$city = new City();
$latitude = 4.175804;
$longitude = 73.509337;
$city->name = 'Male City';
$city->setPoint($latitude, $longitude); // $city->setPoint($latitude, $longitude, 'coordinates', 4326);
$city->save();

$city->lat // 4.175804
$city->lng // 73.509337
```
You can use the `lat` and `lng` attributes to get the latitude and longitude of the point.
29 changes: 29 additions & 0 deletions docs/basic-usage/setting-a-polygon.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
title: Overview
sidebar_position: 3
---

### Setting a Polygon Column

The package supports using a `wkt` string to define the points of the polygon.

:::info

Wkt should be a valid wkt format string.

Example: POLYGON((73.5092 4.1758, 73.5094 4.1758, 73.5094 4.1757, 73.5092 4.1757, 73.5092 4.1758))

Optionally, you can also omit the word "POLYGON" from the wkt string.

:::

You also have the option to manually pass in the database `column` and `srid` as the second argument and third argument.

```php
$city = new City();
$city->name = 'Male City';
$wkt = '(73.50924692977462 4.175893831117514,73.50942707022546 4.175893831117514,73.50942707022546 4.175714168882511,73.50924692977462 4.175714168882511,73.50924692977462 4.175893831117514)';
$city->setPolygon($wkt); // // $city->setPolygon($wkt, 'coordinates', 4326);

$city->save();
```
1 change: 1 addition & 0 deletions docs/basic-usage/testing.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: Testing
sidebar_position: 5
---

Since by default, sqlite doesn't support spatial columns, the `HasSpatial` trait adds a `toTestDbString` that supports testing the geospatial column for both MySQL and sqlite.
Expand Down
2 changes: 2 additions & 0 deletions docs/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ This package is currently under development. If anything works, that's a surpris

This package is an extension to [Laravel Eloquent Spatial](https://github.com/MatanYadaev/laravel-eloquent-spatial), mainly adding support for testing with sqlite, and other helpful functionalities. This documentation will contain the extra features added on top of the original package.

You can refer to the [Laravel Eloquent Spatial](https://github.com/MatanYadaev/laravel-eloquent-spatial) package documentation for the original package features.

0 comments on commit ee508b1

Please sign in to comment.