MatanYadaev / laravel-eloquent-spatial

Laravel Eloquent spatial package.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Update Readme

tomavic opened this issue · comments

commented

Hello,

At first I would like to thank you for this package. ❤️❤️ You are a life saver.

My first impression about this package is awesome, and it satisfies all conditions (at least for me) 😂

But I would like to know how to use Spatial functions ST_Distance_Sphere
By chance, I found the API.md file and it has a pretty well description however it does not contain more examples.

What I want to do is, instead of creating Database procedures, I want to use ST_Distance_Sphere

Here is a code sample of my project:

        // ! show providers listed with in particular distance
        $subqueryDistanceFrom = "ST_Distance_Sphere(Point(trips.longitude_from,"
            . " trips.latitude_from),"
            . " Point($request->long_from, $request->lat_from ))"
            . " as distance_from";
        $subqueryDistanceTo = "ST_Distance_Sphere(Point(trips.longitude_to,"
            . " trips.latitude_to),"
            . " Point($request->long_to, $request->lat_to))"
            . " as distance_to";

        $subqueryDistanceReturnFrom = "ST_Distance_Sphere(Point(trips.longitude_to,"
            . " trips.latitude_to),"
            . " Point($request->long_from, $request->lat_from ))"
            . " as distance_return_from";
        $subqueryDistanceReturnTo = "ST_Distance_Sphere(Point(trips.longitude_from,"
            . " trips.latitude_from),"
            . " Point($request->long_to, $request->lat_to))"
            . " as distance_return_to";


        $subqueryDistanceWhereFrom = "ST_Distance_Sphere(Point(trips.longitude_from,"
            . " trips.latitude_from),"
            . " Point($request->long_from, $request->lat_from ))"
            . " < " . $distanceDelta;

        $subqueryDistanceWhereTo = "ST_Distance_Sphere(Point(trips.longitude_to,"
            . " trips.latitude_to),"
            . " Point($request->long_to, $request->lat_to ))"
            . " < " . $distanceDelta;

        $subqueryDistanceWhereReturnFrom = "ST_Distance_Sphere(Point(trips.longitude_to,"
            . " trips.latitude_to),"
            . " Point($request->long_from, $request->lat_from ))"
            . " < " . $distanceDelta;

        $subqueryDistanceWhereReturnTo = "ST_Distance_Sphere(Point(trips.longitude_from," . " trips.latitude_from),"
            . " Point($request->long_to, $request->lat_to ))" . " < " . $distanceDelta;

I appreciate your help 😊

@tomavic Hi, thanks for the compliments. I'm not sure I understand what do you need and how can I help. Can you please re-explain yourself? Please make it easy for me to understand your needs.

commented

I have an old project with Laravel 6.x which use this SQL procedure ST_Distance_Sphere. Now I am using Laravel 9.x and I am doing code refactor.

I have table called trips which has the following attributes as DOUBLE:

  • latitude_from
  • longitude_from
  • longitude_to
  • latitude_to

Now, I wanted to use a clean way to use ST_Distance_Sphere.

I have successfully created new attributes with POINT and POLYGON, and I want to use your function whereDistanceSphere

How can I achieve that?

Thanks

If you have geometry columns in your table it should work like that: Trip::query()->whereDistanceSphere('from', 'to', 10000)->get()

commented

@MatanYadaev

In my code snippet there are two usages

        $subqueryDistanceFrom = "ST_Distance_Sphere(Point(trips.longitude_from,"
            . " trips.latitude_from),"
            . " Point($request->long_from, $request->lat_from ))"
            . " as distance_from";
        $subqueryDistanceWhereFrom = "ST_Distance_Sphere(Point(trips.longitude_from,"
            . " trips.latitude_from),"
            . " Point($request->long_from, $request->lat_from ))"
            . " < " . $distanceDelta;

When I use Trip::query()->whereDistanceSphere('trips.long_from_point', Point(lat_from, lat_to), '<')

I get Expected 4 arguments. Found 3.intelephense(1005).

===========

Actually this is not my biggest issue here. The problem is that there is no examples inside the README to validate the output of each method. At least there should be test data to be mocked.

My Second question about my code snippet above, I've searched a lot for ST_Distance_Sphere and simple & clean ways to use it instead of DB queries.

@tomavic

  1. You can't use this package if you don't have spatial columns in your DB. It seems like you have a column for latitude and a column for longitude. In order to use this package, you should have one point column.
  2. If you are looking for a way to calculate distance not in the DB, you can use the Geokit package.