Skip to content
This repository has been archived by the owner on Jul 27, 2023. It is now read-only.

Handle Polygon to google from Postgis. #110

Open
maurocrispin21 opened this issue Apr 3, 2018 · 3 comments
Open

Handle Polygon to google from Postgis. #110

maurocrispin21 opened this issue Apr 3, 2018 · 3 comments

Comments

@maurocrispin21
Copy link

maurocrispin21 commented Apr 3, 2018

I need to render polygons on google maps using this library.
I store my cordinateds as geomtry data type on postgis postgres [https://postgis.net/docs/geometry.html]. So my coordinates are not a human readable latlong array. Geometry data type cannot be handle due this library recieves arrays to define polygons

`$coords = [
new LatLng(['lat' => 25.774252, 'lng' => -80.190262]),
new LatLng(['lat' => 18.466465, 'lng' => -66.118292]),
new LatLng(['lat' => 32.321384, 'lng' => -64.75737]),
];

    $polygon = new Polygon([
      'paths' => $coords
    ]);`

Geometry data type has different data type outputs https://postgis.net/docs/reference.html#Geometry_Outputs.
One of that outputs is GeoJSON https://postgis.net/docs/ST_AsGeoJSON.html. That actually google maps api can manage according with google's api doc https://developers.google.com/maps/documentation/javascript/datalayer#load_geojson

How can I handle this type of data to yii2-google-maps-library???

@maurocrispin21 maurocrispin21 changed the title Handle MultiPolygon to google from Postgis. Handle Polygon to google from Postgis. Apr 3, 2018
@maurocrispin21
Copy link
Author

I could not find a proper way to do it. So I dumped geom into points then I used ST_X ST_Y to get points' lat/lon

@namgee
Copy link

namgee commented Sep 6, 2021

how do you do this? please help me

I solved a similar issue when generating polylines.

Model:

public function getLine($rp_zone)
    {
        return Map::findBySQL("
            SELECT 
                ST_Y((dp).geom) AS lat,
                ST_X((dp).geom) AS lng,
                ST_Y(centre) AS centre_lat,
                ST_X(centre) AS centre_lng
            FROM (
                SELECT ST_DumpPoints(geom) AS dp,
                ST_Centroid(geom) AS centre
                FROM traffic.map
                WHERE rp_zone = :rp_zone
            )x
        ",[':rp_zone' => $rp_zone])->asArray()->all();
    }

Controller:

public function actionView($id)
    {
        return $this->render('view', [
            'model' => $model = $this->findModel($id),
        ]);
    }

View:

    $coord = new LatLng([
        'lat' => $model->map->getLine($model->zone_id)[0]['centre_lat'],
        'lng' => $model->map->getLine($model->zone_id)[0]['centre_lng']
        ]);

    $map = new Map([
        'center' => $coord,
        'zoom' => 20,
    ]);

    foreach ($model->map->getLine($model->zone_id) as $coord) {
        $coords[] = new LatLng(['lat' => $coord['lat'], 'lng' => $coord['lng']]);
    }

    $polyline = new Polyline([
        'path' => $coords,
    ]);

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@namgee @maurocrispin21 and others