4

I am trying to save the coordinates of polygon drawn in google map into postgis as polygon datatype.

currently coordinates are JSON type.

But drying manager is not giving me the coordinates of last point and this is correct because in polygon the coordinates of last point is same as first point. For ex- the coordinates are-

{"type":"polygon","coordinates":[[40.48873742102281,-94.471435546875],[39.66491373749128,-94.7021484375],[40.17047886718109,-93.548583984375]]}

Here drawing manager is not giving the coordinates of last point which is same as first point, so while saving them in postgis, the error is-

column "column_name" is of type polygon but expression is of type geometry

I am using-

"INSERT into table_name (column_name) VALUES (ST_GeomFromGeoJSON('".($coordinates) ."'))"; 

column_name is of polygon type.

Edit- Code used to create polygons-

drawingManager.setMap(map);
    google.maps.event.addListener(drawingManager, 'overlaycomplete', function(polygon) {
var coordinates_poly = polygon.overlay.getPath().getArray();

    var lat_poly = [];
    var lng_poly = [];
    var Coordinates = [];
    for(var i = 0; i <coordinates_poly.length; i++){
    lat_poly = coordinates_poly[i].lat();
    lng_poly = coordinates_poly[i].lng();
    Data = [lat_poly,lng_poly];
    Coordinates.push(Data);
    }
    var JSON_Coordinates = JSON.stringify(Coordinates);
    document.getElementById("data").value= "{\"type\":\"polygon\",\"coordinates\":" + JSON_Coordinates +"}";
    });
    }

Where "data" is form element.

Someone please help me how to save coordinates as polygon type.

  • You seem to have only three points but for a minimal polygon, triangle, you need four points. – user30184 Jan 29 '18 at 13:36
  • thats what I am saying. I am drawing a triangle, for example,and the drawing manager is giving coordinates of first, second and third point not the coordinates of closing point that is first point. – ashes Jan 29 '18 at 13:39
  • If you mean this developers.google.com/maps/documentation/javascript/… then you should show your code. – user30184 Jan 29 '18 at 13:44
  • I have added my code, please guide me where I am lagging. – ashes Jan 29 '18 at 13:53
4

ST_GeomFromGeoJSON is a PostGIS function that returns a PostGIS geometry, which in turn can hold a polygon. Therefore, it's ouptut cannot be used to populate a Postgres Polygon column.

Since you are using Postgis, the solution would be to update your table to use the proper geometry type.

That being said, you are swapping coordinates in your code. They must be set as Longitude first, then Latitude. Then you are missing a set of [] around the coordinates. I would recommend reading this guide.

Note that ST_GeomFromGeoJSON will add the missing coordinate (the repetition of the 1st coordinate) for you, though you could do it in your code

google.maps.event.addListener(drawingManager, 'overlaycomplete', function(polygon) {
    var coordinates_poly = polygon.overlay.getPath().getArray();

    var lat_poly = [];
    var lng_poly = [];
    var Coordinates = [];
    for(var i = 0; i <coordinates_poly.length; i++){
      lat_poly = coordinates_poly[i].lat();
      lng_poly = coordinates_poly[i].lng();

      //Long - Lat
      Data = [lng_poly,lat_poly];
      Coordinates.push(Data);
    }

    //Add first coordinate 
    Data = [coordinates_poly[0].lng(),coordinates_poly[0].lat()];
    Coordinates.push(Data);

    var JSON_Coordinates = JSON.stringify(Coordinates);

    //Add missing []
    document.getElementById("data").value= "{\"type\":\"polygon\",\"coordinates\":[" + JSON_Coordinates +"]}";
    });
 }
  • Thanks. It worked. But when I am using "INSERT INTO table_name (column_name) VALUES(ST_SetSRID(ST_GeomFromGeoJSON('".($coordinates)."')),4326)"; It says -"Query failed: ERROR: function st_setsrid(geometry) does not exist " How to resolve this? – ashes Jan 31 '18 at 14:05
  • That was a syntax error "INSERT INTO table_name (column_name) VALUES(ST_SetSRID(ST_GeomFromGeoJSON('".($coordinates)."'),4326))"; worked fine. – ashes Feb 1 '18 at 10:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.