AW: [OpenLayers-Users] How to access polygons or lines as WKT afterdrawing them?

Arnd Wippermann arnd.wippermann at web.de
Sun Oct 30 10:06:52 EDT 2011


Hi,

drawLayer.features is an array of features. 
Try 
alert(feats.constructor) or alert(feats.constructor==Array)

So (var f in feats) get the index of the array

To get the geometry as a WKT String, you can use 
feats[f].geometry


See also OpenLayers examples : vector-formats.html

Specially this function to retrieve your features in different formats:

function serialize(feature) {
    var type = document.getElementById("formatType").value;
    // second argument for pretty printing (geojson only)
    var pretty = document.getElementById("prettyPrint").checked;
    var str = formats['out'][type].write(feature, pretty);
    // not a good idea in general, just for this demo
    str = str.replace(/,/g, ', ');
    document.getElementById('output').value = str;
} 

Arnd

-----Ursprüngliche Nachricht-----
Von: openlayers-users-bounces at lists.osgeo.org
[mailto:openlayers-users-bounces at lists.osgeo.org] Im Auftrag von Pierre
Knobel
Gesendet: Sonntag, 30. Oktober 2011 10:44
An: openlayers-users at lists.osgeo.org
Betreff: [OpenLayers-Users] How to access polygons or lines as WKT
afterdrawing them?

Hi,

I'm trying to write a program starting from this example:
http://openlayers.org/dev/examples/draw-feature.html
The user should be able to draw any number of polygons and lines, and then
when hitting a button on the webpage the javascript code would do something
with them. The problem is that I can't figure out how to access these
geometries.

Basically, what I have is a vector layer defined by:
    drawLayer = new OpenLayers.Layer.Vector("blabla")

>From what I understand from
http://dev.openlayers.org/docs/files/OpenLayers/Layer/Vector-js.html,
http://dev.openlayers.org/docs/files/OpenLayers/Feature/Vector-js.html
and http://dev.openlayers.org/docs/files/OpenLayers/Geometry-js.html,
I should be able to retrieve the WKT of the geometries drawn by the user the
following way:
     drawLayer.features[i].geometry.toString();   //i would be an
integer lower than the number of geometries on my layer

The problem is that it doesn't work, and javascript doesn't make debugging
very easy. I tried to run the following function:
     function getDrawings()  {
            var feats = drawLayer.features;
            alert(typeof feats);
            alert(feats);
            var spam = 0;
            for(var f in feats) {
                   alert(typeof f);
                   alert(f);
                   spam = spam + 1;
            }
            document.getElementById("drawing_list").innerHTML =
String(spam);
        }

The counter 'spam' returns the correct number of geometries (let's say
4 for this example), when I print 'feats' I get '[object Object],[object
Object],[object Object],[object Object]', but 'typeof f' prints a string
which ends up being an incrementing number (0, 1, 2, 3) when I would expect
to get a geometry object.

What am I doing wrong?

Thanks,

Pierre
_______________________________________________
Users mailing list
Users at lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/openlayers-users



More information about the Users mailing list