Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

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.

share|improve this answer
    
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. –  okello Jul 13 '12 at 20:15
    
I've updated my answer. Please check it out. –  drnextgis Jul 14 '12 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. –  okello Jul 14 '12 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] –  drnextgis Jul 14 '12 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? –  okello Jul 14 '12 at 20:19
show 3 more comments

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.

share|improve this answer
    
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. –  drnextgis Jul 17 '12 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. –  okello Jul 17 '12 at 7:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.