Giter Site home page Giter Site logo

Comments (13)

davidpiesse avatar davidpiesse commented on July 19, 2024 4

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.

llioor avatar llioor commented on July 19, 2024

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.

llioor avatar llioor commented on July 19, 2024

Up @grimzy , anyone has an answer please?

from laravel-mysql-spatial.

grimzy avatar grimzy commented on July 19, 2024

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 to JSON the package follows the GeoJSON specification where Point coordinates definition is [0 => longitude, 1 => latitude]
  • when encoding a Point object to String 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.

davidpiesse avatar davidpiesse commented on July 19, 2024

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.

grimzy avatar grimzy commented on July 19, 2024

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

llioor avatar llioor commented on July 19, 2024

Hi Guys.. @davidpiesse @grimzy
Thanks for the answer, I really appreciate it!
The problem is:

  1. 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],
  1. I create the lineString with this array new LineString($arrayOfPoints);
  2. I crate the polygon with the lineString new Polygon([$lineString]);
  3. 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();
  1. 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.

davidpiesse avatar davidpiesse commented on July 19, 2024

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.

llioor avatar llioor commented on July 19, 2024

Hi @davidpiesse ,
Sorry I didn't understand you =\

from laravel-mysql-spatial.

llioor avatar llioor commented on July 19, 2024

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

vahidalvandi avatar vahidalvandi commented on July 19, 2024

why in document you say

new Point(40.7484404, -73.9878441); // (lat, lng)

from laravel-mysql-spatial.

vahidalvandi avatar vahidalvandi commented on July 19, 2024

for fix flip lng,lat use this

https://github.com/vahidalvandi/laravel-mysql-spatial

from laravel-mysql-spatial.

dhcmega avatar dhcmega commented on July 19, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.