Remove the postgis stuff

This commit is contained in:
Jonny Barnes 2020-10-16 20:45:07 +01:00
parent 73fdd9a1ed
commit 3c3ad3e29d
10 changed files with 589 additions and 656 deletions

View file

@ -80,10 +80,8 @@ class PlacesController extends Controller
$place = Place::findOrFail($placeId); $place = Place::findOrFail($placeId);
$place->name = request()->input('name'); $place->name = request()->input('name');
$place->description = request()->input('description'); $place->description = request()->input('description');
$place->location = new Point( $place->latitude = request()->input('latitude');
(float) request()->input('latitude'), $place->longitude = request()->input('longitude');
(float) request()->input('longitude')
);
$place->icon = request()->input('icon'); $place->icon = request()->input('icon');
$place->save(); $place->save();
@ -99,7 +97,7 @@ class PlacesController extends Controller
public function mergeIndex(int $placeId): View public function mergeIndex(int $placeId): View
{ {
$first = Place::find($placeId); $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 = []; $places = [];
foreach ($results as $place) { foreach ($results as $place) {
if ($place->slug !== $first->slug) { if ($place->slug !== $first->slug) {

View file

@ -142,7 +142,10 @@ class MicropubController extends Controller
$matches $matches
); );
$distance = (count($matches[0]) == 3) ? 100 * $matches[0][2] : 1000; $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([ return response()->json([
'response' => 'places', 'response' => 'places',

View file

@ -340,7 +340,7 @@ class Note extends Model
public function getLatitudeAttribute(): ?float public function getLatitudeAttribute(): ?float
{ {
if ($this->place !== null) { if ($this->place !== null) {
return $this->place->location->getLat(); return $this->place->latitude;
} }
if ($this->location !== null) { if ($this->location !== null) {
$pieces = explode(':', $this->location); $pieces = explode(':', $this->location);
@ -360,7 +360,7 @@ class Note extends Model
public function getLongitudeAttribute(): ?float public function getLongitudeAttribute(): ?float
{ {
if ($this->place !== null) { if ($this->place !== null) {
return $this->place->location->getLng(); return $this->place->longitude;
} }
if ($this->location !== null) { if ($this->location !== null) {
$pieces = explode(':', $this->location); $pieces = explode(':', $this->location);

View file

@ -9,37 +9,32 @@ use Eloquent;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\{Builder, Collection, Model}; use Illuminate\Database\Eloquent\{Builder, Collection, Model};
use Illuminate\Support\Carbon; use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use MStaack\LaravelPostgis\Eloquent\PostgisTrait;
use MStaack\LaravelPostgis\Geometries\Point;
/** /**
* App\Models\Place. * App\Models\Place
* *
* @property int $id * @property int $id
* @property string $name * @property string $name
* @property string $slug * @property string $slug
* @property string|null $description * @property string|null $description
* @property Point $location
* @property mixed|null $polygon
* @property Carbon|null $created_at * @property Carbon|null $created_at
* @property Carbon|null $updated_at * @property Carbon|null $updated_at
* @property string|null $icon * @property string|null $icon
* @property string|null $foursquare * @property string|null $foursquare
* @property mixed|null $external_urls * @property mixed|null $external_urls
* @property-read float $latitude * @property float|null $latitude
* @property-read float $longitude * @property float|null $longitude
* @property-read string $longurl * @property-read string $longurl
* @property-read string $shorturl * @property-read string $shorturl
* @property-read string $uri * @property-read string $uri
* @property-read Collection|Note[] $notes * @property-read Collection|\App\Models\Note[] $notes
* @property-read int|null $notes_count * @property-read int|null $notes_count
* @method static Builder|Place findSimilarSlugs($attribute, $config, $slug) * @method static Builder|Place findSimilarSlugs($attribute, $config, $slug)
* @method static Builder|Place near(Point $point, $distance = 1000) * @method static Builder|Place near($location, $distance = 1000)
* @method static \MStaack\LaravelPostgis\Eloquent\Builder|Place newModelQuery() * @method static Builder|Place newModelQuery()
* @method static \MStaack\LaravelPostgis\Eloquent\Builder|Place newQuery() * @method static Builder|Place newQuery()
* @method static \MStaack\LaravelPostgis\Eloquent\Builder|Place query() * @method static Builder|Place query()
* @method static Builder|Place whereCreatedAt($value) * @method static Builder|Place whereCreatedAt($value)
* @method static Builder|Place whereDescription($value) * @method static Builder|Place whereDescription($value)
* @method static Builder|Place whereExternalURL($url) * @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 whereFoursquare($value)
* @method static Builder|Place whereIcon($value) * @method static Builder|Place whereIcon($value)
* @method static Builder|Place whereId($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 whereName($value)
* @method static Builder|Place wherePolygon($value)
* @method static Builder|Place whereSlug($value) * @method static Builder|Place whereSlug($value)
* @method static Builder|Place whereUpdatedAt($value) * @method static Builder|Place whereUpdatedAt($value)
* @mixin Eloquent * @mixin Eloquent
@ -57,7 +52,6 @@ use MStaack\LaravelPostgis\Geometries\Point;
class Place extends Model class Place extends Model
{ {
use Sluggable; use Sluggable;
use PostgisTrait;
/** /**
* Get the route key for the model. * Get the route key for the model.
@ -77,13 +71,13 @@ class Place extends Model
protected $fillable = ['name', 'slug']; protected $fillable = ['name', 'slug'];
/** /**
* The attributes that are Postgis geometry objects. * The attributes that should be cast.
* *
* @var array * @var array
*/ */
protected $postgisFields = [ protected $casts = [
'location', 'latitude' => 'float',
'polygon', 'longitude' => 'float',
]; ];
/** /**
@ -115,21 +109,22 @@ class Place extends Model
* Select places near a given location. * Select places near a given location.
* *
* @param Builder $query * @param Builder $query
* @param Point $point * @param object $location
* @param int $distance * @param int $distance
* @return Builder * @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( $haversine = "(6371 * acos(cos(radians($location->latitude))
sprintf( * cos(radians(places.latitude))
"ST_Distance(%s.location, ST_GeogFromText('%s'))", * cos(radians(places.longitude)
$this->getTable(), - radians($location->longitude))
$point->toWKT() + sin(radians($location->latitude))
) * sin(radians(places.latitude))))";
); return $query
->select() //pick the columns you want here.
return $query->where($field, '<=', $distance)->orderBy($field); ->selectRaw("{$haversine} AS distance")
->whereRaw("{$haversine} < ?", [$distance]);
} }
/** /**
@ -146,26 +141,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. * The Long URL for a place.
* *

View file

@ -32,7 +32,8 @@ class PlaceService
$place = new Place(); $place = new Place();
$place->name = $data['name']; $place->name = $data['name'];
$place->description = $data['description']; $place->description = $data['description'];
$place->location = new Point((float) $data['latitude'], (float) $data['longitude']); $place->latitude = $data['latitude'];
$place->longitude = $data['longitude'];
$place->save(); $place->save();
return $place; return $place;
@ -62,10 +63,8 @@ class PlaceService
$place = new Place(); $place = new Place();
$place->name = Arr::get($checkin, 'properties.name.0'); $place->name = Arr::get($checkin, 'properties.name.0');
$place->external_urls = Arr::get($checkin, 'properties.url.0'); $place->external_urls = Arr::get($checkin, 'properties.url.0');
$place->location = new Point( $place->latitude = Arr::get($checkin, 'properties.latitude.0');
(float) Arr::get($checkin, 'properties.latitude.0'), $place->longitude = Arr::get($checkin, 'properties.longitude.0');
(float) Arr::get($checkin, 'properties.longitude.0')
);
$place->save(); $place->save();
return $place; return $place;

View file

@ -30,7 +30,6 @@
"league/commonmark": "^1.0", "league/commonmark": "^1.0",
"league/flysystem-aws-s3-v3": "^1.0", "league/flysystem-aws-s3-v3": "^1.0",
"mf2/mf2": "~0.3", "mf2/mf2": "~0.3",
"mstaack/laravel-postgis": "~5.0",
"pmatseykanets/laravel-scout-postgres": "^7.0", "pmatseykanets/laravel-scout-postgres": "^7.0",
"predis/predis": "~1.0", "predis/predis": "~1.0",
"ramsey/uuid": "^3.5", "ramsey/uuid": "^3.5",

1112
composer.lock generated

File diff suppressed because it is too large Load diff

View file

@ -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');
});
}
}

View file

@ -291,7 +291,8 @@ class MicropubControllerTest extends TestCase
{ {
$place = new Place(); $place = new Place();
$place->name = 'Test 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(); $place->save();
$faker = \Faker\Factory::create(); $faker = \Faker\Factory::create();
$note = $faker->text; $note = $faker->text;

View file

@ -26,7 +26,7 @@ class PlacesTest extends TestCase
*/ */
public function test_near_method() 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); $this->assertEquals('the-bridgewater-pub', $nearby[0]->slug);
} }
@ -53,7 +53,8 @@ class PlacesTest extends TestCase
{ {
$place = new Place(); $place = new Place();
$place->name = 'Temp 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->external_urls = 'https://www.openstreetmap.org/way/1234';
$place->save(); $place->save();
$service = new PlaceService(); $service = new PlaceService();