I'm using the draw function of OpenLayers for draw a polygon. I need the wkt from the drawn geometry because I need to send it to PostGIS.

  <textarea name="name" rows="20" cols="80"

draw.on('drawend', function(evt){
    var feature = evt.feature;
    tableSRID = 4326;
    epsg = 'EPSG:' + tableSRID;

    var format = new ol.format.WKT()
    wkt = format.writeGeometry(evt.feature.getGeometry().transform('EPSG:3857',epsg))
    console.log('Geometry\n' + wkt + '\n\n')

    completeWKT = 'SRID=' + tableSRID + ';' + wkt
    console.log('WKT with SRID\n' + completeWKT + '\n\n')
    document.getElementById('coordinates').innerHTML = completeWKT;


I can see correctly the wkt into the console.log, I've test it using this service. The problem cames at the end of the draw because I see on the map the geometry with epsg 4326 instead of 3857.

This is one of polygons that I've drawn:

MULTIPOLYGON(((1400149.5641348532 5271139.432674438,1373243.7301784712 5211824.298725141,1452126.7433687733 5203874.847783483,1465579.6603469641 5288261.327010318,1400149.5641348532 5271139.432674438)))

and this is the projected polygon from wkt:

MULTIPOLYGON(((12.577757535156248 42.731151168561155,12.336058316406248 42.33852359891077,13.04467648046875 42.28571587692704,13.165526089843748 42.84402771111729,12.577757535156248 42.731151168561155)))

I see on the map the first MULTIPOLYGON and not his reprojection from wkt.

How I can solve?

NB: I never yet implemented the code for send the wkt to PostGIS, I need to find a solution using Django Rest Framework GIS. But this is another question.

1 Answer 1


You must either clone the geometry before transforming it, or you can specify projections as options to writeGeometry


wkt = format.writeGeometry(evt.feature.getGeometry().clone().transform('EPSG:3857',epsg))


wkt = format.writeGeometry(evt.feature.getGeometry(), {dataProjection: epsg, featureProjection: 'EPSG:3857'});
  • Thank you! What do you think is better? They are equivalent solutions and both working fine. May 13, 2020 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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