# How can I convert Polygon to LinearRing Geometry?

How can one convert a Polygon to LinearRing geometry using OpenLayers? I'm thinking that I can do this by getting bounds of the polygon, then use this to construct a Points array. Finally, use OpenLayers.Geometry.LinearRing(Points_Array) constructor to create the LinearRing. My attempts so far have flopped. Any code samples on how to achieve this will be highly appreciated.

UPDATE: I've accepted @drnexgis' answer for what it taught me on converting geometry to GML format. It only partially answered the question, but it was helpful.

For example:

feature = map.layers[1].features[0];
gml = new OpenLayers.Format.GML();
gml.write(feature);


# UPDATE

There is no API method for convering geometry to gml format, but you can use internal OpenLayers.Format.GML.buildGeometry.polygon function:

gml = new OpenLayers.Format.GML();
gml.buildGeometry.polygon.apply(gml,[map.layers[1].features[0].geometry])


Live demo here.

• Thanks a lot. What I want, however, is to convert Polygon to LinearRing Geometry. What you've given above converts a feature in to a feature GML; not polygon to linearring. Any more suggestions will be appreciated very much. Commented Jul 13, 2012 at 20:15
• I've updated my answer. Please check it out. Commented Jul 14, 2012 at 2:33
• Thanks a lot for your response. But I'm afraid you've not understood my question. How does one convert polygon to linearring? I've updated the question, just in case this wasn't clear from the question. Commented Jul 14, 2012 at 6:59
• I'm not sure whay you mean. In previous version of question you asked about converting to GML linearring, but now your question looks quite differently. Extracting linearring from polygon: map.layers[1].features[0].geometry.components[0] Commented Jul 14, 2012 at 16:24
• Yes, its extracting linearring from polygon. I have a polygon that's selected by a user. From that polygon selection, I'd like to get the corresponding linearring. In your response above, map.layers[1].features[0] represents the polygon? Commented Jul 14, 2012 at 20:19

I managed to convert a Polygon to LinearRing geometry, represented in GML, using the function below.

function getGml(pGeometry) {
var linearRing = new OpenLayers.Geometry.LinearRing(pGeometry.getVertices);
var gmlParser = new OpenLayers.Format.GML();
var feature = new OpenLayers.Feature.Vector();
feature.geometry = linearRing;
var linearRingGml = gmlParser.write(feature);

return linearRingGml;
}


This function's argument expects a Polygon geometry, but it can be adapted for the various geometries.

• In this example you convert feature to gml format. But my previous answer (about converting feature to gml) you marked as wrong. I've added link to live demo which represents my approach for extracting OpenLayers.Geometry.LinearRing from OpenLayers.Geometry.Polygon and converting result to gml representation. Commented Jul 17, 2012 at 7:16
• @drnextgis What I needed is conversion from polygon to linearring, then representing the new geometry in GML. Its a two-step process as you can see in this function. What you gave me was only converting a geometry to GML, not converting one geometry to another. But I'll accept your answer for what it taught me on converting geometry to GML. Thanks a lot for taking time to assist. Commented Jul 17, 2012 at 7:45