[GIS] Get geometry from OpenLayers.Control.WMSGetFeatureInfo JSON

geometryopenlayers-2

I need to store the geometry of a certain feature with OpenLayers.Control.WMSGetFeatureInfo and I'm using these parameters to get it in JSON format

output:'features',
infoFormat:'application/json',
format: new OpenLayers.Format.JSON,

After I store the geometry from the JSON, I'm writing a WKT string with it to get something like.

"MULTIPOLYGON(((-8.098636886812224 39.016921320632704, -8.098572979051546 39.016543430309326,)))"

The problem is that I don't know why but the JSON geometry object is coming with a comma between the LAT and LONG like so:

 "MULTIPOLYGON(((-8.098636886812224,39.016921320632704, -8.098572979051546, 39.016543430309326)))"

What's the best way to get a correct WKT string from the event.features JSON Object or in alternative how to replace every other comma with a blank space using a regular expression in JavaScript?

Replacing the first and keeping the second?

Best Answer

So after I got everything to work I want to share the code. I didn't need the WKT after all, I managed to save the geometry with GeoJSON only.

The result is this and with every property of the clicked features saved in a array so I can edit them later.

enter image description here

function settingInfoPopUp(){

    // POPUP de Info
    infoControl = new OpenLayers.Control.WMSGetFeatureInfo({
        url: 'http://localhost:9090/geoserver/mittic2/wms',
        title: 'Identify features by clicking',
        queryVisible: true,
        maxFeatures: 50,
        output:'features',
        infoFormat:'application/json',
        format: new OpenLayers.Format.JSON,

        eventListeners: {
            getfeatureinfo: function(event) {   

                if(popup != null){
                    map.removePopup(popup);
                    popup.destroy();
                    popup = null;
                }

                vectors.destroyFeatures();
                selectedFeaturesParse(event.features);

                if(selectedFeatures.length > 0){                                    
                    popup = new OpenLayers.Popup.FramedCloud(
                        "featurePopup",
                        map.getLonLatFromPixel(event.xy),                       
                        null,
                        buildInfoControlTextAndVectors(),
                        null,
                        true
                    );
                    map.addPopup(popup);
                }
            }
        }
    });

    map.addControl(infoControl); 
    infoControl.activate();
}

function selectedFeaturesParse(jsonObject) {
    selectedFeatures = [];      
    for (var i=0 ; i < jsonObject.features.length; i++ ){
        selectedFeatures.push({
            objectid:jsonObject.features[i].properties.objectid, 
            conc_geoid: jsonObject.features[i].properties.conc_geoid,
            dicofre: jsonObject.features[i].properties.dicofre,
            lcover_typ: jsonObject.features[i].properties.lcover_typ,
            land_cultu: jsonObject.features[i].properties.land_cultu,
            area_efect: jsonObject.features[i].properties.area_efect,
            production: jsonObject.features[i].properties.production,
            energy_pot: jsonObject.features[i].properties.energy_pot,       
            geom: jsonObject.features[i].geometry           
        });
    }       
}

function buildInfoControlTextAndVectors(){
    var record; 
    var geojson_format = new OpenLayers.Format.GeoJSON({
                'internalProjection': new OpenLayers.Projection("EPSG:3857"),
                'externalProjection': new OpenLayers.Projection("EPSG:4326")
            });

    var feature;    
    var info = "<div class=\"info\">";

    for (var i=0 ; i < selectedFeatures.length; i++ ){
        record = selectedFeatures[i];

        // Aproveita o mesmo ciclo  que itera as formas selecionadas para desenhar também os vectores
        //**********************************************************
        feature = geojson_format.read(record.geom);
        vectors.addFeatures(feature);
        //**********************************************************

        info += "<div class=\"inner\"><table>"
        info += "<tr><td><b>ID: </b><td>" + record.objectid + "</td></tr>";
        info += "<tr><td><b>Concelho: </b><td>" + record.conc_geoid + "</td></tr>";
        info += "<tr><td><b>Freguesia: </b><td>" + record.dicofre + "</td></tr>";
        info += "<tr><td><b>Tipo de Coberto: </b><td>" + record.lcover_typ + "</td></tr>";
        info += "<tr><td><b>Ocupa&#231;&#227;o de Solo: </b><td>" + record.land_cultu + "</td></tr>";
        info += "<tr><td><b>&#193;rea Efectiva: </b><td>" + record.area_efect + " [ha]</td></tr>";
        info += "<tr><td><b>Produ&#231;&#227;o: </b><td>" + record.production + " [ton/ano]</td></tr>";
        info += "<tr><td><b>Potencial Energ&#233;tico: </b><td>" + record.energy_pot + " [MWh/ano]</td></tr></table></div>";

        if (i!= (selectedFeatures.length-1)){
            info += "<br />";
        }
    }

    info += "</div>"
    vectors.redraw();
    return info;
}