Merge pull request #206 from jonnybarnes/drop-postgis-part-2
Drop Postgis part 2
This commit is contained in:
commit
36f319cd75
12 changed files with 610 additions and 674 deletions
|
@ -8,13 +8,11 @@ use App\Http\Controllers\Controller;
|
|||
use App\Models\Place;
|
||||
use App\Services\PlaceService;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\View\View;
|
||||
use MStaack\LaravelPostgis\Geometries\Point;
|
||||
|
||||
class PlacesController extends Controller
|
||||
{
|
||||
protected $placeService;
|
||||
protected PlaceService $placeService;
|
||||
|
||||
public function __construct(PlaceService $placeService)
|
||||
{
|
||||
|
@ -24,7 +22,7 @@ class PlacesController extends Controller
|
|||
/**
|
||||
* List the places that can be edited.
|
||||
*
|
||||
* @return \Illuminate\View\View
|
||||
* @return View
|
||||
*/
|
||||
public function index(): View
|
||||
{
|
||||
|
@ -36,7 +34,7 @@ class PlacesController extends Controller
|
|||
/**
|
||||
* Show the form to make a new place.
|
||||
*
|
||||
* @return \Illuminate\View\View
|
||||
* @return View
|
||||
*/
|
||||
public function create(): View
|
||||
{
|
||||
|
@ -46,12 +44,18 @@ class PlacesController extends Controller
|
|||
/**
|
||||
* Process a request to make a new place.
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
* @return RedirectResponse
|
||||
*/
|
||||
public function store(): RedirectResponse
|
||||
{
|
||||
$data = request()->only(['name', 'description', 'latitude', 'longitude']);
|
||||
$place = $this->placeService->createPlace($data);
|
||||
$this->placeService->createPlace(
|
||||
request()->only([
|
||||
'name',
|
||||
'description',
|
||||
'latitude',
|
||||
'longitude',
|
||||
])
|
||||
);
|
||||
|
||||
return redirect('/admin/places');
|
||||
}
|
||||
|
@ -60,7 +64,7 @@ class PlacesController extends Controller
|
|||
* Display the form to edit a specific place.
|
||||
*
|
||||
* @param int $placeId
|
||||
* @return \Illuminate\View\View
|
||||
* @return View
|
||||
*/
|
||||
public function edit(int $placeId): View
|
||||
{
|
||||
|
@ -73,17 +77,15 @@ class PlacesController extends Controller
|
|||
* Process a request to edit a place.
|
||||
*
|
||||
* @param int $placeId
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
* @return RedirectResponse
|
||||
*/
|
||||
public function update(int $placeId): RedirectResponse
|
||||
{
|
||||
$place = Place::findOrFail($placeId);
|
||||
$place->name = request()->input('name');
|
||||
$place->description = request()->input('description');
|
||||
$place->location = new Point(
|
||||
(float) request()->input('latitude'),
|
||||
(float) request()->input('longitude')
|
||||
);
|
||||
$place->latitude = request()->input('latitude');
|
||||
$place->longitude = request()->input('longitude');
|
||||
$place->icon = request()->input('icon');
|
||||
$place->save();
|
||||
|
||||
|
@ -94,12 +96,12 @@ class PlacesController extends Controller
|
|||
* List the places we can merge with the current place.
|
||||
*
|
||||
* @param int $placeId
|
||||
* @return \Illuminate\View\View
|
||||
* @return View
|
||||
*/
|
||||
public function mergeIndex(int $placeId): View
|
||||
{
|
||||
$first = Place::find($placeId);
|
||||
$results = Place::near(new Point($first->latitude, $first->longitude))->get();
|
||||
$results = Place::near((object) ['latitude' => $first->latitude, 'longitude' => $first->longitude])->get();
|
||||
$places = [];
|
||||
foreach ($results as $place) {
|
||||
if ($place->slug !== $first->slug) {
|
||||
|
@ -115,7 +117,7 @@ class PlacesController extends Controller
|
|||
*
|
||||
* @param int $placeId1
|
||||
* @param int $placeId2
|
||||
* @return \Illuminate\View\View
|
||||
* @return View
|
||||
*/
|
||||
public function mergeEdit(int $placeId1, int $placeId2): View
|
||||
{
|
||||
|
@ -128,7 +130,7 @@ class PlacesController extends Controller
|
|||
/**
|
||||
* Process the request to merge two places.
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
* @return RedirectResponse
|
||||
*/
|
||||
public function mergeStore(): RedirectResponse
|
||||
{
|
||||
|
|
|
@ -12,7 +12,6 @@ use App\Services\TokenService;
|
|||
use Illuminate\Http\JsonResponse;
|
||||
use Monolog\Handler\StreamHandler;
|
||||
use Monolog\Logger;
|
||||
use MStaack\LaravelPostgis\Geometries\Point;
|
||||
|
||||
class MicropubController extends Controller
|
||||
{
|
||||
|
@ -142,7 +141,10 @@ class MicropubController extends Controller
|
|||
$matches
|
||||
);
|
||||
$distance = (count($matches[0]) == 3) ? 100 * $matches[0][2] : 1000;
|
||||
$places = Place::near(new Point($matches[0][0], $matches[0][1]))->get();
|
||||
$places = Place::near(
|
||||
(object) ['latitude' => $matches[0][0], 'longitude' => $matches[0][1]],
|
||||
$distance
|
||||
)->get();
|
||||
|
||||
return response()->json([
|
||||
'response' => 'places',
|
||||
|
|
|
@ -340,7 +340,7 @@ class Note extends Model
|
|||
public function getLatitudeAttribute(): ?float
|
||||
{
|
||||
if ($this->place !== null) {
|
||||
return $this->place->location->getLat();
|
||||
return $this->place->latitude;
|
||||
}
|
||||
if ($this->location !== null) {
|
||||
$pieces = explode(':', $this->location);
|
||||
|
@ -360,7 +360,7 @@ class Note extends Model
|
|||
public function getLongitudeAttribute(): ?float
|
||||
{
|
||||
if ($this->place !== null) {
|
||||
return $this->place->location->getLng();
|
||||
return $this->place->longitude;
|
||||
}
|
||||
if ($this->location !== null) {
|
||||
$pieces = explode(':', $this->location);
|
||||
|
|
|
@ -9,10 +9,7 @@ use Eloquent;
|
|||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\{Builder, Collection, Model};
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
use MStaack\LaravelPostgis\Eloquent\PostgisTrait;
|
||||
use MStaack\LaravelPostgis\Geometries\Point;
|
||||
|
||||
/**
|
||||
* App\Models\Place.
|
||||
|
@ -21,25 +18,23 @@ use MStaack\LaravelPostgis\Geometries\Point;
|
|||
* @property string $name
|
||||
* @property string $slug
|
||||
* @property string|null $description
|
||||
* @property Point $location
|
||||
* @property mixed|null $polygon
|
||||
* @property Carbon|null $created_at
|
||||
* @property Carbon|null $updated_at
|
||||
* @property string|null $icon
|
||||
* @property string|null $foursquare
|
||||
* @property mixed|null $external_urls
|
||||
* @property-read float $latitude
|
||||
* @property-read float $longitude
|
||||
* @property float|null $latitude
|
||||
* @property float|null $longitude
|
||||
* @property-read string $longurl
|
||||
* @property-read string $shorturl
|
||||
* @property-read string $uri
|
||||
* @property-read Collection|Note[] $notes
|
||||
* @property-read Collection|\App\Models\Note[] $notes
|
||||
* @property-read int|null $notes_count
|
||||
* @method static Builder|Place findSimilarSlugs($attribute, $config, $slug)
|
||||
* @method static Builder|Place near(Point $point, $distance = 1000)
|
||||
* @method static \MStaack\LaravelPostgis\Eloquent\Builder|Place newModelQuery()
|
||||
* @method static \MStaack\LaravelPostgis\Eloquent\Builder|Place newQuery()
|
||||
* @method static \MStaack\LaravelPostgis\Eloquent\Builder|Place query()
|
||||
* @method static Builder|Place near($location, $distance = 1000)
|
||||
* @method static Builder|Place newModelQuery()
|
||||
* @method static Builder|Place newQuery()
|
||||
* @method static Builder|Place query()
|
||||
* @method static Builder|Place whereCreatedAt($value)
|
||||
* @method static Builder|Place whereDescription($value)
|
||||
* @method static Builder|Place whereExternalURL($url)
|
||||
|
@ -47,9 +42,9 @@ use MStaack\LaravelPostgis\Geometries\Point;
|
|||
* @method static Builder|Place whereFoursquare($value)
|
||||
* @method static Builder|Place whereIcon($value)
|
||||
* @method static Builder|Place whereId($value)
|
||||
* @method static Builder|Place whereLocation($value)
|
||||
* @method static Builder|Place whereLatitude($value)
|
||||
* @method static Builder|Place whereLongitude($value)
|
||||
* @method static Builder|Place whereName($value)
|
||||
* @method static Builder|Place wherePolygon($value)
|
||||
* @method static Builder|Place whereSlug($value)
|
||||
* @method static Builder|Place whereUpdatedAt($value)
|
||||
* @mixin Eloquent
|
||||
|
@ -57,7 +52,6 @@ use MStaack\LaravelPostgis\Geometries\Point;
|
|||
class Place extends Model
|
||||
{
|
||||
use Sluggable;
|
||||
use PostgisTrait;
|
||||
|
||||
/**
|
||||
* Get the route key for the model.
|
||||
|
@ -77,13 +71,13 @@ class Place extends Model
|
|||
protected $fillable = ['name', 'slug'];
|
||||
|
||||
/**
|
||||
* The attributes that are Postgis geometry objects.
|
||||
* The attributes that should be cast.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $postgisFields = [
|
||||
'location',
|
||||
'polygon',
|
||||
protected $casts = [
|
||||
'latitude' => 'float',
|
||||
'longitude' => 'float',
|
||||
];
|
||||
|
||||
/**
|
||||
|
@ -115,21 +109,23 @@ class Place extends Model
|
|||
* Select places near a given location.
|
||||
*
|
||||
* @param Builder $query
|
||||
* @param Point $point
|
||||
* @param object $location
|
||||
* @param int $distance
|
||||
* @return Builder
|
||||
*/
|
||||
public function scopeNear(Builder $query, Point $point, int $distance = 1000): Builder
|
||||
public function scopeNear(Builder $query, object $location, int $distance = 1000): Builder
|
||||
{
|
||||
$field = DB::raw(
|
||||
sprintf(
|
||||
"ST_Distance(%s.location, ST_GeogFromText('%s'))",
|
||||
$this->getTable(),
|
||||
$point->toWKT()
|
||||
)
|
||||
);
|
||||
$haversine = "(6371 * acos(cos(radians($location->latitude))
|
||||
* cos(radians(places.latitude))
|
||||
* cos(radians(places.longitude)
|
||||
- radians($location->longitude))
|
||||
+ sin(radians($location->latitude))
|
||||
* sin(radians(places.latitude))))";
|
||||
|
||||
return $query->where($field, '<=', $distance)->orderBy($field);
|
||||
return $query
|
||||
->select() //pick the columns you want here.
|
||||
->selectRaw("{$haversine} AS distance")
|
||||
->whereRaw("{$haversine} < ?", [$distance]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -146,26 +142,6 @@ class Place extends Model
|
|||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the latitude from the `location` property.
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function getLatitudeAttribute(): float
|
||||
{
|
||||
return $this->location->getLat();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the longitude from the `location` property.
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function getLongitudeAttribute(): float
|
||||
{
|
||||
return $this->location->getLng();
|
||||
}
|
||||
|
||||
/**
|
||||
* The Long URL for a place.
|
||||
*
|
||||
|
|
|
@ -6,7 +6,6 @@ namespace App\Services;
|
|||
|
||||
use App\Models\Place;
|
||||
use Illuminate\Support\Arr;
|
||||
use MStaack\LaravelPostgis\Geometries\Point;
|
||||
|
||||
class PlaceService
|
||||
{
|
||||
|
@ -32,7 +31,8 @@ class PlaceService
|
|||
$place = new Place();
|
||||
$place->name = $data['name'];
|
||||
$place->description = $data['description'];
|
||||
$place->location = new Point((float) $data['latitude'], (float) $data['longitude']);
|
||||
$place->latitude = $data['latitude'];
|
||||
$place->longitude = $data['longitude'];
|
||||
$place->save();
|
||||
|
||||
return $place;
|
||||
|
@ -62,10 +62,8 @@ class PlaceService
|
|||
$place = new Place();
|
||||
$place->name = Arr::get($checkin, 'properties.name.0');
|
||||
$place->external_urls = Arr::get($checkin, 'properties.url.0');
|
||||
$place->location = new Point(
|
||||
(float) Arr::get($checkin, 'properties.latitude.0'),
|
||||
(float) Arr::get($checkin, 'properties.longitude.0')
|
||||
);
|
||||
$place->latitude = Arr::get($checkin, 'properties.latitude.0');
|
||||
$place->longitude = Arr::get($checkin, 'properties.longitude.0');
|
||||
$place->save();
|
||||
|
||||
return $place;
|
||||
|
|
|
@ -30,7 +30,6 @@
|
|||
"league/commonmark": "^1.0",
|
||||
"league/flysystem-aws-s3-v3": "^1.0",
|
||||
"mf2/mf2": "~0.3",
|
||||
"mstaack/laravel-postgis": "~5.0",
|
||||
"pmatseykanets/laravel-scout-postgres": "^7.0",
|
||||
"predis/predis": "~1.0",
|
||||
"ramsey/uuid": "^3.5",
|
||||
|
|
1112
composer.lock
generated
1112
composer.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
use MStaack\LaravelPostgis\Schema\Blueprint;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
class CreatePlacesTable extends Migration
|
||||
|
@ -17,8 +17,8 @@ class CreatePlacesTable extends Migration
|
|||
$table->string('name');
|
||||
$table->string('slug')->unique();
|
||||
$table->text('description')->nullable();
|
||||
$table->point('location');
|
||||
$table->polygon('polygon')->nullable();
|
||||
$table->text('location');
|
||||
$table->text('polygon')->nullable();
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class RemoveLocationColumn extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('places', function (Blueprint $table) {
|
||||
$table->dropColumn('location');
|
||||
$table->dropColumn('polygon');
|
||||
});
|
||||
}
|
||||
}
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
use App\Models\Place;
|
||||
use Illuminate\Database\Seeder;
|
||||
use MStaack\LaravelPostgis\Geometries\Point;
|
||||
|
||||
class PlacesTableSeeder extends Seeder
|
||||
{
|
||||
|
@ -16,7 +15,8 @@ class PlacesTableSeeder extends Seeder
|
|||
$place = new Place();
|
||||
$place->name = 'The Bridgewater Pub';
|
||||
$place->description = 'A lovely local pub with a decent selection of cask ales';
|
||||
$place->location = new Point('53.4983', '-2.3805');
|
||||
$place->latitude = 53.4983;
|
||||
$place->longitude = -2.3805;
|
||||
$place->external_urls = 'https://foursquare.com/v/123435/the-bridgewater-pub';
|
||||
$place->external_urls = 'https://www.openstreetmap.org/way/987654';
|
||||
$place->save();
|
||||
|
|
|
@ -291,7 +291,8 @@ class MicropubControllerTest extends TestCase
|
|||
{
|
||||
$place = new Place();
|
||||
$place->name = 'Test Place';
|
||||
$place->location = new Point((float) 1.23, (float) 4.56);
|
||||
$place->latitude = 1.23;
|
||||
$place->longitude = 4.56;
|
||||
$place->save();
|
||||
$faker = \Faker\Factory::create();
|
||||
$note = $faker->text;
|
||||
|
|
|
@ -26,7 +26,7 @@ class PlacesTest extends TestCase
|
|||
*/
|
||||
public function test_near_method()
|
||||
{
|
||||
$nearby = Place::near(new Point(53.5, -2.38), 1000)->get();
|
||||
$nearby = Place::near((object) ['latitude' => 53.5, 'longitude' => -2.38], 1000)->get();
|
||||
$this->assertEquals('the-bridgewater-pub', $nearby[0]->slug);
|
||||
}
|
||||
|
||||
|
@ -53,7 +53,8 @@ class PlacesTest extends TestCase
|
|||
{
|
||||
$place = new Place();
|
||||
$place->name = 'Temp Place';
|
||||
$place->location = new Point(37.422009, -122.084047);
|
||||
$place->latitude = 37.422009;
|
||||
$place->longitude = -122.084047;
|
||||
$place->external_urls = 'https://www.openstreetmap.org/way/1234';
|
||||
$place->save();
|
||||
$service = new PlaceService();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue