generated from Javaabu/package-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- doc: updated after arushad comments
- Loading branch information
Showing
7 changed files
with
150 additions
and
146 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
docs/basic-usage/search-for-points-within-a-polygon-boundary.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters