laravel-mysql-spatial icon indicating copy to clipboard operation
laravel-mysql-spatial copied to clipboard

Let SpatialExpression implement the toWkt() method

Open vpratfr opened this issue 4 years ago • 1 comments

This avoids that most spatial query functions fail when invoked from observer methods.

E.g.

Test setup

    $incomingMessage = new RawMessage();
    $incomingMessage->beacon_id = $boat->beacon_id;
    $incomingMessage->boat_id = $boat->id;
    $incomingMessage->timestamp = CarbonImmutable::now();
    $incomingMessage->type = MessageType::LOCATION_UPDATE();
    $incomingMessage->location_precision = 5;
    $incomingMessage->coordinates = new Point(5, 5);
    $incomingMessage->save();

Observer on RawMessage created() event

    $weatherAlerts = WeatherAlert::ongoing()
        ->atLocation($rawMessage->coordinates)
        ->get();

Crash

Error : Call to undefined method Grimzy\LaravelMysqlSpatial\Eloquent\SpatialExpression::toWkt()

Basically, in the observer, when doing ->atLocation($rawMessage->coordinates), the coordinates getter returns a SpatialExpression and not the original Point

This PR simply implements the toWkt method, in a harmless and perfectly safe way as it does the same thing as the existing getSpatialValue method

Fixes #115

vpratfr avatar Apr 06 '20 10:04 vpratfr

I did not add any test as SpatialExpression does not have any of them and the code is very straightforward.

vpratfr avatar Apr 06 '20 11:04 vpratfr