Comments (13)
Something like this. (Have not tested)
NOTE - the main important change is from
new Point($point[0], $point[1])
to
new Point($point[1], $point[0])
private function buildPolygonFromArray($polygon) {
return new Polygon(
new LineString(
collect($polygon['coordinates'][0])->map(function($point){
return new Point($point[1], $point[0]);
})->toArray()
)
);
}
from laravel-mysql-spatial.
Hi Josep and David h @grimzy @davidpiesse
Can you please help me with this one? I'll really appreciate it.
Thanks.
from laravel-mysql-spatial.
Up @grimzy , anyone has an answer please?
from laravel-mysql-spatial.
When saving a Point
into a MySQL database it has to be saved using Point(longitude latitude)
(Well Known Text - WKT).
In the same manner, but this time for output:
- when encoding a
Point
object toJSON
the package follows theGeoJSON
specification wherePoint
coordinates definition is[0 => longitude, 1 => latitude]
- when encoding a
Point
object toString
the package follows the WKT specification:Point(longitude latitude)
- the same as when saving.
I'm not sure how to guide you as I'm not sure how you are planning to use the Polygon
but my best guess would be for you to loop through the coordinates array
in the GeoJSON
and use the coordinates as you please.
Regarding, getPolygonAttribute()
, is this from the integration of spatial fields added in Laravel 5.5? If so, it's not compatible with Grimzy\LaravelMysqlSpatial\Types\Polygon
.
PS @llioor: sorry for the late answer!
from laravel-mysql-spatial.
Sounds like it's just that!
No idea why Lon,Lat is used by so many places when most say Lat,Lon.
Do you input lat,Lon expecting that to persist to the end result but they get flipped?
from laravel-mysql-spatial.
@davidpiesse, my best guess is that the specifications are the same when using geographic fields (longitude/latitude) or geometry fields (X/Y).
I decided to define the Point
constructor as Point($lat, $lng)
because it felt more intuitive when reading/using it.
from laravel-mysql-spatial.
Hi Guys.. @davidpiesse @grimzy
Thanks for the answer, I really appreciate it!
The problem is:
- I create array of
$arrayOfPoints[] = new Point(Lat, Long)
[34.783342, 32.075312],
[34.782876, 32.075323],
[34.782862, 32.074953],
[34.783407, 32.07494],
[34.783342, 32.075312],
- I create the lineString with this array
new LineString($arrayOfPoints);
- I crate the polygon with the lineString
new Polygon([$lineString]);
- Then I just set it to the Class spatial parameter and I save the class
$item = new Item($data);
...
$item->polygon = $this->buildPolygonFromArray($data['polygon']);
$item->save();
- The polygon is been saved automatically on DB in this format:
'POLYGON((32.075312 34.783342,32.075323 34.782876,32.074953 34.782862,32.07494 34.783407,32.075312 34.783342))',0
The problem is when I select the class like:
$item = Item::find(1);
return $item->polygon;
This is what I receive:
{"type":"Polygon","coordinates":[[[32.075312,34.783342],[32.075323,34.782876],[32.074953,34.782862],[32.07494,34.783407],[32.075312,34.783342]]]}
You can see that the points in coordinates
are flipped. Long,Lat instead how I saved them Lat,Long...
As I understood from your answer I will need to go over the array and to flip it manually to Lat,Long format? sounds wrong...
What do you think?
from laravel-mysql-spatial.
Pretty much.
I would suggest as a refactoring exercise using collections for the coord array conversion to polygon.
It should be as simple as switching the [0] and [1] on your coord array when you create the points
from laravel-mysql-spatial.
Hi @davidpiesse ,
Sorry I didn't understand you =\
from laravel-mysql-spatial.
@davidpiesse thank you very much!
Polygon wants to get array so this is the right code:
private function buildPolygonFromArray($polygon) {
return new Polygon([ // here open array
new LineString(
collect($polygon['coordinates'][0])->map(function($point){
return new Point($point[1], $point[0]);
})->toArray()
)] // here close array
);
}
I still do not agree with Point($point[1], $point[0]);
because Point __construct() receive ($lat, $lng) so why should I fake it like ($lng, $lat). @grimzy please consider it.
Guys, thank you very much for the help.
from laravel-mysql-spatial.
why in document you say
new Point(40.7484404, -73.9878441); // (lat, lng)
from laravel-mysql-spatial.
for fix flip lng,lat use this
https://github.com/vahidalvandi/laravel-mysql-spatial
from laravel-mysql-spatial.
I still do not agree with
Point($point[1], $point[0]);
because Point __construct() receive ($lat, $lng) so why should I fake it like ($lng, $lat). @grimzy please consider it.
I don't like it either, it makes sense though. The only case in which the package uses lat,lng is when creating a point, it reverses it, and then everything works with lng, lat.
Given that when you pull the information from the database it's flipped, you have to flip it again if you want to use Point, because it only receives lat,lng.
I think that having a way to pull from the database the coords as they were inserted would be good.
from laravel-mysql-spatial.
Related Issues (20)
- This makes funny things to the DB, and then break stuff…
- Syntax error with Laravel 8, MariaDB 10.6 and SRID HOT 2
- Migration from 4.X to 5.0.0 breaks SRID HOT 2
- Error in save or retrieve HOT 1
- Not able to update geometry field with MultiPoint HOT 1
- Does it work with PHP8.1 ? HOT 4
- Laravel 9 Support HOT 19
- MariaDB doesnt support creating a geometrycolumn with an SRID
- Polygon GeoJson String Validation
- laravel 9 HOT 2
- Invalid parameter value: 3037 Invalid GIS data provided to function st_geometryfromtext
- Cast request spatial data to Eloquent model via fill method is not possible
- Cast request spatial data to Eloquent model via fill method is not possible HOT 1
- Should support sqlite / spatiallite
- {"message":"Call to a member function getLat() on string"}
- Slow Cache retrieval
- Laravel 10 HOT 3
- Laravel 9 Issue HOT 3
- Any Update for Laravel 10 HOT 2
- Type error HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-mysql-spatial.