Search code examples
javascriptandroidopenlayers

How would i iterate over a polygon?


I am reading a polygon using OpenLayers by

var features = format.read(strGML);

This is the GML string structure

<gml:featureMember xmlns:gml="http://www.opengis.net/gml xsi:schemaLocation="http://www.opengis.net/gml http://schemas.opengis.net/gml/3.1.1/profiles/gmlsfProfile/1.0.0/gmlsf.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <feature:feature xmlns:feature="http://example.com/feature">
      <feature:geometry>
           <gml:Polygon>
                 <gml:exterior>
                       <gml:LinearRing>
                             <gml:posList>591674.39 5022898.05 545682.5 4722908.1 571701.44 5322909.29 651691.25 5022904.6 591674.39 5022898.05
                             </gml:posList>
                        </gml:LinearRing>
                 </gml:exterior>
           </gml:Polygon>
      </feature:geometry>
 </feature:feature>

After reading (which works) I need to iterate over each point of this polygon and read coordinates. I tried with various attempts with for...in, but can't get it to work. What would be the correct way to do this?


Solution

  • Anyways, I found a solution:

    var newPoints = [];
    var feature = features[0];
    var points = feature.geometry.getVertices();
    for (var i=0; i<points.length; i++) {
        var lon = points[i].x;
        var lat = points[i].y;
        var point = new OpenLayers.Geometry.Point(lon, lat);
        // do something with Points
        newPoints.push(point);
    }