-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support ST_CENTROID
spatial function
#95
Comments
ST_Centroid
spatial function
ST_Centroid
spatial functionST_CENTROID
spatial function
@leandrocaplan Hi, let me know if it works for you: https://github.com/MatanYadaev/laravel-eloquent-spatial/blob/master/API.md#withcentroid laravel-eloquent-spatial/tests/SpatialBuilderTest.php Lines 375 to 407 in 30e0a85
|
Hi Matan! Sorry for the delay of my answer. I've had a few difficult and busy days, and I'm just now able to give you a proper feedback. First of all, I saw that you updated the package with the new 'withCentroid' method, just after I've posted this issue. I really appreciate that goodwill! Answering you question, asking if the code shown in 'API.md', as an example of usage of the new 'withCentroid' method, worked for me, I can tell you my answer is: The main ones were: -When applying 'withCast()' in the chain after 'withCentroid()':
-When removing that method in the chain, but just executing the query with 'first()' or 'get()':
We should take into consideration that, since Eloquent is a ORM, every Eloquent model and accordingly, every Eloquent Query Builder instance, it's related to a table in our database. However, these kind of functions of MySQL Spatial, such as 'ST_Centroid' or 'ST_GeomFromGeoJSON', doesn't always retrieve data from a table to perform the needed query, since they just return a geometry object based on the values of another geometry, which may be coded directly in the query text. For example: "select ST_CENTROID(ST_GeomFromText('POLYGON((-58.420486450195 -34.619216934912, ... , -58.420486450195 -34.619216934912))', 0)) AS In this way, we may need an instance of 'Illuminate\Database\Query\Builder' (Laravel Query Builder) instead of an instance of 'Illuminate\Database\Eloquent\Builder' (Eloquent Query Builder), which 'MatanYadaev\EloquentSpatial\SpatialBuilder' inherits from, to perform those kind of queries properly, since we don't want to generate those queries from a given Eloquent model. So, in the example of API.md, we have the line:
Being 'Place' an Eloquent ORM model, which creates a new record into our database just for calculating the centroid of a given polygon. This is something we don't want and no need to. So, I'm sharing you here, the section of my code that at least could use, somewhere in it, the new 'withCentroid()' method to achieve its purpose, with the lines which throwed error commented and describing the error:
May you tell me if there's a more proper way to achieve this same result by using this package? Thanks a lot! |
@leandrocaplan can you please fork this repo and reproduce this issue with a failing test? Or even a fresh new Laravel project? |
@MatanYadaev I'll create a new branch of the project in my own repo with the failure, and last version of laravel-eloquent-spatial defined into 'composer.json', that would be OK? |
@leandrocaplan Hi, there are two issues:
There is a hacky workaround, if you want: $emptyModel = new class extends Model {};
$query = (new SpatialBuilder(DB::query()))->setModel($emptyModel);
$centroid = $query
->withCentroid($polygon)
->withCasts(['centroid' => Point::class])
->from(null)
->first()['centroid']; |
@MatanYadaev That worked pretty fine! Just notice this: if you check the API doc, at 'withCentroid' example, it shows:
Instead of:
Besides we should have 'withCasts' instead of 'withCast', we must pass it an array, instead of two arguments like the doc is currently showing. Thanks a lot! |
@leandrocaplan I forgot to push a commit :) Now it's live. Thanks! |
I'm using Laravel Eloquent Spatial in my project.
Despite I've could used it pretty well when selecting Point objects within a Polygon geometry, I had to create a raw SQL request to use those MySQL Spatial methods.
Here's my code of a method in my app, used to return a Point geometry with the centroid of a polygon:
Is there any way to achieve this using Laravel Eloquent Spatial?
The text was updated successfully, but these errors were encountered: