Draw polygon in maps api v3 and store it in postgresql geometric column

1.- The user will draw a polygon in the web application.

2.- When user clicks "Save", I want to save the data to postgresql database.

I am having problems with polygon coordinates ...

POLYGON VALUE COORDINATES IN THE TEXT FIELD, THIS WATCH THIS:

(13.068776734357694, -65.50735473632812), (6.795535025719505, -62.123565673828125), (7.928674801364048, -70.78079223632812)

I GOT THEM WHEN THE USER SHOWS THE MAP

google.maps.event.addListener(drawingManager,'polygoncomplete',function(polygon) {

// complete functions
var coordinates = (polygon.getPath().getArray());
$("#coordinates").val(coordinates);

});

      

SO FAR SO GOOD, when I passed these values ​​in the form and try to store them in the DB, I got this error:

Fatal error: Throw "PDOException" with message "SQLSTATE [XX000]: Internal error: 7 ERROR: Parse error - invalid geometry HINT:" (error 1 "& parse at position 2 in geometry 'in

PHP

$stmt = $link->prepare("INSERT INTO industrias.industrias (rif,nombre,geom) VALUES (:rif,:nombre,:coor)");
$stmt->execute(array(':rif'=>$_POST["rif"],':nombre'=>$_POST["nombre_empresa"],':coor'=>$_POST["coordinates"]));

      

QUESTION 1:

Is there any other function that gets these coordinates in another template that will fit into the column type (Polygon, 4326) ?

QUESTION 2:

Can I use some postgis function in SQL STATEMENT to make it work? something with WKT or something else?

+3


source to share


1 answer


I am doing a geo fencing module where I am saving a lot of shapes to the database. I don't keep coordinated like that.

I am using google algorithem encoding to encode the path of polygons and polylines and then store the encoded string in a data file.

https://developers.google.com/maps/documentation/javascript/examples/geometry-encodings

circles can be saved as lat, lon and radius.

it will be an easy and stable solution in the long run.

If you need coordinates, just decode them using the available google api encoding functions.



Update:

I can't show the app as mine against my company policy, but some code clips to help you:

when finishing drawing:

google.maps.event.addDomListener(drawingManager, 'polygoncomplete', function(polygon) {      
          drawingManager.setDrawingMode(null); // disable drawing mode
          CreateCourseRegionPoly(polygon);          
      });

      

I am doing below to save to database using rest service (note the uri encoding as some encoded characters may appear in encoding):

function CreateCourseRegionPoly(poly){
var polypath= poly.getPath();
var encodeString = google.maps.geometry.encoding.encodePath(polypath); 
   $.ajax({
      url: '../RestApi/CreateRegion',
      type: 'POST',
      contentType: 'application/json',
      cache: false,
      dataType: 'json',          
      data: '{"CourseID":"'+ courseID +'","PolygonPath":"'+encodeURIComponent(encodeString)+'"}',
      error: function (jqXHR, textStatus, errorThrown) {
        alert("Messaging failed: " + errorThrown);
      },
       success: function (data) {
       //d oyour sucess bit here...
       }
    });
}

      

0


source







All Articles