OL3 Polygon WKT – How to Get WKT Representation of a Polygon

javascriptopenlayers

I have a vector layer in OpenLayers 3 that I want to export to a shapefile, I take the geometry of the polygon. In an attempt to fix an issue with my WKT (see this question) I add the first coordinate to end of the list of coordinates, but when exporting the file with these last coordinate the polygon is drawn with a line joining the last points of the polygons, there are any way to draw the polygon without these points, or another way to take the geometry without this issue?

An example of the polygon generated:

enter image description here

enter image description here

Here the example of the coordinates (to open in QGIS):

https://drive.google.com/file/d/0B6CZzmxH4VxrZi16eEM2ZmFKWmc/view?usp=sharing

I'm using this code:

Take the geometry:

     .....
    {
        var featureGeom = [];
        var arreglo = [];
        var arreglo2 = [];

        var controlador = aplicacion.getController("ControlResultados");        
        var vectorSeleccion = controlador.buscarcapa('capaSeleccion').getSource().getFeatures();

        for (var x = 0; x < vectorSeleccion.length; x++) {
            featureGeom.push(vectorSeleccion[x].getGeometry().getCoordinates());
            for (var y = 0; y < featureGeom[x][0].length; y++) {
                arreglo.push(featureGeom[x][0][y]);
            }
        }

        for (var z = 0; z < arreglo.length; z++) {
            arreglo2.push(arreglo[z][0]+" "+arreglo[z][1]);
        }

        var nombreArchivo = "archivoSHPResultado";
        var parametros = {
                archivoPersistencia : nombreArchivo,
                geomPolygon : "POLYGON(("+arreglo2.toString()+"))",
                nombreArchivo: 'CapaSHPResultado'
        };
        this.descargaArchivoSHP(parametros);
    },
    descargaArchivoSHP: function(contenido){
        Ext.Ajax.request({
            url : "ServletShape",
            method : 'POST',
            params : contenido,
            success : function( respuesta ) {
                var rutaActual = location.href.split("//");
                var rutaActualPartes = rutaActual[1].split("/");
                var nuevaRuta = rutaActual[0] + "//" + rutaActualPartes[0] + "/" + rutaActualPartes[1] + "/" + respuesta.responseText;

                window.open(nuevaRuta);

                Ext.MessageBox.show({
                    title: 'WARN',
                    msg: 'OK',
                    buttonText: {
                        yes: 'OK',
                    }
                });
            },
            failure : function() {

                Ext.MessageBox.show({
                    title: 'WARN',
                    msg: 'FAIL',
                    buttonText: {
                        yes: 'OK',
                    }
                });
            }
        }); 
     }
    ......

Best Answer

Adding the first coordinate of first polygon to the last polygon and then force this to be polygon will result to an invalid geometry (like the one in your image)

Instead of messing with the coordinates use the ol.format.wkt to get the geometry as WKT and then pass this to your java class. e.g

var ol3Geom = vectorSeleccion[x].getGeometry();
var format = new ol.format.WKT();
var wktRepresenation  = format.writeGeometry(ol3Geom);

this should give you the geometry you are looking for.