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->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();
@ -99,7 +97,7 @@ class PlacesController extends Controller
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) {

View file

@ -142,7 +142,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',

View file

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

View file

@ -9,37 +9,32 @@ 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.
* App\Models\Place
*
* @property int $id
* @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,22 @@ 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()
)
);
return $query->where($field, '<=', $distance)->orderBy($field);
$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
->select() //pick the columns you want here.
->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.
*

View file

@ -32,7 +32,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 +63,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;

View file

@ -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

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->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;

View file

@ -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();